Alf P. Steinbach wrote: > * Steve Holden: >> It's not clear to me that you can approximate any waveform with a >> suitable combination of square waves, > > Oh. It's simple to prove. At least conceptually! :-) > > Consider first that you need an infinite number of sine waves to create a > perfect square wave. > > The opposite also holds: infinite number of square waves to create a > perfect sine wave (in a way sines and squares are opposites, the most > incompatible).
No, it doesn't. The infinite set of sine waves that make a square wave leave out the sine waves of frequency 2f, 4f, 6f, 8f, ... (2*n*f) ... . Once you've left them out, you can never get them back. So sawtooth waves, for example, can't generally be built out of sets of square waves. You can inject even harmonics up to a given limit by adding "rectangular waves" with given duty cycles, but the "given" part makes the math grubby. Fourier transforms are cute to play with. If you don't care about run-time there's: #!/usr/bin/env python # -*- coding: ASCII -*- ''' $Id$''' import math def dft (sample): '''Discrete Fourier Transform''' n = len (sample) n21 = n / 2 + 1 twopi = math.pi * 2.0 sin = math.sin cos = math.cos rex = [0]*n21 imx = [0]*n21 for k in xrange (n): for i in xrange (n21): a = twopi * k * i / n rex[i] += sin(a) * sample[k] imx[i] -= cos(a) * sample[k] return rex, imx #~ wave = [1]*32 + [-1]*32 # rectangular duty-cycle 1/2 #~ wave = [3]*16 + [-1]*48 # rectangular duty-cycle 1/4 wave = [7]*8 + [-1]*56 # rectangular duty-cycle 1/8 #~ wave = [15]*4 + [-1]*60 # rectangular duty-cycle 1/16 #~ wave = [31]*2 + [-1]*62 # rectangular duty-cycle 1/32 rex, imx = dft (wave) print rex print print imx The real coefficients show how the 8th, 16th, 24th, 32nd harmonics -- where the coefficients are near zero -- have dropped out of the waveform. Mel. -- http://mail.python.org/mailman/listinfo/python-list