0:41

So we will basically, implementing the whole STFT equation shown here.

Â So basically, we'll be iterating over a sound x and

Â we will be computing at every frame, so the frame number is l.

Â So we'll be computing frames of the sound at each

Â frame location we will be multiplying the fragment of sound with the window, w.

Â And therefore, at the end we'll have a sequence of spectra.

Â A sequence of in this case, complex spectra.

Â And we see it as a block diagram so we start from the signal, x of n,

Â we multiply one fragment with our window so we get a windowed signal.

Â That's what we compute the FFT of and then, of course,

Â we compute the magnitude and phase representation of the spectrum.

Â And then, we can undo the whole process by calling the inverse FFT,

Â therefore obtaining the windowed synthesize sound and

Â what we call overlap at strategy.

Â We will be adding together these fragments into a whole signal.

Â And hopefully, if we have done the things correctly,

Â especially if we have chosen the window and the hop size correctly,

Â these output signal will be identical to the original.

Â 3:01

we need to do the sampling rate of that sound.

Â Then we need to pass it, the window we're going to be using W then the size

Â of the FFT capital N and the hop size, how are we going to hop through the sound.

Â And basically, the core of the function is a loop In this case,

Â a wild loop that keeps advancing through the sound by a pointer, this pin.

Â Pin is a pointer that keeps advancing by age, by capital age, by the hop size.

Â So, pin gets advanced at every iteration by capital

Â H until it reaches the end which is the end of the sound.

Â And within that, we basically choose the fragment of the sound

Â that we want to analyze and then we call the DFT anal function.

Â So this is the function that we already discussed last week

Â which is the DFT model, it comes from the package DFT model, okay.

Â So we don't have to worry now here about the zero phase windowing about

Â the zero padding, etc.

Â Everything is done within this DFT-anal function and

Â it receives the fragment of the sound, the window, and the FFT size.

Â And it returns the positive site of the magnitude and phase spectrum, okay.

Â After that, we can do the inverse DFT also calling the DFT-synth function.

Â And therefore, that will return a window fragment of the sound and

Â then we can put it all together, overlap and adding the sound and

Â creating the overall output array.

Â Okay, and apart from that the rest are basically initialization and

Â ways to better analyze the sound and

Â to make sure that the pointer Is can start at the very beginning of the sound.

Â And can end at the very end of the sound, so I'm not going to go into detail

Â of that but please feel free to go over that and understand the scope.

Â Okay, so now we have this function so

Â that returns sound and let's use it.

Â Let's write some code to use this function.

Â So this is a little script that I wrote to call the STFT aal,

Â in which I first have to get out the packages

Â required including the actual STFT function, so

Â this comes from the models and directory.

Â And then, I can get a file, define the window and then,

Â define the size of the window, the FFT size and the hop size.

Â And now, we can create the file with wavread,

Â get the window and call the STFT function.

Â And this will fill up two variables mX and pX which will

Â include the sequence of magnitude and phase spectrum.

Â Okay, so let's run, it let's run test3.

Â Okay, that's quite quick, so it analyse the sound and

Â it adapting all these variables.

Â So, let's go through them, so

Â maybe the first thing we can do is to listen to the sound.

Â So we can just do

Â 7:45

will tell me the dimensions of the ray.

Â Basically, the number of frames and

Â the number of samples in the spectrum that we have.

Â So in fact this tells me that we have 238 frames and

Â each frame has 512 samples.

Â These 512 samples are the samples of the magnitude spectrum of half of

Â the magnitude spectrum.

Â So now, we can access individual frames of that, so if we put for example mX,

Â and we access for example the frame number 50.

Â And we plot the whole spectrum of that 50th frame,

Â these are the values of the magnitude.

Â And of course we can, if we call the function plot with that variable,

Â we will plot the magnitude spectrum of the particular location.

Â Okay, so this is the flute sound, we see the harmonics and

Â we see how the higher values are much softer and it's quite noisy.

Â Of course we can also plot the phase spectrum of that

Â same location, this is the phase.

Â The ungraph phase, so we can see all these a nice looking face shape, okay.

Â Of course we want to visualize the whole spectrum, so

Â in order to visualize the whole spectrum the plotting command

Â that can do that is the what is a the function is called pcolormesh.

Â Pcolormesh it plots a matrix as a kind of a three

Â dimensional shape, so if we give it p and

Â mX it will basically plot all the frames.

Â But it maybe plotted in a different way that we are accustomed to

Â because the vertical is the axis is the time.

Â So in fact, we see time going from bottom to top and

Â the frequency is on the horizontal axis.

Â In fact a better way to plot this with colormesh is transpose it first,

Â so we can call the transpose function.

Â So this will transpose the mX in a different way,

Â so that we can see it in a more meaningful way.

Â And so now we see time going from left to right and frequency going,

Â or the frame, yeah, frequency going from bottom to top.

Â Okay, so that's basically how we compute the magnitude and

Â the phase spectrum using STFT and how we can plot it.

Â And there is a function file that implements all that and

Â makes plots in a nice way and that's the STFT_function.

Â And this is the one that is call from the graphical interface that we also use and

Â here it has one function called main and it goes through the kinds

Â of things we did and it does both the analysis and synthesis.

Â Okay, so the main has its input values all the input parameters and

Â then it reads the sound file, it gets the window It performs analysis.

Â This is exactly what we did now but then it does the synthesis and

Â it output the synthesis file into a location with

Â a given name and then it writes it, okay?

Â By the way, here this file I just copied into the work-space but

Â I had to change the relative path of a number of these names.

Â So for example, I have to change the path of the software models or

Â I have to change the output file name because in here it.

Â It was using particular directory to store the sound files, so

Â here, I just write the directory on this directory.

Â Okay, so let's execute this, that's why I have it in this directory.

Â So, it's STFT function, so I can just run STFT function, okay.

Â And it plots, well, the input sound, the magnitude,

Â the derivative of the phase and the output sound.

Â And this has created a file which is

Â called what is it called is called piano stft.wav and

Â of course, from here again,

Â I can play so, I can say play stft,

Â I know piano, piano Stft.wav.

Â [MUSIC]

Â Okay, and it's basically the same than the original sound.

Â Of course now, from this file I can really change and

Â make the things differently if I want.

Â So feel free to change how the plots are shown but

Â these are quite nice way to plug the things with the correct access,

Â with the correct labels, with the correct scaling.

Â So that we can better visualize the analysis and

Â synthesis of using this TFT and that's all I wanted to say.

Â