找到峰值起点和终点

时间:2017-03-31 10:02:15

标签: matlab signal-processing

我有一系列峰值Peaks image,我使用matlab findpeaks来找到峰值点。

我需要找到峰宽和峰值起点和终点吗?我已经开始

使用此代码,但它没有给我正确的宽度计算:

% If the peak is at index 150
% Scan to the right.
for k = 151:length(signal)
   if signal(k) < signal(k-1)
    % Signal is starting to fall.
    rightIndex = k-1;
    break;
  end
end
% Scan to the left.
for k = 149: -1 : 1
   if signal(k) < signal(k+1)
     % Signal is starting to fall.
     leftIndex = k+1;
     break;
  end
end
peakWidth = rightIndex - leftIndex;

1 个答案:

答案 0 :(得分:0)

这里有两种宽度,由findpeaks计算,一种是通过比较导数与零来计算的:

% generate sinal
x = 0:0.1:10;
y = x.*sin(x/5).*sin(5*x.^2);
% get derivatives
dy = diff(y);
dx = diff(x);
dy_dx = [0 dy./dx];
% plot default annotated peaks
subplot(211);
findpeaks(y,x,'Annotate','extents');
% get peaks with width computed by 'findpeaks'
[pks,locs,peakWidth1,p] = findpeaks(y,x);
subplot(212);
plot(x,y);
hold on
plot(locs,pks,'*m')
% compute starting and ending points
startpoint = zeros(size(pks));
endpoint = zeros(size(pks));
for ii = 1:length(pks)
    plot(locs(ii) + peakWidth1(ii)*[-.5 .5],pks(ii) - p(ii)/2 + [0 0],'y')
    sp = find((x < locs(ii)) & (dy_dx <= 0),1,'last');
    if isempty(sp)
        sp = 1;
    end
    startpoint(ii) = sp;
    ep = find((x > locs(ii)) & (dy_dx >= 0),1,'first') - 1;
    if isempty(ep)
        ep = length(x);
    end
    endpoint(ii) = ep;
    plot(x(startpoint(ii)),y(startpoint(ii)),'og')
    plot(x(endpoint(ii)),y(endpoint(ii)),'sr')
end
% compute second type of width using ending and starting points
peakWidth2 = x(endpoint) - x(startpoint);

enter image description here