Exp 4 Python
Exp 4 Python
EXPERIMENT - 4
Aim - Writing codes to compute DFT (Discrete Fourier Transform) and IDFT (Inverse Discrete Fourier
Transform) for the spectral analysis of signals.
Theory - In mathematics, the discrete Fourier transform (DFT) converts a finite sequence of equally-spaced
samples of a function into a same-length sequence of equally-spaced samples of the discrete-time Fourier
transform (DTFT), which is a complex-valued function of frequency. The DFT is therefore said to be a
frequency domain representation of the original input sequence. If the original sequence spans all the non-
zero values of a function, its DTFT is continuous (and periodic), and the DFT provides discrete samples of
one cycle. If the original sequence is one cycle of a periodic function, the DFT provides all the non-zero
values of one DTFT cycle.
The DFT is the most important discrete transform, used to perform Fourier analysis in many practical
applications. In digital signal processing, the function is any quantity or signal that varies over time, such as
the pressure of a sound wave, a radio signal, or daily temperature readings, sampled over a finite time
interval. In image processing, the samples can be the values of pixels along a row or column of a raster
image. The DFT is also used to efficiently solve partial differential equations, and to perform other operations
such as convolutions or multiplying large integers.
file:///C:/Users/Dell/Downloads/Exp4python.html 1/10
9/30/22, 10:12 AM Exp4python
In [4]:
file:///C:/Users/Dell/Downloads/Exp4python.html 2/10
9/30/22, 10:12 AM Exp4python
In [5]:
file:///C:/Users/Dell/Downloads/Exp4python.html 3/10
9/30/22, 10:12 AM Exp4python
In [8]:
file:///C:/Users/Dell/Downloads/Exp4python.html 4/10
9/30/22, 10:12 AM Exp4python
In [10]:
file:///C:/Users/Dell/Downloads/Exp4python.html 5/10
9/30/22, 10:12 AM Exp4python
In [16]:
import numpy as np
import matplotlib. pyplot as plt
import scipy.fft
n=np.linspace(-5,5, 11)
#assigning values to delta function
del1=1* (n==(0) )
del2=1* (n==(2))
del3=1* (n==(6) )
X = del1 + (2*del2) + (3*del3 )
dft=scipy. fft.fft (X) #Computing DFT
plt.figure (figsize=(7, 7))
plt.subplot (211 )
plt.stem(dft. real, use_line_collection=True) #plotting real
plt.xlabel('n -- -> ')
plt.ylabel('X[n] ---> ')
plt.title('X[n] DFT(real) Abhay Sharma 19102086 A2')
plt. figure(figsize= (7, 7) )
plt.subplot (212)
plt.stem(dft.imag, use_line_collection=True) #plotting imaginary
plt.xlabel('n --->')
plt.ylabel('X[n] --->')
plt.title('X [n] DFT (imag) Abhay Sharma 19102086 A2')
plt.show()
print('X[n] DFT is: ', dft)
file:///C:/Users/Dell/Downloads/Exp4python.html 6/10
9/30/22, 10:12 AM Exp4python
file:///C:/Users/Dell/Downloads/Exp4python.html 7/10
9/30/22, 10:12 AM Exp4python
In [22]:
import numpy as np
import scipy.fft
import matplotlib. pyplot as plt
n=np.linspace(-5, 5, 11)
u=1*(n>=0)
u_3=1*(n>=3)
X = u-u_3
dft=scipy.fft.fft(X)
plt.figure (figsize=(5,5) )
plt.subplot (211)
plt.stem(dft.real, use_line_collection=True )
plt.xlabel('n---> ')
plt.ylabel('X[n]---> ')
plt.title('X[n] DFT(Real) Abhay Sharma 19102086 A2')
plt.figure (figsize=(5,5))
plt.subplot (212 )
plt.stem(dft.imag, use_line_collection=True)
plt.xlabel ('n-- ->')
plt.ylabel('X[n]')
plt.title('X[n] DFT(Imaginary) Abhay Sharma 19102086 A2')
plt.show()
print('X[n]=',dft)
file:///C:/Users/Dell/Downloads/Exp4python.html 8/10
9/30/22, 10:12 AM Exp4python
In [26]:
import numpy as np
import scipy.fft
import matplotlib. pyplot as plt
n=np.linspace(-10, 10, 21)
u1=1*(n>=0)
u2=1* (n>=2)
u3=1*(n>=3)
X = (2*u1) - (4*u2) + (2*u3)
dft = scipy.fft.fft(X)
plt.figure (figsize= (7, 7))
plt.subplot (211)
plt.xlabel('n--->')
plt.stem (dft.real, use_line_collection=True)
plt.ylabel('X[n]')
plt.title('X[n] DFT(real) Abhay Sharma 19102086 A2')
plt.figure(figsize=(7,7))
plt.subplot(212)
plt.stem (dft.imag, use_line_collection=True)
plt.ylabel('X[n]')
plt.xlabel('n--->')
plt.title('X[n] DFT(imag) Abhay Sharma 19102086 A2')
plt.show()
print('X[n] =',dft)
file:///C:/Users/Dell/Downloads/Exp4python.html 9/10
9/30/22, 10:12 AM Exp4python
file:///C:/Users/Dell/Downloads/Exp4python.html 10/10