为什么我的FFT功能不能正常工作

时间:2014-11-13 15:05:59

标签: c# matlab fft

我使用AForge为FFT做了一个函数。它似乎工作,但当我与我的主管核实时,他说输出不正确。他正在使用MatLab的PWelch功能。我们已经发现我们正在使用不同的窗口,但更改它们并没有产生显着的差异。所以再次,该功能确实有效,但根据我的主管,输出是不正确的。请帮忙!

这是我的功能,我希望有人看到一些错误的东西,因为我已经看了两个星期了。进入它的DATA已经等距。

private void FastFoulierMethod()
    {
        int NFFT = 64;
        int N_OVERLAP = 32;
        int numberOfEpochs = samples.Count / NFFT;
        int numberOfSamplesToSelectFromFFT = NFFT-1;

        double[] dataaa = new double[samples.Count];
        for (int i = 0; i < samples.Count - 1; i++)
        {
            dataaa[i] = samples[i].GetValue();//lijst met doubles die we gebruiken
        }

        double[,] pFrame = new double[numberOfEpochs, numberOfSamplesToSelectFromFFT];

        // The first epoch in the page starts at index 0
        int beginIndexOfEpoch = 0;

        for (int i = 0; i < numberOfEpochs; i++)
        {
            // This will get the current epoch by retrieving samples from the sample list
            //  starting at 'beginIndex' with length 'NFFT'. This epoch will need to be detrended next.
            List<double> smapletemp = new List<double>();

            for (int x = beginIndexOfEpoch; x < beginIndexOfEpoch+NFFT; x++)
            {
                smapletemp.Add(dataaa[x]);
            }
            double[] epoch = smapletemp.ToArray();
            if (epoch.Length == 0)
            {
                break;
            }
            // Create array of X-axis values 1,2,3,4 ... n
            // which will be used to perform linear regression.
            double[] xValues = new double[epoch.Length];
            for (int j = 0; j < xValues.Length; j++)
            {
                xValues[j] = j;
            }

            // Perform linear regression on the epoch. This will result in some data that is used later.
            Dictionary<String, double> linearRegressionData = math.performLinearRegression(xValues.ToList(), epoch.ToList());
            // Detrend the epoch
            for (int j = 0; j < epoch.Length; j++)
            {
                double intercept = linearRegressionData["Alpha"]; // Get the intercept from the data.
                double slope = linearRegressionData["Beta"]; // Get the slope from the data.
                //if (1 >= math.StdDev(epoch))
                //{
                    epoch[j] = epoch[j] - intercept - (slope * j); // Detrend the epoch by subtracting the intercept and the slope * j.
                //}
            }

            // Create Complex from the epoch for windowing and FFT processing.
            Complex[] cmplx = new Complex[epoch.Length];
            for (int j = 0; j < cmplx.Length; j++)
            {
                cmplx[j] = new Complex(epoch[j], 0);
            }

            // Perform Hann window function on the Complex.
            math.hann(cmplx);
            // Perform Fast Fourier Transform on the Complex.
            FourierTransform.FFT(cmplx, FourierTransform.Direction.Backward);
            // Create an array for all powers.
            double[] powers = new double[cmplx.Length];
            for (int j = 0; j < epoch.Length; j++)
            {
                powers[j] = cmplx[j].SquaredMagnitude;
            }

            // Add the powers to the power frame.
            for (int j = 0; j < powers.Length-1; j++)
            {
                pFrame[i, j] = powers[j];
            }

            // Shift index for the next epoch.
            beginIndexOfEpoch += NFFT - N_OVERLAP;
            if ( beginIndexOfEpoch + NFFT > samples.Count)
            {
                break;
            }
        }

        // Create an array for the nan-mean values of all epochs.
        // Nan-mean calculates the mean of a set of doubles, ignoring NaN's.
        double[] nanMeanValues = new double[numberOfSamplesToSelectFromFFT];
        List<double[]> Y = new List<double[]>();

        for (int i = 0; i < numberOfSamplesToSelectFromFFT; i++)
        {
            // The sum for calculating the mean.
            double sum = 0.0;
            // The number of elements (doubles) for calculating the mean.
            int count = 0;

            // For all epochs...
            for (int j = 0; j < numberOfEpochs; j++)
            {
                // ...the mean for all doubles at index 'i' is calculated.
                double sample = pFrame[j, i];
                if (!Double.IsNaN(sample))
                {
                    // Only take the double into account when it isn't a NaN.
                    sum += sample;
                    count++;
                }
            }

            // Actually calculate the mean and add it to the array.
            nanMeanValues[i] = sum / count;
        }

        // We now have the mean of all power arrays (or epochs).
        // Create an array with Root Mean Square values.
        double[] squareRootedNanMeans = new double[nanMeanValues.Length];
        for (int i = 0; i < squareRootedNanMeans.Length; i++)
        {
            squareRootedNanMeans[i] = Math.Sqrt(nanMeanValues[i]);
        }

            Y.Add(squareRootedNanMeans);

1 个答案:

答案 0 :(得分:0)

自从我学习FFT以来已经很久了,但除非你的学术任务是生成一个fft函数,否则我建议你使用一些库。编写自己的东西来学习是很棒的,但是如果你需要结果,那就去做吧。

您可以使用具有免费版本的Alglib {http://www.alglib.net/fasttransforms/fft.php}。

希望这有帮助。