public void GetSpectrumData(float[] samples, int channel) { if (!Mathf.IsPowerOfTwo(samples.Length) || samples.Length > MaxChannelSamplesFFT) { throw new ArgumentException(); } int numSamples = 2 * samples.Length; if (null == FFTInOut || numSamples != FFTInOut.Length) { FFTInOut = new double[numSamples]; } GetOutputDataDouble(FFTInOut, channel); FFT.RealFFT(FFTInOut, true); samples[0] = samples[samples.Length - 1] = 0.0f; for (int i = 2; i < FFTInOut.Length; i += 2) { float re = (float)FFTInOut[i]; float im = (float)FFTInOut[i + 1]; samples[i / 2] = Mathf.Sqrt(re * re + im * im) / samples.Length; } }
public void ComputeComirvaMatrixUsingLomontRealFFT(ref Comirva.Audio.Util.Maths.Matrix m, int column, float[] audiodata, int pos) { // apply the window method (e.g HammingWindow, HannWindow etc) win.Apply(ref data, audiodata, pos); double[] fft = new double[data.Length / 2]; Array.Copy(data, fft, data.Length / 2); lomonFFT.RealFFT(fft, true); // fft input will now contain the FFT values // r0, r(n/2), r1, i1, r2, i2 ... m.MatrixData[0][column] = Math.Sqrt(fft[0] * fft[0] * winsize); m.MatrixData[winsize / 2 - 1][column] = Math.Sqrt(fft[1] * fft[1] * winsize); for (int row = 1; row < winsize / 2; row++) { // amplitude (or magnitude) is the square root of the power spectrum // the magnitude spectrum is abs(fft), i.e. Math.Sqrt(re*re + img*img) // use 20*log10(Y) to get dB from amplitude // the power spectrum is the magnitude spectrum squared // use 10*log10(Y) to get dB from power spectrum m.MatrixData[row][column] = Math.Sqrt((fft[2 * row] * fft[2 * row] + fft[2 * row + 1] * fft[2 * row + 1]) * winsize); } }