傅里叶变换C++模式

三组傅里叶变换和反变换的代码

#include 

#include 

#include 

#include 

#include 

#include 

#include  

#include 

using
namespace  std;



#define PI 3.14159265 

#define N  4095       //采样256次



typedef
struct               //定义结构体

{

	double
	real;/*实部*/

	double
		img;/*虚部*/

}complex;



complex x[N * 2], *W;                       /*输出序列的值*/
//序列长度 全局变量



void
add(complex a, complex b, complex *c)      //  复数加运算

{

	c->real = a.real + b.real;

	c->img = a.img + b.img;

}

void
sub(complex a, complex b, complex *c)      //  复数减运算

{

	c->real = a.real - b.real;

	c->img = a.img - b.img;

}

void
mul(complex a, complex b, complex *c)      //复数乘运算

{

	c->real = a.real*b.real - a.img*b.img;

	c->img = a.real*b.img + a.img*b.real;

}

void
divi(complex a, complex b, complex *c)      //  复数除运算

{

	c->real = (a.real*b.real + a.img*b.img) / (b.real*b.real + b.img*b.img);

	c->img = (a.img*b.real - a.real*b.img) / (b.real*b.real + b.img*b.img);

}



void
initW(int
size)                                //欧拉公式运算  

{

	int
		i;

	W = (complex*)malloc(sizeof(complex)* size);    //分配内存空间

	for
		(i = 0; i0)

		{

			j = j << 1;

			j |= (k & 1);

			k = k >> 1;

		}

		if
			(j>i)

		{

			temp = x[i];

			x[i] = x[j];

			x[j] = temp;

		}

	}

}





void
fftx()                             //快速傅里叶函数

{

	long
		int  i = 0, j = 0, k = 0, l = 0;

	complex up, down, product;

	changex(N);

	for
		(i = 0; i= 0.0001)

			printf("+%.2fj  ", x[i].img);

		else
		if (fabs(x[i].img)<0.0001)

			printf("+0.0000j  ");

		else

			printf("%.2fj  ", x[i].img);

	}

	printf("\n");

}



/*void save()           //保存x傅里叶变换结果

{

int i;

ofstream outfile("D:\\result1.txt", ios::out);

if (!outfile)

{

cerr << "open result1.txt erro!" << endl;

exit(1);

}

outfile << "x傅里叶变换结果:" << endl;

for (i = 0; i= 0.0001)

outfile << "+" << x[i].img << "j" << " ";

else   if (fabs(x[i].img)<0.0001)

outfile << "+" << "0.00" << "j" << " ";

else

outfile << x[i].img << "j" << " ";

}

printf("\n");

outfile.close();

}*/



int
main()

