[sdiy] Approximating sine with plain integer math

rsdio at audiobanshee.com rsdio at audiobanshee.com
Thu Apr 7 07:13:54 CEST 2016


Sorry for the repeated text, but here it is with links to the waveform images.


On Apr 6, 2016, at 5:06 PM, John Ames <commodorejohn at gmail.com> wrote:
> On 4/6/16, Richie Burnett <rburnett at richieburnett.co.uk> wrote:
>> Neat.  It's got quite a high distortion for a sinewave as you said, and has
>> only odd harmonics present.  The waveform you have actually generated is the
>> integral of a triangle waveform.
>> 
>> As a triangle is the integral of a squarewave and has a steeper spectral
>> roll-off, your waveform is the integral of the triangle waveform, and has an
>> even steeper spectral roll-off:
> Hah, interesting. I figured from looking at a graph that it might be
> something in the square/triangle family, but I'm not up on my
> higher-level math enough to work out the details as you did.

A long time ago, I wrote some Mac OS X software to generate various waveforms using harmonic series of sine waves. Various controls allowed selecting the number of harmonics; whether odd, even, or all harmonics were included; whether the amplitudes were evenly weighted, 1/N or 1/(N*N), where N is the harmonic number; and one other control that turned out to be necessary. The obvious wave shapes such as square (odd harmonics weighted 1/N) and ramp/sawtooth (all harmonics weighted 1/N) were easily obtained. However, I couldn't create triangle (odd harmonics weighted 1/(N*N)) until I guessed that every other harmonic needed to be inverted in polarity (I don't like to call polarity inversion "phase shift" because it's not the same as a variable filter phase delay).

Here's the interesting part: if all of the harmonics of a triangle wave have the same polarity, rather than alternating polarity, the resulting shape looks like a sine that is more circular. I called it a "circle wave" at the time. This wave shape sounds identical to a triangle wave even though it looks very different. The human hearing system cannot distinguish polarity. Analog circuits like an integrator can synthesize a triangle wave much more easily than this "circle wave" but software DSP can create either one with the same number of calculations.

http://audiobanshee.com/images/Circle.png


This story is my long-winded way of saying that I had a hunch that your approximate sine has more harmonic distortion than a pure sine wave, because I've seen the shape before. Your method of calculation is probably a lot faster than mine, since I allowed for an arbitrary number of sin() function calls and summing of all harmonics (along with Automatic Gain Control to prevent clipping).

By the way, once I introduced the option of inverting every other harmonic, I tried that variation on all of the other popular waveforms like square, sawtooth, etc. There are some very interesting shapes! Unfortunately, they all sound exactly like their counterparts that don't have inverted harmonics, but it's still educational to see.

http://audiobanshee.com/images/SquareFlip.png
http://audiobanshee.com/images/Ramp-Square+Flip.png
http://audiobanshee.com/images/Ramp-Square.png
http://audiobanshee.com/images/Triangle-Even-Flip.png
http://audiobanshee.com/images/Triangle-Even.png


Also: If you start with a triangle wave and add even harmonics (so that the waveform includes all harmonics), then you end up with what I like to call an "italic sine wave." It looks basically like a regular sine wave except that the peaks lean over to one side. Flipping every other harmonic causes the wave to lean the other way.

http://audiobanshee.com/images/Italic.png
http://audiobanshee.com/images/ItalicFlip.png

Brian Willoughby
Sound Consulting

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://synth-diy.org/pipermail/synth-diy/attachments/20160406/77e716fa/attachment.html>


More information about the Synth-diy mailing list