# [PD] Creating a basic oscillator from first principles?

Frank Barknecht fbar at footils.org
Sat Feb 7 19:12:52 CET 2009

```Hallo,
Geoff hat gesagt: // Geoff wrote:

> Its really bugging me everytime I create an example from the book. I
> feel I need to understand the fundamentals of how the basic objects
> are created too.
>
> It seems too easy for me to just use the phasor object provided,

But that's much more effective.

> So I thought I would struggle through it and try to work it out.
>
> This is how I am thinking about it.
> If I want to create a sawtooth oscillator then this is just a ramp
> function i.e. a number that increases by 1 each time.
> therefore if I use a simple float that, that adds 1 each time and a
> modulus % object to cap it, then I will have my repeating ramp wave :)

Yes, that's correct. You should not use a graphical bang here, as that
is a huge (!) waste of ressources.

Anyway generally you can make your approach a bit simpler, too. Instead
of always adding 1 and scaling plus restricting your counter to a large
value, you can also add a smaller value and restrict the output to lie
between 0 and 1.

Example: Say you add one and always go to f=100:

x[n+1] = [(x[n] + 1) % f] / f
x[n+1] = [(x[n] + 1) % 100] / 100

Then instead you can also do this:

x[n+1] = [(x[n]/f + 1/f) % f/f]
x[n+1] = (x[n]/100 + 1/100) % 1

or, as x[n] is restricted anyway do:

phasor = (x[n] + 1/100) % 1

The 1/f part is called the "phase increment".

Note that you need to use a kind of "fmod" for floating point numbers
here, i.e. replace the "%" with a calculation to get the fractional
part:

fractional part of f = f - int(f)

> However I am triggering it by the metro object which is way too slow,
> how can I send a bang to the float object say every sample? or alot
> quicker than one millisecond?

Use a [phasor~] ;-)

You can also built your own fast metro using feedbacked [delay] objects.
I posted one a while ago here.

Ciao
--
Frank

```