JuliaDSP / FourierTransforms.jl

Fourier transforms written in Julia
MIT License
20 stars 2 forks source link

Output comes out permuted? #4

Closed Keno closed 5 years ago

Keno commented 5 years ago

I suspect I'm doing something extremely stupid, but I can't figure it out, so I figured I'd open this issue and maybe somebody who knows more about FFTs can help me figure this out. I'm trying to extend this package to FFTs over some finite field 𝔽 (in finite fields, FFTs are also known as the number-theoretic transform NTT). Everything seems to basically go through if I just generalize the Complex cases (code in gist: https://gist.github.com/Keno/51bcc385baed55120e9ed6111c541cba), but the output comes out permuted when compared to the naive summation:

julia> FourierTransforms.CTPlan(𝔽₁₇, true, 8) * 𝔽₁₇[0:7...]
8-element Array{𝔽₁₇,1}:
 11
  1
 14
  8
 13
  6
 12
  3

julia> _ntt(Ο‰, v) = [sum((v[j]*Ο‰^(j*i)) for j in eachindex(v)) for i in eachindex(v)]

julia> collect(_ntt(𝔽₁₇(9), OffsetArray(x, 0:7)))
8-element Array{𝔽₁₇,1}:
 11
  1
 12
  3
 13
  6
 14
  8

(note that the last three pairs of numbers are reversed). Unfortunately, I can't reproduce this with Complex, so I'm thinking I'm doing something very stupid, but I've wasted several hours on this and been unable to figure it out.

@stevengj as the resident FFT expert and original author of this code, any immediate things that would cause such a problem? @andreasnoack any ideas?

Keno commented 5 years ago

I think I've found this. I may have been using the wrong root of unity for the recursion. Writing a test case and validating now.

Keno commented 5 years ago

Yeah, that was the problem. You can't mix and match which root you're calling primitive or everything will break.