In [4]:

```
# First order Butterworth low-pass filter
import math
#Variable declaration
R = 10.0*10**3 # Resistor R
R1 = 10.0*10**3 # Resistor R1
Rf = 100.0*10**3 # Resistor Rf
C = 0.001*10**-6 # Capacitance
#calculations
#(i)
fc = 1/(2*math.pi*R*C)
#(ii)
Af = 1+(Rf/R1)
#Result
print("(i) cut-off frequency for a first order Butterworth LPF is %.3f kHz\n\n(ii) Passband Voltage gain = %d"%(fc/1000,Af))
```

In [6]:

```
# first order low-pass filter design
import math
#Variable declaration
Af = 2 # Passband voltage gain
fc = 10*10**3 # cut-off frequency
#Calculations
#since Af = 1+Rf/R1, we have Rf = R1
Rf = 10*10**3 # assumed
C = 0.001*10**-6 # assumed
R = 1/(2*math.pi*fc*C)
#Result
print("Rf = R1 = %.0f k-ohm\nC = %.3f micro-F\nR = %.1f k-ohm"%(Rf/1000,C*10**6,R/1000))
```

In [18]:

```
# low-pass filter design
import math
#Variable declaration
Af = 2.5 # Passband voltage gain
fc = 2*10**3 # cut-off frequency
#Calculations
C = 0.01*10**-6 # assumed
R = 1/(2*math.pi*fc*C)
R = R/1000 # k-ohm
Rp = 8.2*10**3 # practical value
x = (Af-1) # Rf/R1
R1 = Rp*(x+1)/x
R1p = 15 # k-ohm
Rf = R1*x/1000 # k-ohm
#Result
print("R = %f k-ohm(practical value %f k-ohm)\nR1 = %.2f k-ohm(say %d k-ohm)\nRf = %d k-ohm"%(R,Rp/1000,R1/1000,R1p,Rf))
```

In [8]:

```
# 2nd order low-pass filter design(refer fig. 7.22)
import math
#Variable declaration
Af = 1.586 # Passband voltage gain
fc = 1000.0 # cut-off frequency
#Calculations
C = 0.005*10**-6 # assumed
R = 1/(2*math.pi*fc*C)
R = R/1000 # k-ohm
x = (Af-1) # Rf/R1
R1 = 33 # k-ohm
Rf = R1*x # k-ohm
#Result
print("R1 = %d k-ohm\nRf = %.3f k-ohm\nR2 = R3 = %.2f k-ohm\nC2 = C3 = %.3f micro-F"%(R1,Rf,R,C*10**6))
```

In [16]:

```
# 2nd order low-pass filter
import math
#Variable declaration
R1 = 12.0*10**3 # resistor R1
Rf = 7.0*10**3 # feedback resistance
R2 = 33.0*10**3 # Resistance R2
R3 = 33.0*10**3 # Resistance R3
C1 = 0.002*10**-6 # Capacitance C1
C2 = 0.002*10**-6 # Capacitance C2
#Calculations
#(i)
fc = 1/(2*math.pi*math.sqrt(R2*R3*C2*C1))
#(ii)
Af = 1+(Rf/R1)
#Result
print("(i) cut-off frequency(fc) = %.3f KHz\n(ii) Passband voltage gain(Af) = %.3f"%(fc/1000,Af))
```

In [31]:

```
# 2nd order low-pass filter design
import math
#Variable declaration
fc = 2000.0 # cut-off frequency
C = 0.033*10**-6 # Capacitance, assumed
#Calculations
R = 1/(2*math.pi*fc*C)
Rp = 2.5
x = 0.586 # Rf/R1
R1 = 2*R*(1+x)/x
R1p = 15.0
Rf = R1p*x
Rfp = 10.0
#Result
print("C2 = C3 = %.3f micro-F"%(C*10**6))
print("R3 = R2 = %.1f k-ohm(say %.1f k-ohm)"%(R/1000,Rp))
print("R1 = %d k-ohm = %d k-ohm (std value)\nRf = %.2f k-ohm = %d k-ohm (std value)"%(R1/1000,R1p,Rf,Rfp))
```

In [35]:

