Discrete Fourier Transform & Epicycles (Script)
Script for Juan Carlos Ponce Campuzano
See exchange of thoughts
#================================================================
# Data and Setup
#================================================================
LP= {(2, 2), (2, 3/2), (2, 1), (2, 1/2), (2, 0), (2, -1 / 2), (2, -1), (2, -3/2), (2, -2), (3/2, -2), (1, -2), (1 / 2, -2), (0, -2), (-1/2, -2), (-1, -2), (-3/2, -2), (-2, -2), (-2, -3/2), (-2, -1), (-2, -1 / 2), (-2, 0), (-2, 1 / 2), (-2, 1), (-2, 3 / 2), (-2, 2), (-3 / 2, 2), (-1, 2), (-1 / 2, 2), (0, 2), (1 / 2, 2), (1, 2), (3 / 2, 2)}
N = Length(LP)
Ln = Sequence(N)
#================================================================
# Shift zero-frequency component to center of spectrum.
#================================================================
Lk = Ln-1-Div(N,2)
#================================================================
# Calculate DFT: LF is a list of frequencies
#================================================================
LF = 1/N*Zip(Sum(Zip( (abs(P);arg(P)-2*pi*k*(n-1)/N), P,LP, n,Ln)), k,Lk)
#================================================================
# Lj : frequency index sorted by size (abs)
#================================================================
LAs= Reverse(Sort(Zip((abs(F),n), F,LF, n,Ln )))
Lj = Zip( y(As), As,LAs)
#================================================================
# Use the first M greatest frequencies
#================================================================
M = Slider(1, N, 1, 1, 160, false, true, false, false)
SetValue(M, N)
Lm = Sequence(1, M)
Lks= First(Zip( Element(Lk,j), j,Lj),M)
LFs= First(Zip( Element(LF,j), j,Lj),M)
LRs= First(Zip( x(As), As,LAs),M)
#================================================================
# Plot M epicycles
#================================================================
t = Slider(0, 2*pi, 0.0099, 0.5, 150, false, true, false, false)
LC1= Zip( Sum( Zip( (abs(F);arg(F)+k*t), F,First(LFs,m), k,Lks) ), m,Lm )
LC = Join({(0,0)}, LC1)
Plast = Last(LC)
PolyL = Polyline(LC)
Epicycles = Zip( Circle(Element(LC, m),R), m,Lm, R,LRs )
#================================================================
# Parametric curve: Inverse of Discrete Fourier Transform
#================================================================
fx(x) = Sum( Zip( x(F)*cos(k*x) - y(F)*sin(k*x), F,LFs, k,Lks) )
fy(x) = Sum( Zip( x(F)*sin(k*x) + y(F)*cos(k*x), F,LFs, k,Lks) )
orbit = Curve(fx(t), fy(t), t, 0, 2 pi)
#================================================================
# Settings
#================================================================
SetValue(t,0)
StartAnimation(t, true)
SetVisibleInView(LF , 1, false)
SetVisibleInView(LAs, 1, false)
SetVisibleInView(LFs, 1, false)
SetVisibleInView(LC1, 1, false)
SetVisibleInView(LC , 1, false)
SetVisibleInView(fx , 1, false)
SetVisibleInView(fy , 1, false)
ShowLabel(PolyL, false)
ShowLabel(orbit, false)