Not able to detect the peak for randon signal

2 views (last 30 days)
function [maxtab, mintab]=peakdet(v, delta, x)
maxtab = [];
mintab = [];
v = v(:); % Just in case this wasn't a proper vector
if nargin < 3
x = (1:length(v))';
else
x = x(:);
if length(v)~= length(x)
error('Input vectors v and x must have same length');
end
end
if (length(delta(:)))>1
error('Input argument DELTA must be a scalar');
end
if delta <= 0
error('Input argument DELTA must be positive');
end
mn = Inf; mx = -Inf;
mnpos = NaN; mxpos = NaN;
lookformax = 1;
for i=1:length(v)
this = v(i);
if this > mx, mx = this; mxpos = x(i); end
if this < mn, mn = this; mnpos = x(i); end
if lookformax
if this < mx-delta
maxtab = [maxtab ; mxpos mx];
mn = this; mnpos = x(i);
lookformax = 0;
end
else
if this > mn+delta
mintab = [mintab ; mnpos mn];
mx = this; mxpos = x(i);
lookformax = 1;
end
end
end
I am using this code for detecting the peak and valley in the signal. But whenever i and giving some random signal it is giving error that vector length should be same.
e g i generate following random signal
> sig_length = 20; sig = rand(1,sig_length);
[c,d]=peakdet(sig,1,sig_length)
??? Error using ==> peakdet at 29
Input vectors v and x must have same length
Can anybody help me out why this error is coming even if length of v and x is same.
Regards Jeevan Patil

Accepted Answer

Wayne King
Wayne King on 21 Jan 2012
Hi, Because you have this code at line 9:
if length(v)~= length(x)
But sig and sig_length are not equal in length. sig is a vector, and sig_length is a scalar, so the length of sig_length is 1.
Perhaps you meant to write in the code
if (length(v) ~= x)
I don't know if that alone makes the code work, but I do see that the code is currently written so that v and x are the same length.

More Answers (0)

Categories

Find more on Measurements and Feature Extraction in Help Center and File Exchange

Products

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!