基數 2 FFT
用於計算 FFT 的最簡單且可能是最著名的方法是 Radix-2 抽取時間演算法。Radix-2 FFT 通過將 N 點時域訊號分解為 N 個時域訊號來工作,每個時域訊號由單個點組成 。
通過位元反轉時域資料陣列的索引來實現訊號分解或時間抽取。因此,對於 16 點訊號,樣品 1(二元 0001)與樣品 8(1000)
交換,樣品 2(0010)
與 4(0100)交換,依此類推。使用位反轉技術的樣本交換可以簡單地在軟體中實現,但是將 Radix 2 FFT 的使用限制為長度為 N = 2 ^ M 的訊號。
時域中的 1 點訊號的值等於其在頻域中的值,因此該分解的單個時域點陣列不需要變換成為頻域點陣列。N 單點; 但是,需要重建為一個 N 點頻譜。使用蝶形計算執行完整頻譜的最佳重建。Radix-2 FFT 中的每個重建階段使用一組類似的指數加權函式 Wn ^ R 執行多個兩點蝴蝶。
FFT 利用 Wn ^ R 的週期性去除離散傅立葉變換中的冗餘計算。在蝴蝶計算的 log2(N)
階段完成光譜重建,給出 X [K]; 矩形的實數和虛數頻域資料。要轉換為幅度和相位(極座標),需要找到絕對值√(Re2 + Im2)和引數 tan-1(Im / Re)。
八點基數 2 FFT 的完整蝶形流程圖如下所示。注意,輸入訊號先前已根據先前概述的時間過程中的抽取重新排序。
FFT 通常在複雜輸入上執行併產生複雜輸出。對於實訊號,虛部可以設定為零並且實部設定為輸入訊號 x [n],然而,涉及純實資料的變換的許多優化是可能的。可以使用指數加權方程確定在整個重建期間使用的 Wn ^ R 的值。
R 的值(指數加權功率)確定光譜重建中的當前階段和特定蝶形內的當前計算。
程式碼示例(C / C++)
用於計算 Radix 2 FFT 的 AC / C++程式碼示例可以在下面找到。這是一個簡單的實現,適用於任何大小 N,其中 N 是 2 的冪。它比最快的 FFTw 實現慢大約 3 倍,但仍然是未來優化或瞭解該演算法如何工作的非常好的基礎。
#include <math.h>
#define PI 3.1415926535897932384626433832795 // PI for sine/cos calculations
#define TWOPI 6.283185307179586476925286766559 // 2*PI for sine/cos calculations
#define Deg2Rad 0.017453292519943295769236907684886 // Degrees to Radians factor
#define Rad2Deg 57.295779513082320876798154814105 // Radians to Degrees factor
#define log10_2 0.30102999566398119521373889472449 // Log10 of 2
#define log10_2_INV 3.3219280948873623478703194294948 // 1/Log10(2)
// complex variable structure (double precision)
struct complex
{
public:
double Re, Im; // Not so complicated after all
};
// Returns true if N is a power of 2
bool isPwrTwo(int N, int *M)
{
*M = (int)ceil(log10((double)N) * log10_2_INV);// M is number of stages to perform. 2^M = N
int NN = (int)pow(2.0, *M);
if ((NN != N) || (NN == 0)) // Check N is a power of 2.
return false;
return true;
}
void rad2FFT(int N, complex *x, complex *DFT)
{
int M = 0;
// Check if power of two. If not, exit
if (!isPwrTwo(N, &M))
throw "Rad2FFT(): N must be a power of 2 for Radix FFT";
// Integer Variables
int BSep; // BSep is memory spacing between butterflies
int BWidth; // BWidth is memory spacing of opposite ends of the butterfly
int P; // P is number of similar Wn's to be used in that stage
int j; // j is used in a loop to perform all calculations in each stage
int stage = 1; // stage is the stage number of the FFT. There are M stages in total (1 to M).
int HiIndex; // HiIndex is the index of the DFT array for the top value of each butterfly calc
unsigned int iaddr; // bitmask for bit reversal
int ii; // Integer bitfield for bit reversal (Decimation in Time)
int MM1 = M - 1;
unsigned int i;
int l;
unsigned int nMax = (unsigned int)N;
// Double Precision Variables
double TwoPi_N = TWOPI / (double)N; // constant to save computational time. = 2*PI / N
double TwoPi_NP;
// complex Variables (See 'struct complex')
complex WN; // Wn is the exponential weighting function in the form a + jb
complex TEMP; // TEMP is used to save computation in the butterfly calc
complex *pDFT = DFT; // Pointer to first elements in DFT array
complex *pLo; // Pointer for lo / hi value of butterfly calcs
complex *pHi;
complex *pX; // Pointer to x[n]
// Decimation In Time - x[n] sample sorting
for (i = 0; i < nMax; i++, DFT++)
{
pX = x + i; // Calculate current x[n] from base address *x and index i.
ii = 0; // Reset new address for DFT[n]
iaddr = i; // Copy i for manipulations
for (l = 0; l < M; l++) // Bit reverse i and store in ii...
{
if (iaddr & 0x01) // Detemine least significant bit
ii += (1 << (MM1 - l)); // Increment ii by 2^(M-1-l) if lsb was 1
iaddr >>= 1; // right shift iaddr to test next bit. Use logical operations for speed increase
if (!iaddr)
break;
}
DFT = pDFT + ii; // Calculate current DFT[n] from base address *pDFT and bit reversed index ii
DFT->Re = pX->Re; // Update the complex array with address sorted time domain signal x[n]
DFT->Im = pX->Im; // NB: Imaginary is always zero
}
// FFT Computation by butterfly calculation
for (stage = 1; stage <= M; stage++) // Loop for M stages, where 2^M = N
{
BSep = (int)(pow(2, stage)); // Separation between butterflies = 2^stage
P = N / BSep; // Similar Wn's in this stage = N/Bsep
BWidth = BSep / 2; // Butterfly width (spacing between opposite points) = Separation / 2.
TwoPi_NP = TwoPi_N*P;
for (j = 0; j < BWidth; j++) // Loop for j calculations per butterfly
{
if (j != 0) // Save on calculation if R = 0, as WN^0 = (1 + j0)
{
//WN.Re = cos(TwoPi_NP*j)
WN.Re = cos(TwoPi_N*P*j); // Calculate Wn (Real and Imaginary)
WN.Im = -sin(TwoPi_N*P*j);
}
for (HiIndex = j; HiIndex < N; HiIndex += BSep) // Loop for HiIndex Step BSep butterflies per stage
{
pHi = pDFT + HiIndex; // Point to higher value
pLo = pHi + BWidth; // Point to lower value (Note VC++ adjusts for spacing between elements)
if (j != 0) // If exponential power is not zero...
{
//CMult(pLo, &WN, &TEMP); // Perform complex multiplication of Lovalue with Wn
TEMP.Re = (pLo->Re * WN.Re) - (pLo->Im * WN.Im);
TEMP.Im = (pLo->Re * WN.Im) + (pLo->Im * WN.Re);
//CSub (pHi, &TEMP, pLo);
pLo->Re = pHi->Re - TEMP.Re; // Find new Lovalue (complex subtraction)
pLo->Im = pHi->Im - TEMP.Im;
//CAdd (pHi, &TEMP, pHi); // Find new Hivalue (complex addition)
pHi->Re = (pHi->Re + TEMP.Re);
pHi->Im = (pHi->Im + TEMP.Im);
}
else
{
TEMP.Re = pLo->Re;
TEMP.Im = pLo->Im;
//CSub (pHi, &TEMP, pLo);
pLo->Re = pHi->Re - TEMP.Re; // Find new Lovalue (complex subtraction)
pLo->Im = pHi->Im - TEMP.Im;
//CAdd (pHi, &TEMP, pHi); // Find new Hivalue (complex addition)
pHi->Re = (pHi->Re + TEMP.Re);
pHi->Im = (pHi->Im + TEMP.Im);
}
}
}
}
pLo = 0; // Null all pointers
pHi = 0;
pDFT = 0;
DFT = 0;
pX = 0;
}