コード例 #1
0
        public static void Example7() //================[ Basic FFT + Windowing Example + ZeroPadding ]================
        {
            // Same Input Signal as Example 1, except everything is a power of two
            double amplitude = 1.0; double frequency = 32768;
            UInt32 length = 1024; double samplingRate = 131072;

            double[] inputSignal = DSP.Generate.ToneSampling(amplitude, frequency, samplingRate, length);

            // Apply window to the Input Data & calculate Scale Factor
            double[] wCoefs       = DSP.Window.Coefficients(DSP.Window.Type.Hamming, length);
            double[] wInputData   = DSP.Math.Multiply(inputSignal, wCoefs);
            double   wScaleFactor = DSP.Window.ScaleFactor.Signal(wCoefs);

            // Instantiate & Initialize a new DFT
            DSPLib.FFT fft = new DSPLib.FFT();
            fft.Initialize(length, length * 3);           // Zero Padding = 1024 * 3

            // Call the FFT and get the scaled spectrum back
            Complex[] cSpectrum = fft.Execute(wInputData);

            // Convert the complex spectrum to note: Magnitude Squared Format
            // See text for the reasons to use Mag^2 format.
            double[] lmSpectrum = DSP.ConvertComplex.ToMagnitude(cSpectrum);

            // Properly scale the spectrum for the added window
            lmSpectrum = DSP.Math.Multiply(lmSpectrum, wScaleFactor);

            // For plotting on an XY Scatter plot generate the X Axis frequency Span
            double[] freqSpan = fft.FrequencySpan(samplingRate);

            // At this point a XY Scatter plot can be generated from,
            // X axis => freqSpan
            // Y axis => lmSpectrum
        }
コード例 #2
0
        /// <summary>
        /// FFT using DSPLib library.
        /// </summary>
        /// <param name="inputArray">The input array.</param>
        /// <param name="logScale">if set to <c>true</c> [log scale]</param>
        /// <returns></returns>
        Tuple <double[], double[]> FastFourierDSPLib(double[] inputArray, bool logScale)
        {
            double[] tempArray = new double[2048];

            for (int i = 0; i < 2048; i++)
            {
                tempArray[i] = inputArray[i];
            }

            UInt32 length       = 2048;
            double samplingRate = 44100;

            double[] spectrum     = new double[4096];
            double[] wCoefs       = DSP.Window.Coefficients(DSP.Window.Type.Hamming, length);
            double[] wInputData   = DSP.Math.Multiply(tempArray, wCoefs);
            double   wScaleFactor = DSP.Window.ScaleFactor.Signal(wCoefs);

            DSPLib.FFT fft = new DSPLib.FFT();
            fft.Initialize(length, length * 3);
            Complex[] cSpectrum = fft.Execute(wInputData);
            spectrum = DSP.ConvertComplex.ToMagnitude(cSpectrum);

            if (logScale == true)
            {
                spectrum = DSP.ConvertMagnitude.ToMagnitudeDBV(spectrum);

                for (int i = 0; i < spectrum.Length; i++)
                {
                    spectrum[i] -= 51;
                }
            }

            spectrum = DSP.Math.Multiply(spectrum, wScaleFactor);
            double[] freqSpan = fft.FrequencySpan(samplingRate);
            var      tuple    = new Tuple <double[], double[]>(spectrum, freqSpan);

            return(tuple);
        }