public static void compute_fft(double[] data, List <complex> vecList) { for (int i = 0; i < (int)FFTLen; ++i) { if (i < FrmLen) { complex temp = new complex(data[i]); vecList.Add(temp); } else { complex temp = new complex(0); vecList.Add(temp); } } //complex[] List = vecList.ToArray<complex>(); FFT(512, vecList); }
public static void FFT(uint ulN, List <complex> vecList) //complex[] { //得到指数,这个指数实际上指出了计算FFT时内部的循环次数 uint ulPower = 0; //指数 uint ulN1 = ulN - 1; //ulN1=511 while (ulN1 > 0) { ulPower++; ulN1 /= 2; } //反序,因为FFT计算后的结果次序不是顺序的,需要反序来调整。可以在FFT实质部分计算之前先调整,也可以在结果 //计算出来后再调整。本程序中是先调整,再计算FFT实质部分 BitArray bsIndex = new BitArray(sizeof(int) * 8); uint ulIndex; //反转后的序号 uint ulK; for (ulong p = 0; p < ulN; p++) { ulIndex = 0; ulK = 1; bsIndex = new BitArray(BitConverter.GetBytes((uint)p)); for (uint j = 0; j < ulPower; j++) { ulIndex += bsIndex[(int)(ulPower - j - 1)] ? ulK : 0; ulK *= 2; } if (ulIndex > p) //只有大于时,才调整,否则又调整回去了 { complex c = vecList[(int)p]; vecList[(int)p] = vecList[(int)ulIndex]; vecList[(int)ulIndex] = c; } } //计算旋转因子 List <complex> vecW = new List <complex>(); for (uint i = 0; i < ulN / 2; i++) { vecW.Add(new complex((double)(Math.Cos(2 * i * PI / ulN)), (double)(-1 * Math.Sin(2 * i * PI / ulN)))); } //计算FFT uint ulGroupLength = 1; //段的长度 uint ulHalfLength = 0; //段长度的一半 uint ulGroupCount = 0; //段的数量 complex cw; //WH(x) complex c1; //G(x) + WH(x) complex c2; //G(x) - WH(x) complex[] vecW1 = vecW.ToArray <complex>(); for (uint b = 0; b < ulPower; b++) { ulHalfLength = ulGroupLength; ulGroupLength *= 2; for (int j = 0; j < ulN; j += (int)ulGroupLength) { for (int k = 0; k < (int)ulHalfLength; k++) { cw = vecW1[k * ulN / ulGroupLength] * vecList[j + k + (int)ulHalfLength]; c1 = vecList[j + k] + cw; c2 = vecList[j + k] - cw; vecList[j + k] = c1; vecList[j + k + (int)ulHalfLength] = c2; } } } }