public static void DFT2(Complex[,] data, Direction direction)
{
int n = data.GetLength(0); // rows
int m = data.GetLength(1); // columns
double arg, cos, sin;
var dst = new Complex[System.Math.Max(n, m)];
// process rows
for (int i = 0; i < n; i++)
{
for (int j = 0; j < dst.Length; j++)
{
dst[j] = Complex.Zero;
arg = -(int)direction * 2.0 * System.Math.PI * (double)j / (double)m;
// sum source elements
for (int k = 0; k < m; k++)
{
cos = System.Math.Cos(k * arg);
sin = System.Math.Sin(k * arg);
double re = data[i, k].Real * cos - data[i, k].Imaginary * sin;
double im = data[i, k].Real * sin + data[i, k].Imaginary * cos;
dst[j] += new Complex(re, im);
}
}
// copy elements
if (direction == Direction.Forward)
{
// devide also for forward transform
for (int j = 0; j < dst.Length; j++)
data[i, j] = dst[j] / m;
}
else
{
for (int j = 0; j < dst.Length; j++)
data[i, j] = dst[j];
}
}
// process columns
for (int j = 0; j < m; j++)
{
for (int i = 0; i < n; i++)
{
dst[i] = Complex.Zero;
arg = -(int)direction * 2.0 * System.Math.PI * (double)i / (double)n;
// sum source elements
for (int k = 0; k < n; k++)
{
cos = System.Math.Cos(k * arg);
sin = System.Math.Sin(k * arg);
double re = data[k, j].Real * cos - data[k, j].Imaginary * sin;
double im = data[k, j].Real * sin + data[k, j].Imaginary * cos;
dst[i] += new Complex(re, im);
}
}
// copy elements
if (direction == Direction.Forward)
{
// devide also for forward transform
for (int i = 0; i < dst.Length; i++)
data[i, j] = dst[i] / n;
}
else
{
for (int i = 0; i < dst.Length; i++)
data[i, j] = dst[i];
}
}
}