aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorClyne <clyne@bitgloo.com>2024-07-23 12:21:52 -0400
committerClyne <clyne@bitgloo.com>2024-07-23 12:21:52 -0400
commit318497398bf78242b2dda309b5b70a153a75f8f7 (patch)
tree51b547da799978ee786529764224a08dbea37c03
parent91869542b5fac22b2467045ef394370fb8402765 (diff)
Add 'aweight.py'
-rw-r--r--aweight.py48
1 files changed, 48 insertions, 0 deletions
diff --git a/aweight.py b/aweight.py
new file mode 100644
index 0000000..3c06bcd
--- /dev/null
+++ b/aweight.py
@@ -0,0 +1,48 @@
+from scipy import signal
+from waveform_analysis import A_weighting
+from math import pi
+import numpy
+import matplotlib.pyplot as plt
+
+
+# Create our X axis values, the frequencies to test at:
+gs = 2 * pi * numpy.geomspace(10, 24000, 1000)
+
+
+# This is the ideal A-weighting filter:
+z, p, k = A_weighting(48000, output='zpk')
+_, hA = signal.freqz_zpk(z, p, k, gs, fs=48000)
+
+
+# And here is our three-stage implementation:
+z, p, k = signal.iirfilter(1, [640, 10000], btype='bandpass', analog=False, fs=48000, output='zpk')
+_, hB = signal.freqz_zpk(z, p, k * 1.17, gs, fs=48000)
+
+z, p, k = signal.iirfilter(1, 12000, btype='lowpass', analog=False, fs=48000, output='zpk')
+_, hL = signal.freqz_zpk(z, p, k, gs, fs=48000)
+
+z, p, k = signal.iirfilter(1, 120, btype='highpass', analog=False, fs=48000, output='zpk')
+_, hH = signal.freqz_zpk(z, p, k, gs, fs=48000)
+
+
+# Lastly, our SOS approximation:
+sos = [[ 0.5, 0, -0.5, 1, -1.062, 0.14],
+ [ 0.5, 0.5, 0, 1, 0, 0 ],
+ [ 1.0, -1.0, 0, 1, -0.985, 0 ]]
+wS, hS = signal.sosfreqz(sos, fs=48000)
+
+
+# Plot everything:
+plt.semilogx(gs, 20 * numpy.log10(hA), label='A')
+plt.semilogx(gs, 20 * numpy.log10(hB * hL * hH), label='bp+lp+hp')
+plt.semilogx(wS, 20 * numpy.log10(hS), label='sos')
+
+plt.title('Frequency response')
+plt.xlabel('Frequency [Hz]')
+plt.ylabel('Amplitude [dB]')
+plt.ylim(-30, 10)
+plt.xlim(20, 24000)
+plt.grid(True, color='0.7', linestyle='-', which='major', axis='both')
+plt.grid(True, color='0.9', linestyle='-', which='minor', axis='both')
+plt.legend()
+plt.show()