0% found this document useful (0 votes)
20 views7 pages

Code Linearphase Fir

This document defines functions for designing and analyzing finite impulse response (FIR) filters using different window functions. It designs FIR filters with different window functions and plots the magnitude and phase response of each filter.

Uploaded by

Mithilesh Mahure
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views7 pages

Code Linearphase Fir

This document defines functions for designing and analyzing finite impulse response (FIR) filters using different window functions. It designs FIR filters with different window functions and plots the magnitude and phase response of each filter.

Uploaded by

Mithilesh Mahure
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

4/23/24, 10:42 AM Untitled17

In [1]: import numpy as np


import matplotlib.pyplot as plt
from numpy import fft
pi=3.14

#Defining window fuction

def rectangular(n):
return 1

def hamming(n):
N=len(n)-1
return 0.54 + 0.46*np.cos((2*pi*n)/N)

def hanning(n):
N=len(n)-1
return 0.5 - 0.5*np.cos((2*pi*n)/N)

def blackmann(n):
N=len(n)
return 0.42 + 0.5*np.cos((2*pi*n)/N) + 0.08*np.cos((4*pi*n)/N)

In [2]: #Function for FIR


def FIR_filter(M,fc,fs,window,symm):

#Cutoff freq
wc=(2*pi*fc)/fs

#Alpha
if M%2==1:
alpha=(M-1)/2
else:
alpha=M/2-1

#Defining hn
n=np.arange(0,M)
if symm==True:
hn=(wc/pi)*np.sinc((n-alpha)*wc)
else:
hn=(wc/pi)*np.sinc((n-alpha)*wc)
for a in range(M//2+1,M):
hn[i]=-1*hn[i]

#Define window
if window=='rectangular':
wn=rectangular(n)
elif window=='hamming':
wn=hamming(n)
elif window=='hanning':
wn=hanning(n)
elif window=='blackmann':
wn=blackmann(n)

h1n=hn*wn
h1w=np.fft.fft(h1n)

return h1n,h1w

file:///C:/Users/Acer/Downloads/Untitled17.html 1/7
4/23/24, 10:42 AM Untitled17

In [3]: fsamp=400
fcut=100
M=61
N=M-1
n=[]
for i in range(M):
n.append((2*pi*i)/N-pi)
n=np.array(n)
outputf,outputw=FIR_filter(M,fcut,fsamp,'rectangular',True)

f1=plt.figure()

plt.title('Magnitude Response rectangular ')


plt.xlabel('Frequency [rad/sample]')
plt.ylabel('Magnitude [dB]')
plt.grid()
plt.plot(n,20*np.log10(np.abs(outputw)))

f2=plt.figure()
plt.title('Phase Response rectangular')
plt.xlabel('Frequency [rad/sample]')
plt.ylabel('Phase [radians]')
plt.grid()
plt.plot(n,np.angle(outputw))

Out[3]: [<matplotlib.lines.Line2D at 0x1dbe0668a70>]

file:///C:/Users/Acer/Downloads/Untitled17.html 2/7
4/23/24, 10:42 AM Untitled17

In [4]: outputf,outputw=FIR_filter(M,fcut,fsamp,'hamming',True)

f1=plt.figure()
plt.title('Magnitude Response hamming')
plt.xlabel('Frequency [rad/sample]')
plt.ylabel('Magnitude [dB]')
plt.grid()
plt.plot(n,20*np.log10(np.abs(outputw)))

f2=plt.figure()
plt.title('Phase Response hamming')
plt.xlabel('Frequency [rad/sample]')
plt.ylabel('Phase [radians]')
plt.grid()
plt.plot(n,np.angle(outputw))

Out[4]: [<matplotlib.lines.Line2D at 0x1dbe079b1d0>]

file:///C:/Users/Acer/Downloads/Untitled17.html 3/7
4/23/24, 10:42 AM Untitled17

In [5]: outputf,outputw=FIR_filter(M,fcut,fsamp,'hanning',True)

f1=plt.figure()
plt.title('Magnitude Response hanning')
plt.xlabel('Frequency [rad/sample]')

file:///C:/Users/Acer/Downloads/Untitled17.html 4/7
4/23/24, 10:42 AM Untitled17

plt.ylabel('Magnitude [dB]')
plt.grid()
plt.plot(n,20*np.log10(np.abs(outputw)))

f2=plt.figure()
plt.title('Phase Response hanning')
plt.xlabel('Frequency [rad/sample]')
plt.ylabel('Phase [radians]')
plt.grid()
plt.plot(n,np.angle(outputw))

Out[5]: [<matplotlib.lines.Line2D at 0x1dbe28bf6b0>]

file:///C:/Users/Acer/Downloads/Untitled17.html 5/7
4/23/24, 10:42 AM Untitled17

In [6]: outputf,outputw=FIR_filter(M,fcut,fsamp,'blackmann',True)

f1=plt.figure()
plt.title('Magnitude Response blackmann')
plt.xlabel('Frequency [rad/sample]')
plt.ylabel('Magnitude [dB]')
plt.grid()
plt.plot(n,20*np.log10(np.abs(outputw)))

f2=plt.figure()
plt.title('Phase Response blackmann')
plt.xlabel('Frequency [rad/sample]')
plt.ylabel('Phase [radians]')
plt.grid()
plt.plot(n,np.angle(outputw))

Out[6]: [<matplotlib.lines.Line2D at 0x1dbe3bfad80>]

file:///C:/Users/Acer/Downloads/Untitled17.html 6/7
4/23/24, 10:42 AM Untitled17

file:///C:/Users/Acer/Downloads/Untitled17.html 7/7

You might also like