コード例 #1
0
ファイル: frmMain.cs プロジェクト: ray5527880/PRSpline
        private Complex FFTW(int index_Entry, int index_Value)
        {
            int SIZE = Convert.ToInt32(mCFGData.SamplingPoint / mCFGData.Hz);

            double[] data = new double[SIZE];

            Complex[] cdata = new Complex[SIZE];
            for (int i = 0; i < SIZE; i++)
            {
                if ((index_Entry < SIZE / 2 && (i < SIZE / 2 - index_Entry)) || (i + index_Entry - (SIZE / 2 - 1)) > mCFGData.TotalPoint)
                {
                    cdata[i] = new Complex(0, 0);
                }
                else
                {
                    cdata[i] = new Complex(Convert.ToDouble(mDATData.arrData[i + index_Entry - SIZE / 2].value[index_Value]), 0);
                }
            }
            fftw_complexarray input  = new fftw_complexarray(SIZE);
            fftw_complexarray ReData = new fftw_complexarray(SIZE);

            input.SetData(cdata);

            fftw_plan pf = fftw_plan.dft_1d(SIZE, input, ReData, fftw_direction.Forward, fftw_flags.Estimate);

            pf.Execute();
            var data_Complex = ReData.GetData_Complex();

            return(data_Complex[1]);
        }
コード例 #2
0
ファイル: ConvolutionBuilder.cs プロジェクト: Maasik/FFTTools
        /// <summary>
        ///     Calculate function self-convolution values
        /// </summary>
        /// <param name="f">Function values</param>
        /// <returns></returns>
        public double[] Build(double[] f)
        {
            int length = (_functionType == FunctionType.Periodic) ? f.Length : (f.Length + f.Length - 1);

            var       input   = new fftw_complexarray(length);
            var       output  = new fftw_complexarray(length);
            fftw_plan forward = fftw_plan.dft_1d(length, input, output,
                                                 fftw_direction.Forward,
                                                 fftw_flags.Estimate);
            fftw_plan backward = fftw_plan.dft_1d(length, input, output,
                                                  fftw_direction.Backward,
                                                  fftw_flags.Estimate);

            var complex = new Complex[length];

            for (int i = 0; i < f.Length; i++)
            {
                complex[i] = f[i];
            }
            input.SetData(complex);
            forward.Execute();
            complex = output.GetData_Complex();
            input.SetData(complex.Select(x => x * x / length).ToArray());
            backward.Execute();
            complex = output.GetData_Complex();

            return(complex.Select(x => x.Magnitude).ToArray());
        }
