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

/*  file : dft.h
 *  description : 复数类、离散傅里叶变换类模板
 *  formula : X(k)= ∑n={0,N-1}x(n) e^-j2π/Nnk
 *
 *
 *  Auther : Hanc<741764646@qq.com>
 *  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

 

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

明故宫的记忆

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值