Advertisement
farry

low-pass-filtering.py

Nov 18th, 2017
333
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.12 KB | None | 0 0
  1. #! /usr/bin/env python3
  2.  
  3. # Low-pass:
  4. # voltage divider:  gain = Xc/(Xc+R) = 1/(1+R/Xc) = 1/(1+jωCR)
  5. # At 3db, freq ωₒ: R = |Xc| = 1/ωₒC  thus  CR = 1/ωₒ
  6. # gain = 1/(1+jω/ωₒ) = 1/(1+jf/fₒ)
  7.  
  8. import numpy
  9. import matplotlib.pyplot as plt
  10.  
  11. # define input waveform as 010100 trapezoid
  12. lo = numpy.array( [0] * 200 )
  13. hi = numpy.array(list(range(10)) + [10] * 200 + list(range(9,-1,-1))) / 10.0
  14. wave = numpy.concatenate( [lo,hi,lo,hi,lo,lo] )
  15.  
  16. # Func for low-pass RC filter of waveform
  17. def low_pass( waveform, freq, f3db, hue, offset ):
  18.   fftwaveform = numpy.fft.rfft(waveform)
  19.   harmonics = numpy.arange(len(fftwaveform))
  20.   gains = 1.0 / (1.0 + 1j * (harmonics * freq) / f3db)
  21.   fftfiltered = fftwaveform * gains
  22.   filtered = numpy.fft.irfft(fftfiltered)
  23.   plt.plot(filtered * 0.8 + offset, color=hue, lw=2.0)
  24.  
  25. # input_waveform, base_freq, RC_filter_freq, screen_colour, screen_offset
  26. low_pass(wave, 1, 1e10, "black", 4)
  27. low_pass( wave, 1, 20, "green", 3)
  28. low_pass( wave, 1, 6, "blue", 2)
  29. low_pass( wave, 1, 3, "red", 1)
  30. low_pass( wave, 1, 1.2, "orange", 0)
  31.  
  32. plt.savefig("low-pass-filtering.png")
  33. plt.show()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement