# Wavetable Waveforms

A wavetable, simply is a table of values that will create a waveform when looped through via a microcontroller and sent out via a DAC.

A square wave is the simplest, the table could be as easy as SquareTable[] = {0,1};
You would then set a timer going that would look at the values and set a pin to the value in the table, i.e on or off

#### How it works

A timer runs firing an interrupt ever so many microseconds.. Each time the interrupt is run it loops through the wave table and sets the DAC to the output at that step. (DAC is A14 of the Teensy 3.2)
To get the output frequency we want we need to calculate it as follows:

We want 100Hz
We divide the frequency by 1,000,000 (1 million is how many microseconds in a second, Hz is cycles per second)

Then we take that value and divide it by the number of samples
The value then is how often in microseconds to fire the interrupt
So intervaltime =  (1,000,000 / freq) /samples

We need to trigger that interrupt every 77.51 microseconds
The teensy handles floating point numbers so this gives us a good accurate output frequency)

##### Frequency and Resolution

There is a trade off between quality of the waveform (resolution) and the max output frequency. I’m using 129 steps, and a max output of 128 (1.67 volts) This means I can have a decent frequency and a decent waveform, having too few samples makes the output more sketchy. Above: Zoomed in sine wave, as you can see the staircase effect, this is where the sample on the x axis is changing the voltage on the y. Having more samples will reduce this but will slow the max frequency down because the MCU has to loop through more samples (remember the calculation above) Above: My generated sine wave, 129 samples and max 128 amplitude. This is good up to 3KHz with my code