```
# 2nd order low-pass filter design
import math
#Variable declaration
fc = 1000.0 # cut-off frequency
C = 0.0047*10**-6 # Capacitance, assumed
#Calculations
R = 1/(2*math.pi*fc*C)
Rp = 33
x = 0.586 # Rf/R1
R1 = 30.0 # R1 in k-ohm
Rf = R1*x
Rfp = 20.0
#Result
print("C2 = C3 = %.4f micro-F"%(C*10**6))
print("R3 = R2 = %.2f k-ohm(say %.1f k-ohm)"%(R/1000,Rp))
print("R1 = %d k-ohm\nRf = %.2f k-ohm = %d k-ohm (std value)"%(R1,Rf,Rfp))
```

In [51]:

```
# low pass second order Butterworth filter design
import math
#Variable declaration
fc = 1500.0 # cut-off frequency
alfa = 1.414 # damping factor
#Calculations
Rf = 2-alfa
Ri = 1 # for equal component model
Af = 1+Rf/Ri
print("Fig 7.26(a) shows the equal component second order Butterworth filter with critical frequency wc = 1 rad/sec.")
print("Where R = R1 = R2 and\nC = C1 = C2")
wc = 2*math.pi*fc
wc = math.floor(wc*100)/100
print("\nWe scale this to 1.5 kHz\nTherefore, wc = %.2f rad/sec"%(wc))
R = 1/wc
print("R1 = R2 = R = %.3f *10^-4 ohm\nFig 7.26(b) shows 1.5 kHz second order Butterworth filter."%(R*10**4))
R = R*10**7
C = 1*10**-7
print("\nTo increase R to a reasonable vaule , we multiply R1 and R2 by 10^7")
print("and to keep the fc unchanged we decrease the C1 and C2 by the same factor.")
print("R1 = R2 = %.3f k-ohm\nC1 = C2 = %.1f micro-F or %.0f nF"%(R/1000,C*10**6,C*10**9))
```

In [53]:

```
print("Theeoretical example")
```

In [52]:

```
print("Theeoretical example")
```

In [73]:

```
# 2nd order Butterworth Low pass filter
import math
#Variable declaration
fc = 10*10**3 # Cut-off frequency
n = 2 # order of the filter
one_by_Q = 1.414 # from table
#Calculations
C = 0.01*10**-6 # assumed
R = 1/(2*math.pi*fc*C)
Af = 3-(one_by_Q)
Ri = 10*10**3 # assumed
Rf = (Af-1)*Ri
wc = 2*math.pi*fc
f1=1000
w1 = 2*math.pi*f1
f2 = 2000
w2 = 2*math.pi*f2
f3 = 5000
w3 = 2*math.pi*f3
f4 = 10000
w4 = 2*math.pi*f4
f5 = 50000
w5 = 2*math.pi*f5
f6 = 100000
w6 = 2*math.pi*f6
Hs1 = 20*math.log10(Af/(math.sqrt(1+(w1/wc)**4)))
Hs2 = 20*math.log10(Af/(math.sqrt(1+(w2/wc)**4)))
Hs3 = 20*math.log10(Af/(math.sqrt(1+(w3/wc)**4)))
Hs4 = 20*math.log10(Af/(math.sqrt(1+(w4/wc)**4)))
Hs5 = 20*math.log10(Af/(math.sqrt(1+(w5/wc)**4)))
Hs6 = 20*math.log10(Af/(math.sqrt(1+(w6/wc)**4)))
#Result
print("R1 = R2 = %.3f k-ohm\nAf = %.3f\nC1 = C2 = %.2f micro-F\nRi = %d k-ohm\nRf = %.2f k-ohm"%(R/1000,Af,C*10**6,Ri/1000,Rf/1000))
print("\nFrequency in Hz\t\t Gain Magnitude in dB|H(s)|")
print("%d\t\t\t\t%d"%(f1,Hs1))
print("%d\t\t\t\t%.3f"%(f2,Hs2))
print("%d\t\t\t\t%.2f"%(f3,Hs3))
print("%d\t\t\t\t%.0f"%(f4,math.ceil(Hs4)))
print("%d\t\t\t\t%.2f"%(f5,math.ceil(Hs5*100)/100))
print("%d\t\t\t\t%.2f"%(f6,Hs6))
```

In [85]:

