MATLAB Answers

0

findpeaks alternative - help fixing code

Asked by Dan Page on 13 Dec 2018
Latest activity Edited by Bruno Luong
on 13 Dec 2018
For my project i dont have access to findpeaks so need to make my own version. I have attempted to do this so that when input with frequency and relativePower up to 3 frequencies which occur at peak relativePower values are outputted.
( The sort function used works by sorting the first input into ascending order and then applying the same changes to the second input)
But currently I get errors when trying to run it.
I have attached values of frequency and relative power.
function [ThreeFrequencyPeaks] = PeakFind(frequency,relativePower)
rP = relativePower;
% If the nth value of rP is less than 0.1 delete the nth rP value and the
% nth frequency value.
for n=1:length(rP)
if rP(n)<=0.1
rP(n) = [];
frequency(n) = [];
end
end
% Check the rP value either side of the nth value of rP and if the nth
% value is bigger than both of them save the rP to powerPeaks and save the
% corresponding frequency to frequencyPeaks
for n= 2:length(rP)
if rP(n) > rP(n-1) && rP(n)>rP(n+1)
frequencyPeaks = frequency(n);
powerPeaks = rP(n);
else
end
end
% Sort frequencyPeaks with respect to powerPeaks using my function
[~, FrequencyPeaks] = mysortdata(powerPeaks, frequencyPeaks);
% Flip FrequencyPekas so that it goes biggest to smallest
FrequencyPeaksFliped = fliplr(FrequencyPeaks);
if length(t) > 3
ThreeFrequencyPeaks = FrequencyPeaksFliped(1:3);
elseif length == 2
ThreeFrequencyPeaks = FrequencyPeaksFliped(1:2);
else
ThreeFrequencyPeaks = FrequencyPeaksFliped(1);
end
end
function [time, signal] = mysortdata(time, signal)
% Make sure we have data to sort
if numel(time) <= 1
return
end
% Define points of where to split the vectors
PivotTime = time(end);
PivotSignal = signal(end);
%Remove this point from vecotr
time(end) = [];
signal(end) = [];
less = (time <= PivotTime);
% input Less and More into this function again
[LessTime, LessSignal] = mysortdata(time(less), signal(less));
[MoreTime, MoreSignal] = mysortdata(time(~less), signal(~less));
% Put vectors back together
time = [LessTime, PivotTime, MoreTime];
signal = [LessSignal, PivotSignal, MoreSignal];
end

  1 Comment

May be you forget, but a friendly reminder: if you use the code published in this Answer as showed above you might want to acknowdledge it.

Sign in to comment.

Products


Release

R2017b

1 Answer

Answer by Sean de Wolski
on 13 Dec 2018

How about: islocalmin and islocalmax?

  0 Comments

Sign in to comment.