快速傅里叶变换FFT的学习笔记一:C语言代码的简单实现

快速傅里叶变换FFT的学习笔记一:C语言代码的简单实现

fft.c

#include "math.h"
#include "fft.h"

void conjugate_complex(int n,complex in[],complex out[])
{
	int i = 0;
	for(i=0;i<n;i++)
	{
		out[i].imag = -in[i].imag;
		out[i].real = in[i].real;
	}
}

void c_abs(complex f[],float out[],int n)
{
	int i = 0;
	float t;
	for(i=0;i<n;i++)
	{
		t = f[i].real * f[i].real + f[i].imag * f[i].imag;
		out[i] = sqrt(t);
	}
}

float c_value(complex f)
{
	return f.real * f.real + f.imag * f.imag;
}


void c_plus(complex a,complex b,complex *c)
{
	c->real = a.real + b.real;
	c->imag = a.imag + b.imag;
}

void c_sub(complex a,complex b,complex *c)
{
	c->real = a.real - b.real;
	c->imag = a.imag - b.imag;
}

void c_mul(complex a,complex b,complex *c)
{
	c->real = a.real * b.real - a.imag * b.imag;
	c->imag = a.real * b.imag + a.imag * b.real;
}

void c_div(complex a,complex b,complex *c)
{
	c->real = (a.real * b.real + a.imag * b.imag)/(b.real * b.real +b.imag * b.imag);
	c->imag = (a.imag * b.real - a.real * b.imag)/(b.real * b.real +b.imag * b.imag);
}

#define SWAP(a,b)  tempr=(a);(a)=(b);(b)=tempr

void Wn_i(int n,int i,complex *Wn,char flag)
{
	Wn->real = cos(2*PI*i/n);
	if(flag == 1)
		Wn->imag = -sin(2*PI*i/n);
	else if(flag == 0)
		Wn->imag = -sin(2*PI*i/n);
}

//傅里叶变化
void fft(int length,complex f[])
{
	complex t,wn;//中间变量
	int i,j,k,m,n,l,r,M;
	int la,lb,lc;

	/*----计算分解的级数M=log2(length)----*/
	for(i=length,M=1;(i=i/2)!=1;M++);

	/*----按照倒位序重新排列原信号----*/
	for(i=1,j=length/2;i<=length-2;i++)
	{
		if(i<j)
		{
			t=f[j];
			f[j]=f[i];
			f[i]=t;
		}
		k=length/2;
		while(k<=j)
		{
			j=j-k;
			k=k/2;
		}
		j=j+k;
	}

	/*----FFT算法----*/
	for(m=1;m<=M;m++)
	{
		la=pow(2,m); 	//la=2^m代表第m级每个分组所含节点数
		lb=la/2;    	//lb代表第m级每个分组所含碟形单元数

		//同时它也表示每个碟形单元上下节点之间的距离
		/*----碟形运算----*/
		for(l=1;l<=lb;l++)
		{
			r=(l-1)*pow(2,M-m);
			for(n=l-1;n<length-1;n=n+la) //遍历每个分组,分组总数为N/la
			{
				lc=n+lb;  //n,lc分别代表一个碟形单元的上、下节点编号
				Wn_i(length,r,&wn,1);		//wn=Wnr
				c_mul(f[lc],wn,&t);			//t = f[lc] * wn复数运算
				c_sub(f[n],t,&(f[lc]));		//f[lc] = f[n] - f[lc] * Wnr
				c_plus(f[n],t,&(f[n]));		//f[n] = f[n] + f[lc] * Wnr
			}
		}
	}
}

//傅里叶逆变换
void ifft(int length,complex f[])
{
	int i=0;
	conjugate_complex(length,f,f);
	fft(length,f);
	conjugate_complex(length,f,f);
	for(i=0;i<length;i++)
	{
		f[i].imag = (f[i].imag)/length;
		f[i].real = (f[i].real)/length;
	}
}

fft.h

#ifndef __FFT_H__
#define __FFT_H__

typedef struct //复数类型
{
  float real;		//实部
  float imag;		//虚部
}complex;

#define PI 3.1415926535897932384626433832795028841971


///////////////////////////////////////////
void conjugate_complex(int n,complex in[],complex out[]);
float c_value(complex f);								//复数取模
void c_plus(complex a,complex b,complex *c);			//复数加
void c_mul(complex a,complex b,complex *c) ;			//复数乘
void c_sub(complex a,complex b,complex *c);				//复数减法
void c_div(complex a,complex b,complex *c);				//复数除法
void fft(int length,complex f[]);							//傅立叶变换 输出也存在数组f中
void ifft(int length,complex f[]); 						// 傅里叶逆变换
void c_abs(complex f[],float out[],int n);				//复数数组取模
////////////////////////////////////////////
#endif

main.c


void measure_fft(float *phase, float *amplitude)
{
	int index=0, max_index=1;
	float max_value, lx, ly;
    for(index = 0; index < FFT_LEN; index++)
    {
    	fft_buff[index].real = input[index];
    	fft_buff[index].imag = 0;
    } 
    fft(FFT_LEN, fft_buff);		//进行FFT处理

    //从1开始,去掉直流分量
    max_value=c_value(fft_buff[max_index]);
    for(index = 1; index < FFT_LEN; index++)
    {
    	if(c_value(fft_buff[index])>max_value)
    	{
    		max_index = index;
    		max_value = c_value(fft_buff[index]);
    	}
    }
    if(max_index!=1)
    {
    	printf("error! fft freq wrong!\r\n"); 
    }

    lx = fft_buff[max_index].real ;
    ly = fft_buff[max_index].imag ;
    *phase = atanf(lx / ly);
    *amplitude = sqrt(max_value);
}

int main(void)
{
	... ...
}

注意:

  1. input数组是AD采集的数组
  2. fft_buff数组是用来FFT变换,结果也存在fft_buff中
  3. 去掉第一个点的直流分量
  4. 目标频率在第二个点中,而且目标频率点的模最大,所以加了简单的判断

快速傅里叶变换FFT的学习笔记一:C语言代码的简单实现