```
# 4th order Butterworth low pass filter(refer fig. 7.33)
import math
#Variable declaration
fc = 1000.0 # upper cut-off frequency
C = 0.1*10**-6 # aasumed
R = 1/(2*math.pi*fc*C)
R = R/1000
R = math.floor(R*1000)/1000
alfa1 = 0.765
alfa2 = 1.848
Af1 = 3-alfa1
Af2 = 3- alfa2
# for first stage
Ri1 = 10*10**3 # assumed
Rf1 = (Af1-1)*Ri1
# for second stage
Ri2 = 100*10**3 # assumed
Rf2 = (Af2-1)*Ri2
#Result
print("R = %.3f k-ohm\nAf1 = %.3f\t Af2 = %.3f\n\nFor first stage:\nRi = %d k-ohm\tRf = %.1f k-ohm"%(R,Af1,Af2,Ri1/1000,Rf1/1000))
print("\nfor second stage:\nRi = %d k-ohm\tRf = %.1f k-ohm"%(Ri2/1000,Rf2/1000))
```

In [87]:

```
# Value of R in first order butterworth high pass filter
import math
#Vartianble declaration
fc = 10*10**3 # cut-off frequency
C = 0.0047*10**-6 # Capacitance
#Calculations
R = 1/(2*math.pi*fc*C)
#Result
print("R = %.3f k-ohm"%(R/1000))
```

In [94]:

```
# first order butterworth high pass filter
import math
#Variable declaration
R = 15*10**3 # resistance
C = 0.01*10**-6 # capacitance
Rf = 10*10**3 # feedback resistance
R1 = 5*10**3 # resistace R1
#Calcualtions
fc = 1/(2*math.pi*R*C)
fc = math.floor(fc)
wc = 2*math.pi*fc
#Result
print("(i) Cut-off frequency, fc = %d Hz\n(ii) Cut-off frequency, wc = %f k rad/sec"%(fc,wc/1000))
# Answer for wc is wrong in the book
```

In [101]:

```
# cut-off frequency and voltage gain(refer fig. 7.36)
import math
# Variable declaration
R1 = 27.0*10**3 # resistor R1
R2 = R1 # resistor R2
R3 = R1 # resistor R3
RL = 10*10**3 # resistor Rl
Rf = 16.0*10**3 # resistor Rf
C2 = 0.005*10**-6 # capacitance
C3 = 0.005*10**-6 # capacitance
#Calculations
fc = 1/(2*math.pi*math.sqrt(R2*R3*C2*C3))
Af = 1+Rf/R1
#Result
print("Cut-off frequency, fc = %.2f kHz\nAf = %.3f"%(fc/1000,math.floor(Af*1000)/1000))
```

In [125]:

```
# 2nd order high pass Bessel filter
import math
#Variable declaration
fb = 2000.0 # Break frequency
alfa = 1.732 # damping factor, from table
kf = 1.274 # frequency factor, from table
#calculations
R1 = alfa/2
R2 = 2/alfa
Rf = R2
C = 1
print("R1 = %.3f ohm\nR2 = Rf = %.3f ohm"%(R1,R2))
print("\n2nd order high pass filter with wc = 1 rad/sec is shown in fig 7.40")
fc = fb/kf
fc = math.floor(fc*100)/100
wc = 2*math.pi*fc
wc = math.ceil(wc*10)/10
print("\nfc = %.2f Hz\nwc = %.1f rad/sec"%(fc,wc))
print("\nIn frequency scaling proicedure, we keep the capacitor unchanged and scale down the resistors as follow:")
R1 = R1/wc
R1 = R1*10**5
R1 = math.floor(R1*10**4)/10**4
R2 = R2/wc
print("R1 = %.4f*10^-5\t\t\tR2 = %.4f*10^-4"%(R1,R2*10**4))
print("\nThe above mentioned components are not practical. So we multyply each resistor by 10^8 and divide each capacitor by 10^8 to")
print("get the final component values as under:")
R1 = R1*10**3
R2 = R2*10**8
C = 1*10**-8
print("R1 = %f k-ohm\t\t\tR2 = %f k-ohm\t\tC = %d nF\n\nFinal diagram is shown in fig. 7.42"%(R1/1000,R2/1000,C*10**9))
```

In [147]:

