%% -*- mode: octave -*- %% %% https://ccrma.stanford.edu/~jos/sasp/Matlab_listing_findpeaks_m.html %% %% FINDPEAKS Find up to npeaks interpolated peaks in data. %% %% [peakamps,peaklocs,peakwidths,resid] = %% findpeaks(data,npeaks,minwidth,maxwidth,minpeak,debug); %% %% finds up to npeaks interpolated peaks in real data. %% A peak is rejected if its width is %% less than minwidth samples wide(default=1), or %% less than minpeak in magnitude (default=min(data)). %% Quadratic interpolation is used for peak interpolation. %% Left-over data with peaks removed is returned in resid. %% Peaks are returned in order of decreasing amplitude. %% They can be sorted in order of location as follows: %% [peaklocs,sorter] = sort(peaklocs); %% amps = zeros(size(peakamps)); %% npeaks = length(peaklocs); %% for col=1:npeaks, %% amps(:,col) = peakamps(:,sorter(col)); %% end; %% They can be sorted in order of width as follows: %% [peakwidths,sorter] = sort(peakwidths); %% peakamps = peakamps(sorter); %% peaklocs = peaklocs(sorter); function [peakamps,peaklocs,peakwidths,resid] = ... sphear_findpeaks(data,npeaks,minwidth,maxwidth,minpeak,debug); len = length(data); if nargin<6, debug = 0; end; if nargin<5, minpeak = min(data); end; if nargin<4, maxwidth = 0; end; if nargin<3, minwidth = 1; end; if nargin<2, npeaks = len; end; peakamps = zeros(1,npeaks); peaklocs = zeros(1,npeaks); peakwidths = zeros(1,npeaks); if debug, peaksarr = 1.1*max(data)*ones(size(data)); end; if debug, orgdata = data; end; if debug, npeaks, end nrej = 0; pdebug=debug; ipeak=0; while ipeak1) && (data(llim)<=dmin+tol)), camp = data(llim); llim = llim - 1; if (camp1 peaksarr(plocq) = pamp; maxloc = min(len,2*round(max(peaklocs))); ttl = sprintf('Peak %d = %0.2f at %0.2f, width %d',ipeak,pamp,ploc,pwid); if x == -1 pdebug = 0; end end else nrej = nrej + 1; if (nrej >= 10), warning('*** findpeaks: giving up (10 rejected peaks in a row)'); break; end; end; end; if (ipeak