{

	clock_t
		start = clock();

	double
		duration;

	/*对x进行傅里叶变换*/

	int
		i, j = 0, t = 0;

	double
		data[N * 2] = { 0 };

	double
		result[N * 2] = { 0 };

	float
		tx = 0;

	//ofstream outfile1("D:\\result.txt", ios::out);

	for
		(t = 0; t < N; t++)

	{

		data[t] = 5 + 7 * cos(30 * PI / 128 * t - 30 * PI / 180) + 3 * cos(80 * PI / 128 * t - 90 * PI / 180);

	}

	for
		(i = 0; i

 傅里叶变换,加入一个低通滤波器

// fourie2.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 

#define pi (double) 3.14159265359
#define POWER 9
using namespace std;
/*复数的定义*/
typedef struct
{
	double re;
	double im;
}COMPLEX;
/*复数的加运算*/
COMPLEX Add(COMPLEX c1, COMPLEX c2)
{
	COMPLEX c;
	c.re = c1.re + c2.re;
	c.im = c1.im + c2.im;
	return c;
}
/*负数的减运算*/
COMPLEX Sub(COMPLEX c1, COMPLEX c2)
{
	COMPLEX c;
	c.re = c1.re - c2.re;
	c.im = c1.im - c2.im;
	return c;
}
/*复数的乘运算*/
COMPLEX Mul(COMPLEX c1, COMPLEX c2)
{
	COMPLEX c;
	c.re = c1.re*c2.re - c1.im*c2.im;
	c.im = c1.re*c2.im + c1.im*c2.re;
	return c;
}
/*快速傅立叶变换
TD为时域值,FD为频域值,power为2的幂数*/
void FFT(COMPLEX *TD, COMPLEX *FD, int power)
{
	int count;
	int i, j, k, bfsize, p;
	double angle;
	COMPLEX *W, *X1, *X2, *X;
	/*计算傅立叶变换点数*/
	count = 1 << power;
	/*分配运算器所需存储器*/
	W = (COMPLEX *)malloc(sizeof(COMPLEX)*count / 2);
	X1 = (COMPLEX *)malloc(sizeof(COMPLEX)*count);
	X2 = (COMPLEX *)malloc(sizeof(COMPLEX)*count);
	/*计算加权系数*/
	for (i = 0; i aa;
	for (j = 0; jlength/2&&i<=length/2+frequence)
		{
			//gauss[i] = exp(-pow(length / 2 - i, 2) / (2 * frequence*frequence));
			double H = exp(-pow(frequence - i, 2) / (2 * (length / 2)*(length / 2)));
			double M = (0.5 + 0.5*cos(2 * pi*(i - frequence) / (length / 2)));
			FD[i].re = FD[i].re*M;
		}

	}
	//convolution(current_re, gauss, data_out, length, length);
	//for (int i = 0; i < length;i++)
	//{
	//	FD[i].re = data_out[i];
	//}
	//delete[] data_out;
	//delete[] current_re;
	//delete[] gauss;
}

void low_pass_filter(string path,int cut_frequence)
{
	ifstream file_in;
	file_in.open(path);//输入数据路径
	string lineStr;
	string str;
	int i = 0;
	double dv6[1 << POWER];
	while (getline(file_in, lineStr))
	{
		stringstream ss(lineStr);
		string str;
		int j = 0;
		double b[2];
		while (getline(ss, str, ','))
		{
			double a = atof(str.c_str());
			b[j] = a;
			j++;
		}
		if (i == 1<

 下面也是一个低通滤波器,速度快一点

#include  
#include  
#include 
#include 
#include 
#include 
#include 

using namespace std;
#define LENGTH 512
//w0=40时达到预期效果

/*-----------------------------------------------------------------//
//                 Fast Fourier Transform program                  //
//-----------------------------------------------------------------//
//  Algrithem: Cooley-Tukey Algrithem ( decimation in frequency )  //
//                                                                 //
//   xr  : Real part of original data as the input;                //
//         Real part of the output.                                //
//   xi  : Imaginary part of original data as the input;           //
//         Imaginary part of the output.                           //
//   nr  : must be an positive integer,                            //
//         N=pow(2,nu) is the length of input array (points).      //
//   T   : =1.0 is forward transform;  =-1.0 is reverse transform  //
//                                                                 //
//-----------------------------------------------------------------*/
void fft(float *xr, float *xi, int nr, float T)
{
	int IBIT(int j, int m);
	int   i, j, k, l, n, n2, nr1, i1, j1, k2, k1n2;
	float c, s, p, tr, ti, trc, tic, ars;

	n = (int)(pow(2.0, nr));
	n2 = n / 2;
	nr1 = nr - 1;

	k = 0;
	for (i = 1; i <= nr; i++)
	{
	loop1:  for (j = 1; j <= n2; j++)
	{
				k2 = k / (int)(pow(2.0, nr1));
				p = (float)(IBIT(k2, nr));
				ars = (float)(6.2831852*p / (float)(n));  //辐角
				c = (float)(cos(ars));
				s = (float)(sin(ars));
				k1n2 = k + n2;
				tr = xr[k1n2] * c + xi[k1n2] * s*T;
				ti = xi[k1n2] * c - xr[k1n2] * s*T;
				xr[k1n2] = xr[k] - tr;
				xi[k1n2] = xi[k] - ti;
				xr[k] = xr[k] + tr;
				xi[k] = xi[k] + ti;
				k++;
	}
			k += n2;
			if (kj)
		{
			j1 = j - 1;
			i1 = i - 1;
			trc = xr[j1];
			tic = xi[j1];
			xr[j1] = xr[i1];
			xi[j1] = xi[i1];
			xr[i1] = trc;
			xi[i1] = tic;
		}
	}

	if (T<0.0)
	{
		for (j = 0; j w0&&j <= w0 + w1)
			{
				double aa = (0.54 + 0.46*cos(2 * 3.1415927*(j - w0) / w1));
				xx[j] *= (0.54 + 0.46*cos(2 * 3.1415927*(j - w0) / w1));  
				yy[j] = 0.0;
			}
			else
			if (j>w0 + w1)
				yy[j] = xx[j] = 0.0;
		}

		for (j = 1; j

 

你可能感兴趣的:(C++,算法学习和研究)