Finding the x value for a given y value on a plot

Hi,
I would like to get help with finding the corresponding x values for a given y value.
With reference to the plot that I have, I managed to find the peak values (as denoted by the blue arrows) for each curve of the graph. Now I would like to find the x values (time) for 30% and 90% of the peak y value (torque) that I found.
If possible, I would also like to know how I can find the highest slope/gradient of each curve on the graph between the times at 30% and 90% of the peak torque value.
x = TimeSeconds;
y = TorqueNewtonMeters;
graph = plot (x,y);
pks = findpeaks(y,'MinPeakProminence', 100);
thirtypercentpks = 0.3*pks;
ninetypercentpks = 0.9*pks;
Thank you!

Answers (1)

Check out the 2nd output to findpeaks, it gives you the x-values.
Check out the gradient() function to address the 2nd part of your question.

4 Comments

Hi Adam,
Thanks for your reply! However, I'm looking to find the x values for the y value where it is 30% and 90% of the peak I found, and not the x value corresponding to the peak value itself. Would the same code still work?
I see. Before you edited the question, it seemed that you were trying to find the x values for 30-90% of your peaks.
How far are you in solving this? For example, there are at least two ways to define the 30% and 90% marks. Are you describing the x% of the vertical distance between some baseline and the peak?
If that y value falls between two coordinates, do you intend to choose the closest one, or do you indend to interpolate to higher resolution?
Once you have the y-coordinate that approximates (or exactly matches) the y-value of the x% mark, you can compute its distance from the peak's x-value and then add/subtract that distance to get the x-value.
For now, I am still trying to determine the x values that correspond to 30% and 90% of the peak. If that y value falls between two coordinates, I intend to interpolate to a higher resolution.
But your method makes sense, I might try computing the distance from the peak's x-value and then subtracting that distance to get the x-value. May I ask what code I should apply to carry out this action?
The purpose of finding the x values is so that I am able to define the time period within which I will be extracting the slope/gradient (rate of torque development) from.
Thank you so much!
1) You can use vq = interp1(x,v,xq) to resample the (x,y) data to a finer resolution. The finer the resolution, the smaller the error will be between the selected y-value and the actual y-value that represents the x% mark.
2) Compute the height of each peak. Then compute 30% and 90% of the peak value. Let's say that y value at the peak is p, the y value at the 30% mark would be p*.3.
3) Locate the closest values to p*.3 and p*.9 y-values just before and just after the peak. You may want to segment the curves from the closest near-0 y-value to the peak. There are lots of ways to approach this step so once you get here, try some approaches and let us know if you get stuck & show us what you've got.

Sign in to comment.

Asked:

on 14 Apr 2020

Edited:

on 17 Apr 2020

Community Treasure Hunt

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

Start Hunting!