Help with Fourier synthesis of a sawtooth wave

Hi there,

I’m carrying out Fourier analysis and synthesis on square and sawtooth waves as part of a university project, however, I’ve run into a problem and am unsure whether I have done something incorrectly or not. I’ve been effectively told “generate some waveforms and see what happens”, so my knowledge of Fourier is still very limited.

Using FFT in Audacity, I was successful in building a 1 KHz square wave out of sin waves. However, when I attempted the sawtooth wave, I found that the resulting shape of my waveform was inverted (see picture below).

The top waveform is a 1 KHz sawtooth wave created using the tone generator.
The bottom wave is my reconstructed wave using the first 5 harmonics. As you can see, the shape is inverted.

Have I incorrectly analyzed the waveform or is something else happening here?

Your analysis appears to be correct, but the starting phase for your sine waves is reversed (rotated 180 degrees).

Nyquist can be quite useful for playing with this sort of thing.
(also: http://wiki.audacityteam.org/wiki/Nyquist_Plug-ins_Reference)

This code can be run in the “Nyquist Prompt” effect:

(setf out (s-rest 1))
(setq f0 1000.0)
(setq k 6)

(dotimes (n k (mult (/ -2 pi) out))
  (setf out 
    (sum out 
         (mult (/ 1.0 (1+ n))
               (osc (hz-to-step (* (1+ n) f0)))))))

Here is the comparison between a measured sawtooth (256 Hz, FS = 8192) and the calculated values from Steve’s code (in dB).

Frequency	Level (meas.)	Level (calc.)
256	-3.94664	-3.922397541
512	-9.92532	-9.942997454
768	-13.3771	-13.46482263
1024	-15.7774	-15.96359737
1280	-17.5883	-17.90179763
1536	-19.0154	-19.48542255
1792	-20.1678	-20.82435834
2048	-21.1103	-21.98419728
2304	-21.8843	-23.00724773
2560	-22.5175	-23.92239754
2816	-23.0292	-24.75025124
3072	-23.4329	-25.50602246
3328	-23.7383	-26.20126459
3584	-23.9521	-26.84495825
3840	-24.0787	-27.44422272
4096	-30.1412	-28.00479719