0:00

Hello, welcome back to the course on audio signal processing for music applications.

Â This week, we're talking about the short-time Fourier transform.

Â And one of the fundamental elements of this transform is the analysis window.

Â So in this programming lecture I want to talk about windows but

Â from a programming perspective.

Â 0:42

But since I have to do all this zero phase windowing and

Â center things around zero and zero padding.

Â At the end, the code is a little bit longer than what it would be desirable.

Â So let's go through the code and talk about the main aspects of it.

Â Well first, we have the packages that we need,

Â import, numpy, scipy and matplotlib.

Â Then we have the lines that relate to the window, so

Â we defined the window length in this case,

Â 63 and the very row that we keep that is M, capital M.

Â Then we called a function GetWindow,

Â which it has two parameters.

Â The name of the window on the size, so the window I chose is the hanning window.

Â Which is the raised cosine function.

Â And then I need the variables that keep the middle of the window information.

Â And it has to handle this even are not situations so

Â the middle can be different depending on even an outside.

Â So the two sides might have different number of samples.

Â Okay, then we need to prepare the window for computing FFT.

Â So to place the window at the zeroth location, so

Â what we're going to do is we create a buffer of the size of the 51

Â that we want to compute, in this case 512 samples.

Â And then we place the window around the zeroth sample.

Â So we take the second half of the window and place it at the beginning and

Â the first half of the window and place it at the end.

Â 2:45

Then we can compute the spectrum of this buffer, okay.

Â So we will compute the buffer using the FFT algorithm and

Â then we comvert the complex spectrum into absolute value and phase.

Â The absolute value in order to convert it to decibels,

Â which is the way that we like to see the information of the magnitude,

Â we have to make sure that there are no zeroes so

Â that the lock of zero is not there.

Â So we will have to check if the absolute value is

Â below this epsilon value which is the minimum value we can have in Python.

Â So that's why we have these three lines of code.

Â 3:34

And then we compute the angle of the complex values.

Â And then in order to show it better, to display the spectrum,

Â the magnitude and phase spectra and

Â see it centered in the middle, let's say of the array.

Â We undo this zero phase windowing thing so

Â that we place back the data in the middle of the array,

Â it's easier to visualize.

Â Okay let's run it, so

Â we are in a iPython with Pylab in it, so

Â we have the matplotlib package already

Â within it and we can run the script.

Â Okay, that's it and now we can start plotting things that we have computed.

Â For example, we can plot the window, okay.

Â So, this is the hanning window, which is this raised cosine going from

Â zero to one and the 63 samples that we have computed.

Â 4:45

Then we can plot how this window has been placed in the FFT buffer.

Â So we can plot the FFT buffer.

Â Okay, so here the window is centered at zero.

Â So we have the second half from zero to half of the window, and the first half

Â 5:22

Okay so this is the magnitude spectrum again centered around zero so

Â here we have the positive frequency values.

Â And in the second part we have the negative values.

Â So a better way to display that is to move things around and

Â locate the main lobe in the center so that's how we did and

Â mx1 has the information in the center.

Â 5:52

And if we plot the phase, we can also show the location

Â of the phases and here what is important of course are the phases in the middle.

Â And here we have these two pi discontinuities but basically everything

Â is zero, modulus two pi and it's a clear.

Â It's good because that means we centered the window around zero and

Â therefore we have what we call a zero phase window.

Â Okay, in order to visualize the X axis better.

Â Here, I typed some commands to plot it

Â in a way that the access are better shown.

Â So here what I did was to plot against an X axis array.

Â That has been normalized so

Â that the zero parting does not affect, so we divide by N and multiple by M.

Â So we actually see the samples, let's say of, with respect to the window and

Â they also have normalized the magnitude so that the maximum is zero decibels.

Â And I'm only plotting the values that go in the x axis from minus 22 to 20 and

Â the dB values, I'm plotting from minus 80 to zero.

Â Now, if we run this script with these lines added,

Â well we are seeing the magnitude spectrum of the hanning window.

Â But with an x axis that shows the center around zero.

Â And decibels starting from zero.

Â So now we can check the values we talked about that describe the window.

Â We mentioned that the main lobe.

Â The width of the main lobe is an important characteristic of the window.

Â In here we can see, if we look from the bottom

Â right side where our x value is of the cursor.

Â We can see that this main lobe sort of deep is at minus two.

Â And this other one is at two so clearly this main lobe at this location,

Â the weight is around four samples or to be called four bins.

Â And this is what we mentioned about the hanning window.

