0:01

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

Â In this programming class of the third week of the course,

Â I want to introduce the actual code that is within

Â the sms tool's package that implements analysis and synthesis using the DFT.

Â In particular, what we're going to show is

Â 0:57

Okay so let's go to the editor and let's show

Â the dftModel.py file that is within the model's

Â subdirectory of the sms tool's package, okay?

Â In this file you will find three functions,

Â three function definitions.

Â The first one is called dftModel and this is one that implements whole analysis and

Â synthesis so the input is the input sound, x.

Â The window that we're going to multiply by and

Â the FFT size capital N and

Â it will return the output sound y, okay?

Â And then many times we just need the analysis part or the synthesis part.

Â So in this file we also find just the analysis.

Â So for the same input x, the window w, and

Â capital N, it returns the magnitude and phase spectrum.

Â And the DFT synthesis, the function the implements

Â the synthesis part has an input, magnitude, and phase spectrum.

Â And the size of the sound to be generated of the windowed sound,

Â and it will return y, which is our output signal.

Â Okay, and within that basically it has the code

Â that we talked about in the previous programming class.

Â In which we just have to window the signal and put it the signal

Â in away that is centered around zero, what we call zero phase windowing.

Â So for example let's look it at the dftAnal

Â function which is very much like we showed in last class.

Â So this beginning is declaration and creation of some variables.

Â 3:26

And then we will generate the magnitude and phase values by

Â taking the log for the magnitude and unwrapping the phases.

Â So that we'll get the nice looking resulting spectra.

Â And in the synthesis, it basically does the inverse.

Â So again we have to declare all these variables.

Â And then, well we have to initialize the output sound.

Â And a major difference is that the input, the magnitude and

Â phase spectra, are only half of the values.

Â Since the spectra are symmetric,

Â the dftAnal function only returns the positive frequencies.

Â 4:14

Therefore, in the synthesis,

Â we have to recompute the negative frequencies from the positive ones.

Â And of course, since our symmetric is very simple.

Â The positive frequencies are just direct so getting the magnitude and phases,

Â we just convert to rectangular to complex numbers.

Â So by using this kind of euler's formula and then for

Â the negative frequencies, we just get those.

Â But doing it, reading it in a reverse way so that we get the symmetry aspect of it.

Â And of course the phase we negated so that we get negation of the phases.

Â And that's it, then we obtain the inverse fourier transform

Â by calling the IFFT function that gives as input the complex spectrum.

Â Then we just get the real part because we want the real signal out of that.

Â And then we have to undo this zero phase, this centering around zero so

Â we get back the signal as we had it originally.

Â Okay, so you can look at these with more detail,

Â both the synthesis and the analysis of functions.

Â I think they're quite well explained here so you can have a grasp of them.

Â But in order to test them, I've created a small program.

Â 5:48

Okay, in which I get all these packages.

Â I specify the directory where these models are,

Â both the utility functions that I'm going to use under the dftModel.

Â So from the current directory, I have to specify this relative path

Â to the model directory so that it finds these two packages,

Â utilFunctions and the dftModel Package, okay?

Â And now we can start reading a file.

Â So I read piano sound file.

Â Also relative to this current directory, that's where the sounds are.

Â And I use this function that already I have been using to

Â return an array of floating point values.

Â Then, I use a function that generates the windows.

Â Okay, get window allows us to generate smoothing windows,

Â and we'll talk more about it next week.

Â 6:53

In this particular case, the hamming window of a given size, 511 and

Â it returns an array of these values.

Â Okay, then we need to get the fragment within the input array that

Â I'm going to analyze.

Â So we start at second 0.2 and then I have to convert to samples,

Â so I multiply it to the sampling rate and I get the samples of x that

Â start at second 0.2, and that last for M samples.

Â 7:25

And now we are ready to compute the DFT of that.

Â So I call the dftAnal function from that module that we just saw,

Â the package we name it DFT, so we call the dftAnal.

Â And I pass it the fragment of the sound.

Â The window that I want to use, and the size of the FFT,

Â which I gave the number 1,024.

Â And then I can send the output values to the dftSynth,

Â 8:07

There is a whole bunch of normalization factors that have to be used and

Â sometimes are not so intuitive.

Â But anyway, I can check it and

Â can make sure that you need this normalization factor.

Â Okay, so now from the terminal, I am in the work

Â space and I have these files, the test4 there.

Â So I can start iPython with the Pylap command so

Â that I get the matplotlib package

Â into that and I can run the test4.

Â Okay that executes that, so let's check that we did the things right so

Â for example let's plot the x which is going to be our input signal.

Â Okay, that's the whole piano signal, a little bit long.

Â More than, close to 17,000 samples.

Â No, 170,000 samples.

Â Okay, we can plot just the fragment that we chose, x1.

Â Okay, this is just a fragment of starting at 0.2.

Â We cal also plot that smoothing window that we applied.

Â So we can plot w.

Â This is our window that we're going to multiply by the signal.

Â 10:04

pX and that's the phase, unwrapped phase spectrum that we have.

Â Okay, so that's basically the whole thing that we want to show.

Â And then there is one another file in the model's interface directory.

Â That is the one that basically integrates the analysis, synthesis and

Â makes nice plots.

Â And in fact, this is the one that is being called by the interface

Â that we are showing in the demo classes.

Â So this is the DFT model function file,

Â that again it imports all these packages the same way that we did.

Â And then it has one single function, a main that has a predefined

Â variables that of course they can be changed and that we can compute.

Â And then it does the computing as we have done now, and

Â then it plots several figures with correct plotting axis and labels, etc., etc.

Â So I copied this file into the work space.

Â So I have it as a dftModel function.

Â So if I run this, it will execute

Â the default parameters and it will compute and plot this.

Â Okay, so this is the result of this analysis,

Â synthesis of a fragment of the piano.

Â And here now it shows in a nice way with the labels,

Â the time in seconds, the frequency in hertz and

Â the amplitude explained in decibels, the phase again,

Â and then the output signal, which is the window version of the input signal.

Â And of course as we showed in another class, we can zoom in

Â into fragments of this to visualize certain aspect of that.

Â So we can understand some particular harmonic how it behaves.

Â 12:43

directory that you have find here in the sms tools that you download.

Â You can look at there's quite a bit of information about that and well explained.

Â And of course the sounds that we're using come from freesound and

Â of course there are also in within the sms tools directory.

Â So it's easier to have access to.

Â That's all, so with this we have seen how to program the DFT.

Â And how to use the actual existing code

Â of the sms tools to perform the analysis and synthesis.

Â And hopefully this has allowed you to understand better the DFT.

Â And this will be fundamental for understanding the next step,

Â which will be next week which is going to be the short time fourier transform.

Â We're going to put together this DFT analysis into

Â an analysis into this framework that really works for any sound.

Â And that allows us to analyze and

Â synthesize larger sounds not just small fragments.

Â So this is all for today and I hope to see you next time, bye bye.

Â