矩形函数的数值傅立叶变换 [英] Numerical Fourier Transform of rectangular function

查看:1166
本文介绍了矩形函数的数值傅立叶变换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这篇文章的目的是通过一个众所周知的解析傅立叶变换的例子来正确理解Python或Matlab上的数值傅立叶变换.为此,我选择了矩形函数,在此报告了它的解析表达式及其傅里叶变换. https://en.wikipedia.org/wiki/Rectangular_function

The aim of this post is to properly understand Numerical Fourier Transform on Python or Matlab with an example in which the Analytical Fourier Transform is well known. For this purpose I choose the rectangular function, the analytical expression of it and its Fourier Transform are reported here https://en.wikipedia.org/wiki/Rectangular_function

这里是Matlab中的代码

Here the code in Matlab

x = -3 : 0.01 : 3;
y = zeros(length(x));
y(200:400) = 1;
ffty = fft(y);
ffty = fftshift(ffty);
plot(real(ffty))

这里是Python中的代码

And here the code in Python

import numpy as np
import matplotlib.pyplot as plt
x = np.arange(-3, 3, 0.01)
y = np.zeros(len(x))
y[200:400] = 1
ffty = np.fft.fft(y)
ffty = np.fft.fftshift(ffty)
plt.plot(np.real(ffty))

在两种编程语言中,我都有一些问题的结果: 首先,傅立叶变换不像预期的那样真实,此外,即使选择实部,该解决方案也不像解析解决方案:实际上,这里报告的第一个图应至少应为形状,而第二个图则应为我从计算中得到的结果.

In both the two programming langueages I have the some result with the some problems: First of all the fourier transfrom is not real as expected, moreover even choosing the real part, the solution does not looks like the analytical solution: in fact the first plot reported here is as it should be at least in shape and the second plot is what I get from my calculations.

有没有人可以建议我如何解析计算矩形函数的傅立叶变换?

Is there anyone who could suggest me how to analytically calculate the fourier transform of the rectangular function?

推荐答案

您的Matlab代码中有两个问题:

There are two problems in your Matlab code:

首先,y = zeros(length(x));应该为y = zeros(1,length(x));.当前,您创建的是正方形矩阵,而不是向量.

First, y = zeros(length(x)); should be y = zeros(1,length(x));. Currently you create a square matrix, not a vector.

第二,如果y是,则DFT(或FFT)将是实数和对称的.您的y应该是对称的,这意味着相对于0.因此,使用y(1:100) = 1; y(end-98:end) = 1;代替y(200:400) = 1;.回想一下DFT就像一个信号的傅立叶级数,从该信号中您的输入仅是一个周期,而第一个采样对应于时刻0.

Second, the DFT (or FFT) will be real and symmetric if y is. Your y should be symmetric, and that means with respect to 0. So, instead of y(200:400) = 1; use y(1:100) = 1; y(end-98:end) = 1;. Recall that the DFT is like the Fourier series of a signal from which your input is just one period, and the first sample corresponds to time instant 0.

所以:

x = -3 : 0.01 : 3;
y = zeros(1,length(x));
y(1:100) = 1; y(end-98:end) = 1;
ffty = fft(y);
ffty = fftshift(ffty);
plot(ffty)

给予

>> isreal(ffty)
ans =
     1

这篇关于矩形函数的数值傅立叶变换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