コード例 #3
0
        // Initializes FFTW and all arrays
        // n: Logical size of the transform
        public FFTWtest(int n)
        {
            System.Console.WriteLine("Starting test with n = " + n + " complex numbers");
            fftLength = n;

            // create two unmanaged arrays, properly aligned
            pin  = fftwf.malloc(n * 8);
            pout = fftwf.malloc(n * 8);

            // create two managed arrays, possibly misalinged
            // n*2 because we are dealing with complex numbers
            fin  = new float[n * 2];
            fout = new float[n * 2];
            // and two more for double FFTW
            din  = new double[n * 2];
            dout = new double[n * 2];

            // get handles and pin arrays so the GC doesn't move them
            hin   = GCHandle.Alloc(fin, GCHandleType.Pinned);
            hout  = GCHandle.Alloc(fout, GCHandleType.Pinned);
            hdin  = GCHandle.Alloc(din, GCHandleType.Pinned);
            hdout = GCHandle.Alloc(dout, GCHandleType.Pinned);

            // create a few test transforms
            fplan1 = fftwf.dft_1d(n, pin, pout, fftw_direction.Forward, fftw_flags.Estimate);
            fplan2 = fftwf.dft_1d(n, hin.AddrOfPinnedObject(), hout.AddrOfPinnedObject(),
                                  fftw_direction.Forward, fftw_flags.Estimate);
            fplan3 = fftwf.dft_1d(n, hout.AddrOfPinnedObject(), pin,
                                  fftw_direction.Backward, fftw_flags.Measure);
            // end with transforming back to original array
            fplan4 = fftwf.dft_1d(n, hout.AddrOfPinnedObject(), hin.AddrOfPinnedObject(),
                                  fftw_direction.Backward, fftw_flags.Estimate);
            // and check a quick one with doubles, just to be sure
            fplan5 = fftw.dft_1d(n, hdin.AddrOfPinnedObject(), hdout.AddrOfPinnedObject(),
                                 fftw_direction.Backward, fftw_flags.Measure);

            // create a managed plan as well
            min   = new fftw_complexarray(din);
            mout  = new fftw_complexarray(dout);
            mplan = fftw_plan.dft_1d(n, min, mout, fftw_direction.Forward, fftw_flags.Estimate);

            // fill our arrays with an arbitrary complex sawtooth-like signal
            for (int i = 0; i < n * 2; i++)
            {
                fin[i] = i % 50;
            }
            for (int i = 0; i < n * 2; i++)
            {
                fout[i] = i % 50;
            }
            for (int i = 0; i < n * 2; i++)
            {
                din[i] = i % 50;
            }

            // copy managed arrays to unmanaged arrays
            Marshal.Copy(fin, 0, pin, n * 2);
            Marshal.Copy(fout, 0, pout, n * 2);
        }
コード例 #4
0
ファイル: FFTWtest.cs プロジェクト: yhl533556/FFTWSharp
        // Tests a single plan, displaying results
        // plan: Pointer to plan to test
        public void TestPlan(object plan, string planName)
        {
            // a: adds, b: muls, c: fmas
            double a = 0, b = 0, c = 0;

            int start = System.Environment.TickCount;

            if (plan is IntPtr)
            {
                IntPtr umplan = (IntPtr)plan;

                for (int i = 0; i < repeatPlan; i++)
                {
                    fftwf.execute(umplan);
                }

                fftwf.flops(umplan, ref a, ref b, ref c);
            }

            if (plan is fftw_plan)
            {
                fftw_plan mplan = (fftw_plan)plan;

                for (int i = 0; i < repeatPlan; i++)
                {
                    mplan.Execute();
                }

                fftw.flops(mplan.Handle, ref a, ref b, ref c);
            }

            if (plan is fftwf_plan)
            {
                fftwf_plan mplan = (fftwf_plan)plan;

                for (int i = 0; i < repeatPlan; i++)
                {
                    mplan.Execute();
                }

                fftwf.flops(mplan.Handle, ref a, ref b, ref c);
            }

            double mflops = (((a + b + 2 * c)) * repeatPlan) / (1024 * 1024);
            long   ticks  = (System.Environment.TickCount - start);

            //Console.WriteLine($"Plan '{planName}': {ticks.ToString("#,0")} us | mflops: {FormatNumber(mflops)} | mflops/s: {(1000*mflops/ticks).ToString("#,0.0")}");
            Console.WriteLine("Plan '{0}': {1,8:N0} us | mflops: {2,8:N0} | mflops/s: {3,8:N0}", planName, ticks, mflops, (1000 * mflops / ticks));
        }
