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.

采纳的回答

Mathieu NOE
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

更多回答(2 个)

KSSV
KSSV 2022-12-22

Torsten
Torsten 2022-12-22
If you have the t-y values as vectors available, you can use this code to determine the t-values where y-y_tau=0:
y_tau = 0.63*y_max;
y = y - y_tau;
i = find(y(1:end-1).*y(2:end) <= 0)
t_root = t(i)-y(i).*(t(i+1)-t(i))./(y(i+1)-y(i))

类别

Help CenterFile Exchange 中查找有关 Specialized Power Systems 的更多信息

产品


版本

R2022a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by