A simple-to-use sound file writer
Alf P. Steinbach
alfps at start.no
Thu Jan 14 16:27:23 CET 2010
* Steve Holden:
> Alf P. Steinbach wrote:
>> * Steve Holden:
>>> Alf P. Steinbach wrote:
>>>> Just as a contribution, since someone hinted that I haven't really
>>>> contributed much to the Python community.
>>>> The [simple_sound] code will probably go into my ch 3 at <url:
>>>> http://tinyurl.com/programmingbookP3>, but sans sine wave generation
>>>> since I haven't yet discussed trig functions, and maybe /with/ changes
>>>> suggested by you?
>>> I wouldn't hold back on the sine wave just because it would represent a
>>> "forward reference". That's OK sometimes. Why not just put a comment in
>>> to the effect that "The sine wave is created using a function from the
>>> math module, which we'll be looking at in ..."?
>>> Since the sine is the basis for all other waveforms its omission would
>>> seem more than a little strange to anyone knowledgeable about audio, for
>> I don't know very much if anything about audio. For example, in the code
>> what I called "sawtooth" wave is really "triangle" wave. The sawtooth is
>> simpler, what I called "linear" in the code.
>> And if you wonder, I was just checking the terminology now before
>> starting to write it up... Perhaps should have done that before posting
>> code. But once I got the idea of posting it I just posted it.
>> Anyway, as I recall any wave can be decomposed into sine waves, or
>> square waves, or almost whatever kind of waves. Think about a square
>> wave of frequency f and one of frequency 3f and perhaps third the
>> amplitude (not sure), combined that's already a good start on a sine
>> wave. With some ugly staircasing but hey. And as a matter of programming
>> practicality, a triangle wave sounds almost like a sine wave. It's just
>> a little more edgy or "hairy", a slight buzz.
> 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).
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
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!
> though I admit the idea has
> intuitive appeal. But I know beyond a shadow of a doubt from my
> education that any periodic function with a fundamental frequency f can
> be approximated to whatever desired accuracy by the sum of sine and
> cosine waves of frequencies Nf (N = 0, 1, 2, 3, ...) of appropriate
> amplitudes (the frequency 0 component allows you to insert a "DC shift"
> for waveforms that aren't symmetrical about zero). I seem to remember
> the Fourier's theorem was the fundamental proof.
Yes, the good Fourier had a thing for roundish curves. He saw them everywhere
and in anything. Eventually he found an infinite number of them in, to others,
> There is a very pretty discussion of all this, if you are mathematically
> inclined, in
> with a specific example for the sawtooth waveform.
> I would definitely recommend renaming the waveforms you *do* use to
> conform with accepted terminology. This will reduce reader confusion.
More information about the Python-list