コード例 #5
0
        /// <summary>
        /// Generate a spectrogram array spaced linearily
        /// </summary>
        /// <param name="samples">audio data</param>
        /// <param name="fftWindowsSize">fft window size</param>
        /// <param name="fftOverlap">overlap in number of samples (normaly half of the fft window size) [low number = high overlap]</param>
        /// <returns>spectrogram jagged array</returns>
        public static double[][] CreateSpectrogramFFTWLIB(float[] samples, int fftWindowsSize, int fftOverlap)
        {
            int numberOfSamples = samples.Length;

            // overlap must be an integer smaller than the window size
            // half the windows size is quite normal
            double[] windowArray = FFTWindow.GetWindowFunction(FFTWindowType.HANNING, fftWindowsSize);

            // width of the segment - e.g. split the file into 78 time slots (numberOfSegments) and do analysis on each slot
            int numberOfSegments = (numberOfSamples - fftWindowsSize) / fftOverlap;
            var frames           = new double[numberOfSegments][];

            // even - Re, odd - Img
            var complexSignal = new double[2 * fftWindowsSize];

            for (int i = 0; i < numberOfSegments; i++)
            {
                // apply Hanning Window
                for (int j = 0; j < fftWindowsSize; j++)
                {
                    // Weight by Hann Window
                    complexSignal[2 * j] = (double)(windowArray[j] * samples[i * fftOverlap + j]);

                    // need to clear out as fft modifies buffer (phase)
                    complexSignal[2 * j + 1] = 0;
                }

                // prepare the input arrays
                var       complexInput  = new fftw_complexarray(complexSignal);
                var       complexOutput = new fftw_complexarray(complexSignal.Length / 2);
                fftw_plan fft           = fftw_plan.dft_1d(complexSignal.Length / 2, complexInput, complexOutput, fftw_direction.Forward, fftw_flags.Estimate);

                // perform the FFT
                fft.Execute();

                // get the result
                frames[i] = complexOutput.Abs;

                // free up memory
                complexInput  = null;
                complexOutput = null;
                //GC.Collect();
            }
            return(frames);
        }
コード例 #6
0
        /// <summary>
        /// Perform the Fast Fourier Transform utilisizing the FFTW library
        /// </summary>
        /// <param name="in">Input Signal</param>
        /// <param name="out">Output Signal</param>
        /// <param name="N">N</param>
        /// <param name="method">FFT Method (DFT, IDFT, DHT)</param>
        public static void FFT(ref double[] @in, ref double[] @out, int N, FFTMethod method)
        {
            var complexInput  = new fftw_complexarray(@in);
            var complexOutput = new fftw_complexarray(@out);

            switch (method)
            {
            case FFTMethod.DFT:
                // fftw_kind.R2HC: input is expected to be real while output is returned in the halfcomplex format
                fftw_plan fft = fftw_plan.r2r_1d(N, complexInput, complexOutput, fftw_kind.R2HC, fftw_flags.Estimate);
                fft.Execute();
                @out = complexOutput.Values;

                // free up memory
                fft = null;
                break;

            case FFTMethod.IDFT:
                // fftw_kind.HC2R: input is expected to be halfcomplex format while output is returned as real
                fftw_plan ifft = fftw_plan.r2r_1d(N, complexInput, complexOutput, fftw_kind.HC2R, fftw_flags.Estimate);
                ifft.Execute();
                //@out = complexOutput.ValuesDividedByN; // dividing by N gives the correct scale
                @out = complexOutput.Values;

                // free up memory
                ifft = null;
                break;

            case FFTMethod.DHT:
                fftw_plan dht = fftw_plan.r2r_1d(N, complexInput, complexOutput, fftw_kind.DHT, fftw_flags.Estimate);
                dht.Execute();
                @out = complexOutput.Values;

                // free up memory
                dht = null;
                break;
            }

            // free up memory
            complexInput  = null;
            complexOutput = null;
            GC.Collect();
        }
コード例 #7
0
        public MaxLenSequenceBuilder(int nBits)
        {
            NumBits   = nBits;
            Feedback  = 1 << (NumBits - 1);
            Period    = CalcPeriod(Feedback);
            _testAray = new ulong[(Period >> 6) + 1]; //AllocMemory();
            --Feedback;

            FullPeriod = Period; //var N = Period * minDisc * nPer;
            var ArrSz = ((FullPeriod + 1) / 2) * 2;

            _mls          = new double[ArrSz];
            _correlations = new double[ArrSz];

            _real     = new fftw_complexarray(ArrSz / 2);
            _complex  = new fftw_complexarray(FullPeriod / 2 + 1);
            _forward  = fftw_plan.dft_r2c_1d(FullPeriod, _real, _complex, fftw_flags.Estimate | fftw_flags.DestroyInput);
            _backward = fftw_plan.dft_c2r_1d(FullPeriod, _complex, _real, fftw_direction.Backward, fftw_flags.Estimate);
        }
