Esempio n. 1
0
        public static double[] FFTWPaddedFFT(ref double[] @in)
        {
            Debug.Assert(@in.Length > 0);
            int n = @in.Length;

            int padded = n > 256 ? MathUtils.NextLowPrimeFactorization(n) : n;

            Array.Resize <double>(ref @in, padded);

            // 4096 real numbers on input processed by FFTW dft_r2c_1d transform gives
            // 4096/2+1 = 2049 complex numbers at output
            // prepare the input arrays
            var fftwInput = new FFTW.DoubleArray(@in);

            int complexSize = (padded >> 1) + 1;             // this is the same as (padded / 2 + 1);
            var fftwOutput  = new FFTW.ComplexArray(complexSize);

            FFTW.ForwardTransform(fftwInput, fftwOutput);

            Array.Resize <double>(ref @in, n);

            // free up memory
            GC.Collect();

            return(fftwOutput.ComplexValues);
        }
Esempio n. 2
0
        public static void ForwardTransform(FFTW.DoubleArray input, FFTW.ComplexArray output)
        {
            IntPtr plan = FFTW.dft_r2c_1d(input.Length, input.Handle, output.Handle, Flags.Estimate);

            //FFTW.print_plan(plan);
            FFTW.execute(plan);
            FFTW.destroy_plan(plan);
        }
Esempio n. 3
0
        public static void BackwardTransform(FFTW.ComplexArray input, FFTW.DoubleArray output)
        {
            // TODO: will there be a time where input.Length should be used instead?
            IntPtr plan = FFTW.dft_c2r_1d(output.Length, input.Handle, output.Handle, Flags.Estimate);

            //FFTW.print_plan(plan);
            FFTW.execute(plan);
            FFTW.destroy_plan(plan);
        }
Esempio n. 4
0
            /// <summary>
            /// Initialize a complex array using a complex double array (alternating between real and imaginary values)
            /// that has the length of N/2+1.
            /// </summary>
            public ComplexArray(double[] complexArray)
            {
                // make sure to have room for both arrays
                int length = complexArray.Length;

                Handle = FFTW.malloc(Marshal.SizeOf(typeof(double)) * length);
                Marshal.Copy(complexArray, 0, Handle, length);

                // Update length to reflect a N/2+1 input
                Length = length;
            }
Esempio n. 5
0
            public DoubleArray(IEnumerable <double> data, Func <int, int, double> window)
            {
                Length = data.Count();
                Handle = FFTW.malloc(Marshal.SizeOf(typeof(double)) * Length);
                var buffer = new double[Length];

                for (int i = 0; i < Length; i++)
                {
                    buffer[i] = data.ElementAt(i) * window(i, Length);
                }
                Marshal.Copy(buffer, 0, Handle, Length);
            }
Esempio n. 6
0
        public static double[] FFTWPaddedIFFT(ref double[] @in, bool doProperScaling = true)
        {
            Debug.Assert(@in.Length > 1);

            int originalLength = @in.Length;
            int n = @in.Length - 2;

            // the complex array should in theory always be correct size?
            // int padded = n > 256 ? MathUtils.NextLowPrimeFactorization(n) : n;
            int padded = n;
            //Array.Resize<double>(ref @in, padded);

            // prepare the input arrays
            var fftwBackwardInput  = new FFTW.ComplexArray(@in);
            var fftwBackwardOutput = new FFTW.DoubleArray(padded);

            // this method needs that the backwards transform uses the output.length as it's N
            // i.e. FFTW.dft_c2r_1d(output.Length, input.Handle, output.Handle, Flags.Estimate);
            FFTW.BackwardTransform(fftwBackwardInput, fftwBackwardOutput);

            double[] @out = null;
            if (doProperScaling)
            {
                @out = fftwBackwardOutput.ValuesDivedByN;
            }
            else
            {
                // in the original method it wasn't scaled correctly (meaning ValuesDivedByN)
                @out = fftwBackwardOutput.Values;
            }

            //Array.Resize<double>(ref @in, originalLength);

            // free up memory
            GC.Collect();

            return(@out);
        }
Esempio n. 7
0
            /// <summary>
            /// Initialize a complex array using real and imaginary data
            /// that has the length of N/2+1.
            /// </summary>
            /// <param name="realData">real data</param>
            /// <param name="imagData">imaginary data</param>
            public ComplexArray(double[] realData, double[] imagData)
            {
                if (realData.Length != imagData.Length)
                {
                    throw new ArgumentException(
                              "data length for real data [" + realData.Length + "] is not the same as imaginary data [" + imagData.Length + "]");
                }

                // make sure to have room for both arrays
                int length = realData.Length * 2;
                var buffer = new double[length];

                for (int i = 0; i < realData.Length; i++)
                {
                    buffer[2 * i]     = realData[i];
                    buffer[2 * i + 1] = imagData[i];
                }
                Handle = FFTW.malloc(Marshal.SizeOf(typeof(double)) * length);
                Marshal.Copy(buffer, 0, Handle, length);

                // Update length to reflect a N/2+1 input
                Length = (realData.Length - 1) * 2;
            }
Esempio n. 8
0
 public DoubleArray(double[] data)
 {
     Length = data.Length;
     Handle = FFTW.malloc(Marshal.SizeOf(typeof(double)) * Length);
     Marshal.Copy(data, 0, Handle, Length);
 }
Esempio n. 9
0
 public DoubleArray(int length)
 {
     Length = length;
     Handle = FFTW.malloc(Marshal.SizeOf(typeof(double)) * Length);
 }