/// <summary> /// 1-D Fast Fourier Transform. /// </summary> /// /// <param name="real">The real part of the complex numbers to transform.</param> /// <param name="imag">The imaginary part of the complex numbers to transform.</param> /// <param name="direction">The transformation direction.</param> /// /// <example> /// <code source="Unit Tests\Accord.Tests.Math\FourierTransformTest.cs" region="doc_fft" /> /// </example> /// public static void FFT(double[] real, double[] imag, FourierTransform.Direction direction) { if (direction == FourierTransform.Direction.Forward) { FFT(real, imag); } else { FFT(imag, real); } if (direction == FourierTransform.Direction.Backward) { for (int i = 0; i < real.Length; i++) { real[i] /= real.Length; imag[i] /= real.Length; } } }
/// <summary> /// 1-D Fast Fourier Transform. /// </summary> /// /// <param name="data">The data to transform..</param> /// <param name="direction">The transformation direction.</param> /// public static void FFT(Complex[] data, FourierTransform.Direction direction) { int n = data.Length; if (n == 0) { return; } if (direction == FourierTransform.Direction.Backward) { for (int i = 0; i < data.Length; i++) { data[i] = new Complex(data[i].Imaginary, data[i].Real); } } if ((n & (n - 1)) == 0) { // Is power of 2 TransformRadix2(data); } else { // More complicated algorithm for arbitrary sizes TransformBluestein(data); } if (direction == FourierTransform.Direction.Backward) { for (int i = 0; i < data.Length; i++) { double im = data[i].Imaginary; double re = data[i].Real; data[i] = new Complex(im / n, re / n); } } }
public static object FHT(double[] Data, object BackwardFlagOpt) { bool backwards = Utils.GetOptionalParameter(BackwardFlagOpt, false); FourierTransform.Direction direction = (backwards ? FourierTransform.Direction.Backward : FourierTransform.Direction.Forward); try { HilbertTransform.FHT(Data, direction); } catch (Exception e) { Debug.WriteLine(e.ToString()); } double[,] ret = new double[Data.Length, 1]; for (int i = 0; i < Data.Length; ++i) { ret[i, 0] = Data[i]; } return(ret); }
internal unsafe static Image <Complex, TDepth> FFT <TDepth>(this Image <Complex, TDepth> image, FourierTransform.Direction direction, bool inPlace = false) where TDepth : struct { Image <Complex, TDepth> dest = null; if (inPlace) { dest = image; } else { dest = image.Clone(); } if (typeof(TDepth).Equals(typeof(float))) { FourierTransform.FFT2((ComplexF *)dest.ImageData, dest.Width, dest.Height, dest.Stride, direction); } else if (typeof(TDepth).Equals(typeof(double))) { throw new NotImplementedException(); //TODO: implement for double //FourierTransform.FFT2((Complex*)dest.ImageData, dest.Width, dest.Height, dest.Stride, direction); } else { throw new NotSupportedException(); } return(dest); }
/// <summary> /// Calculates Fast Fourier transform. /// </summary> /// <param name="image">Input image.</param> /// <param name="direction">Forward or backward direction.</param> /// <param name="inPlace">Process in place or not.</param> /// <returns>Processed image. If <paramref name="inPlace"/> is used the result is the same as input image therefore may be omitted.</returns> private unsafe static Image <Complex, double> FFT(this Image <Complex, double> image, FourierTransform.Direction direction, bool inPlace = false) { return(FFT <double>(image, direction, inPlace)); }
/// <summary> /// Calculates Fast Fourier transform. /// </summary> /// <param name="image">Input image.</param> /// <param name="direction">Forward or backward direction.</param> /// <param name="inPlace">Process in place or not.</param> /// <returns>Processed image. If <paramref name="inPlace"/> is used the result is the same as input image therefore may be omitted.</returns> public unsafe static Image <Complex, float> FFT(this Image <Complex, float> image, FourierTransform.Direction direction, bool inPlace = false) { return(FFT <float>(image, direction, inPlace)); }
/// <summary> /// 2-D Discrete Fourier Transform. /// </summary> /// /// <param name="data">The data to transform.</param> /// <param name="direction">The transformation direction.</param> /// /// <example> /// <code source="Unit Tests\Accord.Tests.Math\FourierTransformTest.cs" region="doc_dft2" /> /// </example> /// public static void DFT2(Complex[][] data, FourierTransform.Direction direction) { int m = data.Columns(); if (direction == FourierTransform.Direction.Forward) { // process rows for (int i = 0; i < data.Length; i++) { // transform it DFT(data[i], FourierTransform.Direction.Forward); } // process columns var col = new Complex[data.Length]; for (int j = 0; j < m; j++) { // copy column for (int i = 0; i < col.Length; i++) { col[i] = data[i][j]; } // transform it DFT(col, FourierTransform.Direction.Forward); // copy back for (int i = 0; i < col.Length; i++) { data[i][j] = col[i]; } } } else { // process columns var col = new Complex[data.Length]; for (int j = 0; j < m; j++) { // copy column for (int i = 0; i < col.Length; i++) { col[i] = data[i][j]; } // transform it DFT(col, FourierTransform.Direction.Backward); // copy back for (int i = 0; i < col.Length; i++) { data[i][j] = col[i]; } } // process rows for (int i = 0; i < data.Length; i++) { // transform it DFT(data[i], FourierTransform.Direction.Backward); } } }