```
# Wide bandpass filter design
import math
#Variable declaration
fl = 100.0 # lower cut-off frequency
fh = 1000.0 # higher cut-off frequency
Af = 4
# Calculations
# 1. low-pass filter components
C_dash = 0.01*10**-6 # assumed
R_dash = 1/(2*math.pi*fh*C_dash)
R_dash = R_dash/1000
Af2 = 2
x1 = (Af2 - 1) # Rf_dash/R1_dash
R1_dash = 10 # k-ohm
Rf_dash = x1*R1_dash
# 2. High pass filter components
C = 0.05*10**-6
R = 1/(2*math.pi*fl*C)
Af1 = 2
x2 = (Af1-1)
R1 = 10 # k-ohm
Rf = x2*R1
# 3. Quality factor Q
Q = math.sqrt(fl*fh)/(fh-fl)
#Result
print("1.The componets for the low-pass filter are as under:")
print("R1_dash = %d k-ohm\nR_dash = %.1f k-ohm\nRf_dash = %d k-ohm\nC = %.2f micro-F"%(R1_dash,R_dash,Rf_dash,C_dash*10**6))
print("\n2.The componets for the high pass filter are as under:")
print("R1 = %d k-ohm\nR = %.2f k-ohm\nRf = %d k-ohm\nC = %.2f micro-F"%(R1,R/1000,Rf,C*10**6))
print("\n3.Quality factor\nQ = %.3f"%Q)
```

In [171]:

```
# Narrow band pass filter design
import math
#Variable declaration
fc = 2000.0 # cut-off frequency
Af = 10 # voltage gain
Q = 4 # quality factor
#Calculations
C = 0.01*10**-6 # assumed
R1 = Q/(2*math.pi*fc*C*Af)
R1 = R1/1000 # k-ohm
R2 = Q/(2*math.pi*fc*C*((2*Q**2)-Af))
R2 = R2/1000
R3 = Q/(math.pi*fc*C)
R3 = R3 /1000
fc_new = 1000.0
R2_dash = R2*(fc/fc_new)**2
#Result
print("C1 = C2 = %.2f micro-F\nR1 = %.3f k-ohm\nR2 = %.3f k-ohm\nR3 = %.2f k-ohm"%(C*10**6,R1,math.floor(R2*1000)/1000,R3))
print("We use\t R1 = 3.3 k-ohm,R2 = 1.5 k-ohm, R3 = 63 k-ohm")
print("\nNew value of R2 = %.3f k-ohm.\nWe use\t R2 = %.1f k-ohm"%(R2_dash,R2_dash))
```

In [191]:

```
# Wide band reject filter
import math
#Variable declaration
fh = 100.0 # higher cut-off frequency
fl = 2000.0 # lower cut-off frequency
# Calculations
# (i) High-pass section
C = 0.01*10**-6 # assumed
R = 1/(2*math.pi*fl*C)
Af1 = 2
x1 = Af1-1 # Rf/R1
R1 = 10 # k-ohm
Rf = x1*R1
#(ii) Low-pass section
C1 = 0.1*10**-6 # assumed
R_dash = 1/(2*math.pi*fh*C1)
Af2 = 2
x2 = Af2-1 # Rf'/R1'
R1_dash = 10 # k-ohm
Rf_dash = x2*R1_dash
#(iii) Summing amplifier
R2 = 10.0 # k-ohm
R3 = R4 = R2
Rom = (R2*R3/(R2+R3))*R4/(R4+(R2*R3/(R2+R3)))
#result
print("(i) High Pass section:\nC = %.2f micro-F\nR = %.2f k-ohm\nR1 = %d k-ohm\nRf = %d k-ohm\n"%(C*10**6,R/1000,R1,Rf))
print("(ii) Low Pass section:\nC = %.1f micro-F\nR = %.3f k-ohm\nR1 = %d k-ohm\nRf = %d k-ohm"%(C1*10**6,R_dash/1000,R1_dash,Rf_dash))
print("\n(iii) Summing amplifier:\nRom = %f k-ohm"%(Rom))
print("\nThe completed circuit diagram is shownn in figure 7.47")
```

In [193]:

