#lang racket
(require plot
(planet williams/science/math)
(planet williams/science/statistics)
(planet williams/science/fft))
(printf "Radix 2 Complex FFT - Decimation in Time~n")
(define data (build-vector 4096 (lambda (t) (+ (sin (* t (/ 2*pi 500))) 0.0))))
(define data-points (for/list ((j (in-vector data))
(i (in-naturals)))
(vector i j)))
(plot (points data-points #:size 3 #:color "blue")
#:title "Data"
#:x-min 0
#:x-max (length data-points)
#:y-min -1.0
#:y-max 1.0)
(fft-complex-radix2-forward data)
(define data-max (for/fold ((max-magnitude 0.0))
((x (in-vector data)))
(max (magnitude x) max-magnitude)))
(define data-points-forward (for/list ((j (in-vector data))
(i (in-naturals)))
(vector i (magnitude j))))
(plot (points data-points-forward #:size 3 #:color "red")
#:title "Radix 2 Complex FFT (Forward)"
#:x-min 0
#:x-max (length data-points-forward)
#:y-max data-max)
(fft-complex-radix2-inverse data)
(define data-points-inverse (for/list ((j (in-vector data))
(i (in-naturals)))
(vector i (real-part j))))
(plot (points data-points-inverse #:size 3 #:color "blue")
#:title "Radix 2 Complex FFT (Inverse)"
#:x-min 0
#:x-max (length data-points-inverse)
#:y-min -1.0
#:y-max 1.0)