MATLAB Answers

0

Finding X value with known Y value on plot

Asked by Serena Solanki on 4 Nov 2017
Latest activity Answered by Akira Agata
on 7 Nov 2017
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

Show 1 older comment
I have tried interpolation, however as I do not have the function for the plotted curve- only the data points which have been plotted on the graph. I think I will have to read them off by eye. I have tried everything but no success :(
Curve fit the data, plot a new curve once you have the function, interpolate from that
You might want to look in the File Exchange for keyword "digitize", as there are some tools to convert plot images into data values.

Sign in to comment.

2 Answers

Answer by KSSV
on 7 Nov 2017

  0 Comments

Sign in to comment.


Answer by 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'})

  0 Comments

Sign in to comment.