1

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

user3667111
  • 611
  • 6
  • 21

1 Answers1

1

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).
Tasos Papastylianou
  • 21,371
  • 2
  • 28
  • 57
  • so Y = Buffer.. is out of the for loop and the hammed_Y is in? I have no idea what repmat is, I'm new to matlab – user3667111 Oct 30 '16 at 16:40
  • `repmat` repeats a matrix along a dimension. In my example, `h(:)` ensures `h` is in 'column' form, and `repmat` repeats it one time along the rows (i.e. no repetition), and `size(Y,2)` times along columns. This gives you a matrix of the same size as `Y` where all columns are identical (i.e. your `h`), so you can then perform "elementwise" simple multiplication with Y in a 'vectorized' manner. No need for loops whatsoever. – Tasos Papastylianou Oct 30 '16 at 16:42
  • Is there a way I could plot the entire waveform with the overlapping? – user3667111 Oct 30 '16 at 16:44
  • What do you mean? You want to plot each frame (post-hamming) separately? Note that this is *not* applying a (padded) hamming window over an entire signal at different intervals. This is just producing a bunch of equal sized frames for processing. – Tasos Papastylianou Oct 30 '16 at 16:53
  • oh okay, I understand. So I would have to loop through hammed_Y to perform spectral analysis on each frame? – user3667111 Oct 30 '16 at 16:56
  • depends on your analysis. If it's a simple `fft` you can just pass your `hammed_Y` directly into `fft()` and it will output a complex matrix of the same size as `hammed_Y`, where each column is the fft for that frame. (i.e. no need to loop for each column). Always check if there is a vectorised way to perform an operation before going for the looped approach! – Tasos Papastylianou Oct 30 '16 at 17:02
  • So I am doing the MFCC analysis, this seems a much better way of doing it – user3667111 Oct 30 '16 at 17:08