Not able to detect the peak for randon signal
1 次查看(过去 30 天)
显示 更早的评论
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
0 个评论
采纳的回答
Wayne King
2012-1-21
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.
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Measurements and Feature Extraction 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!