コード例 #8
0
ファイル: FFTWtest.cs プロジェクト: gknilsen/alphanumerics
        static void Main(string[] args)
        {
            const int sampleSize = 1024;

            double[] din  = new double[sampleSize * 2];
            double[] dout = new double[sampleSize * 2];

            din[0] = 1;

            fftw_complexarray mdin  = new fftw_complexarray(din);
            fftw_complexarray mdout = new fftw_complexarray(dout);

            fftw_plan plan = fftw_plan.dft_1d(sampleSize, mdin, mdout, fftw_direction.Forward, fftw_flags.Estimate);

            plan.Execute();

            plan = fftw_plan.dft_1d(sampleSize, mdout, mdin, fftw_direction.Forward, fftw_flags.Estimate);
            plan.Execute();

            System.Numerics.Complex[] o = mdin.GetData_Complex();
        }
コード例 #9
0
    /// <summary>
    /// Perform the Fast Fourier Transform utilisizing the FFTW library
    /// </summary>
    /// <param name="in">Input Signal</param>
    /// <param name="out">Output Signal</param>
    /// <param name="N">N</param>
    /// <param name="method">FFT Method (DFT, IDFT, DHT)</param>
    public static void FFT(ref double[] @in, ref double[] @out, int N, FFTMethod method)
    {
        fftw_complexarray complexInput  = new fftw_complexarray(@in);
        fftw_complexarray complexOutput = new fftw_complexarray(@out);

        switch (method)
        {
        case FFTMethod.DFT:
            fftw_plan fft = fftw_plan.r2r_1d(N, complexInput, complexOutput, fftw_kind.R2HC, fftw_flags.Estimate);
            fft.Execute();
            @out = complexOutput.Values;

            // free up memory
            fft = null;
            break;

        case FFTMethod.IDFT:
            fftw_plan ifft = fftw_plan.r2r_1d(N, complexInput, complexOutput, fftw_kind.HC2R, fftw_flags.Estimate);
            ifft.Execute();
            @out = complexOutput.ValuesDividedByN;

            // free up memory
            ifft = null;
            break;

        case FFTMethod.DHT:
            fftw_plan dht = fftw_plan.r2r_1d(N, complexInput, complexOutput, fftw_kind.DHT, fftw_flags.Estimate);
            dht.Execute();
            @out = complexOutput.Values;

            // free up memory
            dht = null;
            break;
        }

        // free up memory
        complexInput  = null;
        complexOutput = null;
        GC.Collect();
    }
コード例 #10
0
        // seem to the be the fastest FFT?
        static double[] FFTWLIB(double[] signal)
        {
            var complexSignal = FFTUtils.DoubleToComplexDouble(signal);

            // prepare the input arrays
            var       complexInput  = new fftw_complexarray(complexSignal);
            var       complexOutput = new fftw_complexarray(complexSignal.Length / 2);
            fftw_plan fft           = fftw_plan.dft_1d(complexSignal.Length / 2, complexInput, complexOutput, fftw_direction.Forward, fftw_flags.Estimate);

            // perform the FFT
            fft.Execute();

            // get the result
            var spectrum_fft_abs = complexOutput.Abs;

            //Export.ExportCSV("audio_buffer_padded2.csv", signal);
            //Export.ExportCSV("spectrum_fft_abs2.csv", spectrum_fft_abs2, fftSize);

            // free up memory
            complexInput  = null;
            complexOutput = null;

            return(spectrum_fft_abs);
        }
