/// <summary> /// One dimensional Discrete Fourier Transform. /// </summary> /// /// <param name="data">Data to transform.</param> /// <param name="direction">Transformation direction.</param> /// public static void DFT(Complex[] data, Direction direction) { int n = data.Length; double arg; Complex[] dst = new Complex[n]; // for each destination element 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 j = 0; j < n; j++) { dst[i] += data[j] * Complex.FromPolarCoordinates(1.0, j * arg); } } // copy elements if (direction == Direction.Forward) { // devide also for forward transform for (int i = 0; i < n; i++) { data[i] = dst[i] / n; } } else { for (int i = 0; i < n; i++) { data[i] = dst[i]; } } }
/// <summary> /// Two dimensional Discrete Fourier Transform. /// </summary> /// /// <param name="data">Data to transform.</param> /// <param name="direction">Transformation direction.</param> /// public static void DFT2(Complex[,] data, Direction direction) { int n = data.GetLength(0); // rows int m = data.GetLength(1); // columns double arg; Complex[] dst = new Complex[System.Math.Max(n, m)]; // process rows for (int i = 0; i < n; i++) { for (int j = 0; j < m; 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++) { dst[j] += data[i, k] * Complex.FromPolarCoordinates(1.0, k * arg); } } // copy elements if (direction == Direction.Forward) { // devide also for forward transform for (int j = 0; j < m; j++) { data[i, j] = dst[j] / m; } } else { for (int j = 0; j < m; 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++) { dst[i] += data[k, j] * Complex.FromPolarCoordinates(1.0, k * arg); } } // copy elements if (direction == Direction.Forward) { // devide also for forward transform for (int i = 0; i < n; i++) { data[i, j] = dst[i] / n; } } else { for (int i = 0; i < n; i++) { data[i, j] = dst[i]; } } } }