programdemos/fourier-epicycles/fourier-epicycles.xtl

Fourier epicycles: any closed curve is a sum of circles turning at whole-number speeds. Its points z = x + i y go through a discrete Fourier transform, written as two matrix products with the cosines and sines of one outer product of angles; each frequency is a circle, and the circles, strongest first, chained end to end, trace the curve. A running sum along the chain gives, at once, the curve rebuilt from the first 1, 2, 3, ... circles. Run it with "xetal run demos/fourier-epicycles/fourier-epicycles.xtl"; it prints the strength of each frequency, the four strongest speeds, and the mean error rebuilt from 1, 2, 4, 8 and all 16 circles. The live page lets you pick or draw a curve and slides how many circles trace it: https://softwarewrighter.github.io/X_eTaL-demos/fourier-epicycles/

source

x : Float

value · line 16

x, y: the curve, a square of side 2, 16 points round its edge.

x ← 1.0 1.0 1.0 1.0 1.0 0.5 0.0 -0.5 -1.0 -1.0 -1.0 -1.0 -1.0 -0.5 0.0 0.5
Used in: n, re, im, dx

y : Float

value · line 17
y ← -1.0 -0.5 0.0 0.5 1.0 1.0 1.0 1.0 1.0 0.5 0.0 -0.5 -1.0 -1.0 -1.0 -1.0
Used in: re, im, dy

n : Int

value · line 22

-- the angles: one outer product. n: how many points. k: 0 .. n - 1. A, C, S: row k, column j of A is 2 pi k j / n, the angle of frequency k at sample j; C and S its cosine and sine.

n ← t̲ally x
Used in: k, A, re, im, f, T, err

k : Float

value · line 23
k ← f̲loat o̲ffsets n
Used in: A, f, T, zr, zi, dx, dy

A : Float

value · line 24
A ← (2.0 × (p̲i @) ÷ f̲loat n) × k '× t̲able k
Used in: C, S

C : Float

value · line 25
C ← c̲os A
Used in: re, im

S : Float

value · line 26
S ← s̲in A
Used in: re, im

The transform: two matrix products

re : Float

value · line 33

re, im, amp: Z_k = (1 / n) sum over j of z_j e^(-i A[k;j]), as real and imaginary parts: the cosines and sines times x and y (two matrix products, i_nner), then each frequency's strength.

re ← ((C '+ '× i̲nner x) + S '+ '× i̲nner y) ÷ f̲loat n
Used in: amp, zr

im : Float

value · line 34
im ← ((C '+ '× i̲nner y) − S '+ '× i̲nner x) ÷ f̲loat n
Used in: amp, zi

amp : Float

value · line 35
amp ← ((re × re) + im × im) ^ 0.5

f : Float

value · line 38

f: each circle's speed: k, or k - n past the middle (turning the other way), so the circles stay small.

f ← k − f̲loat n × 1 × k ≥ f̲loat n d̲iv 2

The circles, strongest first

order : Int

value · line 43

order: the frequencies' rank by strength (g_rade).

order ← g̲rade n̲eg amp

T : Float

value · line 46

T, zr, zi, vx, vy: circle c at time j (row j, column c): its vector Z e^(i f t_j), turned by its own speed and time.

T ← (2.0 × (p̲i @) ÷ f̲loat n) × k '× t̲able order s̲elect f
Used in: vx, vy

zr : Float

value · line 47
zr ← (k 'r̲ight t̲able order s̲elect re)
Used in: vx, vy

zi : Float

value · line 48
zi ← (k 'r̲ight t̲able order s̲elect im)
Used in: vx, vy

vx : Float

value · line 49
vx ← (zr × c̲os T) − zi × s̲in T
Used in: cx

vy : Float

value · line 50
vy ← (zr × s̲in T) + zi × c̲os T
Used in: cy

The chain: a running sum along the circles

cx : Float

value · line 56

cx, cy: column c is where the first c circles end at each time, so column c is the curve rebuilt from the c strongest circles.

cx ← '+ s̲\₂ vx
Used in: dx

cy : Float

value · line 57
cy ← '+ s̲\₂ vy
Used in: dy

The error of each reconstruction

dx : Float

value · line 63

dx, dy, err: the mean distance from the curve, for 1, 2, ... n circles.

dx ← cx − x 'l̲eft t̲able k
Used in: err

dy : Float

value · line 64
dy ← cy − y 'l̲eft t̲able k
Used in: err

err : Float

value · line 65
err ← ('+ r̲/ ((dx × dx) + dy × dy) ^ 0.5) ÷ f̲loat n

ᵘr̲ound : Float -> Float

function · line 68
ᵘr̲ound ← { v → (f̲loat f̲loor 0.5 + v × 1000.0) ÷ 1000.0 }