Hello, welcome back to the course on Model Signal Processing for Music Applications.

In the previous programming lecture,

we talked about the HPR model, the Harmonic Plus Residual model.

That was the idea of modeling a sound with a harmonic and residual component.

And we talked about the implementation in the SMS tools package.

Now, we want to go a step further and talk about the hpsModel,

the Harmonics plus Stochastic model.

So now, we are modelling the residual that we obtained with

the stochastic modeling approach.

So, we obtain a harmonic and stochastic representation of a particular sound.

So the diagram, the block diagram, is very similar.

The whole thing for the analysis is the same

until we obtain the residual spectrum of X, Xr[k].

And then instead of just going and summing it with a harmonic component,

what we are doing is approximating it with a stochastic approximation.

So, by assuming the magnitude spectrum and

then the synthesis is done by generating random numbers for

the phase, and combining it with a sort of an interpolated version

of these approximations so that we generate a complete spectrum.

And this stochastic spectrum can then be added back to the harmonic component.

The great advantage of these stochastic approximation is that now

we are more flexible, we can do many more things on this

representation apart from being much more compact.

See, this is stochastic approximation,

reduces the amount of information that is required to reconstruct a signal.

So apart from being a compactor presentation, is a more flexible,

and will be able to do quite a few things with that.

So let's start from the same code that we looked at in the last lecture,

which was this code in which, well now I changed the sound.

Now I use the foot sound, but basically it performs

the harmonic analysis by taking a 50 and detecting the zero,

so it performs the harmonic analysis steps.

And then it synthesizes those harmonics and

it subtracts them from the orignal signal.

It recomputes the spectrum of the original signal with the same window size and

type than the one that is in this generated sinusoidal spectrum.

So we need to multiply it by a window, and

we need to choose the window size that was the same

then was used for the sinusoidal synthesis.

And, we can then subtract the harmonics from

the original signal and obtain the residual.

And now in this lecture,

basically we focus on the stochastic approximation of these residuals.

These three lines are doing that.

So, the first is we are converting it to a DV scale.

So, we are converting half of the magnitude spectrum to a DV scale and

we obtain a new magnitude spectrum.

And then we call this function called resample with this, my new spectrum,

and of course with values that are above a certain threshold.

And we are converting it to a shorter array.

And the size of this new array,

is the size that we started from multiplied by stocf.

And stocf is our stochastic factor approximation.

Basically, is moving approximation or down sampling factor.

So basically, it means that the output array

will be 40% of the original spectrum.

And then, we are up sampling again to the size that we

started with in order that then we can do the resynthesis.

So in fact, resample can be use both for down sampling and for up sampling.

And it uses the DFT approach.

So, let's talk a little bit about that.

So, we can go to do the documentation on the resample of function within scipy.

So this is the documentation, so it takes an array,

it takes the size of the output array.

So either less or more, so you can down sample or up sample.

And well, there is some optional arguments also that allow us to give a particular

window or to give more multidimensional array and process only part of that.

And since Python of course is all open source,

we can also see the source code of this function.

And as I said, well, every sample uses an frequency-response

approach to do downsampling and upsampling.

And what it does is, it performs first the FFT

of the input signal then it will basically cut the spectrum or

append samples depending if its down sampling or up sampling.

And then, once we have recomputed a longer or a shorter spectrum by cutting or

appending zeroes, we perform the inverse of 50.

That's a quite straightforward, simple way of performing

interpolation or downsampling or upsampling.

So we go back here, so this returning for

the first three samples, we will return less samples and

then afterwards we will convert it to this up sample version

that will be a smooth version of the spectrum we started with.

Let's run this, so we can see how that works.

So, let's run test4.

Okay and now we can plot the original spectrum.

So, mxr is already the dB version of the spectrum and half of it.

Okay, so this is our residual spectrum.

And then we plot on top of that the stocEnv, that's of the same size but

a smooth version because it's a result of having first down tempo.

And then, up sample with interpolation.

Okay so, as you see we are very much in fact if we zoom into any of these regions,

we see that the green line is the smooth version but

it is of course is smoother than the original one,

but it follows quite well, the original signal.

So, let's change the stochastic factor of 0.4 and let's put maybe 0.1.

So then, that will be a stronger smoothing factor.

And if we save this, and then we compute it.