Datenablage für Tinnitus Therapie Projektarbeit von Julian Seyffer und Heiko Ommert SS2020
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

DigitalFilterTest.py 2.3KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. import matplotlib.pyplot as plt # For plotting
  2. from math import sin, pi, cos # For generating input signals
  3. import numpy as np
  4. import sys # For reading command line arguments
  5. fs = 44100 # sampling frequency (Abtastfrequenz)
  6. koeff = {
  7. "b0": 0,
  8. "b1": 0,
  9. "b2": 0,
  10. "a1": 0,
  11. "a2": 0
  12. }
  13. def koeffizienten_berechnen(omega, r):
  14. # Koeffizientenberechnung nach Tobola VL S.107 - IIR Filter, 2. Ordnung
  15. koeff["b0"] = 1
  16. koeff["b1"] = 0
  17. koeff["b2"] = -1
  18. koeff["a1"] = -2*r*cos(omega)
  19. koeff["a2"] = r**2
  20. def filter(x):
  21. y = [0]*len(x)
  22. for k in range(4, len(x)):
  23. y[k] = koeff["b0"]*x[k] + koeff["b1"]*x[k-1] + koeff["b2"]*x[k-2] - koeff["a1"]*y[k-1] - koeff["a2"]*y[k-2]
  24. return y
  25. dauer_ms = 10000 # 10 Sekunden
  26. num_samples = dauer_ms * (fs / 1000) # framerate -pro Sekunde- umgerechnet in -pro Millisekunde-
  27. t = np.linspace(0, 10, int(num_samples)) # array zum darstellen der x-Achse
  28. amp = 1
  29. f_input1 = 1
  30. f_input2 = 50
  31. f_input3 = 10
  32. input1 = []
  33. input2 = []
  34. input3 = []
  35. for x in range(int(num_samples)): # einen einfachen Sinus ins array schreiben
  36. input1.append(amp * sin(2 * pi * f_input1 * (x / fs)))
  37. input2.append(amp * sin(2 * pi * f_input2 * (x / fs)))
  38. #input3.append(amp * sin(2 * pi * f_input3 * (x / fs)))
  39. input_ges = np.add(input1, input2) # Sinus aufaddieren um ein halbwegs realistisches Audiosignal zu bekommen
  40. #input_ges = np.add(input_ges, input3)
  41. #Filterparameter hier einstellen
  42. fr = 5
  43. omega = 2*pi*fr/fs
  44. r = 0.95
  45. koeffizienten_berechnen(omega, r) # Koeffizienten berechnen mit Mittelfrequenz 10Hz
  46. output = filter(input_ges)
  47. ### Plot the signals for comparison
  48. plt.figure(1)
  49. plt.subplot(231)
  50. plt.ylabel('Amplitude')
  51. plt.xlabel('t [s]')
  52. plt.title('f_1=' + str(f_input1) + "Hz")
  53. plt.plot(t, input1)
  54. plt.subplot(232)
  55. plt.ylabel('Amplitude')
  56. plt.xlabel('t [s]')
  57. plt.title('f_2=' + str(f_input2) + "Hz")
  58. plt.plot(t, input2)
  59. # plt.subplot(233)
  60. # plt.ylabel('Amplitude')
  61. # plt.xlabel('t [s]')
  62. # plt.title('f_3=' + str(f_input3) + "Hz")
  63. # plt.plot(t, input3)
  64. plt.subplot(234)
  65. plt.ylabel('Amplitude')
  66. plt.xlabel('t [s]')
  67. plt.title('(input in Filter) f_ges = f_1 + f_2')
  68. plt.plot(t, input_ges)
  69. plt.subplot(235)
  70. plt.ylabel('Amplitude')
  71. plt.xlabel('Samples')
  72. plt.title('gefiltertes Signal, mit f_r = ' + str(fr) + "Hz und r=" + str(r))
  73. plt.plot(t, output)
  74. plt.show()