Â And then also we talked about the side lobe level, the highest side lobe level.

Â And here again we can put the cursor at highest side lobe level and

Â it tells me kind of the value that this has.

Â And it's around minus 31 decibels which is the kind of thing that we mention.

Â 8:49

Okay so now we can keep changing the window and

Â for example we can put instead of the hamming, instead of the hanning we can put

Â the hamming and compute the same script.

Â And okay that's a different window and

Â if we can measure the main lobe width we see that it also has four samples,

Â but now the side lobe level is much lower.

Â So the highest side lobe is around minus 42 decibels,

Â which is the kind of things we mentioned in the theory class, okay?

Â And if we go to another window, for example we go to the Blackman window,

Â 9:38

Blackman window and we save it and run it.

Â Okay well, yes that's what we saw in the theory class and

Â now the main lobe is wider.

Â It's around six bins and

Â the side lobe level is much lower around 58 decibels.

Â And then finally the last window that we also talked about,

Â this was the Blackman-Harris window.

Â Blackman-Harris window that we can compute.

Â And if we run it, okay.

Â So now, in fact we are not seeing the side lobes and this is because

Â the range that we are specifying doesn't even reach where the side lobe is.

Â So let's change the display.

Â And let's change the axis, instead of having minus 80.

Â Let's put at least minus 100 so

Â that when we show it, now we are seeing the side lobes.

Â In fact the highest side lobe is around minus 92 decibels.

Â And now the main lobe is quite wider, is around eight bins.

Â Okay so this is a good way to visualize and try to understand the windows.

Â And so please feel free to play around with that.

Â 11:14

And this is another script.

Â This is much shorter, because instead of calling the FFT and

Â worrying about all this centering around zero.

Â And worrying about the zero parting etc., I call directly the dftAnal

Â function that we have in the SMS tools directory.

Â So now what I do is I specify the directory of

Â the models where I have all the models of the SMS tools and

Â I import the dftModel file, okay?

Â So that I can call this dftAnal function that receives as input signal,

Â the window, and the FFT size and automatically does all

Â the operations that we just showed in the previous script.

Â Okay, so in this script basically what I do is I get

Â sine wave with a given frequency.

Â In this case, a frequency is 5,000 Hertz.

Â Of course a sampling rate of 44,100 and

Â I compute 101 samples of a sinusoid which are basically

Â going to be the same length of the window size I am using.

Â For example, in this case I am using the hanning window, but

Â maybe let's change it and let's use, for

Â example the hamming window.

Â 12:55

Okay, and now we compute the analysis.

Â And here, I also display in a way the output so

Â that I centered everything on top about the hertz.

Â So we're going to display only the positive part,

Â because the dftAnal only computes at the positive part of the spectrum.

Â Then I will be displaying it, showing it in hertz.

Â So let's execute this file, test1.

Â 13:30

Okay, and this is the sinusoid of what I have analyzed.

Â And this is the shape of the window, in fact.

Â So this proves what we talked about in theory.

Â That when we analyze a sine wave, and we multiplied by a given window.

Â In fact, what we've seen in the spectrum, is the transfer of the window.

Â Centered at the frequency of the sinusoid, okay?

Â 5,000 hertz and with the amplitude of the sinusoid.

Â The amplitude in this case was normalized and is not here an important thing.

Â But we would also see the amplitude of the sinusoid.

Â And of course we see the standard characteristics of the window and

Â here the x axis is in hertz.

Â So we need to understand the relationship between

Â frequency in hertz and all these other values, the beans that we talked about.

Â And of course, if we change the window and

Â we compute the like the one we mentioned,

Â the Blackman-Harris, okay so

Â now we do this computation, okay and now we are seeing.

Â Again the Blackman-Harris window, the magnitude spectrum but

Â centered at the frequency of the sinusoid.

Â Okay, so that's basically all I wanted to talk about in this lecture.

Â So we have been using some packages from Python and also of course,

Â we have been using the sms tools in order to get DFT analysis code from that.

Â 15:28

And that's all, so we have been focusing on one aspect of the short-time

Â Fourier transform which is analysis window which is a fundamental element.

Â And unless we understand how windows work it's going to be very difficult to

Â understand how spectrograms and how the short-time Fourier transform and

Â other spectral analysis systems work.

Â So please make sure that you understand the concept of windowing and

Â how it affects when either by itself when we see

Â the spectrum or when we window a particular signal.

Â And therefore, so how we see the spectrum of

Â particular signal having been windowed in a certain way.

Â