Sunday 26 January 2014

Thoughts On Aliasing In Geometric Wave Form Generation (part 1)

Generating good digital sound is a never ending struggle against sampling issues. Even generating a simple waveform is not as easy as one might expect.
The spectrogram of the naive Audacity sawtooth and a band width limited Sonic Field generated saw tooth.
As a comment on my video 'The Sound Of Just And Equal Temperament' briankav made an interesting comment about aliasing in the generation of sawtooth waves. By generating a saw tooth at say 96000 samples per second (sps) some of the frequencies will be above the Nyquist limit of 48000sps. These frequencies will show up as negative frequencies which turn up in the signal as lower pitches with inverted phase.

OK - let me put that in a more straight forward way. If we generate a 4900Hz sawtooth then the 10th harmonic will be 49000Hz which is 1000Hz above the Nyquist limit for 96000sps. Just like the spokes on a wagon wheel in an old western movie, the frequency will actually turn up backwards (do you remember the wheels looking like they turned a reverse?). Instead of 49000Hz we get -1000Hz which is 1000Hz with reverse phase.

Ouch - so we make a sawtooth at 4.9KHz and we get a 1KHz frequency! That is not so good. Fortunately, most of the time the magnitude of these aliased frequencies is so low they are not a problem. They can however accumulate and produce 'mush' in the sound (for want of a better description).

Below we have a naive 96000sps saw tooth and one which is band width limited to be below the Nquist limit. Both are at 440Hz.
A band width limited sawtooth (top) and a naive sawtooth (bottom).
In the above image I show a true sawtooth as generated from additive synthesis in Sonic Field. It has a maximum harmonic of just over 20KHz. It does not actually look like a very good sawtooth! It is all wiggly and each cycle is slightly different from the previous one; nevertheless, it is the correct shape. The lower sawtooth looks much better (it was generated from the sawtooth tone function in Audacity). However, it is just wrong. We can see that from the spectra below.
The bandwidth limited sawtooth from Sonic Field 
The naive sawtooth from Audacity
A different form of naive sawtooth created from the MakeSawTooth processor in Sonic Field
A third, and also not so good, approach to making a sawtooth is to convert a sine wave into one using a cross over detector. That does not produce so many aliased frequencies but in instead it smears noise throughout the the signal. I _think_ this is due to frequency modulation of the sawtooth by the sample frequency. However, please don't hold me to that explanation!

Anyhow - here is the Sonic Field patch to make a pure sawtooth. The trick is to keep adding in harmonics until we get over 20KHz and then stop. That way it is just impossible to get any aliasing if the sample rate is high enough to be more than double the highest included harmonic.
{
    Bunch !signals
    ?pitch !o-pitch
    1      !cut
    {
        {
            (?length,?pitch)SinWave Invert   !signal
            (>signal,(1,?cut)/)NumericVolume !signal
        }Do !signal
        (>pitch ,?o-pitch)+       !pitch
        (>cut,1)+                 !cut
        (?signal,>signals)AddEnd  !signals
        ?pitch Println
        (?pitch,20000)lt        
    }
    loop
    ?signals Mix Normalise !signal
}!alias-free-saw

[
   Parameters
   ==========
   Length
   Pitch
]

...

440 !pitch
60000 !length
?alias-free-saw Do !sig
((>sig),"temp/temp.wav")WriteFile32
(((?length,?pitch)SinWave MakeSawTooth Normalise),"temp/x.wav")WriteFile32

No comments:

Post a Comment