
Finding the first intersection point between 2 lines
조회 수: 8 (최근 30일)
이전 댓글 표시
Hello all,
I need to find the time constant for which I plotted a line at y = 63% of the maximum value (y_max)
The other line comes from a Simulink model in which I can switch between different parameters, the output (y) differs every time (the line isn't plotted from a function). I'm using the following code which uses the 'interp1' function:
y_tau = 0.63*y_max;
[y,idx] = unique(y);
t = t(idx);
t_constant = interp1(y,x,y_max)
However, this always gives the second intersection point in the graph (see picture below). In this particular case:
t_constant = 190.2194
. How can I get the first intersection point using this code or any other method? Thank you in advance.
댓글 수: 0
채택된 답변
Mathieu NOE
2022년 12월 23일
hello
try this
code is fairly simple to use as you only have to specify the y threshold value
threshold = 0.63*max(y); % 63% of peak amplitude

% dummy data
n = 1000;
x = 10*(0:n-1)/n;
y = 1+square(x);
[B,A] = butter(1,0.004);
y = filter(B,A,y);
% main code
threshold = 0.63*max(y); % 63% of peak amplitude
t0_pos1 = find_zc(x,y,threshold);
t0_pos1 = t0_pos1(1); % keep only first crossing point
figure(1)
plot(x,y,'b',x,threshold*ones(size(x)),'k--',t0_pos1,threshold*ones(size(t0_pos1)),'*r','linewidth',2,'markersize',12);grid on
legend('signal','threshold','signal positive slope crossing points');
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function [Zx] = find_zc(x,y,threshold)
% positive slope "zero" crossing detection, using linear interpolation
y = y - threshold;
zci = @(data) find(diff(sign(data))>0); %define function: returns indices of +ZCs
ix=zci(y); %find indices of + zero crossings of x
ZeroX = @(x0,y0,x1,y1) x0 - (y0.*(x0 - x1))./(y0 - y1); % Interpolated x value for Zero-Crossing
Zx = ZeroX(x(ix),y(ix),x(ix+1),y(ix+1));
end
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Specialized Power Systems에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!