0% found this document useful (0 votes)
19 views

Exp 4 Python

Uploaded by

jappisardar1
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views

Exp 4 Python

Uploaded by

jappisardar1
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

9/30/22, 10:12 AM Exp4python

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]:

import matplotlib.pyplot as plt


import numpy as np
import scipy.fft
x1=([0,4,2,0])
dft=scipy.fft.fft(x1)
plt.subplot (2, 1, 1)
plt.stem(dft.real, use_line_collection = True)
plt.xlabel ('k')
plt.title(' Real part of DFT Abhay Sharma 19102086 A2')
plt.ylabel(' Real{x[k]}')
plt.show()
plt.subplot (2, 1, 2)
plt.stem (dft.imag, use_line_collection = True)
plt.xlabel('k')
plt.ylabel(' Img{X{k}}')
plt.title(' Imaginary Part of DFT Abhay Sharma 19102086 A2')
plt.show()
print (' DFT X[k] =' ,dft)

DFT X[k] = [ 6.-0.j -2.-4.j -2.-0.j -2.+4.j]

file:///C:/Users/Dell/Downloads/Exp4python.html 2/10
9/30/22, 10:12 AM Exp4python

In [5]:

import matplotlib. pyplot as plt


import numpy as np
import scipy.fft
Xk=[6, -2-4j, -2, -2+4j]
idft=scipy.fft.ifft(Xk)
plt.subplot (2, 1, 1)
plt.stem(idft.real, use_line_collection = True)
plt.ylabel(' Real{x[n]}')
plt.xlabel('n')
plt.title(' Real part of IDFT Abhay Sharma 19102086 A2')
plt.show()
plt.subplot(2, 1, 2)
plt.stem(idft.imag, use_line_collection = True)
plt.xlabel('n')
plt.ylabel(' Img{x[n]} ')
plt.title(' Imaginary Part of IDFT Abhay Sharma 19102086 A2')
plt.show()

file:///C:/Users/Dell/Downloads/Exp4python.html 3/10
9/30/22, 10:12 AM Exp4python

In [8]:

from scipy.fft import fft, fftfreq, fftshift


import matplotlib. pyplot as plt
import numpy as np
from numpy import pi
from scipy.fft import fft, fftfreq, fftshift
import matplotlib.pyplot as plt
import numpy as np
N=32
t = np.arange(N)
x=np.sin(t)
sp = fftshift(fft(x))
freq = fftshift(fftfreq(N) )
plt.figure(figsize=(4, 8) )
plt.subplot(211)
plt.grid()
plt.plot(t, x)
plt.xlabel('t')
plt.ylabel('sin(t) ')
plt.title('Abhay Sharma 19102086 A2')
plt.subplot(212)
plt.grid()
plt.plot (freq, abs(sp) )
plt.xlabel('k')
plt.ylabel(' Magnitude Spectrum {|x[k]|}')
plt.title('Abhay Sharma 19102086 A2')
plt.show()

file:///C:/Users/Dell/Downloads/Exp4python.html 4/10
9/30/22, 10:12 AM Exp4python

In [10]:

import matplotlib.pyplot as plt


import numpy as np
from scipy.fft import fft, fftfreq, fftshift
import scipy.fft
idft=scipy.fft. ifft (Xk)
Xk = [2, 3+2j,0, -1*(-3+2j)]
plt.subplot (2, 1, 1)
plt.xlabel('n')
plt.stem(idft.real, use_line_collection = True)
plt.title('Real part of IDFT Abhay Sharma 19102086 A2')
plt.ylabel(' Real{x[n]}')
plt.show()
plt.subplot (2, 1, 2)
plt.stem(idft. imag, use_line_collection = True)
plt.xlabel ( 'n ')
plt.ylabel(' Img{x[n]}')
plt.title(' Imaginary Part of IDFT Abhay Sharma 19102086 A2')
plt.show()
print('IDFT X[n] =',idft)

IDFT X[n] = [ 2. +0.j -0.5+0.j -1. +0.j 1.5+0.j]

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

X[n] DFT is: [ 3. -0.j -2.26921444+1.22976659j 0.55662386


-1.43900207j
1.02764633+0.32553206j -1.50357093+1.47309711j 0.68851519-2.80908543j
0.68851519+2.80908543j -1.50357093-1.47309711j 1.02764633-0.32553206j
0.55662386+1.43900207j -2.26921444-1.22976659j]

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)

X[n]= [ 3. -0.j -2.57384668+0.75574957j 1.54019223-0.98982


144j
-0.46846794+0.54064082j -0.12866295+0.28173256j 0.13078534-0.909632j
0.13078534+0.909632j -0.12866295-0.28173256j -0.46846794-0.54064082j
1.54019223+0.98982144j -2.57384668-0.75574957j]

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

X[n] = [ 2. -0.j -2.15338557-0.86776748j 2.57531162+1.5636


6296j
-3.1588336 -1.94985582j 3.74999697+1.94985582j -4.17918709-1.56366296j
4.29589694+0.86776748j -4. +0.j 3.26330183-0.86776748j
-2.13706334+1.56366296j 0.74396224-1.94985582j 0.74396224+1.94985582j
-2.13706334-1.56366296j 3.26330183+0.86776748j -4. -0.j
4.29589694-0.86776748j -4.17918709+1.56366296j 3.74999697-1.94985582j
-3.1588336 +1.94985582j 2.57531162-1.56366296j -2.15338557+0.86776748j]

file:///C:/Users/Dell/Downloads/Exp4python.html 10/10

You might also like