```
# #Active notch filter
import math
#Variable declaration
fn = 50 # frequency to be rejected
#Calculations
C = 0.47*10**-6 # Capacitance, assumed
R = 1/(2*math.pi*fn*C)
#Result
print("C = %.2f micro-F\nR = %.3f k-ohm. We use %.1f k-ohm"%(C*10**6,R/1000,R/1000))
```

In [199]:

```
# phase shift between the input and output voltages for all pass filter
import math
#Variable declaration
R = 10*10**3 # resistance
C = 0.01*10**-6 # Capacitance
f = 2000.0 # frequency for which phase shift is to be calculated
#Calculations
fi = -2*math.atan(2*math.pi*R*f*C)
fi = math.ceil(math.degrees(fi)*100)/100
#Result
print("Phase shift = %.2f°"%fi)
```

In [203]:

```
# Wide band pass filter
import math
#Variable declaration
fl = 200.0 # lower cut-off frequency
fh = 1000.0 # higher cut-off frequency
#Calculations
fc = math.sqrt(fl*fh)
Q = fc/(fh-fl)
#Result
print("fc = %.1f Hz\nQ = %.3f\nfilter with Q = 0.8 is more selective."%(fc,Q))
```

In [216]:

```
# Wide bandpass filter design
import math
#Variable declaration
f1 = 5*10**3 # cut-off frequency
f2 = 15*10**3 # ciut-off frequency
Af = 2 # Passband gain
#Calculations
# low pass filter
C_dash = 0.01*10**-6 # assumed
R_dash = 1/(2*math.pi*f2*C_dash)
R_dash = R_dash/1000
Af1 = Af2 = 1.414
x1 = Af2-1 # Rf'/R1'
R1_dash = 10 # k-ohm
Rf_dash = x1*R1_dash
# high pass filter
C = 0.05*10**-6 # assumed
R = 1/(2*math.pi*f1*C)
x2 = Af1-1 # Rf'/R1'
R1 = 10 # k-ohm
Rf = x2*R1
#Result
print("Low pass filter component:")
print("C' = %.2f micro-F\nR' = %.3f k-ohm\nR1'= %d k-ohm\nRf'= %.2f k-ohm"%(C_dash*10**6,R_dash,R1_dash,Rf_dash))
print("\nHigh pass filter component:")
print("C = %.2f micro-F\nR = %.2f k-ohm\nR1 = %d k-ohm\nRf = %.2f k-ohm"%(C*10**6,R,R1,Rf))
```

In [274]:

```
# output waveform of circuit in fig.7.54
%matplotlib inline
import matplotlib.pyplot as plt
from numpy import arange,sin,pi
t = arange(0.0,2,0.01)
t1 = arange(0.0,2.0,0.01)
t2 = arange(0.0,1.002,0.01)
t3 = arange(1.0,2,0.01)
t4 = arange(-15.0,15.0,0.01)
S = sin(math.pi*t)
plt.axis([0,2.0,-16,16])
plt.plot(t1,t1*0/t1,'b')
plt.plot(t4/t4,t4,'g')
plt.plot(t2,15*t2/t2,'g')
plt.plot(t3,-15*t3/t3,'g')
plt.plot(t,S,'r')
plt.title("Output of the circuit shown in fig. 7.54")
plt.text(-0.3,14.8,'+Vsat')
plt.text(-0.3,-15.2,'-Vsat')
```

Out[274]:

In [275]:

```
# Cut-off frequency
import math
#Variable declaration
C = 0.1*10**-6 # capacitance
R = 10*10**3 # Resistance
#Calculations
fh = 1/(2*math.pi*C*R)
#Result
print("cut-off frequency = %.4f Hz"%fh)
```

In [284]:

```
# first order low pass filter design(refer fig 7.56)
import math
#Variable declaration
fc = 2000.0 # cut-off frequency
Af = 2 # passband gain
#Calculations
C = 0.01*10**-6 # Capacitance, assumed
R = 1/(2*math.pi*fc*C)
R = R/1000 # k-ohm
R = math.floor(R*10**3)/1000
x = Af -1 # Rf/R1
R1 = 10 # k-ohm
Rf = x*R1
#Result
print("C = %.2f micro-F\nR = %.3f k-ohm\nR1 = %d k-ohm\nRf = %d k-ohm"%(C*10**6,R,R1,Rf))
```