波峰波谷法计算信号的周期及其Matlab程序

波峰波谷法原理是:查找信号相邻波峰波谷间隔采样点的数量,乘上采样间隔的时间,最后求平均值即是信号的周期。但该方法对噪声较为敏感,采用波峰波谷法计算滤波后信号的周期,如图所示。

波峰波谷法计算信号的周期及其Matlab程序_第1张图片

该方法主要是需要找到信号的极大值和极小值,程序如下:

function [max_p, max_v, min_p, min_v] = find_extrema(y)
% find maximas
% [tmp_v, tmp_p] = findpeaks(y);
tmp_p = find(y(2:end-1)>y(1:end-2) & y(2:end-1)>y(3:end)) + 1;
tmp_v = y(tmp_p);
max_p = zeros(1, length(tmp_p)+2);
max_v = zeros(size(max_p));
max_p(1) = 1;
max_v(1) = y(1);
max_p(end) = length(y);
max_v(end) = y(end);
max_p(2:(end-1)) = tmp_p;
max_v(2:(end-1)) = tmp_v;
% additional check
if length(max_p) > 5
    m1 = ( max_v(3) - max_v(2) ) / ( max_p(3) - max_p(2) );
    test = max_v(2) - m1 * ( max_p(2) - max_p(1) );
    if test > max_v(1)
        max_v(1) = test;
    end
    m2 = ( max_v(end-1) - max_v(end-2) ) / ( max_p(end-1) - max_p(end-2) );
    test = max_v(end-1) + m2 * ( max_p(end) - max_p(end-1) );
    if test > max_v(end)
        max_v(end) = test;
    end
end

% find minimas
% [tmp_v, tmp_p] = findpeaks(-y);
tmp_p = find(y(2:end-1) 5
    m1 = ( min_v(3) - min_v(2) ) / ( min_p(3) - min_p(2) );
    test = min_v(2) - m1 * ( min_p(2) - min_p(1) );
    if test < min_v(1)
        min_v(1) = test;
    end
    m2 = ( min_v(end-1) - min_v(end-2) ) / ( min_p(end-1) - min_p(end-2) );
    test = min_v(end-1) + m2 * ( min_p(end) - min_p(end-1) );
    if test < min_v(end)
        min_v(end) = test;
    end
end
end

 

你可能感兴趣的:(信号处理)