/* file : dft.h
* description : 复数类、离散傅里叶变换类模板
* formula : X(k)= ∑n={0,N-1}x(n) e^-j2π/Nnk
*
*
* Auther : Hanc<[email protected]>
* Create Time : 2018.8.14
*
*/
#ifndef DFT_H
#define DFT_H
#include <iostream>
#include <memory.h>
#include <cmath>
#define PI 3.1415926535
namespace HCLib
{
//复数类
class complex
{
private:
double real; //实部
double imaginary; //虚部
public:
complex()
{
}
complex(double a,double b)
{
real = a;
imaginary = b;
}
void SetReal(double v)
{
real = v;
}
void SetImag(double v)
{
imaginary = v;
}
double GetReal()
{
return real;
}
double GetImag()
{
return imaginary;
}
//复数求模
double CalModular()
{
return sqrt((real*real) + (imaginary*imaginary));
}
//操作符重载:两复数对象相加
complex operator + (const complex& parm)
{
complex ret;
ret.real = real + parm.real;
ret.imaginary = imaginary + parm.imaginary;
return ret;
}
//操作符重载:两复数对象相减
complex operator - (const complex& parm)
{
complex ret;
ret.real = real - parm.real;
ret.imaginary = imaginary - parm.imaginary;
return ret;
}
//操作符重载:两复数对象相乘
complex operator * (const complex& parm)
{
complex ret;
ret.real = real * parm.real - imaginary * parm.imaginary;
ret.imaginary = real * parm.imaginary + imaginary * parm.real;
return ret;
}
//操作符重载:两复数对象 +=
complex& operator += (const complex& parm)
{
this->real += parm.real;
this->imaginary += parm.imaginary;
return *this;
}
};
template<typename T,int N>
class DFT
{
protected:
T m_array[N]; //原始数字信号
complex m_complex[N]; //处理后的数字信号复数
T m_value[N]; //处理后的数字信号模值
public:
DFT()
{
memset(m_array,0,sizeof(m_array));
memset(m_value,0,sizeof(m_value));
memset(m_complex,0,sizeof(m_complex));
}
double GetReal(int i)
{
return m_complex[i].GetReal();
}
double GetImag(int i)
{
return m_complex[i].GetImag();
}
T GetMValue(int i)
{
return m_value[i];
}
void Insert_SrcData(T* src,int n)//拷贝数字信号至m_array
{
for(int i=0;i<n;i++)
m_array[i] = src[i];
}
void Dft_Cal_Point(int p) //计算频域上一个点的DFT的实部、虚部、模值
{
complex thispoint;
complex part[N];
memset(&thispoint,0,sizeof(thispoint));
for(int i=0; i<N; i++)
{
part[i].SetReal(cos(2*PI/N*p*i)*m_array[i]);
part[i].SetImag(sin(2*PI/N*p*i)*m_array[i]);
thispoint += part[i];
}
m_complex[p] = thispoint;
m_value[p] = m_complex[p].CalModular();
}
void Dft_Cal() //计算所有DFT
{
for(int i=0;i<N;i++)
{
Dft_Cal_Point(i);
}
}
};
}
#endif // DFT_H
使用C++类模板实现离散傅里叶变换(DFT)
猜你喜欢
转载自blog.csdn.net/weixin_42314225/article/details/82010892
今日推荐
周排行