MATLAB: How to modify the findpeaks command to give me a width at 80% of peak height (instead of at the build in ‘halfheight’)

analysisdatadata analysisfindpeaksfindpeaks peak signal process halfheighthalfheightMATLABMATLAB and Simulink Student Suitepeakprocesssignal

I am trying to analyze peaks but and need to determine the width of signal at 80% of the peak height. Matlab has a built in criterion when you specify findpeaks(…,'WidthReference",'halfheight') and this gives you 50%.
I have tried to modify the code but have so far been unsuccessful. If anybody can help I would really appreciate it.
Thank you

Best Answer

I'd suggest not modifying findpeaks but to postprocess the results instead--a sample from one of the examples of the basic process I'd undertake:
load mtlb % a sample dataset
select = mtlb(1001:1200); % a subsection that's got a few peaks...
x=1:length(select); % corollary variable of bin locations
[pks,locs,wdths]=findpeaks(select,x,'MinPeakHeight',2); % the result for just a few >2
% The "engine" to postprocess...
fnInterpL=@(p,l,w) interp1(select(l-fix(w):l),l-fix(w):l,0.8*p); % interpolate lower side
fnInterpH=@(p,l,w) interp1(select(l:l+fix(w)),l:l+fix(w),0.8*p); % and upper...
This can be used as:
>> [arrayfun(fnInterpL,pks,locs.',wdths.') arrayfun(fnInterpH,pks,locs.',wdths.')]
ans =
51.3944 53.5555
82.5567 84.8145
145.4656 146.3182
>>
NB: the transpose on the locations/widths vectors; for some reason findpeaks doesn't return all three in same orientation as arrayfun needs.
One could generalize the above a little more by passing in the limit percent as well. Also note the above has the anonymous functions with the builtin signal; probably in a general case would be best to just go ahead and do an m-file where can pass it as well...
ADDENDUM
The above are the boundaries; the width is the difference of course:
>> w80=[arrayfun(fnInterpH,pks,locs.',wdths.')-arrayfun(fnInterpL,pks,locs.',wdths.') ]
w80 =
2.1611
2.2578
0.8526
>>