Advertisement
elandeholm

Butterworth filter

Oct 11th, 2023
1,139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.70 KB | Science | 0 0
  1. import numpy as np
  2. from cmath import sqrt, exp, pi
  3. import matplotlib.pyplot as plt
  4.  
  5. # s = Laplace Transform op
  6. # wc, cutoff (angular frequency)
  7. # n = number of poles
  8.  
  9. def H(s, wc, n, hp=False):
  10.     Hs = 1
  11.    
  12.     if hp:
  13.         s =  1j - 1j*s
  14.         wc = 1 - wc
  15.    
  16.     for k in range(1, n+1):
  17.         x =  1j * pi * (2 * k + n - 1) / (2 * n)
  18.        
  19.         Hs *= wc / (s - wc * exp(x))
  20.        
  21.     return Hs
  22.  
  23. def db_gain(w, wc, n):
  24.     return 20 * np.log(abs(H(w, wc, n, hp=False)))
  25.  
  26. max_frequency = 44100.0
  27.  
  28. omega_axis = np.linspace(0, 1, num=1024)
  29.  
  30. plt.xscale("log")
  31. plt.xlabel("Hz")
  32. plt.ylabel("dB")
  33.  
  34. plt.plot(max_frequency * omega_axis, db_gain(omega_axis, 0.125, 2))
  35.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement