# Attn Don Re: sinewave synthesis

• posted

Recently someone mentioned sinewaves, to which Don referred to Hardware Hacker #85 (page 77ish:

). This contains a "squashed triangle integrated to look like a sine" algorithm.

Well, I tried it, and the first cycle looks sinusoidal, but there's LF oscillation or something on it, too.

Give this QBasic code a run:

-=-=-=-

SCREEN 12 DEFINT A-Z

DIM Buffer(639)

x = 100 Rate = 0 Clip = 10

DO IF x > 0 THEN Rate = Rate - 1 ELSE Rate = Rate + 1 IF Rate > Clip THEN x = x + Clip ELSEIF y < -Clip THEN x = x - Clip ELSE x = x + Rate END IF Buffer(j) = x j = j + 1 IF j >= 640 THEN j = 0

k = j WAIT &H3DA, 8 FOR i = 0 TO 638 PRESET (i, 240 - Buffer(k) * 2) k = k + 1 IF k >= 640 THEN k = 0 NEXT k = j + 1 IF k >= 640 THEN k = 0 FOR i = 0 TO 638 PSET (i, 240 - Buffer(k) * 2) k = k + 1 IF k >= 640 THEN k = 0 NEXT LOOP UNTIL INKEY\$ = CHR\$(27)

-=-=-=-

Ironically, I tried good old Newton's method on d^2x/dt^2 - x = 0 and got a fair looking circle (with quadrature, no less). Rounding errors are moderate, but I don't know how good the stability is, which is always the problem with numerical methods.

Bresenham's circle drawing algoritm is another related one.

Tim

```--
Deep Friar: a very philosophical monk.
Website: http://webpages.charter.net/dawill/tmoranwms```
• posted

One thing I would suggest is to not attempt some approximation of a true integrator, but to model an R-C lowpass filter whose "corner frequency" is maybe 1/4 or 1/2 of the frequency of the squashed triangle wave that you are trying to integrate into a sine wave.

One more thing: Differential equations describing steady sinusoidal oscillation have a second derivative term and a term with no degree of derivative. A damped sinusoidal oscillation has in addition a first derivative term. I have a hunch that if you add a first derivative term with not a constant coefficient but a "coefficient" that is some constant times (magnitude of oscillation minus its desired magnitude), the oscillation "damps towards desired magnitude" as opposed to damping towards zero. You probably want the constant included in such a first derivative term on the small side if all it has to "damp" is rounding errors. You probably also want that small enough for corrections in magnitude resulting from this to take at least a cycle or two to mostly achieve - major corrections in magnitude faster than that may add significant harmonic content.

- Don Klipstein ( snipped-for-privacy@misty.com)

• posted

You mean Euler's method, right? I don't know how you'd apply Newton's method to that differential equation...

If you did mean Euler's method you could try using the "improved Euler" method. It requires double the computation time, but it is unconditionally stable and has an error improvement of h^2 - that is if the error was 1% with the standard method it would be .001% with the improved method. The algorithm goes like:

1. Let m_n = f(x,y);

1. Compute u_n+1 using standard Euler method, i.e. u_n+1 = y_n + h*m_n;

2. Let m_n+1 = f(x_n+1, u_n+1);

1. Let g_n+1 = 1/2*(m_n + m_n+1);

2. Then let y_n+1 = y_n + h*g_n+1.

You'd have to do this twice of course because you've got a 2nd order differential equation. Actually, in this case the computation time may not be that much longer than the standard method because there's no dependent variable term, thus I think you could skip step 2.

ElectronDepot website is not affiliated with any of the manufacturers or service providers discussed here. All logos and trade names are the property of their respective owners.