In audio synthesis, the sine wave is a fundamental element in many algorithms, sounds, and effects. A sine wave can be used as an LFO (low-frequency oscillator) to control various elements of a sound like its amplitude or frequency. Sine waves can also be used for additive synthesis, such as generating band-limited versions of saw, triangle, or square waves. Many audio processing applications are also real-time applications, so the computation needs to happen quickly and consistently.

While doing research into audio processing and functional reactive programming, I stumbled upon the work of Paul Hudak. His work on the *The Haskell School of Music* and *Euterpea* are inspiring. In his paper *Audio Processing and Sound Synthesis in Haskell (2009)* with Eric Cheng, a section is devoted to the optimizing of signal functions, sine waves in particular.

While the sample code in the paper is in Haskell, we're going to be working with Rust as it's what I use for audio processing.

## Initial implementation

A straight-forward method for generating a sine wave is to use the standard `sin`

function. In order to generate a sine wave of the frequency $f$, we'll use the equation $\sin(2 \pi f t)$, where $t$ is a discrete notion of the current time (which we can calculate using the sample rate $f_s$ and a count of the *n*th sample).

```
struct Sine {
n: usize,
frequency: f64,
fs: f64,
}
impl Sine {
fn step(&mut self) -> f64 {
let &mut Sine {
ref mut n,
frequency,
fs,
} = self;
let t = *n as f64 / fs;
let value = (2.0 * PI * frequency * t).sin();
*n += 1;
value
}
}
```

## The Goertzel algorithm & fixed rate oscillators

The Goertzel algorithm is used to detect a single frequency/tone in a waveform. A common application of the algorithm can be seen in analog telephones where each button generates a unique tone and the Goertzel algorithm is used to detect from the tone which button was pressed. The Goertzel algorithm computes a value of $\sin(a + nb)$ using the previous two values in the series. With some algebra and trigonometry, we can find an equation which holds for all *n* and can be used to develop a fixed-frequency sine wave generator that creates the next value in the series from the two previous.

So, here's the algorithm:

In order to use it for our sine wave generator, we need to solve for an *x* and *y* that hold for all *n*. Using the identity $sin(a + b) = sin a cos b + cos a sin b$, we can rearrange the equation like so:

Therefore:

In order for the equation to hold for all *n*, we need the following to be true.

Solving for *x* and *y* yields $x = -1$ and $y = 2 \cos b$. When we apply that to the original equation, we get:

Now, assuming that the initial phase offset for our sine wave generator is 0 ($a$):

Hooray! Given that our sine wave generator has a fixed frequency, $f$ and therefore $w$ will not change for each calculation of the next sample. This allows us to cache $w$ along with the previous two samples and therefore compute the next sample of the sine wave with only one multiplication and one subtraction!

## Fast implementation

Now, let's turn this into code. We can break the above equation down a bit more to simplify the creation of a sine wave generator $S$:

We'll initialize our `delay`

lines with $S(0)$ and $S(1)$ along with $c$. Then, for each `step`

of the wave, we'll calculate the next sample `s`

, update the `delay`

lines, and return `s`

.

```
pub struct Sine {
c: f64,
delay: [f64; 2],
}
impl Sine {
pub fn with_frequency(frequency: f64, fs: f64) -> Sine {
let tau = 2.0 * PI;
let omh = tau * frequency / fs;
Sine {
c: 2.0 * omh.cos(),
delay: [0.0, omh.sin()],
}
}
}
impl Sine {
fn step(&mut self) -> f64 {
let s = self.c * self.delay[1] - self.delay[0];
self.delay[0] = self.delay[1];
self.delay[1] = s;
s
}
}
```

And there we have it!

## References

- Eric Cheng and Paul Hudak. Audio Processing and Sound Synthesis in Haskell. 2009.