Finding X value with known Y value on plot
68 views (last 30 days)
Show older comments
I have plotted a graph based on known sets of data for x and y. I have then added a horizontal line to the plot at the y-value where I want to find out the corresponding x-values (shown in red on the plot).
I want to know what the x-values are where these two lines intercept. I do not have the equation for the blue signal. Is there a function/ command I could use that will do this?
% step one of inputting file
filename='Test_Results_19.xls'
ColumnA= xlsread(filename,'A2:A2088')
ColumnB= xlsread(filename,'B2:B2088')
B=ColumnB
A=ColumnA
%Step 2- do the FFT of the values
Z=fft(A)
W=fft(B)
%Step 3 find the modulus of values in column B
X=abs(Z)
Y=abs(W)
plot(X,Y)
title('Magnitude of frequency against frequency')
xlabel('Frequency')
ylabel('H(w)')
%finding the peak value and W1 and W2
[fmax, imax]=max(Y);
Qmax=fmax;
Q2=(Qmax/sqrt(2));
%Need to find corresponding x-values to the found y-values
x1=0
x2=45*10^4
y1=Q2
plot(X,Y,[x1 x2],[y1 y1])
hold on;
4 Comments
Kelb77
on 6 Nov 2017
Curve fit the data, plot a new curve once you have the function, interpolate from that
Walter Roberson
on 6 Nov 2017
You might want to look in the File Exchange for keyword "digitize", as there are some tools to convert plot images into data values.
Answers (3)
Akira Agata
on 7 Nov 2017
One possible way to find the threshold-crossing points is interpolating the data by spline function and find the crossing points by using fzero function. The following is the sample script to do this.
% Sample data y = f(x)
x = linspace(-2*pi, 2*pi, 15);
y = sech(x) + 0.2*rand(size(x));
% Threshold
th = 0.5;
% Spline interpolation
pp = spline(x,y);
% Find the indexes where y(i) < th and y(i+1) > th
idx = find((y(1:end-1)-th).*(y(2:end)-th) < 0);
% Find the root of f(x)-th = 0
x0 = zeros(size(idx));
y0 = zeros(size(idx));
pt = 1;
for kk = idx
x1 = x(kk);
func = @(x) pp.coefs(kk,:)*[(x-x1)^3; (x-x1)^2; (x-x1);1]-th;
[x0(pt),y0(pt)] = fzero(func,x(kk));
pt = pt+1;
end
% Show the result
xx = -2*pi:0.1:2*pi;
yy = spline(x,y,xx);
figure
plot(x,y,'-o')
hold on
plot(xx,yy)
plot([-2*pi 2*pi],[th th])
plot(x0,y0+th,'ro')
legend({'Data','Spline interpolation','Threshold','Estimated points'})
2 Comments
Walter Roberson
on 10 Sep 2020
At the moment the loop looks to me to be doing about the same as ppval() would do?
See Also
Categories
Find more on Scatter Plots in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!