diff --git a/selfdrive/test/test_onroad.py b/selfdrive/test/test_onroad.py index bbb99d13b9b218..1e4669fb0a6b16 100644 --- a/selfdrive/test/test_onroad.py +++ b/selfdrive/test/test_onroad.py @@ -56,7 +56,7 @@ "system.logmessaged": 0.2, "system.tombstoned": 0, "logcatd": 0, - "system.micd": 6.0, + "system.micd": 5.0, "system.timed": 0, "selfdrive.pandad.pandad": 0, "system.statsd": 0.4, diff --git a/system/micd.py b/system/micd.py index 8b738ebe939bcf..af1aa3136063a7 100755 --- a/system/micd.py +++ b/system/micd.py @@ -1,5 +1,6 @@ #!/usr/bin/env python3 import numpy as np +from functools import cache from cereal import messaging from openpilot.common.realtime import Ratekeeper @@ -10,7 +11,16 @@ FFT_SAMPLES = 4096 REFERENCE_SPL = 2e-5 # newtons/m^2 SAMPLE_RATE = 44100 -SAMPLE_BUFFER = 4096 # (approx 100ms) +SAMPLE_BUFFER = 4096 # approx 100ms + + +@cache +def get_a_weighting_filter(): + # 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) def calculate_spl(measurements): @@ -27,16 +37,8 @@ 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)) - # Calculate the frequency axis for the signal - freqs = np.fft.fftfreq(measurements_windowed.size, d=1 / SAMPLE_RATE) - - # Calculate the A-weighting filter - # https://en.wikipedia.org/wiki/A-weighting - 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))) - A /= np.max(A) # Normalize the filter - # Apply the A-weighting filter to the signal - return np.abs(np.fft.ifft(np.fft.fft(measurements_windowed) * A)) + return np.abs(np.fft.ifft(np.fft.fft(measurements_windowed) * get_a_weighting_filter())) class Mic: