Continue to Site

# Needed: Peaks and Troughs Algorithm

Status
Not open for further replies.

#### JimTex

##### Newbie level 1
find peaks

I'm looking for a reliable algroithm for finding and characterizing the Peaks and Troughs in Time series data. My time series is relatively short (500-1000 points) and the primary peaks and troughs are not repeated. Please see the attached Image.

Thanks
Jim

find peaks algorithm

if a sample is smaller than it's previous sample and also smaller than the next sample, then that sample is a trough. A similar logic can be formed to find peaks.

You have to lowpass the data first, otherwise you will get too many false troughs

Here's an example script I threw together:

%Create a signal that looks kinda like yours
t = 1:1000;
noise = randn(1,length(signal));
signal = 10*sin(.02*t) + 4*sin(.05*t)+noise;

%Output vector
output = zeros(1,length(signal));

%Lowpass the signal to get rid of high-frequency content. Note that
%filtfilt will avoid biasing the signal. The exact values of the filter
%were tweaked until they looked right
[b a]=butter(4,.05);
lowpass_signal = (filtfilt(b, a, signal));

plot(t, signal, t, lowpass_signal);
title('Comparison of signals');
legend('Original', 'Filtered');

%Find the rate of change of the filtered signal
slope = diff(lowpass_signal);

%Trough:
% Zero slope, less than surrounding values
% Plug a -1 into the output vector at that spot
%Peak:
% Zero slope, greater than surrounding values
% Plug a -1 into the output vector at that spot

for i = 1length(signal)-6)
if (abs(slope(i))<.005 && lowpass_signal(i-5)<lowpass_signal(i) && lowpass_signal(i+5)<lowpass_signal(i))
output(i)=1;
elseif (abs(slope(i))<.005 && lowpass_signal(i-1)>lowpass_signal(i) && lowpass_signal(i+5)>lowpass_signal(i))
output(i)=-1;
end
end

disp('Troughs are located at index: ');
find(output==-1)

disp('Peaks are located at index: ');
find(output==1)

Status
Not open for further replies.