A simple-to-use sound file writer
Alf P. Steinbach
alfps at start.no
Thu Jan 14 11:56:51 EST 2010
* Grant Edwards:
> On 2010-01-14, Alf P. Steinbach <alfps at 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.
Oh, you have... The end result above (for finite n) is a sequence of sample
values of a sine wave. Ordinary digital representation of sound is exactly the
same, a sequence of sample values.
> I've also never seen that representation used anywhere.
Yes, you have. A sequence of sample values is the representation used in any
direct wave file. Like [.wav] and, I believe, [.aiff].
> Can you provide any references?
I don't have any references for the above procedure, it's sort of trivial.
Probably could find some references with an hour of googling. But no point.
Cheers & hth.,
- Alf
PS: To extend the above to a non-symmetric waveform, just first decompose that
waveform into sine waves (Fourier transform), then add up the square wave
representations of each sine wave. :-)
More information about the Python-list
mailing list