Created
April 15, 2019 19:20
-
-
Save mjs3339/257ab1fdc558c5e91687a6997189f9d9 to your computer and use it in GitHub Desktop.
C# Complex Fast Fourier Transform
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
public class ComplexFFT | |
{ | |
public Complex[] Fft(Complex[] arr) | |
{ | |
var length = arr.Length; | |
var hLength = length >> 1; | |
if (length == 1) | |
return arr; | |
var evenCoefficients = new Complex[hLength]; | |
var oddCoefficients = new Complex[hLength]; | |
var roots = new Complex[length]; | |
var y = new Complex[length]; | |
for (var i = 0; i < length; i++) | |
{ | |
var alpha = 2 * Math.PI * i / length; | |
roots[i] = new Complex(Math.Cos(alpha), Math.Sin(alpha)); | |
} | |
for (var i = 0; i < hLength; i++) | |
{ | |
evenCoefficients[i] = arr[i * 2]; | |
oddCoefficients[i] = arr[i * 2 + 1]; | |
} | |
var evenOrderCoefficientTransform = Fft(evenCoefficients); | |
var oddOrderCoefficientTransform = Fft(oddCoefficients); | |
for (var i = 0; i < hLength; i++) | |
{ | |
y[i] = evenOrderCoefficientTransform[i] + roots[i] * oddOrderCoefficientTransform[i]; | |
y[i + hLength] = evenOrderCoefficientTransform[i] - roots[i] * oddOrderCoefficientTransform[i]; | |
} | |
return y; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment