I have a timeseries of movement data (motion capture). I am trying to identify the direction of movement: if there is a maximum, that would be movement UP, if there is a minimum, that would be a movement DOWN.
I am using the function peakdet found here http://www.billauer.co.il/peakdet.html and modified by user @Yuk here How to differentiate between a double peak and a single peak array in MATLAB?
I want to be able to categorize the occuring movements as either UP or DOWN and I thought that identifying the maxima and minima might help. However, I shall explain the problem using the figures below:
As you can see, the first image has found both maxima and minima, but while the red peaks are actual movements UPWARDS, the minima are not. The opposite goes for the second figure, the green minima are indeed the movements DOWNWARDS, but the red peaks are no real movements at all.
I am wondering how to modify the this code, in order to plot minima and maxima, ONLY when their amplitude is big enough (hence signalling a TRUE movement):
%# run peakdet twice left-to-right and right-to-left
delta = 10;
[ymaxtab, ymintab] = peakdet(y, delta, x);
[ymaxtab2, ymintab2] = peakdet(y(end:-1:1), delta, x(end:-1:1));
ymaxtab = unique([ymaxtab; ymaxtab2],'rows');
ymintab = unique([ymintab; ymintab2],'rows');
%# plot the curve and show extreme points based on number of peaks
plot(x,y)
hold on
if size(ymaxtab,1) == 2 && size(ymintab,1) == 1 %# if double peak
plot(ymintab(:,1),ymintab(:,2),'r.','markersize',30)
elseif size(ymaxtab,1) == 1 && size(ymintab,1) == 0 %# if single peak
plot(ymaxtab(:,1),ymaxtab(:,2),'r.','markersize',30)
else %# if more (or less)
plot(ymintab(:,1),ymintab(:,2),'r.','markersize',30)
plot(ymaxtab(:,1),ymaxtab(:,2),'r.','markersize',30)
end
hold off
...where delta "sets what magnitude of fluctuations you want to ignore."
Code source is: How to differentiate between a double peak and a single peak array in MATLAB? UPDATE: I tried a moving average filter, but what seems to happen is that the time series becomes more smoothed, yet, still, minima and maxima are identified in the same manner. I also tried the function Findpeaks with [ypk,yt] = findpeaks( Ix,'MinPeakProminence', 5); but while I know how to find the peaks, I don't know how to find the valleys (invertig the timeseries upside downgives very weird results), so usign that method, I am at a loss for indetifying valleys.
I am wondering whether I should change the rule for the threshold, to something like: a peak is only a peak, in either direction if it is 2 standard deviations away from the mean of the entire time series array. But I don't know how to implement it in the code posted above.