使用C++类模板实现离散傅里叶变换(DFT)

/*  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 
#include 
#include 

#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
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

 

你可能感兴趣的:(算法)