コード例 #11
0
        /// <summary>
        ///     Blur bitmap with the Fastest Fourier Transform
        /// </summary>
        /// <returns>Blured bitmap</returns>
        private double[,,] Blur(double[,,] imageData)
        {
            int length = imageData.Length;
            int n0     = imageData.GetLength(0);
            int n1     = imageData.GetLength(1);
            int n2     = imageData.GetLength(2);

            var       input   = new fftw_complexarray(length);
            var       output  = new fftw_complexarray(length);
            fftw_plan forward = fftw_plan.dft_3d(n0, n1, n2, input, output,
                                                 fftw_direction.Forward,
                                                 fftw_flags.Estimate);
            fftw_plan backward = fftw_plan.dft_3d(n0, n1, n2, input, output,
                                                  fftw_direction.Backward,
                                                  fftw_flags.Estimate);

            var doubles = new double[length];

            Buffer.BlockCopy(imageData, 0, doubles, 0, length * sizeof(double));
            double average = doubles.Average();
            double delta   = Math.Sqrt(doubles.Average(x => x * x) - average * average);

            switch (_keepOption)
            {
            case KeepOption.AverageAndDelta:
                break;

            case KeepOption.Sum:
                average = doubles.Sum();
                break;

            case KeepOption.Square:
                average = Math.Sqrt(doubles.Sum(x => x * x));
                break;

            case KeepOption.AverageSquare:
                average = Math.Sqrt(doubles.Average(x => x * x));
                break;

            default:
                throw new NotImplementedException();
            }

            input.SetData(doubles.Select(x => new Complex(x, 0)).ToArray());
            forward.Execute();
            Complex[] complex = output.GetData_Complex();

            var data   = new Complex[n0, n1, n2];
            var buffer = new double[length * 2];

            GCHandle complexHandle = GCHandle.Alloc(complex, GCHandleType.Pinned);
            GCHandle dataHandle    = GCHandle.Alloc(data, GCHandleType.Pinned);
            IntPtr   complexPtr    = complexHandle.AddrOfPinnedObject();
            IntPtr   dataPtr       = dataHandle.AddrOfPinnedObject();

            Marshal.Copy(complexPtr, buffer, 0, buffer.Length);
            Marshal.Copy(buffer, 0, dataPtr, buffer.Length);
            switch (_mode)
            {
            case Mode.BlinderSize:
                Blind(data, _blinderSize);
                break;

            case Mode.FilterStep:
                int filterStep  = _filterStep;
                var blinderSize = new Size(MulDiv(n1, filterStep, filterStep + 1),
                                           MulDiv(n0, filterStep, filterStep + 1));
                Blind(data, blinderSize);
                break;

            default:
                throw new NotImplementedException();
            }
            Marshal.Copy(dataPtr, buffer, 0, buffer.Length);
            Marshal.Copy(buffer, 0, complexPtr, buffer.Length);

            complexHandle.Free();
            dataHandle.Free();

            input.SetData(complex);
            backward.Execute();
            doubles = output.GetData_Complex().Select(x => x.Magnitude).ToArray();

            double average2 = doubles.Average();
            double delta2   = Math.Sqrt(doubles.Average(x => x * x) - average2 * average2);

            switch (_keepOption)
            {
            case KeepOption.AverageAndDelta:
                break;

            case KeepOption.Sum:
                average2 = doubles.Sum();
                break;

            case KeepOption.Square:
                average2 = Math.Sqrt(doubles.Sum(x => x * x));
                break;

            case KeepOption.AverageSquare:
                average2 = Math.Sqrt(doubles.Average(x => x * x));
                break;

            default:
                throw new NotImplementedException();
            }
            // a*average2 + b == average
            // a*delta2 == delta
            double a = (_keepOption == KeepOption.AverageAndDelta) ? (delta / delta2) : (average / average2);
            double b = (_keepOption == KeepOption.AverageAndDelta) ? (average - a * average2) : 0;

            Debug.Assert(Math.Abs(a * average2 + b - average) < 0.1);
            doubles = doubles.Select(x => Math.Round(a * x + b)).ToArray();

            Buffer.BlockCopy(doubles, 0, imageData, 0, length * sizeof(double));
            return(imageData);
        }
