With NAudio (light)
using NAudio.Wave; // for sound card access using NAudio.Dsp; // for FastFourierTransform // prepare the complex data which will be FFT'd Complex[] fft_buffer = new Complex[fft_size]; for (int i=0; i < fft_size; i++) { fft_buffer.X = (float)(unanalyzed_values* FastFourierTransform.HammingWindow(i, fft_size)); fft_buffer.Y = 0; } // perform the FFT FastFourierTransform.FFT(true, (int)Math.Log(fft_size, 2.0), fft_buffer); // a list with FFT values List<double> new_data = new List<double>(); for (int i = 0; i < spec_data[spec_data.Count - 1].Count; i++) { // should this besqrt(X^2+Y^2)? double val; val = (double)fft_buffer.X+ (double)fft_buffer.Y; val = Math.Abs(val); if (checkBox1.Checked)val = Math.Log(val); new_data.Add(val); } With Accord (heavy)
using System.Numerics; public double[] FFT(double[] data) { int nPoints = data.Length; // whatever wemeasure must be a power of 2 for (int i = 0; i < data.Length; i++) data = Math.Sin(i); // fill it withsome data double[] fft = new double[nPoints]; // this is wherewe will store the output (fft) Complex[] fftComplex = new Complex[nPoints]; // the FFTfunction requires complex format for (int i = 0; i < data.Length; i++) fftComplex = new Complex(data, 0.0); // make itcomplex format Accord.Math.FourierTransform.FFT(fftComplex, Accord.Math.FourierTransform.Direction.Forward); for (int i = 0; i < data.Length; i++) fft = fftComplex.Magnitude;// back to double return fft; //todo: this couldbe much faster by reusing variables }
|