c语言编写傅里叶变换函数 c语言傅立叶变换函数( 三 )


// FFT.pch 将成为预编译头
// stdafx.obj 将包含预编译类型信息
#include "stdafx.h"
// TODO: 在 STDAFX.H 中
//引用任何所需的附加头文件,而不是在此文件中引用
求基2、基4、基8FFT(快速傅里叶变换)的c语言程序,要能运行得出来的1.FFTc语言编写傅里叶变换函数:
// data为输入和输出c语言编写傅里叶变换函数的数据c语言编写傅里叶变换函数,N为长度
bool CFFT::Forward(complex *const Data, const unsigned int N)
{
if (!Data || N1 || N(N - 1))
return false;
//排序
Rearrange(Data, N);
//FFT计算:const bool Inverse = false
Perform(Data, N);
return true;
}
2.IFFT:
// Scale 为是否缩放
bool CFFT::Inverse(complex *const Data, const unsigned int N,
const bool Scale /* = true */)
{
if (!Data || N1 || N(N - 1))
return false;
//排序
Rearrange(Data, N);
//FFT计算c语言编写傅里叶变换函数,ture表示是逆运算
Perform(Data, N, true);
//对结果进行缩放
if (Scale)
CFFT::Scale(Data, N);
return true;
}
3.排序:
void CFFT::Rearrange(complex *const Data, const unsigned int N)
{
//Swap position
unsigned int Target = 0;
//Process all positions of input signal
for (unsigned int Position = 0; PositionN; ++Position)
{
//Only for not yet swapped entries
if (TargetPosition)
{
//Swap entries
const complex Temp(Data[Target]);
Data[Target] = Data[Position];
Data[Position] = Temp;
}
//Bit mask
unsigned int Mask = N;
//While bit is set
while (Target(Mask = 1))
//Drop bit
Target = ~Mask;
//The current bit is 0 - set it
Target |= Mask;
}
}
4.FFT计算:
void CFFT::Perform(complex *const Data, const unsigned int N,
const bool Inverse /* = false */)
{
const double pi = Inverse ? 3.14159265358979323846 : -3.14159265358979323846;
//Iteration through dyads, quadruples, octads and so on...
for (unsigned int Step = 1; StepN; Step = 1)
{
//Jump to the next entry of the same transform factor
const unsigned int Jump = Step1;
//Angle increment
const double delta = pi / double(Step);
//Auxiliary sin(delta / 2)
const double Sine = sin(delta * .5);
//Multiplier for trigonometric recurrence
const complex Multiplier(-2. * Sine * Sine, sin(delta));
//Start value for transform factor, fi = 0
complex Factor(1.);
//Iteration through groups of different transform factor
for (unsigned int Group = 0; GroupStep; ++Group)
{
//Iteration within group
for (unsigned int Pair = Group; PairN; Pair += Jump)
{
//Match position
const unsigned int Match = Pair + Step;
//Second term of two-point transform
const complex Product(Factor * Data[Match]);
//Transform for fi + pi
Data[Match] = Data[Pair] - Product;
//Transform for fi
Data[Pair] += Product;
}
//Successive transform factor via trigonometric recurrence
Factor = Multiplier * Factor + Factor;
}
}
}
5.缩放:
void CFFT::Scale(complex *const Data, const unsigned int N)
{
const double Factor = 1. / double(N);
//Scale all data entries
for (unsigned int Position = 0; PositionN; ++Position)
Data[Position] *= Factor;
}
求个快速傅里叶变换的C语言程序voidfft()
{
int nn,n1,n2,i,j,k,l,m,s,l1;
float ar[1024],ai[1024]; // 实部 虚部
float a[2050];
float t1,t2,x,y;
float w1,w2,u1,u2,z;
float fsin[10]={0.000000,1.000000,0.707107,0.3826834,0.1950903,0.09801713,0.04906767,0.02454123,0.01227154,0.00613588,};// 优化
float fcos[10]={-1.000000,0.000000,0.7071068,0.9238796,0.9807853,0.99518472,0.99879545,0.9996988,0.9999247,0.9999812,};

推荐阅读