sourcedemos/fourier-epicycles/fourier-epicycles.xtl

1⍝!/usr/bin/env xetal 2⍝# Fourier epicycles: any closed curve is a sum of circles turning at 3⍝# whole-number speeds. Its points z = x + i y go through a discrete 4⍝# Fourier transform, written as two matrix products with the cosines 5⍝# and sines of one outer product of angles; each frequency is a 6⍝# circle, and the circles, strongest first, chained end to end, 7⍝# trace the curve. A running sum along the chain gives, at once, the 8⍝# curve rebuilt from the first 1, 2, 3, ... circles. Run it with 9⍝# "xetal run demos/fourier-epicycles/fourier-epicycles.xtl"; it 10⍝# prints the strength of each frequency, the four strongest speeds, 11⍝# and the mean error rebuilt from 1, 2, 4, 8 and all 16 circles. The 12⍝# live page lets you pick or draw a curve and slides how many 13⍝# circles trace it: https://softwarewrighter.github.io/X_eTaL-demos/fourier-epicycles/ 14 15⍝# x, y: the curve, a square of side 2, 16 points round its edge. 16x ← 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 17y ← -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 18 19⍝# -- the angles: one outer product. n: how many points. k: 0 .. n - 1. 20⍝# A, C, S: row k, column j of A is 2 pi k j / n, the angle of 21⍝# frequency k at sample j; C and S its cosine and sine. 22n ← t̲ally x 23k ← f̲loat o̲ffsets n 24A ← (2.0 × (p̲i @) ÷ f̲loat n) × k '× t̲able k 25C ← c̲os A 26S ← s̲in A 27 28⍝## The transform: two matrix products 29 30⍝# re, im, amp: Z_k = (1 / n) sum over j of z_j e^(-i A[k;j]), as real 31⍝# and imaginary parts: the cosines and sines times x and y (two 32⍝# matrix products, i_nner), then each frequency's strength. 33re ← ((C '+ '× i̲nner x) + S '+ '× i̲nner y) ÷ f̲loat n 34im ← ((C '+ '× i̲nner y) − S '+ '× i̲nner x) ÷ f̲loat n 35amp ← ((re × re) + im × im) ^ 0.5 36⍝# f: each circle's speed: k, or k - n past the middle (turning the 37⍝# other way), so the circles stay small. 38f ← k − f̲loat n × 1 × k ≥ f̲loat n d̲iv 2 39 40⍝## The circles, strongest first 41 42⍝# order: the frequencies' rank by strength (g_rade). 43order ← g̲rade n̲eg amp 44⍝# T, zr, zi, vx, vy: circle c at time j (row j, column c): its 45⍝# vector Z e^(i f t_j), turned by its own speed and time. 46T ← (2.0 × (p̲i @) ÷ f̲loat n) × k '× t̲able order s̲elect f 47zr ← (k 'r̲ight t̲able order s̲elect re) 48zi ← (k 'r̲ight t̲able order s̲elect im) 49vx ← (zr × c̲os T) − zi × s̲in T 50vy ← (zr × s̲in T) + zi × c̲os T 51 52⍝## The chain: a running sum along the circles 53 54⍝# cx, cy: column c is where the first c circles end at each time, so 55⍝# column c is the curve rebuilt from the c strongest circles. 56cx ← '+ s̲\₂ vx 57cy ← '+ s̲\₂ vy 58 59⍝## The error of each reconstruction 60 61⍝# dx, dy, err: the mean distance from the curve, for 1, 2, ... n 62⍝# circles. 63dx ← cx − x 'l̲eft t̲able k 64dy ← cy − y 'l̲eft t̲able k 65err ← ('+ r̲/ ((dx × dx) + dy × dy) ^ 0.5) ÷ f̲loat n 66⍝ -- end of the core ------------------------------------- 67 68ᵘr̲ound ← { v → (f̲loat f̲loor 0.5 + v × 1000.0) ÷ 1000.0 } 69⍝ The strength of each frequency 0 .. 15. 70ᵘr̲ound amp 71⍝ The speeds of the four strongest circles. 724 t̲ake order s̲elect f 73⍝ The mean error rebuilt from 1, 2, 4, 8 and all 16 circles. 74ᵘr̲ound 1 2 4 8 16 s̲elect err