0:18

many sounds that are not quite well modeled with sinusoids or

Â with harmonic analysis approach.

Â That there are signals that are very noisy,

Â that are what we call stochastic that require a different modeling approach.

Â 0:36

This modeling approach is based more on statistical signal processing on.

Â Describing the sound from a probabilistic point of view.

Â In this programming class, I want to go over that from a programming perspective,

Â so I'll present the models that we have been implementing in the SMS tools,

Â and that implement this idea of stochastic modeling.

Â 1:02

So we'll basically be talking about this plot diagram in which we

Â start from a signal and then after windowing it and computing FFT,

Â we just keep the magnitude spectrum in stochastic signals,

Â the phase spectrum is not relevant.

Â And so the approximation is based on the magnitude spectrum

Â that then it's approximated with a smooth curve, and

Â this smooth curve is what we call the Stochastic model.

Â And from that we can reconstruct, or we can regenerate a signal,

Â 1:39

by sort of reconstructing a magnitude spectrum and

Â then generating phases randomly so that we can create another complex spectrum

Â that captures some of the essence of the original sound.

Â And then by overlap adding, we can generate a signal that

Â hopefully if the original signal had this stochastic properties will be

Â quite similar to the original sound.

Â So let's go to text editor and here I wrote a very short

Â script that implements this idea of

Â approximating a fragment of a sound with this stochastic model.

Â Okay. So we start by importing a few

Â packages that we need.

Â And then the core of the code is basically reading a sound file,

Â this is the ocean sound file that we already have been using,

Â so it's clearly a stochastic type of signal.

Â And then in order to analyze the spectrum of it we have to define

Â the FFT size and the window size in this type of sounds and

Â this type of situation, there is no need to zero pad.

Â There is no need to have odd-sized windows.

Â We have M, the window size, and N, the FFT size,

Â of the same size, which is 256.

Â Then the key perimeter is this what we call the stochastic factor which

Â is basically the smoothing factor of the down sampling factor.

Â So point 2 in this case that we defined,

Â it means that we are basically reducing the information by a factor of

Â 0.2 and getting only that kind of the surface information.

Â 3:43

Then we get the window.

Â In this case, we get a humming window, but

Â it can be any type of window, it doesn't have to be very complex.

Â In fact I would say that maybe it would be easier to just use the hunting,

Â 3:57

because it is even simpler, and it works.

Â Okay, then we need to select the portion of the sound we are going to analyze.

Â In this case we chose a section in the sample 10,000 and

Â then plus the window size and we multiplied by the window.

Â 4:31

And we do it by using this Python function called resample.

Â Resample is a function that basically uses an FFT approach,

Â so it uses a spectral analysis approach to downsample a signal,

Â or to resample a signal.

Â Roles we can down sample or up sample a signal.

Â So we give a function, in this case mx, and

Â we just make sure there is nothing below -200 decibels.

Â 5:04

It wouldn't make any sense.

Â And we specify the new length that we want to create.

Â So the regional length was of size n over 2,

Â because we're only using half of the spectrum.

Â Okay, so from n over 2 samples, we are going to generate n over

Â 2 multiplied by this stochastic factor approximation.

Â So basically, if this is 0.2,

Â you're going to have one-fifth of the samples that we started with.

Â Okay, let's run this little script.

Â So we will run test.

Â And we can plot the signal that we start with,

Â the windowed fragment that we start with.

Â So this is the fragment of the ocean sound already windowed by a humming window.

Â 6:02

Okay, and then we are computing the spectrum of that.

Â So the spectrum is this mx array.

Â Okay, so this is magnitude spectrum of that.

Â And now we can plot on top of that the smooth approximation or

Â the down sample approximation of that, so which is mXenv.

Â But in order to make sure that it's shown on top of this array,

Â which is a longer size, we have to plot it differently.

Â So we have to say

Â mp.arrange of mxm.

Â The size of that.

Â And then if we dividie by stock factor,

Â this should stretch these points

Â to the size that we had of the MX.

Â 7:04

Okay, so yes.

Â So here we show in green the approximation, the smooth approximation.

Â Of course it has less samples, so mxm has the size of it is 25 samples.

Â So from the 128 samples that the positive

Â side of this magnitude spectrum has,

Â we have reduced it to 25.

Â Okay?

Â Of course, if we change the stochastic factor,

Â we will have more or less samples to be approximated.

Â But clearly this green line is an approximation to the blue line.

Â 7:51

Okay, so this is kind of a stochastic approximation that

Â we're going to be using thus.

Â And now let's go to this other script.

Â And what we will be doing is to actually synthesizing a sound from this.

Â So we have the same first part up until here, okay, so we generate the envelope.

Â And now what we're going to do is to resample again

Â 8:37

And then to create the whole complex spectrum, we will have to

Â deal with the symmetry so that we will recreate the magnitude and phase for

Â the negative frequencies and of course convert it to complex numbers.

Â So the positive spectrum, the positive complex spectrum would be 10 to the power

Â of the magnitude multiplied by the exponential of the phase.

Â 9:06

