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.