コード例 #12
0
        /// <summary>
        ///     Catch pattern bitmap with the Fastest Fourier Transform
        /// </summary>
        /// <returns>Matrix of values</returns>
        private Matrix <double> Catch(Image <Gray, double> image)
        {
            const double f      = 1.0;
            int          length = image.Data.Length;
            int          n0     = image.Data.GetLength(0);
            int          n1     = image.Data.GetLength(1);
            int          n2     = image.Data.GetLength(2);

            Debug.Assert(n2 == 1);

            // Allocate FFTW structures
            var input  = new fftw_complexarray(length);
            var output = new fftw_complexarray(length);

            fftw_plan forward = fftw_plan.dft_3d(n0, n1, n2, input, output,
                                                 fftw_direction.Forward,
                                                 fftw_flags.Estimate);
            fftw_plan backward = fftw_plan.dft_3d(n0, n1, n2, input, output,
                                                  fftw_direction.Backward,
                                                  fftw_flags.Estimate);

            var matrix = new Matrix <double>(n0, n1);

            double[,,] patternData = _patternImage.Data;
            double[,,] imageData   = image.Data;
            double[,] data         = matrix.Data;

            var doubles = new double[length];

            // Calculate Divisor
            Copy(patternData, data);
            Buffer.BlockCopy(data, 0, doubles, 0, length * sizeof(double));
            input.SetData(doubles.Select(x => new Complex(x, 0)).ToArray());
            forward.Execute();
            Complex[] complex = output.GetData_Complex();

            Buffer.BlockCopy(imageData, 0, doubles, 0, length * sizeof(double));
            input.SetData(doubles.Select(x => new Complex(x, 0)).ToArray());
            forward.Execute();

            input.SetData(complex.Zip(output.GetData_Complex(), (x, y) => x * y).ToArray());
            backward.Execute();
            IEnumerable <double> doubles1 = output.GetData_Complex().Select(x => x.Magnitude);

            if (_fastMode)
            {
                // Fast Result
                Buffer.BlockCopy(doubles1.ToArray(), 0, data, 0, length * sizeof(double));
                return(matrix);
            }

            // Calculate Divider (aka Power)
            input.SetData(doubles.Select(x => new Complex(x * x, 0)).ToArray());
            forward.Execute();
            complex = output.GetData_Complex();

            CopyAndReplace(_patternImage.Data, data);
            Buffer.BlockCopy(data, 0, doubles, 0, length * sizeof(double));
            input.SetData(doubles.Select(x => new Complex(x, 0)).ToArray());
            forward.Execute();

            input.SetData(complex.Zip(output.GetData_Complex(), (x, y) => x * y).ToArray());
            backward.Execute();
            IEnumerable <double> doubles2 = output.GetData_Complex().Select(x => x.Magnitude);

            // Result
            Buffer.BlockCopy(doubles1.Zip(doubles2, (x, y) => (f + x * x) / (f + y)).ToArray(), 0, data, 0,
                             length * sizeof(double));
            return(matrix);
        }
コード例 #13
0
 public static extern void fftwf_execute(fftw_plan plan);
コード例 #14
0
 public static extern void fftw_execute_dft_c2r(fftw_plan plan,
                                                ref fftw_complex idata,
                                                ref double odata);
コード例 #15
0
 public static extern void fftw_execute_dft_r2c(fftw_plan plan,
                                                ref double idata,
                                                ref fftw_complex odata);
コード例 #16
0
 public static extern void fftw_execute_dft(fftw_plan plan,
                                            ref fftw_complex idata,
                                            ref fftw_complex odata);