AndyECE
Joined: 17 Oct 2008 Posts: 80 Helped: 12
|
04 Nov 2009 1:44 Needed: Peaks and Troughs Algorithm |
|
|
|
|
You have to lowpass the data first, otherwise you will get too many false troughs
Here's an example script I threw together:
Added after 8 seconds:
%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 = 1:(length(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)
|
|