aboutsummaryrefslogtreecommitdiff
path: root/fft.bqn
blob: c735dc50d45b06a06d812d60da9fada1f3346c10 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# Fast Fourier transform
# Argument has shape ⟨l⟩ for real values or ⟨2,l⟩ for complex, l←2⋆n
# Result is complex encoded as shape ⟨2,l⟩.

SinCos  •math

{
  𝕨𝕊𝕩: (-𝕨1¯1)𝕊𝕩 ;

  inv  𝕨¯1
  "FFT argument must be a list or have two list cells" ! (3⌊=)0,1,2=≠,0𝕩
  n  2  l  ¯1⊑≢𝕩
  "FFT length must be a power of two" ! =n
  s  2 ˜ n

  r  (Cos⋈Sin) π × (1s)  -inv ÷ l÷2   # Roots of unity
  M  -´×  +´×                         # Complex multiplication
  F  { (r (20<«𝕨1)/¨)  (+˝¨(𝕨⍉≍)¨r M-˝¨)𝕩 } # FFT loop
  ÷linv >⥊¨ (n) F´˜ s¨ (1==)<˘,⊢⋈0¨ 𝕩
}