Okay, well this is of course the conversion from DV to linear scale.

Â And then for the negative part, we do the same thing but

Â we reverse the order and the phases that of that.

Â And then, we just take the inverse FFT of the complex spectrum.

Â So let's run this, this is Test1 okay?

Â And in here we have the magnitude spectrum that we started, MX.

Â Okay so this is the original spectrum.

Â And now we can plant on top of that the approximated and

Â that up sample to have the same size.

Â In fact and my has the same size as this.

Â So now this green line started from the samples that we showed before.

Â So it was a more jagged line and had fewer points.

Â And now it has converted to a smooth version

Â that has the same size than the regional spectrum.

Â And of course the phase spectrum, if we plot the p-y,

Â this is the phase spectrum that we generated,

Â which is just random numbers from 0 to pi, so 2 to pi.

Â And by taking the inverse of 50, we can just obtain the output signal.

Â So this is the synthesized signal from that approximated spectrum.

Â Of course this is different from the xw that we have started from.

Â 10:50

So that now the green is the original signal we started from.

Â Clearly very different, but that's not the point here.

Â Because, being a stochastic signal,

Â the shape in the time domain is not that relevant.

Â What is important is this general distribution in the magnetic spectrum.

Â 11:09

Okay, so now let's look how the actual analysis and

Â synthesis work for longer sounds.

Â So in here, we have the stochastic model.py file,

Â in which we have different functions that implement

Â the analysis of a complete stochastic signal and

Â the synthesis from the analyze stochastic representation.

Â And basically does, they do what I have done for

Â a single frame in which the stochastic model are null, it start from the signal,

Â the size, size, and the stochastic factor.

Â And it iterates over the whole sound.

Â And it does what we just did, it performs FFT.

Â It finds the DB magnitude spectrum then it uses the resample function to

Â down sample it to a fewer samples depending on the stochastic factor.

Â And it keeps attending these to a sequence of envelopes.

Â Because we're going to have a time-varying envelope for

Â the stochastic representation.

Â And of course, the synthesis does again, what I showed before,

Â it has to up-sample the magnitude spectrum to the complete size,

Â it has to generate the random phases, and it has to convert to the complex spectrum.

Â And to overlap that to recover the whole signal.

Â So let's call this for a particular sound.

Â So in here I wrote a little script that takes that ocean sound and

Â it calls the stochastic model now, so it analyzes the whole sound.

Â Okay, and it is a hop size of 128.

Â The FFT size, it used to be twice as that instead of specifying an FFT size,

Â the default is normally to just take twice the hub size.

Â And then the restore in the stochastic factor which is 0.2.

Â So now if we ran this test 2.

Â 13:23

Okay, now we have created stac m that if we looked at the shape of that,

Â that's a metrics that has 2,217 frames.

Â And each frame has 25 points which are the points of the approximation.

Â And this, we can look at the individual frames,

Â but basically what we normally do is we plot

Â it with pcolormesh, okay the pcolormesh.

Â And we'll display this array in like a color 3D type of plot.

Â Of course, this is shown in the wrong direction,

Â we are accustomed to see time in the horizontal axis.

Â So what we normally do is show

Â it as with np.transpose.

Â Okay, so this will reverse the plot and

Â it will show the x-axis as time and

Â the vertical axis as frequency.

Â Okay, so this is the plot of the spectral

Â representation of these coefficients of the stochastic representation.

Â 14:59

function that basically does this stochastic analysis and

Â synthesis for a complete sound and at the same time it plots it nicely.

Â This is the code that is used from the interface and

Â it plots a nice spectrum realm of the stochastic representation.

Â Same here.

Â We have this main function that has some parameters by default.

Â It reads a sound.

Â It performs the analysis we just did.

Â And it does the synthesis from the whole analysis.

Â 15:37

Then it outputs a sound file and then it plots the different components,

Â the input sound, the stochastic representation and output sound.

Â So let's run these,

Â run the stochastic model function.py, okay.

Â And these plots, input sound, the stochastic approximation, and

Â the output sound.

Â And of course we can, now it has saved the file,

Â called ocean stochastic model.wav, and

Â I can just play it by typing play, and ocean, okay.

Â [NOISE].

Â Okay, so this has played the complete sound.

Â And as we showed before, well it's quite similar even though it's not identical,

Â it definitely sounds like an ocean sound.

Â And even though it has lost some of the low frequencies,

Â so we might have to change a little bit the stochastic approximation in order to

Â recover the complete sound a little bit better.

Â 17:02

So we talked about this stochastic model implementation that we have

Â in the sms-tools package.

Â And of course, we use quite a bit of the Python functions for

Â those from numpy, scipy and also the plotting of matplotlib.

Â 17:21

So we have seen how to implement

Â the stochastic model that we talked about in class, in the theory class.

Â And this will be one important component to then, now in the next classes

Â to incorporate it into the harmonic or sinusoidal plus stochastic representation.

Â So therefore, we will be using this model to analyze

Â the residuals that we obtain by subtracting the sinusoids or the harmonic.

Â So I hope to see you next lecture.

Â Bye, bye.

Â