How to change axis of graph and interpolate data

Sorry new to matlab if this seems like a basic question
Currently I have a plot that I have attached.I want to find the intersections between the plot and the line by interpolating the data. What functions/operations do i need to use on matlab
Thanks in advance

1 件のコメント

Star Strider
Star Strider 2014 年 6 月 26 日
It would help if you attached your plot.
The best way to do this is to save it and attach it as a ‘.fig’ file, since that contains your data as well.

サインインしてコメントする。

 採用された回答

Star Strider
Star Strider 2014 年 6 月 26 日

2 投票

This works:
% GET INFORMATON FROM FIGURE:
openfig('Figure 3#.fig');
h1c = get(gca, 'Children');
Xdc = get(h1c, 'XData');
Xd = cell2mat(Xdc);
Ydc = get(h1c, 'YData');
Yd = cell2mat(Ydc);
% CALCULATIONS:
Ydn = diff(Yd, [], 1); % Subtract line from curve to create zero-crossings
Zx = circshift(Ydn, [0 1]) .* Ydn; % Use circshift to detect them
Zxi = find(Zx < 0); % Their indices
for k1 = 1:length(Zxi) % Use interp1(Y,X,0) to get line intercepts as Xzx
Xzx(k1) = interp1([Ydn(Zxi(k1)-1) Ydn(Zxi(k1))], [Xd(1,Zxi(k1)-1) Xd(1,Zxi(k1))], 0)
end
% PLOT ZERO-CROSSINGS ON FIGURE TO CHECK:
hold on
plot(Xzx, repmat(Yd(1,1),1,length(Xzx)), '*r')
hold off
The result:
Since you have the original data and don’t have to get it from the figure, you may want to change the variable designations from my Xd and Yd to yours. If you simply want the X-intercepts (my variable Xzx), then leave it as it is and use (or rename) Xzx as calculated here.

8 件のコメント

Vaultec
Vaultec 2014 年 6 月 26 日
Thank you very much, that helped a lot
Star Strider
Star Strider 2014 年 6 月 26 日
My pleasure!
I’m glad it does what you want.
PhD_77
PhD_77 2014 年 10 月 7 日
編集済み: PhD_77 2014 年 10 月 7 日
Hi Star Strider,
I have similar case and tried your code but I received an error. the error messege is;
-------------
" Attempted to access Ydn(0); index must be a positive integer or logical.
Error in ltb (line 29) Xzx(k1) = interp1([Ydn(Zxi(k1)-1) Ydn(Zxi(k1))], [Xd(1,Zxi(k1)-1) Xd(1,Zxi(k1))], 0);
---------------
Let me just explain what I want to do. suppose we generate the following signal;
t = linspace(0,4*pi,100);
y = sin(t);
>> plot(t,y)
I want first to plot the points on the crossing points between the sin wave and say (horizontal threshold line at y=0.6). so in this case we have something like the image below;
all what I want to know is the time length of ab, bc, cd and de ?
would you please help me on that. Thanks in advance.
AM
Star Strider
Star Strider 2014 年 10 月 7 日
That specific approach was probably specific to that Question. (Except for assigning ‘Xd’ and ‘Yd’, ignore everything above %CALCULATIONS, unless you’re getting data from a .fig file.) For your problem, I would simply subtract 0.6 from your curve, use that as ‘Ydn’ (so Ydn = Yd-0.6;), and go from there. The rest should work.
‘all what I want to know is the time length of ab, bc, cd and de’
Use diff(Xzx) for that. It will give you all of them in one call to diff.
PhD_77
PhD_77 2014 年 10 月 7 日
編集済み: PhD_77 2014 年 10 月 7 日
Hi Star Strider, Many Thanks.
My data is attached. When I run your code I got an error message:
[Attempted to access Ydn(0); index must be a positive integer or logical. Error in bb (line 27) Xzx(k1) = interp1([Ydn(Zxi(k1)-1) Ydn(Zxi(k1))], [Xd(1,Zxi(k1)-1) Xd(1,Zxi(k1))], 0); ]
Can you please try it. Note that, the first column on the attached file is for time (Xd) and the second column is for y axis (Yd). You can use 0.6 as a horizontal threshold line.
look forward to hearing from you.
Kind regards
Star Strider
Star Strider 2014 年 10 月 7 日
編集済み: Star Strider 2014 年 10 月 7 日
My pleasure!
This works:
D = dlmread('xy.txt');
D0 = D(:,2)-0.6;
Dzx = find((D0.*circshift(D0,[-1 0])) <= 0);
Dzx = Dzx(1:end-1);
for k1 = 1:size(Dzx,1)
Ixr = Dzx(k1)-3:Dzx(k1)+3;
Dnp(k1) = interp1(D0(Ixr), D(Ixr,1), 0);
end
Dnp = unique(Dnp);
Xdif = diff([0 Dnp]);
figure(1)
plot(D(:,1), D(:,2))
hold on
plot(D(Dzx,1), D(Dzx,2), 'pr')
hold off
grid
dstr = strsplit(sprintf('\\Delta=%.5f\n',Xdif))
text(Dnp, 0.6*ones(size(Dnp)), dstr(1:end-1), 'HorizontalAlignment','left', 'VerticalAlignment','bottom')
Since you wanted the differences in the values of x where y=0.6, those are provided in ‘Xdif’ and displayed on the plot.
My Answer has already been Accepted, but if my revised code works for you, a Vote would be nice!
PhD_77
PhD_77 2014 年 10 月 8 日
Hi Star Strider, Thank you very much indeed. For previous t-y data it was working fine. However I have collected more data and extend t,y plot but it seems the points are not aligned well at y=0.6. Please see the new t-y data (attached). Look forward to hearing from you Many thanks for your help
Star Strider
Star Strider 2014 年 10 月 8 日
My pleasure!
That’s probably as good as it gets. Even when I expand the interpolation to ±50 indices, it doesn’t get more precise. I suspect that with slopes that extreme, you’re also encountering floating-point approximation error. (When I take the mean and standard error of the estimated zero-crossings, I get 0.60014 and 0.000183 respectively.)
My only suggestion is that you increase your sampling frequency by a factor of 10 or more, and perhaps increasing the precision of your ADC as well. That will probably improve the ability of the algorithm to approximate your 0.6 threshold with those data.

サインインしてコメントする。

その他の回答 (1 件)

Sara
Sara 2014 年 6 月 26 日

0 投票

with interpolation, you will likely not find the intersections.

カテゴリ

質問済み:

2014 年 6 月 26 日

コメント済み:

2014 年 10 月 8 日

Community Treasure Hunt

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

Start Hunting!

Translated by