Content and Objective

  • Show results of filter design by Fourier approximation and Remez
  • Filters of two different orders are designed and frequency responses are shown

Import


In [1]:
# importing
import numpy as np
from scipy import signal

import matplotlib.pyplot as plt
import matplotlib

# showing figures inline
%matplotlib inline

In [2]:
# plotting options 
font = {'size'   : 20}
plt.rc('font', **font)
plt.rc('text', usetex=True)

matplotlib.rc('figure', figsize=(18, 10) )

Filter Design by Fourier Approximation

parameters


In [3]:
# sampling time
t_s = 1.
f_s = 1. / t_s

N_fft = 2048
f = np.arange( -f_s/2, f_s/2, f_s/N_fft)    

# end of passband and filter length
f_g = f_s/3
K_1 = 21
K_2 = 105

design filters


In [4]:
# define ideal lowpass in the frequency regime
H_w = np.zeros(N_fft)
H_w[ np.where( np.abs(f)<f_g) ] = 1

# find impulse response by IFFT and restricting to K values
h_1_part = np.fft.ifft( H_w*np.exp(-1j*2*np.pi*f*(K_1-1)/2), N_fft)[:(K_1+1)//2]
h_1 = np.append( h_1_part, (h_1_part[::-1])[1:])
#h /= np.linalg.norm(h)

h_2_part = np.fft.ifft( H_w*np.exp(-1j*2*np.pi*f*(K_2-1)/2), N_fft)[:(K_2+1)//2]
h_2 = np.append( h_2_part, (h_2_part[::-1])[1:])#, h_2_part)
#h_2 /= np.linalg.norm(h_2)        


# find frequency responses
freq, H_1 = signal.freqz(h_1, worN=f*2*np.pi, whole=True) 
H_1 = np.fft.fftshift(H_1)

freq, H_2 = signal.freqz(h_2, worN=f*2*np.pi, whole=True)
H_2 = np.fft.fftshift(H_2)

plotting


In [5]:
# plotting
plt.figure(1)
plt.clf()

plt.subplot(131)
plt.plot( np.arange(len(h_1)), h_1, label='$K=$'+str(K_1))
plt.plot( np.arange(len(h_2)), h_2, label='$K=$'+str(K_2))    
plt.grid(True)
plt.legend(loc='upper right')
plt.xlabel('$n$')
plt.title('$h[n]$')

plt.subplot(132)
plt.plot( f, np.abs(H_1), label='$K=$'+str(K_1))
plt.plot( f, np.abs(H_2), label='$K=$'+str(K_2))    
plt.grid(True)   
plt.legend(loc='center right')    
plt.xlabel('$f/\mathrm{Hz}$')
plt.title('$|H(f)|$')    

plt.subplot(133)
plt.plot( f, 10*np.log10(np.abs(H_1)), label='$K=$'+str(K_1))
plt.plot( f, 10*np.log10(np.abs(H_2)), label='$K=$'+str(K_2))    
plt.grid(True)   
plt.legend(loc='center right')    
plt.xlabel('$f/\mathrm{Hz}$')
plt.title('$|H(f)| \\; (dB)$')


/home/jaekel/.anaconda3/lib/python3.7/site-packages/numpy/core/_asarray.py:85: ComplexWarning: Casting complex values to real discards the imaginary part
  return array(a, dtype, copy=False, order=order)
/home/jaekel/.anaconda3/lib/python3.7/site-packages/numpy/core/_asarray.py:85: ComplexWarning: Casting complex values to real discards the imaginary part
  return array(a, dtype, copy=False, order=order)
Out[5]:
Text(0.5, 1.0, '$|H(f)| \\; (dB)$')

Filter Design by Remez

parameters


In [6]:
# sampling time
t_s = 1.
f_s = 1. / t_s

N_fft = 2048
f = np.arange( -f_s/2, f_s/2, f_s/N_fft)    

# end of passband and filter length
f_g = f_s/3
K_1 = 11
K_2 = 51

design filters


In [7]:
# define ideal lowpass in the frequency regime
H_w = np.zeros(N_fft)
H_w[ np.where( np.abs(f)<f_g) ] = 1

# find impulse response 
h = signal.remez( K_1, [0, f_g, 1.2*f_g, .5], [1,0])
h_2 = signal.remez(K_2, [0, f_g, 1.2*f_g, .5], [1,0])    

# find frequency responses
freq, H = signal.freqz(h, worN=f*2*np.pi, whole=True) 
freq, H_2 = signal.freqz(h_2, worN=f*2*np.pi, whole=True)

plotting


In [8]:
# plotting
plt.figure(1)
plt.clf()

plt.subplot(131)
plt.plot( np.arange(len(h)), h, label='$K=$'+str(K_1))
plt.plot( np.arange(len(h_2)), h_2, label='$K=$'+str(K_2))    
plt.grid(True)
plt.legend(loc='upper right')
plt.xlabel('$n$')
plt.title('$h[n]$')

plt.subplot(132)
plt.plot( f, np.abs(H), label='$K=$'+str(K_1))
plt.plot( f, np.abs(H_2), label='$K=$'+str(K_2))    
plt.grid(True)   
plt.legend(loc='center right')    
plt.xlabel('$f/\mathrm{Hz}$')
plt.title('$|H(f)|$')    

plt.subplot(133)
plt.plot( f, 10*np.log10(np.abs(H)), label='$K=$'+str(K_1))
plt.plot( f, 10*np.log10(np.abs(H_2)), label='$K=$'+str(K_2))    
plt.grid(True)   
plt.legend(loc='center right')    
plt.xlabel('$f/\mathrm{Hz}$')
plt.title('$|H(f)| \\; (dB)$')


Out[8]:
Text(0.5, 1.0, '$|H(f)| \\; (dB)$')

In [ ]: