MATLAB: How to isolate the highest peaks of sound signals in a batch of signals and trim each signal with specific time windows before and after the peak

maximumpeakprocessingsignalSignal Processing Toolboxsoundtrim

I have many sound signals and I would like to detect the highest peak and trim them with a specific amount of time before and after the maximum peak. How can I achieve this?

Best Answer

You can have all your signals in a folder and use the "dir" function to get a list of all your sound signal files. For example:
>> all_signals = dir('*.wav')
Then you can read each signal using the "audioread" function as follows:
>> [y,Fs] = audioread('<name of file>')
Please refer to the following link for more information about the "audioread" function:
You can calculate the array of time that corresponds to the signal as follows:
>> time_in_seconds = (1:size(y,1))/Fs
To find the peaks you can usually use the "findpeaks" function, but if you just wish to find the highest peak, using the "max" function is a simple approach. Please refer to the following 2 links for more information about the "findpeaks" and "max" functions:
Then you can trim the signals by indexing and by calculating how many indices of the signal correspond to the desired time that you want before and after the maximum peak. Here is an example:
>> trimmed_signal = y(index_val - round((ms_left/1000)/time_step_in_sec):index_val + round((ms_right/1000)/time_step_in_sec));
Variables:
ms_left: time in milliseconds that you want to keep to the left of the peak
ms_right: time in milliseconds that you want to keep to the right of the peak
index_val: index corresponding to the maximum value in the signal (the position of the highest peak)
time_step_in_sec: time step in seconds which is equal to 1/Fs