MATLAB - Hamming window, overlap 50%

6.3k Views Asked by At

So I have wrote some code which takes an audio file and splits into frames of 320 samples for the 16000hz.

I have took the hamming window of each frame as shown by the code:

fs=16000;
[x,fs] = audioread('01.wav');

%Pre-emphasis filter (Y[n]=X [n]-0.95x[n - 1])
b = [1 -0.95];
y = filter(b,1,x);

%windowing
numSamples = length(y);
frameLength = 320;
numFrames = floor(numSamples/frameLength);
for frame = 1:numFrames,
   firstSample = (frame * frameLength) - (frameLength - 1);
   lastSample = (frame * frameLength);

   shortTimeFrame = y(firstSample:lastSample);
   h = hamming(320);

   hs = h.*shortTimeFrame;
   plot(hs, 'r');
end

How would I then overlap the hamming windows by 50%? I have seen other questions on SO and seen answers such as this:

y = buffer(h, 1, floor(64 * 0.5));

But have had no luck with it

1

There are 1 best solutions below

7
On BEST ANSWER

Have a look at the documentation for the buffer function.

The first argument is your signal (i.e. not the hamming window). If you do:

Y = buffer (x, 320, 160)

you will get out a matrix Y where your signal has been split into overlapping frames; that is, every column of Y is of size 320 (i.e. a frame), and the last 160 elements of one column are identical to the first 160 elements of the next column.

Applying the hamming window of your choice to each 'frame' is then a simple case of multiplying each column with the hamming window, e.g.

hammed_Y = Y .* repmat (h(:), [1, size(Y, 2)]);


inb4pedants: repmat isn't the most efficient way to do this, but it's the clearest to demonstrate the concept. Prefer bsxfun if possible, (or broadcasting in octave).