diff --git a/system/micd.py b/system/micd.py index e3359e9fb0b2ea..8b61d2e9c5d76a 100755 --- a/system/micd.py +++ b/system/micd.py @@ -13,6 +13,8 @@ SAMPLE_BUFFER = 4096 # (approx 100ms) def precompute_a_weighting(): + # Calculate the A-weighting filter + # https://en.wikipedia.org/wiki/A-weighting freqs = np.fft.fftfreq(FFT_SAMPLES, d=1 / SAMPLE_RATE) A = 12194 ** 2 * freqs ** 4 / ((freqs ** 2 + 20.6 ** 2) * (freqs ** 2 + 12194 ** 2) * np.sqrt((freqs ** 2 + 107.7 ** 2) * (freqs ** 2 + 737.9 ** 2))) return A / np.max(A) @@ -33,6 +35,8 @@ def calculate_spl(measurements): def apply_a_weighting(measurements: np.ndarray) -> np.ndarray: # Generate a Hanning window of the same length as the audio measurements measurements_windowed = measurements * np.hanning(len(measurements)) + + # Apply the A-weighting filter to the signal weighted = np.abs(np.fft.ifft(np.fft.fft(measurements_windowed) * A_WEIGHTING)) return weighted