Numerical Fourier Transform of a Rectangular Function

The purpose of this post is to correctly understand the numerical Fourier transform in Python or Matlab with an example in which the Analytical Fourier transform is well known. To do this, I select a rectangular function, its analytical expression and its Fourier transform are reported here https://en.wikipedia.org/wiki/Rectangular_function

Here is 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))

And here is 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))

: , , , , , : , , , .

-, , ?

enter image description here

enter image description here

+4
2

Matlab :

-, y = zeros(length(x)); y = zeros(1,length(x));. , .

-, DFT ( FFT) , y. y , 0. , y(200:400) = 1; y(1:100) = 1; y(end-98:end) = 1;. , , - , 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

enter image description here

+7

Python

import matplotlib.pyplot as plt
import numpy as np

x = np.arange(-3, 3, 0.01)
y = np.zeros(len(x))
y[200:400] = 1
yShift = np.fft.fftshift(y)
fftyShift = np.fft.fft(yShift)
ffty = np.fft.fftshift(fftyShift)

plt.plot(ffty)
plt.show()
0

Source: https://habr.com/ru/post/1661242/


All Articles