# How to smooth a script-generated Sine wave's frequency?



## magneto538 (May 4, 2017)

Hello folks,
This is a math-related question.

I am using the majestic new Math features in Kontakt 5.6.5. Basically I am building an LFO using the script (so without the LFO module, which - for a number of reasons - is not an option in this case). I am using the sin() function to generate the wave - I am using the wave formula sin(2π*freq*time + phase) to accomplish this. This function is placed in the listener callback, so that a continuous sine wave is generated.

The sine wave is generated correctly. The problem occurs when I change the frequency of the wave with a slider: when the slider is moving, the value of the wave is forced to the current time, therefore clipping occurs. This happens because at a given time _t _the value of sin() is different for each frequency.

So I have a bunch of options here. The idea I had was to apply a phase shift in order to match the correct value of sin(), which means that when I am changing the frequency, the phase of the new wave is shifted so that at time _t _the value of sin() remains the same (which is the whole point of it actually). I am not sure how to accomplish this though.

I think that what I'm looking for is some kind of a 'smoothing' function. How can I accomplish this?

EDIT: attached is a GIF of the issue. When you see the weird stuff going on on the oscilloscope, that's when I am changing the frequency.


----------



## Darius (May 5, 2017)

I would be in favour of rationalising your waveforms into 2pi radians - allowing you to use phase to get the right value. This would mean calculating amplitude with sin(2pi*x), x being phase. Phase would be calculated iteratively, adding on an amount each time. This amount depends on your input frequency and sampling rate. Off the top of my head this step size should be something as simple as frequency/samplerate (it should be a fraction of 1.).

A smoothing function is likely to cause problems in amplitude and phase in different frequencies.


----------



## magneto538 (May 5, 2017)

Darius said:


> I would be in favour of rationalising your waveforms into 2pi radians - allowing you to use phase to get the right value. This would mean calculating amplitude with sin(2pi*x), x being phase. Phase would be calculated iteratively, adding on an amount each time. This amount depends on your input frequency and sampling rate. Off the top of my head this step size should be something as simple as frequency/samplerate (it should be a fraction of 1.).
> 
> A smoothing function is likely to cause problems in amplitude and phase in different frequencies.



Thank you for your answer Darius. Actually I got to the same conclusion and already set up things in the way you suggested. A topic that helped me quite a lot about this can be found on https://dsp.stackexchange.com/questions/124/how-to-implement-a-digital-oscillator .


----------



## Darius (May 5, 2017)

Great work - it would also be fun to add 'harmonics' to your LFO! You should be able to add together a series of sin(2pi*x*harmonic), where 'harmonic' is an integer, to make a differently shaped waveform. For example, you can add odd harmonics to make the waveform more square.


----------



## magneto538 (May 9, 2017)

Darius said:


> Great work - it would also be fun to add 'harmonics' to your LFO! You should be able to add together a series of sin(2pi*x*harmonic), where 'harmonic' is an integer, to make a differently shaped waveform. For example, you can add odd harmonics to make the waveform more square.



Definitely a nice idea - although it's not necessary for what I'm working on. 
Using the principle stated above, I successfully got to have ramp, sine, unipolar sine and triangle wave, and that's enough for me at the moment. I will consider adding these into Koala so that these resources could help someone else.


----------

