Q:

MATLAB - Finding Peaks and Valleys

So I have 2 .wav files that was recorded frequency. One is the original audio and the other one is the filtered one. Both are stereo .wav files.
Right now, I am supposed to find the peaks and valleys of these 2 .wav file.
I was told to that I have to take 3 consecutive data and determine which of the 3 is the max or the min. If the middle one is the max or the min, then peak/valley have been found. Repeat this until there are no more data.
How should I achieve the above?
I was told to do a sliding window of size 3 and take 3 points by 3 points.
I googled it but could not find how to start. Could anyone assist please. Thank You!

A:

first you have to make your audio files into single channel, this can be easily done by:
a = audioread('filename');
b = sum(a,1)./2;

Next you can split it into frames of 3 - this can easily be achieved by 'reshape' or perhaps easier for you, just use a for loop:
reshapemethod = reshape(b,[floor(size(b,2)/3),3])

or loop:
for t=1:floor(size(b,2)/3)
   firstpoint = b((t-1)*3+1);
   secondpoint = b(firstpoint+1);
   thirdpoint = b(firstpoint+2)
   %//Use if statement to compare 
   %//OR just do
   [peaks(t) locationOfPeaks(t)]= findpeaks([firstpoint,secondpoint,thirdpoint]);
   %//For Valleys, just multiply first second and third point by -1 and then use the findpeaks function.
   [vallys(t) locationOfValleys(t)] = findpeaks([-1*firstpoint,-1*secondpoint,-1*thirdpoint]);
end

next you can use findpeaks to find the peaks in the 3 data points, or just compare them using a if clause, this is so basic, so I leave it for you to do. 

