Finding indices when the curve stops decreasing
3 次查看(过去 30 天)
显示 更早的评论
Hi,
I have a curve I like to find the maximum and then find the indices under the peak for which it stops decreasing on each sides.
How can I do this?
I can find the maximum by
max(y)
but how can I determine once the curve stops decreasing? The curve is a bit noisy.
I guess I could maybe use;
diff(y)
but how do I estimate the tolerance for which
diff(y) > some tolerance.
The noise level s kind of big and varies from curve to curve. The one I am looking at now goes from 1500 to -1500 at baseline. I would determine the tolerance automatically if possible.
0 个评论
回答(3 个)
Matt Tearle
2014-10-20
Otherwise, you might want to try some kind of smoothing before looking for the max:
>> nwindow = 5;
>> ySmooth = conv(y,ones(nwindow,1)/nwindow,'same');
Then look for where the difference goes through zero (and the second difference is negative). You can use the diff function to do finite difference derivatives by providing the x values as well as the y.
0 个评论
SK
2014-10-20
Suppose the maximum was at index im. Then take:
dyrgt = diff( y(im : end) );
dylft = diff( y(im : -1 : 1) );
irgt = im + find(dyrgt >= 0, 1) - 1;
ilft = im - find(dylft >= 0, 1) + 1;
3 个评论
SK
2014-10-21
I did the following with your data sets:
data_s = smooth(data, 20);
plot(data_s);
[~, im] = max(data_s);
dyrgt = diff( data_s(im : end) );
dylft = diff( data_s(im : -1 : 1) );
irgt = im + find(dyrgt >= 0, 1) - 1;
ilft = im - find(dylft >= 0, 1) + 1;
hold on
plot(ilft, 1, '*r');
plot(irgt, 1, '*r');
% ____
data2_s = smooth(data2, 20);
[~, im] = max(data2_s);
dyrgt = diff( data2_s(im : end) );
dylft = diff( data2_s(im : -1 : 1) );
irgt = im + find(dyrgt >= 0, 1) - 1;
ilft = im - find(dylft >= 0, 1) + 1;
close all
plot(data2_s);
hold on
plot(ilft, 1, '*r');
plot(irgt, 1, '*r');
It appears to give the correct points, unless you want the small hump on the left of the peak in data2 to be treated as noise.
SK
2014-10-20
编辑:SK
2014-10-20
For some reason, didn't see the part about the noise. In that case prior smoothing would be an option. Another more involved, but in my opinion more robust, option would be to use a piecewise linear fit and then take the two points at the bottom of the W.
2 个评论
SK
2014-10-21
I shouldn't have opened my mouth.
Can you assume that your data is essentially VV shaped? Meaning there is basically only one "real" peak and it falls off on both sides and then possibly rises again somewhat at the sides. Then you have an ansatz for your function shape ie: piecewise continuous with four linear pieces. You can then use nlinfit() with a custom model function. Read the docs for nlinfit(). Perhaps you can also use the fit() function with fittype as 'linearinterp'.
The advantage of picewise linear is that it is the 'roughest' approximation so it should cover all those bumps and jaggies and give you the "one true solution" for the troughs (but it probably wont give the exact minima). For example in your data2, you could leave it to take care of that hump on the left - it will decide whether it is significant or not.
On the other hand you could use a smooth approximation (cubic splines say) to approximate the curve, but the problem with smooth curves is that they may follow the shape of your data too closely so there may still be some ambiguity about points where the troughs are.
Anyway, you'll have to think about this, depending on the exact problem you want to solve and the amount of time you have on your hands.
Sorry, but I'm not sure I can be of more help.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Smoothing 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!