Skip to content

Instantly share code, notes, and snippets.

@jakobrs
Created May 28, 2023 09:43
Show Gist options
  • Save jakobrs/7739fb927e000cc5014f19f67b417f9a to your computer and use it in GitHub Desktop.
Save jakobrs/7739fb927e000cc5014f19f67b417f9a to your computer and use it in GitHub Desktop.
#lang racket
(define (split x)
(match x
['() '(() . ())]
[(list-rest x y zs)
(match-let ([(cons xs ys) (split zs)])
(cons
(cons x xs)
(cons y ys)))]))
(define (cis a)
(exp (* 0+1i a)))
(define (double xs) (append xs xs))
(define (fft v)
(match v
['() '()]
[(list x) (list x)]
[_ (match-let* ([(cons evens odds) (split v)]
[evens-fft (fft evens)]
[odds-fft (fft odds)]
[n (length v)]
[w (cis (/ (* 2 pi) n))]
[ks (stream->list (in-range n))]
[ws (map (lambda (k) (expt w k)) ks)])
(map (lambda (a w b) (+ a (* w b)))
(double evens-fft)
ws
(double odds-fft)))]))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment