Skip to content

The phase accumulator

Every synthesis page so far has an oscillator at its heart: a sawtooth to filter, sine waves to add, a carrier to modulate, a table to read through. This page is about how a computer (or a chip) actually makes one, a sample at a time, with almost nothing: a register and an adder. It’s called a phase accumulator, or direct digital synthesis (DDS), or a numerically controlled oscillator (NCO), depending on who’s building it: the SID chip has three, so does every soft synth, and so does every digital radio.

This page leans on Carving away harmonics for aliasing and Samples and wavetables for reading a waveform from a table.

An oscillator’s job is to say where in its cycle it is, each sample: its phase. Keep the phase as a number in a register of N bits, and every sample add a fixed step to it. When it passes the top it wraps round to the bottom, as a counter does, and the wrap is the start of the next cycle. That’s all.

A 4-bit accumulator: the phase is a number from 0 to 15, drawn as a dial, and every sample the step is added and it wraps round past 15. Below, the phase itself as a wave: a sawtooth. Step through it, or run it. A step of 3 takes 16 / 3 = 5.33 samples a turn, so at 48,000 samples a second it goes round 9,000 times a second. Past a step of 8 it goes more than half way round each sample, and looks like it’s turning slowly backwards: that’s aliasing (the wagon-wheel effect).

A turn is 2ᴺ, and each sample moves the phase on by the step, so it goes round step / 2ᴺ times a sample:

frequency = step × sample rate / 2ᴺ

and the step for a frequency is that turned round: step = frequency × 2ᴺ / sample rate, rounded to a whole number. The step is the frequency, in units of the sample rate / 2ᴺ. The wrap costs nothing: an N-bit adder simply drops the carry out of its top bit.

The step has to be a whole number, so the frequencies an accumulator can make are whole multiples of the sample rate / 2ᴺ. With few bits those are far apart, and the notes come out of tune.

How far each of the piano’s 88 notes comes out from where it should be, in cents (hundredths of a semitone), with the nearest whole-number step, for the accumulator’s bits and the sample rate. The low notes suffer most: their steps are small numbers, so rounding them is a big change.

With 16 bits at 48 kHz, one step is 0.73 Hz, and the lowest A, at 27.5 Hz, needs a step of 37.55: rounded to 38, it’s 20.8 cents sharp, enough to hear. With 24 bits the worst note is 0.085 cents off; with 32, a step is 0.00001 Hz and every note is exact to far better than anyone could hear. Bits are cheap, so oscillators use plenty: 32 is usual in software.

The SID chip’s accumulators are 24 bits, stepped a million times a second (its clock), so a step is 0.06 Hz: its lowest A is 1.4 cents off, inaudible. Its frequency register is the step: write the number for a note, and that’s the pitch, as on the SID page.

The phase, read as it is, is a sawtooth. The other classic shapes come from it with almost no arithmetic, which is why hardware loves this design:

  • sawtooth: the phase (its top bits, scaled to −1 … 1);
  • square: just the phase’s top bit: 1 for the first half of the turn, 0 for the second;
  • pulse: 1 while the phase is below a threshold, the pulse width;
  • triangle: the phase folded at its top bit (counting up through the first half, down through the second);
  • sine: the phase looked up in a table of one cycle (next section).

Top: the phase, rising and wrapping, 50 samples a turn. Bottom: the shape read from it. Listen to each at a pitch of your choice. On high notes the sawtooth and square sound harsh and out of tune: their sharp corners have harmonics far above half the sample rate, which fold back as aliases. The fixes, PolyBLEP and band-limited tables, are on Carving away harmonics and Samples and wavetables.

A sine wave has no shortcut from the bits: it needs sin(), which is expensive, or a table of one cycle, which is cheap. The table has 2ᵀ entries, and the phase’s top T bits say which entry to read. The phase’s other bits, below those, are simply dropped: the phase is truncated.

Dropping them is an error, and not a random one. The bits dropped go round in a pattern that repeats with the phase, so the error repeats too, and a repeating error is a tone: small extra frequencies called spurs, here and there across the spectrum. The bigger the table, the smaller the error, and so the smaller the spurs.

The spectrum of a sine (755 Hz at 48 kHz) from a 32-bit accumulator and a table of 2ᵀ entries. Look for the small peaks besides the tone: those are the spurs. Interpolate blends the two nearest entries in proportion to the dropped bits; dither adds a random amount, less than one entry’s worth, to the phase before looking up, which turns the spurs’ pattern into noise spread evenly. Listen to a tiny table: the spurs are audible as a buzz.

Measured on this tone, the loudest spur is about 6 dB below the tone per bit of table address: 23.5 dB for a table of 16 entries, 48.1 dB for 256, 72.2 dB for 4,096. (In theory 6.02 dB a bit, as the error is at most half a table step.) Interpolating between neighbours roughly doubles that: 96.3 dB for 256 entries. Dither doesn’t make the error smaller, but stops it repeating: the spurs melt into a flat noise floor, which the ear minds much less than tones. Each costs something: a bigger table more memory, interpolation a multiply, dither a random number.

A glide, vibrato, a new note: all of them just change the step. The phase carries on from wherever it was, at its new speed. Nothing jumps, so nothing clicks.

That’s less obvious than it sounds. The textbook formula for a sine, sin(2π f t), gives the right wave for one frequency, but change f half way through and the wave jumps: the phase it computes is for the new frequency all the way from t = 0, not from where the old wave had got to.

A tune, played by changing the frequency at each note three ways: changing the accumulator’s step and keeping its phase; starting the phase at 0 for each note; and working each sample out afresh as sin(2π f t). Around the first change of note: keeping the phase, the wave just bends; the other two jump, and every jump is a click. Listen.

At the first change of note, keeping the phase, the wave moves 0.038 between samples there, less than its own biggest step anywhere on the highest note (0.068). Starting the phase again, it jumps 0.489; with sin(2π f t), 1.082: more than half of the wave’s whole range, in one sample. That’s why oscillators keep a phase and add to it, rather than work it out from the time.

And it’s what makes FM possible: change the step every sample, by another oscillator’s output, and the phase follows smoothly, bending the wave’s speed up and down.

A digital radio needs oscillators too, at the frequency it’s tuned to, and they’re the same phase accumulator: there called an NCO. Two lookups of the same phase give a sine and a cosine, a quarter of a turn apart, which is exactly what’s needed to shift a signal down to zero frequency and keep both its halves (the I and Q of the modulation pages). Tuning is changing the step; locking onto a carrier (the Costas loop on Modulation, part 2) is nudging it a little each sample. In a chip, an accumulator, a sine table and a step register are a small circuit, which is why DDS chips make precise, instantly retunable frequencies for test equipment and radios.

  • An oscillator is a phase accumulator: a register to which a step is added every sample, wrapping round. frequency = step × sample rate / 2ᴺ.
  • The step is a whole number, so N sets how close it gets to any frequency: 16 bits at 48 kHz misses the low notes by up to 21 cents; 24 or 32 bits is plenty.
  • Sawtooth, square, pulse and triangle come straight from the phase’s bits; a sine from a table, whose truncated lookup leaves spurs about 6 dB per address bit down, less with interpolation, turned to noise by dither.
  • Changing the step keeps the phase continuous, so glides, vibrato and FM are free of clicks.
  • Analog Devices, A Technical Tutorial on Digital Signal Synthesis (1999): DDS in chips, spurs and all.
  • Joseph Tierney, Charles Rader and Bernard Gold, “A Digital Frequency Synthesizer”, IEEE Transactions on Audio and Electroacoustics (1971): where the idea was first written down.