MATLAB: How to get a window of signal data before and after its peak

findfindpeaksSignal Processing Toolboxwindow

I have a 2 column array. It represents an exponential rise/fall of voltage and the time for each datapoint.
In the voltage, I want to locate the data in a window between the first time the voltage exceeds 0.2*Vpeak up to when it falls below 0.4*Vpeak.
The values before and after the window are then to be replaced with zeros.

Best Answer

First, you can use the "findpeaks" function to find all the local peaks in your data:
>> [pks, locs] = findpeaks(voltage);
Then, from all the peaks, determine the global peak:
>> [vPeak, iPeak] = max(pks);
>> locPeak = locs(iPeak);
Finally, use the "find" function to find the indices of when your signal reaches 0.2*Vpeak and drops to 0.4*Vpeak of its peak:
>> idx_start = find(voltage(1:locPeak) < 0.2*vPeak, 1, 'last');
>> idx_end = locPeak + find(voltage(locPeak:end) < 0.4*vPeak, 1, 'first');