in [Python]

Prev: dict's as dict's key.
Next: enhancing 'list'
From: Steve Holden on 14 Jan 2010 11:07 Alf P. Steinbach wrote: > * Steve Holden: [...] > With the goal of just a rough approximation you can go about it like this: > > 1. Divide a full cycle of the sine wave into n intervals. With > sine wave frequency f this corresponds to n*f sample rate for digital > representation. > > 2. Each interval will be approximated by a rectangular bar extending > up to or down to the sine wave. As it happens this (the bar's > height) is > the sample value in a digital representation. > > 3. In the first half of the cycle, for each bar create that bar as > a square wave of frequency f, amplitude half the bar's height, and > phase > starting at the bar's left, plus same square wave with negative sign > (inverted amplitude) and phase starting at the bar's right. And voil�, > not only this bar generated but also the corresponding other-way > bar in > second half of cycle. > > 4. Sum all the square waves from step 3. > > 5. Let n go to infinity for utter perfectness! :-) > > And likewise for any other waveform. > > After all, it's the basis of digital representation of sound! > > I'm sorry, but this is merely hand-waving. It looks appealing, but there's no rigor there. regards Steve -- Steve Holden +1 571 484 6266 +1 800 494 3119 PyCon is coming! Atlanta, Feb 2010 http://us.pycon.org/ Holden Web LLC http://www.holdenweb.com/ UPCOMING EVENTS: http://holdenweb.eventbrite.com/
From: Mel on 14 Jan 2010 11:09 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 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.
From: Alf P. Steinbach on 14 Jan 2010 11:14 * Mel: > 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. The way to build a sine wave out of square waves is not a Fourier transform. See the post you replied to for a simple procedure to build the sine wave. Cheers & hth., - Alf
From: Alf P. Steinbach on 14 Jan 2010 11:14 * Steve Holden: > Alf P. Steinbach wrote: >> * Steve Holden: > [...] >> With the goal of just a rough approximation you can go about it like this: >> >> 1. Divide a full cycle of the sine wave into n intervals. With >> sine wave frequency f this corresponds to n*f sample rate for digital >> representation. >> >> 2. Each interval will be approximated by a rectangular bar extending >> up to or down to the sine wave. As it happens this (the bar's >> height) is >> the sample value in a digital representation. >> >> 3. In the first half of the cycle, for each bar create that bar as >> a square wave of frequency f, amplitude half the bar's height, and >> phase >> starting at the bar's left, plus same square wave with negative sign >> (inverted amplitude) and phase starting at the bar's right. And voil�, >> not only this bar generated but also the corresponding other-way >> bar in >> second half of cycle. >> >> 4. Sum all the square waves from step 3. >> >> 5. Let n go to infinity for utter perfectness! :-) >> >> And likewise for any other waveform. >> >> After all, it's the basis of digital representation of sound! >> >> > I'm sorry, but this is merely hand-waving. It looks appealing, but > there's no rigor there. Bullshit. Cheers, - Alf
From: Grant Edwards on 14 Jan 2010 11:40
On 2010-01-14, Alf P. Steinbach <alfps (a)start.no> wrote:>> 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! :-) [...] > With the goal of just a rough approximation you can go about > it like this: > > 1. Divide a full cycle of the sine wave into n intervals. > With sine wave frequency f this corresponds to n*f > sample rate for digital representation. > > 2. Each interval will be approximated by a rectangular bar > extending up to or down to the sine wave. As it happens > this (the bar's height) is the sample value in a digital > representation. > > 3. In the first half of the cycle, for each bar create that > bar as a square wave of frequency f, amplitude half the > bar's height, and phase starting at the bar's left, plus > same square wave with negative sign (inverted amplitude) > and phase starting at the bar's right. And voil?, not > only this bar generated but also the corresponding > other-way bar in second half of cycle. > > 4. Sum all the square waves from step 3. > > 5. Let n go to infinity for utter perfectness! :-) > > And likewise for any other waveform. > > After all, it's the basis of digital representation of sound! Huh? I've only studied basic DSP, but I've never heard/seen that as the basis of digital represention of sound. I've also never seen that representation used anywhere. Can you provide any references? -- Grant Edwards grante Yow! CHUBBY CHECKER just at had a CHICKEN SANDWICH in visi.com downtown DULUTH! |