forked from cab79/Matlab_files
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfindpeaks_minbase.m
990 lines (844 loc) · 31.8 KB
/
findpeaks_minbase.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
function [Ypk,Xpk,Wpk,Ppk] = findpeaks_minbase(Yin,varargin)
% CAB updated: minpeakprominance only requires a prominance on one side of
% the peak, not both.
%FINDPEAKS Find local peaks in data
% PKS = FINDPEAKS(Y) finds local peaks in the data vector Y. A local peak
% is defined as a data sample which is either larger than the two
% neighboring samples or is equal to Inf.
%
% [PKS,LOCS]= FINDPEAKS(Y) also returns the indices LOCS at which the
% peaks occur.
%
% [PKS,LOCS] = FINDPEAKS(Y,X) specifies X as the location vector of data
% vector Y. X must be a strictly increasing vector of the same length as
% Y. LOCS returns the corresponding value of X for each peak detected.
% If X is omitted, then X will correspond to the indices of Y.
%
% [PKS,LOCS] = FINDPEAKS(Y,Fs) specifies the sample rate, Fs, as a
% positive scalar, where the first sample instant of Y corresponds to a
% time of zero.
%
% [...] = FINDPEAKS(...,'MinPeakHeight',MPH) finds only those peaks that
% are greater than the minimum peak height, MPH. MPH is a real-valued
% scalar. The default value of MPH is -Inf.
%
% [...] = FINDPEAKS(...,'MinPeakProminence',MPP) finds peaks guaranteed
% to have a vertical drop of more than MPP from the peak on both sides
% without encountering either the end of the signal or a larger
% intervening peak. The default value of MPP is zero.
%
% [...] = FINDPEAKS(...,'Threshold',TH) finds peaks that are at least
% greater than both adjacent samples by the threshold, TH. TH is a
% real-valued scalar greater than or equal to zero. The default value of
% TH is zero.
%
% FINDPEAKS(...,'WidthReference',WR) estimates the width of the peak as
% the distance between the points where the signal intercepts a
% horizontal reference line. The points are found by linear
% interpolation. The height of the line is selected using the criterion
% specified in WR:
%
% 'halfprom' - the reference line is positioned beneath the peak at a
% vertical distance equal to half the peak prominence.
%
% 'halfheight' - the reference line is positioned at one-half the peak
% height. The line is truncated if any of its intercept points lie
% beyond the borders of the peaks selected by the 'MinPeakHeight',
% 'MinPeakProminence' and 'Threshold' parameters. The border between
% peaks is defined by the horizontal position of the lowest valley
% between them. Peaks with heights less than zero are discarded.
%
% The default value of WR is 'halfprom'.
%
% [...] = FINDPEAKS(...,'MinPeakWidth',MINW) finds peaks whose width is
% at least MINW. The default value of MINW is zero.
%
% [...] = FINDPEAKS(...,'MaxPeakWidth',MAXW) finds peaks whose width is
% at most MAXW. The default value of MAXW is Inf.
%
% [...] = FINDPEAKS(...,'MinPeakDistance',MPD) finds peaks separated by
% more than the minimum peak distance, MPD. This parameter may be
% specified to ignore smaller peaks that may occur in close proximity to
% a large local peak. For example, if a large local peak occurs at LOC,
% then all smaller peaks in the range [N-MPD, N+MPD] are ignored. If not
% specified, MPD is assigned a value of zero.
%
% [...] = FINDPEAKS(...,'SortStr',DIR) specifies the direction of sorting
% of peaks. DIR can take values of 'ascend', 'descend' or 'none'. If not
% specified, DIR takes the value of 'none' and the peaks are returned in
% the order of their occurrence.
%
% [...] = FINDPEAKS(...,'NPeaks',NP) specifies the maximum number of peaks
% to be found. NP is an integer greater than zero. If not specified, all
% peaks are returned. Use this parameter in conjunction with setting the
% sort direction to 'descend' to return the NP largest peaks. (see
% 'SortStr')
%
% [PKS,LOCS,W] = FINDPEAKS(...) returns the width, W, of each peak by
% linear interpolation of the left- and right- intercept points to the
% reference defined by 'WidthReference'.
%
% [PKS,LOCS,W,P] = FINDPEAKS(...) returns the prominence, P, of each
% peak.
%
% FINDPEAKS(...) without output arguments plots the signal and the peak
% values it finds
%
% FINDPEAKS(...,'Annotate',PLOTSTYLE) will annotate a plot of the
% signal with PLOTSTYLE. If PLOTSTYLE is 'peaks' the peaks will be
% plotted. If PLOTSTYLE is 'extents' the signal, peak values, widths,
% prominences of each peak will be annotated. 'Annotate' will be ignored
% if called with output arguments. The default value of PLOTSTYLE is
% 'peaks'.
%
% % Example 1:
% % Plot the Zurich numbers of sunspot activity from years 1700-1987
% % and identify all local maxima at least six years apart
% load sunspot.dat
% findpeaks(sunspot(:,2),sunspot(:,1),'MinPeakDistance',6)
% xlabel('Year');
% ylabel('Zurich number');
%
% % Example 2:
% % Plot peak values of an audio signal that drop at least 1V on either
% % side without encountering values larger than the peak.
% load mtlb
% findpeaks(mtlb,Fs,'MinPeakProminence',1)
%
% % Example 3:
% % Plot all peaks of a chirp signal whose widths are between .5 and 1
% % milliseconds.
% Fs = 44.1e3; N = 1000;
% x = sin(2*pi*(1:N)/N + (10*(1:N)/N).^2);
% findpeaks(x,Fs,'MinPeakWidth',.5e-3,'MaxPeakWidth',1e-3, ...
% 'Annotate','extents')
%
% See also MAX, FINDCHANGEPTS.
% Copyright 2007-2015 The MathWorks, Inc.
%#ok<*EMCLS>
%#ok<*EMCA>
%#codegen
cond = nargin >= 1;
if ~cond
coder.internal.assert(cond,'MATLAB:narginchk:notEnoughInputs');
end
cond = nargin <= 22;
if ~cond
coder.internal.assert(cond,'MATLAB:narginchk:tooManyInputs');
end
% extract the parameters from the input argument list
[y,yIsRow,x,xIsRow,minH,minP,minW,maxW,minD,minT,maxN,sortDir,annotate,refW] ...
= parse_inputs(Yin,varargin{:});
% find indices of all finite and infinite peaks and the inflection points
[iFinite,iInfite,iInflect] = getAllPeaks(y);
% keep only the indices of finite peaks that meet the required
% minimum height and threshold
iPk = removePeaksBelowMinPeakHeight(y,iFinite,minH,refW);
iPk = removePeaksBelowThreshold(y,iPk,minT);
% indicate if we need to compute the extent of a peak
needWidth = minW>0 || maxW<inf || minP>0 || nargout>2 || strcmp(annotate,'extents');
if needWidth
% obtain the indices of each peak (iPk), the prominence base (bPk), and
% the x- and y- coordinates of the peak base (bxPk, byPk) and the width
% (wxPk)
[iPk,bPk,bxPk,byPk,wxPk] = findExtents(y,x,iPk,iFinite,iInfite,iInflect,minP,minW,maxW,refW);
else
% combine finite and infinite peaks into one list
[iPk,bPk,bxPk,byPk,wxPk] = combinePeaks(iPk,iInfite);
end
% find the indices of the largest peaks within the specified distance
idx = findPeaksSeparatedByMoreThanMinPeakDistance(y,x,iPk,minD);
% re-order and bound the number of peaks based upon the index vector
idx = orderPeaks(y,iPk,idx,sortDir);
idx = keepAtMostNpPeaks(idx,maxN);
% use the index vector to fetch the correct peaks.
iPk = iPk(idx);
if needWidth
[bPk, bxPk, byPk, wxPk] = fetchPeakExtents(idx,bPk,bxPk,byPk,wxPk);
end
if nargout > 0
% assign output variables
if needWidth
[Ypk,Xpk,Wpk,Ppk] = assignFullOutputs(y,x,iPk,wxPk,bPk,yIsRow,xIsRow);
else
[Ypk,Xpk] = assignOutputs(y,x,iPk,yIsRow,xIsRow);
end
else
% no output arguments specified. plot and optionally annotate
hAxes = plotSignalWithPeaks(x,y,iPk);
if strcmp(annotate,'extents')
plotExtents(hAxes,x,y,iPk,bPk,bxPk,byPk,wxPk,refW);
end
scalePlot(hAxes);
end
%--------------------------------------------------------------------------
function [y,yIsRow,x,xIsRow,Ph,Pp,Wmin,Wmax,Pd,Th,NpOut,Str,Ann,Ref] = parse_inputs(Yin,varargin)
% Validate input signal
validateattributes(Yin,{'numeric'},{'nonempty','real','vector'},...
'findpeaks','Y');
yIsRow = isrow(Yin);
y = Yin(:);
% copy over orientation of y to x.
xIsRow = yIsRow;
% indicate if the user specified an Fs or X
hasX = ~isempty(varargin) && (isnumeric(varargin{1}) || ...
coder.target('MATLAB') && isdatetime(varargin{1}) && numel(varargin{1})>1);
if hasX
startArg = 2;
if isscalar(varargin{1})
% Fs
Fs = varargin{1};
validateattributes(Fs,{'double'},{'real','finite','positive'},'findpeaks','Fs');
x = (0:numel(y)-1).'/Fs;
else
% X
Xin = varargin{1};
if isnumeric(Xin)
validateattributes(Xin,{'double'},{'real','finite','vector','increasing'},'findpeaks','X');
else % isdatetime(Xin)
validateattributes(seconds(Xin-Xin(1)),{'double'},{'real','finite','vector','increasing'},'findpeaks','X');
end
if numel(Xin) ~= numel(Yin)
if coder.target('MATLAB')
throwAsCaller(MException(message('signal:findpeaks:mismatchYX')));
else
coder.internal.errorIf(true,'signal:findpeaks:mismatchYX');
end
end
xIsRow = isrow(Xin);
x = Xin(:);
end
else
startArg = 1;
% unspecified, use index vector
x = (1:numel(y)).';
end
if coder.target('MATLAB')
try %#ok<EMTC>
% Check the input data type. Single precision is not supported.
chkinputdatatype(y);
if isnumeric(x)
chkinputdatatype(x);
end
catch ME
throwAsCaller(ME);
end
else
chkinputdatatype(y);
chkinputdatatype(x);
end
M = numel(y);
cond = (M < 3);
if cond
coder.internal.errorIf(cond,'signal:findpeaks:emptyDataSet');
end
%#function dspopts.findpeaks
defaultMinPeakHeight = -inf;
defaultMinPeakProminence = 0;
defaultMinPeakWidth = 0;
defaultMaxPeakWidth = Inf;
defaultMinPeakDistance = 0;
defaultThreshold = 0;
defaultNPeaks = [];
defaultSortStr = 'none';
defaultAnnotate = 'peaks';
defaultWidthReference = 'halfprom';
if coder.target('MATLAB')
p = inputParser;
addParameter(p,'MinPeakHeight',defaultMinPeakHeight);
addParameter(p,'MinPeakProminence',defaultMinPeakProminence);
addParameter(p,'MinPeakWidth',defaultMinPeakWidth);
addParameter(p,'MaxPeakWidth',defaultMaxPeakWidth);
addParameter(p,'MinPeakDistance',defaultMinPeakDistance);
addParameter(p,'Threshold',defaultThreshold);
addParameter(p,'NPeaks',defaultNPeaks);
addParameter(p,'SortStr',defaultSortStr);
addParameter(p,'Annotate',defaultAnnotate);
addParameter(p,'WidthReference',defaultWidthReference);
parse(p,varargin{startArg:end});
Ph = p.Results.MinPeakHeight;
Pp = p.Results.MinPeakProminence;
Wmin = p.Results.MinPeakWidth;
Wmax = p.Results.MaxPeakWidth;
Pd = p.Results.MinPeakDistance;
Th = p.Results.Threshold;
Np = p.Results.NPeaks;
Str = p.Results.SortStr;
Ann = p.Results.Annotate;
Ref = p.Results.WidthReference;
else
parms = struct('MinPeakHeight',uint32(0), ...
'MinPeakProminence',uint32(0), ...
'MinPeakWidth',uint32(0), ...
'MaxPeakWidth',uint32(0), ...
'MinPeakDistance',uint32(0), ...
'Threshold',uint32(0), ...
'NPeaks',uint32(0), ...
'SortStr',uint32(0), ...
'Annotate',uint32(0), ...
'WidthReference',uint32(0));
pstruct = eml_parse_parameter_inputs(parms,[],varargin{startArg:end});
Ph = eml_get_parameter_value(pstruct.MinPeakHeight,defaultMinPeakHeight,varargin{startArg:end});
Pp = eml_get_parameter_value(pstruct.MinPeakProminence,defaultMinPeakProminence,varargin{startArg:end});
Wmin = eml_get_parameter_value(pstruct.MinPeakWidth,defaultMinPeakWidth,varargin{startArg:end});
Wmax = eml_get_parameter_value(pstruct.MaxPeakWidth,defaultMaxPeakWidth,varargin{startArg:end});
Pd = eml_get_parameter_value(pstruct.MinPeakDistance,defaultMinPeakDistance,varargin{startArg:end});
Th = eml_get_parameter_value(pstruct.Threshold,defaultThreshold,varargin{startArg:end});
Np = eml_get_parameter_value(pstruct.NPeaks,defaultNPeaks,varargin{startArg:end});
Str = eml_get_parameter_value(pstruct.SortStr,defaultSortStr,varargin{startArg:end});
Ann = eml_get_parameter_value(pstruct.Annotate,defaultAnnotate,varargin{startArg:end});
Ref = eml_get_parameter_value(pstruct.WidthReference,defaultWidthReference,varargin{startArg:end});
end
% limit the number of peaks to the number of input samples
if isempty(Np)
NpOut = M;
else
NpOut = Np;
end
% ignore peaks below zero when using halfheight width reference
if strcmp(Ref,'halfheight')
Ph = max(Ph,0);
end
validateattributes(Ph,{'numeric'},{'real','scalar','nonempty'},'findpeaks','MinPeakHeight');
if isnumeric(x)
validateattributes(Pd,{'numeric'},{'real','scalar','nonempty','nonnegative','<',x(M)-x(1)},'findpeaks','MinPeakDistance');
else
if coder.target('MATLAB') && isduration(Pd)
validateattributes(seconds(Pd),{'numeric'},{'real','scalar','nonempty','nonnegative'},'findpeaks','MinPeakDistance');
else
validateattributes(Pd,{'numeric'},{'real','scalar','nonempty','nonnegative'},'findpeaks','MinPeakDistance');
end
end
validateattributes(Pp,{'numeric'},{'real','scalar','nonempty','nonnegative'},'findpeaks','MinPeakProminence');
if coder.target('MATLAB') && isduration(Wmin)
validateattributes(seconds(Wmin),{'numeric'},{'real','scalar','finite','nonempty','nonnegative'},'findpeaks','MinPeakWidth');
else
validateattributes(Wmin,{'numeric'},{'real','scalar','finite','nonempty','nonnegative'},'findpeaks','MinPeakWidth');
end
if coder.target('MATLAB') && isduration(Wmax)
validateattributes(seconds(Wmax),{'numeric'},{'real','scalar','nonnan','nonempty','nonnegative'},'findpeaks','MaxPeakWidth');
else
validateattributes(Wmax,{'numeric'},{'real','scalar','nonnan','nonempty','nonnegative'},'findpeaks','MaxPeakWidth');
end
if coder.target('MATLAB') && isduration(Pd)
validateattributes(seconds(Pd),{'numeric'},{'real','scalar','nonempty','nonnegative'},'findpeaks','MinPeakDistance');
else
validateattributes(Pd,{'numeric'},{'real','scalar','nonempty','nonnegative'},'findpeaks','MinPeakDistance');
end
validateattributes(Th,{'numeric'},{'real','scalar','nonempty','nonnegative'},'findpeaks','Threshold');
validateattributes(NpOut,{'numeric'},{'real','scalar','nonempty','integer','positive'},'findpeaks','NPeaks');
Str = validatestring(Str,{'ascend','none','descend'},'findpeaks','SortStr');
Ann = validatestring(Ann,{'peaks','extents'},'findpeaks','SortStr');
Ref = validatestring(Ref,{'halfprom','halfheight'},'findpeaks','WidthReference');
%--------------------------------------------------------------------------
function [iPk,iInf,iInflect] = getAllPeaks(y)
% fetch indices all infinite peaks
iInf = find(isinf(y) & y>0);
% temporarily remove all +Inf values
yTemp = y;
yTemp(iInf) = NaN;
% determine the peaks and inflection points of the signal
[iPk,iInflect] = findLocalMaxima(yTemp);
%--------------------------------------------------------------------------
function [iPk, iInflect] = findLocalMaxima(yTemp)
% bookend Y by NaN and make index vector
yTemp = [NaN; yTemp; NaN];
iTemp = (1:numel(yTemp)).';
% keep only the first of any adjacent pairs of equal values (including NaN).
yFinite = ~isnan(yTemp);
iNeq = [1; 1 + find((yTemp(1:end-1) ~= yTemp(2:end)) & ...
(yFinite(1:end-1) | yFinite(2:end)))];
iTemp = iTemp(iNeq);
% take the sign of the first sample derivative
s = sign(diff(yTemp(iTemp)));
% find local maxima
iMax = 1 + find(diff(s)<0);
% find all transitions from rising to falling or to NaN
iAny = 1 + find(s(1:end-1)~=s(2:end));
% index into the original index vector without the NaN bookend.
iInflect = iTemp(iAny)-1;
iPk = iTemp(iMax)-1;
%--------------------------------------------------------------------------
function iPk = removePeaksBelowMinPeakHeight(Y,iPk,Ph,widthRef)
if ~isempty(iPk)
iPk = iPk(Y(iPk) > Ph);
if isempty(iPk) && ~strcmp(widthRef,'halfheight')
if coder.target('MATLAB')
warning(message('signal:findpeaks:largeMinPeakHeight', 'MinPeakHeight', 'MinPeakHeight'));
end
end
end
%--------------------------------------------------------------------------
function iPk = removePeaksBelowThreshold(Y,iPk,Th)
base = max(Y(iPk-1),Y(iPk+1));
iPk = iPk(Y(iPk)-base >= Th);
%--------------------------------------------------------------------------
function [iPk,bPk,bxPk,byPk,wxPk] = findExtents(y,x,iPk,iFin,iInf,iInflect,minP,minW,maxW,refW)
% temporarily filter out +Inf from the input
yFinite = y;
yFinite(iInf) = NaN;
% get the base and left and right indices of each prominence base
[bPk,iLB,iRB] = getPeakBase(yFinite,iPk,iFin,iInflect);
% keep only those indices with at least the specified prominence
[iPk,bPk,iLB,iRB] = removePeaksBelowMinPeakProminence(yFinite,iPk,bPk,iLB,iRB,minP);
% get the x-coordinates of the half-height width borders of each peak
[wxPk,iLBh,iRBh] = getPeakWidth(yFinite,x,iPk,bPk,iLB,iRB,refW);
% merge finite and infinite peaks together into one list
[iPk,bPk,bxPk,byPk,wxPk] = combineFullPeaks(y,x,iPk,bPk,iLBh,iRBh,wxPk,iInf);
% keep only those in the range minW < w < maxW
[iPk,bPk,bxPk,byPk,wxPk] = removePeaksOutsideWidth(iPk,bPk,bxPk,byPk,wxPk,minW,maxW);
%--------------------------------------------------------------------------
function [peakBase,iLeftSaddle,iRightSaddle] = getPeakBase(yTemp,iPk,iFin,iInflect)
% determine the indices that border each finite peak
[iLeftBase, iLeftSaddle] = getLeftBase(yTemp,iPk,iFin,iInflect);
[iRightBase, iRightSaddle] = getLeftBase(yTemp,flipud(iPk),flipud(iFin),flipud(iInflect));
iRightBase = flipud(iRightBase);
iRightSaddle = flipud(iRightSaddle);
peakBase = max(yTemp(iLeftBase),yTemp(iRightBase));
%--------------------------------------------------------------------------
function [iBase, iSaddle] = getLeftBase(yTemp,iPeak,iFinite,iInflect)
% pre-initialize output base and saddle indices
iBase = zeros(size(iPeak));
iSaddle = zeros(size(iPeak));
% table stores the most recently encountered peaks in order of height
peak = zeros(size(iFinite));
valley = zeros(size(iFinite));
iValley = zeros(size(iFinite));
n = 0;
i = 1;
j = 1;
k = 1;
% pre-initialize v for code generation
v = NaN;
iv = 1;
while k<=numel(iPeak)
% walk through the inflections until you reach a peak
while iInflect(i) ~= iFinite(j)
v = yTemp(iInflect(i));
iv = iInflect(i);
if isnan(v)
% border seen, start over.
n = 0;
else
% ignore previously stored peaks with a valley larger than this one
while n>0 && valley(n)>v;
n = n - 1;
end
end
i = i + 1;
end
% get the peak
p = yTemp(iInflect(i));
% keep the smallest valley of all smaller peaks
while n>0 && peak(n) < p
if valley(n) < v
v = valley(n);
iv = iValley(n);
end
n = n - 1;
end
% record "saddle" valleys in between equal-height peaks
isv = iv;
% keep seeking smaller valleys until you reach a larger peak
while n>0 && peak(n) <= p
if valley(n) < v
v = valley(n);
iv = iValley(n);
end
n = n - 1;
end
% record the new peak and save the index of the valley into the base
% and saddle
n = n + 1;
peak(n) = p;
valley(n) = v;
iValley(n) = iv;
if iInflect(i) == iPeak(k)
iBase(k) = iv;
iSaddle(k) = isv;
k = k + 1;
end
i = i + 1;
j = j + 1;
end
%--------------------------------------------------------------------------
function [iPk,pbPk,iLB,iRB] = removePeaksBelowMinPeakProminence(y,iPk,pbPk,iLB,iRB,minP)
% compute the prominence of each peak
Ppk = y(iPk)-y(iRB);
% keep those that are above the specified prominence
idx = find(Ppk >= minP);
iPk = iPk(idx);
pbPk = pbPk(idx);
iLB = iLB(idx);
iRB = iRB(idx);
%--------------------------------------------------------------------------
function [wxPk,iLBh,iRBh] = getPeakWidth(y,x,iPk,pbPk,iLB,iRB,wRef)
if isempty(iPk)
% no peaks. define empty containers
base = zeros(size(iPk));
iLBh = zeros(size(iPk));
iRBh = zeros(size(iPk));
elseif strcmp(wRef,'halfheight')
% set the baseline to zero
base = zeros(size(iPk));
% border the width by no more than the lowest valley between this peak
% and the next peak
iLBh = [iLB(1); max(iLB(2:end),iRB(1:end-1))];
iRBh = [min(iRB(1:end-1),iLB(2:end)); iRB(end)];
iGuard = iLBh > iPk;
iLBh(iGuard) = iLB(iGuard);
iGuard = iRBh < iPk;
iRBh(iGuard) = iRB(iGuard);
else
% use the prominence base
base = pbPk;
% border the width by the saddle of the peak
iLBh = iLB;
iRBh = iRB;
end
% get the width boundaries of each peak
wxPk = getHalfMaxBounds(y, x, iPk, base, iLBh, iRBh);
%--------------------------------------------------------------------------
function bounds = getHalfMaxBounds(y, x, iPk, base, iLB, iRB)
if isnumeric(x)
bounds = zeros(numel(iPk),2);
else
bounds = [x(1:numel(iPk)) x(1:numel(iPk))];
end
% interpolate both the left and right bounds clamping at borders
for i=1:numel(iPk)
% compute the desired reference level at half-height or half-prominence
refHeight = (y(iPk(i))+base(i))/2;
% compute the index of the left-intercept at half max
iLeft = findLeftIntercept(y, iPk(i), iLB(i), refHeight);
if iLeft < iLB(i)
xLeft = x(iLB(i));
else
xLeft = linterp(x(iLeft),x(iLeft+1),y(iLeft),y(iLeft+1),y(iPk(i)),base(i));
end
% compute the index of the right-intercept
iRight = findRightIntercept(y, iPk(i), iRB(i), refHeight);
if iRight > iRB(i)
xRight = x(iRB(i));
else
xRight = linterp(x(iRight), x(iRight-1), y(iRight), y(iRight-1), y(iPk(i)),base(i));
end
% store result
bounds(i,:) = [xLeft xRight];
end
%--------------------------------------------------------------------------
function idx = findLeftIntercept(y, idx, borderIdx, refHeight)
% decrement index until you pass under the reference height or pass the
% index of the left border, whichever comes first
while idx>=borderIdx && y(idx) > refHeight
idx = idx - 1;
end
%--------------------------------------------------------------------------
function idx = findRightIntercept(y, idx, borderIdx, refHeight)
% increment index until you pass under the reference height or pass the
% index of the right border, whichever comes first
while idx<=borderIdx && y(idx) > refHeight
idx = idx + 1;
end
%--------------------------------------------------------------------------
function xc = linterp(xa,xb,ya,yb,yc,bc)
% interpolate between points (xa,ya) and (xb,yb) to find (xc, 0.5*(yc-yc)).
xc = xa + (xb-xa) .* (0.5*(yc+bc)-ya) ./ (yb-ya);
% invoke L'Hospital's rule when -Inf is encountered.
if isnumeric(xc) && isnan(xc) || coder.target('MATLAB') && isdatetime(xc) && isnat(xc)
% yc and yb are guaranteed to be finite.
if isinf(bc)
% both ya and bc are -Inf.
if isnumeric(xa)
xc = 0.5*(xa+xb);
else
xc = xa+0.5*(xb-xa);
end
else
% only ya is -Inf.
xc = xb;
end
end
%--------------------------------------------------------------------------
function [iPk,bPk,bxPk,byPk,wxPk] = removePeaksOutsideWidth(iPk,bPk,bxPk,byPk,wxPk,minW,maxW)
if isempty(iPk) || minW==0 && maxW == inf;
return
end
% compute the width of each peak and extract the matching indices
w = diff(wxPk,1,2);
idx = find(minW <= w & w <= maxW);
% fetch the surviving peaks
iPk = iPk(idx);
bPk = bPk(idx);
bxPk = bxPk(idx,:);
byPk = byPk(idx,:);
wxPk = wxPk(idx,:);
%--------------------------------------------------------------------------
function [iPkOut,bPk,bxPk,byPk,wxPk] = combinePeaks(iPk,iInf)
iPkOut = union(iPk,iInf);
bPk = zeros(0,1);
bxPk = zeros(0,2);
byPk = zeros(0,2);
wxPk = zeros(0,2);
%--------------------------------------------------------------------------
function [iPkOut,bPkOut,bxPkOut,byPkOut,wxPkOut] = combineFullPeaks(y,x,iPk,bPk,iLBw,iRBw,wPk,iInf)
iPkOut = union(iPk, iInf);
% create map of new indices to old indices
[~, iFinite] = intersect(iPkOut,iPk);
[~, iInfinite] = intersect(iPkOut,iInf);
% prevent row concatenation when iPk and iInf both have less than one
% element
iPkOut = iPkOut(:);
% compute prominence base
bPkOut = zeros(size(iPkOut));
bPkOut(iFinite) = bPk;
bPkOut(iInfinite) = 0;
% compute indices of left and right infinite borders
iInfL = max(1,iInf-1);
iInfR = min(iInf+1,numel(x));
% copy out x- values of the left and right prominence base
% set each base border of an infinite peaks halfway between itself and
% the next adjacent sample
if isnumeric(x)
bxPkOut = zeros(size(iPkOut,1),2);
bxPkOut(iFinite,1) = x(iLBw);
bxPkOut(iFinite,2) = x(iRBw);
bxPkOut(iInfinite,1) = 0.5*(x(iInf)+x(iInfL));
bxPkOut(iInfinite,2) = 0.5*(x(iInf)+x(iInfR));
else
bxPkOut = [x(1:size(iPkOut,1)) x(1:size(iPkOut,1))];
bxPkOut(iFinite,1) = x(iLBw);
bxPkOut(iFinite,2) = x(iRBw);
bxPkOut(iInfinite,1) = x(iInf) + 0.5*(x(iInfL)-x(iInf));
bxPkOut(iInfinite,2) = x(iInf) + 0.5*(x(iInfR)-x(iInf));
end
% copy out y- values of the left and right prominence base
byPkOut = zeros(size(iPkOut,1),2);
byPkOut(iFinite,1) = y(iLBw);
byPkOut(iFinite,2) = y(iRBw);
byPkOut(iInfinite,1) = y(iInfL);
byPkOut(iInfinite,2) = y(iInfR);
% copy out x- values of the width borders
% set each width borders of an infinite peaks halfway between itself and
% the next adjacent sample
if isnumeric(x)
wxPkOut = zeros(size(iPkOut,1),2);
wxPkOut(iFinite,:) = wPk;
wxPkOut(iInfinite,1) = 0.5*(x(iInf)+x(iInfL));
wxPkOut(iInfinite,2) = 0.5*(x(iInf)+x(iInfR));
else
wxPkOut = [x(1:size(iPkOut,1)) x(1:size(iPkOut,1))];
wxPkOut(iFinite,:) = wPk;
wxPkOut(iInfinite,1) = x(iInf)+0.5*(x(iInfL)-x(iInf));
wxPkOut(iInfinite,2) = x(iInf)+0.5*(x(iInfR)-x(iInf));
end
%--------------------------------------------------------------------------
function idx = findPeaksSeparatedByMoreThanMinPeakDistance(y,x,iPk,Pd)
% Start with the larger peaks to make sure we don't accidentally keep a
% small peak and remove a large peak in its neighborhood.
if isempty(iPk) || Pd==0
idx = (1:numel(iPk)).';
return
end
% copy peak values and locations to a temporary place
pks = y(iPk);
locs = x(iPk);
% Order peaks from large to small
[~, sortIdx] = sort(pks,'descend');
locs_temp = locs(sortIdx);
idelete = ones(size(locs_temp))<0;
for i = 1:length(locs_temp)
if ~idelete(i)
% If the peak is not in the neighborhood of a larger peak, find
% secondary peaks to eliminate.
idelete = idelete | (locs_temp>=locs_temp(i)-Pd)&(locs_temp<=locs_temp(i)+Pd);
idelete(i) = 0; % Keep current peak
end
end
% report back indices in consecutive order
idx = sort(sortIdx(~idelete));
%--------------------------------------------------------------------------
function idx = orderPeaks(Y,iPk,idx,Str)
if isempty(idx) || strcmp(Str,'none')
return
end
if strcmp(Str,'ascend')
[~,s] = sort(Y(iPk(idx)),'ascend');
else
[~,s] = sort(Y(iPk(idx)),'descend');
end
idx = idx(s);
%--------------------------------------------------------------------------
function idx = keepAtMostNpPeaks(idx,Np)
if length(idx)>Np
idx = idx(1:Np);
end
%--------------------------------------------------------------------------
function [bPk,bxPk,byPk,wxPk] = fetchPeakExtents(idx,bPk,bxPk,byPk,wxPk)
bPk = bPk(idx);
bxPk = bxPk(idx,:);
byPk = byPk(idx,:);
wxPk = wxPk(idx,:);
%--------------------------------------------------------------------------
function [YpkOut,XpkOut] = assignOutputs(y,x,iPk,yIsRow,xIsRow)
% fetch the coordinates of the peak
Ypk = y(iPk);
Xpk = x(iPk);
% preserve orientation of Y
if yIsRow
YpkOut = Ypk.';
else
YpkOut = Ypk;
end
% preserve orientation of X
if xIsRow
XpkOut = Xpk.';
else
XpkOut = Xpk;
end
%--------------------------------------------------------------------------
function [YpkOut,XpkOut,WpkOut,PpkOut] = assignFullOutputs(y,x,iPk,wxPk,bPk,yIsRow,xIsRow)
% fetch the coordinates of the peak
Ypk = y(iPk);
Xpk = x(iPk);
% compute the width and prominence
Wpk = diff(wxPk,1,2);
Ppk = Ypk-bPk;
% preserve orientation of Y (and P)
if yIsRow
YpkOut = Ypk.';
PpkOut = Ppk.';
else
YpkOut = Ypk;
PpkOut = Ppk;
end
% preserve orientation of X (and W)
if xIsRow
XpkOut = Xpk.';
WpkOut = Wpk.';
else
XpkOut = Xpk;
WpkOut = Wpk;
end
%--------------------------------------------------------------------------
function hAxes = plotSignalWithPeaks(x,y,iPk)
% plot signal
hLine = plot(x,y,'Tag','Signal');
hAxes = ancestor(hLine,'Axes');
% turn on grid
grid on;
if numel(x)>1
hAxes.XLim = hLine.XData([1 end]);
end
% use the color of the line
color = get(hLine,'Color');
hLine = line(hLine.XData(iPk),y(iPk),'Parent',hAxes, ...
'Marker','o','LineStyle','none','Color',color,'tag','Peak');
% if using MATLAB use offset inverted triangular marker
if coder.target('MATLAB')
plotpkmarkers(hLine,y(iPk));
end
%--------------------------------------------------------------------------
function plotExtents(hAxes,x,y,iPk,bPk,bxPk,byPk,wxPk,refW)
% compute level of half-maximum (height or prominence)
if strcmp(refW,'halfheight')
hm = 0.5*y(iPk);
else
hm = 0.5*(y(iPk)+bPk);
end
% get the default color order
colors = get(0,'DefaultAxesColorOrder');
% plot boundaries between adjacent peaks when using half-height
if strcmp(refW,'halfheight')
% plot height
plotLines(hAxes,'Height',x(iPk),y(iPk),x(iPk),zeros(numel(iPk),1),colors(2,:));
% plot width
plotLines(hAxes,'HalfHeightWidth',wxPk(:,1),hm,wxPk(:,2),hm,colors(3,:));
% plot peak borders
idx = find(byPk(:,1)>0);
plotLines(hAxes,'Border',bxPk(idx,1),zeros(numel(idx),1),bxPk(idx,1),byPk(idx,1),colors(4,:));
idx = find(byPk(:,2)>0);
plotLines(hAxes,'Border',bxPk(idx,2),zeros(numel(idx),1),bxPk(idx,2),byPk(idx,2),colors(4,:));
else
% plot prominence
plotLines(hAxes,'Prominence',x(iPk), y(iPk), x(iPk), bPk, colors(2,:));
% plot width
plotLines(hAxes,'HalfProminenceWidth',wxPk(:,1), hm, wxPk(:,2), hm, colors(3,:));
% plot peak borders
idx = find(bPk(:)<byPk(:,1));
plotLines(hAxes,'Border',bxPk(idx,1),bPk(idx),bxPk(idx,1),byPk(idx,1),colors(4,:));
idx = find(bPk(:)<byPk(:,2));
plotLines(hAxes,'Border',bxPk(idx,2),bPk(idx),bxPk(idx,2),byPk(idx,2),colors(4,:));
end
if coder.target('MATLAB')
hLine = get(hAxes,'Children');
tags = get(hLine,'tag');
legendStrs = {};
searchTags = {'Signal','Peak','Prominence','Height','HalfProminenceWidth','HalfHeightWidth','Border'};
for i=1:numel(searchTags)
if any(strcmp(searchTags{i},tags))
legendStrs = [legendStrs, ...
{getString(message(['signal:findpeaks:Legend' searchTags{i}]))}]; %#ok<AGROW>
end
end
if numel(hLine)==1
legend(getString(message('signal:findpeaks:LegendSignalNoPeaks')), ...
'Location','best');
else
legend(legendStrs,'Location','best');
end
end
%--------------------------------------------------------------------------
function plotLines(hAxes,tag,x1,y1,x2,y2,c)
% concatenate multiple lines into a single line and fencepost with NaN
n = numel(x1);
if isnumeric(x1)
line(reshape([x1(:).'; x2(:).'; NaN(1,n)], 3*n, 1), ...
reshape([y1(:).'; y2(:).'; NaN(1,n)], 3*n, 1), ...
'Color',c,'Parent',hAxes,'tag',tag);
elseif coder.target('MATLAB') && isdatetime(x1)
line(reshape(datenum([x1(:).'; x2(:).'; NaT(1,n)]), 3*n, 1), ...
reshape([y1(:).'; y2(:).'; NaN(1,n)], 3*n, 1), ...
'Color',c,'Parent',hAxes,'tag',tag);
end
%--------------------------------------------------------------------------
function scalePlot(hAxes)
% In the event that the plot has integer valued y limits, 'axis auto' may
% clip the YLimits directly to the data with no margin. We search every
% line for its max and minimum value and create a temporary annotation that
% is 10% larger than the min and max values. We then feed this to "axis
% auto", save the y limits, set axis to "tight" then restore the y limits.
% This obviates the need to check each line for its max and minimum x
% values as well.
minVal = Inf;
maxVal = -Inf;
if coder.target('MATLAB')
hLines = findall(hAxes,'Type','line');
for i=1:numel(hLines)
data = get(hLines(i),'YData');
data = data(isfinite(data));
if ~isempty(data)
minVal = min(minVal, min(data(:)));
maxVal = max(maxVal, max(data(:)));
end
end
xlimits = xlim;
axis auto
% grow upper and lower y extent by 5% (a total of 10%)
p = .05;
y1 = (1+p)*maxVal - p*minVal;
y2 = (1+p)*minVal - p*maxVal;
% artificially expand the data range by the specified amount
hTempLine = line(xlimits([1 1]),[y1 y2],'Parent',hAxes);
% save the limits
ylimits = ylim;
delete(hTempLine);
else
axis auto
ylimits = ylim;
end
% preserve expanded y limits but tighten x axis.
axis tight
ylim(ylimits);
xlim(xlimits);
% [EOF]