详细的频域滤波学习笔记(2)--单变量的离散傅里叶变换(DFT)

这里直接给出一维离散傅里叶变换的公式,如下:

在这里插入图片描述
由欧拉公式可以进一步变形,得到以下形式,方便编写程序
在这里插入图片描述
单变量的傅里叶变换变换相对简单,为了更好地理解其含义,这里给出一个例子,相信看完这个例子,就会明白整个计算过程到底是怎么回事。
在这里插入图片描述贴上实现代码

#include "stdafx.h"
#include<iostream>
#include<cmath>
#include<complex>
#include<ctime>

#define PI 3.14159
#define MAX 100
using namespace std;

void DFT1dim(complex<double>*Input, double Length, int flag)
{
 complex<double> *Output = new complex<double>(Length);//新建一个与输入相同长度的数据指针
 complex<double>wn;//旋转因子
 for (int i = 0; i < Length; i++)
 {
  Output[i] = 0;
  for (int j = 0; j < Length; j++)
  {
   wn = complex<double>(cos(2.0*PI*i*j / Length), sin(-flag * 2 * PI*i*j / Length));
   Output[i] += Input[i] * wn;
  }
  if (Output[i].imag()>0)
   cout <<i<<":" <<Output[i].real() << "+" << Output[i].imag() << "j" << endl;
  else
   cout <<i<<":"<< Output[i].real() << Output[i].imag() << "j" << endl;
 }
 if (flag == -1)
 {
  for (int i = 0; i < Length; i++)
  {
   Input[i] = Output[i] / Length;
  }
 }
 delete[] Output;
}

complex<double> *SetInput(double size)
{
 srand((double)time(0));
 complex<double>* Output=new complex<double>[size];
 
 for (int i = 0; i < size; i++)
 {
  Output[i] = complex<double>(rand()%MAX ,rand()%MAX );
 }
 return Output;
}

void Print( complex<double>*Output,double length)
{
 for (int i = 0; i < length; i++)
 {
  /*cout << Output[i] <<endl;*/
  if (Output[i].imag()>0)
   cout << Output[i].real() << "+" << Output[i].imag() <<"j" <<endl;
  else
   cout<< Output[i].real() << Output[i].imag() <<"j"<< endl;
 }
 
}

int main()
{
 complex<double>*Input = SetInput(4);
 cout << "时域内数据:" << endl;
 Print(Input,4);
 cout << "频域内数据:" << endl;
 DFT1dim(Input,4,1);
    return 0;
}

在这里插入图片描述
我这里采用的是随机生成的复数来进行傅里叶变换,如上面例子一样每个数据会对应输出一个在频域内的值,对这些值可以进行求幅值(模)以及相角。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值