From c88714c7f3ed338c0f5d9fe0525f3b42966ca787 Mon Sep 17 00:00:00 2001 From: Shane Smiskol Date: Thu, 1 Dec 2022 12:52:36 -0800 Subject: [PATCH 01/15] record --- system/micd.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/system/micd.py b/system/micd.py index d2a5a2849f2b3a..d2d0b1b7d709dc 100755 --- a/system/micd.py +++ b/system/micd.py @@ -19,11 +19,14 @@ def __init__(self, pm): self.measurements = np.empty(0) self.spl_filter = FirstOrderFilter(0, 4, DT_MIC, initialized=False) + self.all_measurements = np.empty(0) def update(self): # self.measurements contains amplitudes from -1 to 1 which we use to # calculate an uncalibrated sound pressure level if len(self.measurements) > 0: + self.all_measurements = np.concatenate((self.measurements, self.measurements)) + np.save('/data/test_recording', self.all_measurements) # https://www.engineeringtoolbox.com/sound-pressure-d_711.html sound_pressure = np.sqrt(np.mean(self.measurements ** 2)) # RMS of amplitudes sound_pressure_level = 20 * np.log10(sound_pressure / REFERENCE_SPL) if sound_pressure > 0 else 0 # dB From c71a1b4f9a72fcfac71c9ad0fe974014d228f417 Mon Sep 17 00:00:00 2001 From: Shane Smiskol Date: Thu, 1 Dec 2022 12:54:43 -0800 Subject: [PATCH 02/15] record --- system/micd.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/system/micd.py b/system/micd.py index d2d0b1b7d709dc..6de94ae3e9222f 100755 --- a/system/micd.py +++ b/system/micd.py @@ -25,7 +25,7 @@ def update(self): # self.measurements contains amplitudes from -1 to 1 which we use to # calculate an uncalibrated sound pressure level if len(self.measurements) > 0: - self.all_measurements = np.concatenate((self.measurements, self.measurements)) + self.all_measurements = np.concatenate((self.all_measurements, self.measurements)) np.save('/data/test_recording', self.all_measurements) # https://www.engineeringtoolbox.com/sound-pressure-d_711.html sound_pressure = np.sqrt(np.mean(self.measurements ** 2)) # RMS of amplitudes From 1ddddddab369b275fd830c4b64342397a3ca14d2 Mon Sep 17 00:00:00 2001 From: Shane Smiskol Date: Thu, 1 Dec 2022 15:47:55 -0800 Subject: [PATCH 03/15] draft --- system/micd.py | 65 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) diff --git a/system/micd.py b/system/micd.py index 6de94ae3e9222f..6bfdb1916d535d 100755 --- a/system/micd.py +++ b/system/micd.py @@ -12,6 +12,71 @@ REFERENCE_SPL = 2 * 10 ** -5 # newtons/m^2 + +def calc_loudness(measurements): + # https://www.engineeringtoolbox.com/sound-pressure-d_711.html + sound_pressure = np.sqrt(np.mean(measurements ** 2)) # RMS of amplitudes + sound_pressure_level = 20 * np.log10(sound_pressure / REFERENCE_SPL) if sound_pressure > 0 else 0 # dB + return sound_pressure, sound_pressure_level + + +def a_weight(signal: np.ndarray) -> np.ndarray: + # Calculate the frequency axis for the signal + freqs = np.fft.fftfreq(signal.size, d=1 / 44100) + + # Calculate the A-weighting filter + A = 3.5041384e16 * freqs ** 4 / ((freqs ** 2 + 20.598997 ** 2) * (freqs ** 2 + 12194 ** 2) * np.sqrt((freqs ** 2 + 107.65265 ** 2) * (freqs ** 2 + 737.86223 ** 2))) + # 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.fft.ifft(np.fft.fft(signal) * A) + +def apply_a_weighting(signal: np.ndarray) -> np.ndarray: + # Compute the A-weighting filter + f = np.array([20.598997, 107.65265, 737.86223, 12194.217]) + A1000 = 1.9997 + num = (2 * np.pi * f) ** 2 * A1000 + den = np.array([1, 2 * np.pi * f, (2 * np.pi * f)**2]) + H = num / den + + # Filter the signal with the A-weighting filter + return np.convolve(signal, H, mode="same") + + +def extract_frequencies(audio_data, sample_rate): + # # Generate a Hanning window of the same length as the audio data + # hanning_window = np.hanning(len(audio_data)) + # + # # Multiply the audio data by the Hanning window to apply the window + # audio_data_windowed = audio_data * hanning_window + + # Perform the FFT on the windowed audio data + fft_result = np.fft.fft(audio_data) + + # Get the absolute value of the complex FFT result + fft_magnitudes = np.abs(fft_result) + + # Calculate the frequencies corresponding to the FFT result + fft_frequencies = np.fft.fftfreq(len(audio_data), d=1/sample_rate) + + # Return the frequencies and their corresponding magnitudes + return (fft_frequencies, fft_magnitudes) + +def inverse_dft(frequencies, magnitudes, sample_rate=44100): + # Compute the length of the audio signal + signal_length = len(frequencies) + + # Create an array of complex numbers representing the spectrum of the signal + spectrum = magnitudes * np.exp(1j * 2 * np.pi * frequencies / sample_rate) + + # Compute the inverse Fourier transform of the spectrum + amplitudes = np.fft.ifft(spectrum) + + # Return the time-domain amplitudes + return amplitudes + + class Mic: def __init__(self, pm): self.pm = pm From 1c5b41b34449d68714543e105fa539736eac2430 Mon Sep 17 00:00:00 2001 From: Shane Smiskol Date: Thu, 1 Dec 2022 16:08:40 -0800 Subject: [PATCH 04/15] some clean up --- system/micd.py | 85 +++++++++++++++----------------------------------- 1 file changed, 26 insertions(+), 59 deletions(-) diff --git a/system/micd.py b/system/micd.py index 6bfdb1916d535d..5ee28903d3b98a 100755 --- a/system/micd.py +++ b/system/micd.py @@ -10,71 +10,31 @@ RATE = 10 DT_MIC = 1. / RATE REFERENCE_SPL = 2 * 10 ** -5 # newtons/m^2 +SAMPLE_RATE = 44100 - -def calc_loudness(measurements): +def calculate_spl(measurements): # https://www.engineeringtoolbox.com/sound-pressure-d_711.html sound_pressure = np.sqrt(np.mean(measurements ** 2)) # RMS of amplitudes sound_pressure_level = 20 * np.log10(sound_pressure / REFERENCE_SPL) if sound_pressure > 0 else 0 # dB return sound_pressure, sound_pressure_level -def a_weight(signal: np.ndarray) -> np.ndarray: +def apply_a_weighting(measurements: np.ndarray) -> np.ndarray: + # Generate a Hanning window of the same length as the audio measurements + hanning_window = np.hanning(len(measurements)) + measurements_windowed = measurements * hanning_window + # Calculate the frequency axis for the signal - freqs = np.fft.fftfreq(signal.size, d=1 / 44100) + freqs = np.fft.fftfreq(measurements_windowed.size, d=1 / SAMPLE_RATE) # Calculate the A-weighting filter + # TODO: create global for 3.5041384e16 A = 3.5041384e16 * freqs ** 4 / ((freqs ** 2 + 20.598997 ** 2) * (freqs ** 2 + 12194 ** 2) * np.sqrt((freqs ** 2 + 107.65265 ** 2) * (freqs ** 2 + 737.86223 ** 2))) - # 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.fft.ifft(np.fft.fft(signal) * A) - -def apply_a_weighting(signal: np.ndarray) -> np.ndarray: - # Compute the A-weighting filter - f = np.array([20.598997, 107.65265, 737.86223, 12194.217]) - A1000 = 1.9997 - num = (2 * np.pi * f) ** 2 * A1000 - den = np.array([1, 2 * np.pi * f, (2 * np.pi * f)**2]) - H = num / den - - # Filter the signal with the A-weighting filter - return np.convolve(signal, H, mode="same") - - -def extract_frequencies(audio_data, sample_rate): - # # Generate a Hanning window of the same length as the audio data - # hanning_window = np.hanning(len(audio_data)) - # - # # Multiply the audio data by the Hanning window to apply the window - # audio_data_windowed = audio_data * hanning_window - - # Perform the FFT on the windowed audio data - fft_result = np.fft.fft(audio_data) - - # Get the absolute value of the complex FFT result - fft_magnitudes = np.abs(fft_result) - - # Calculate the frequencies corresponding to the FFT result - fft_frequencies = np.fft.fftfreq(len(audio_data), d=1/sample_rate) - - # Return the frequencies and their corresponding magnitudes - return (fft_frequencies, fft_magnitudes) - -def inverse_dft(frequencies, magnitudes, sample_rate=44100): - # Compute the length of the audio signal - signal_length = len(frequencies) - - # Create an array of complex numbers representing the spectrum of the signal - spectrum = magnitudes * np.exp(1j * 2 * np.pi * frequencies / sample_rate) - - # Compute the inverse Fourier transform of the spectrum - amplitudes = np.fft.ifft(spectrum) - - # Return the time-domain amplitudes - return amplitudes + return np.fft.ifft(np.fft.fft(measurements_windowed) * A) class Mic: @@ -84,21 +44,28 @@ def __init__(self, pm): self.measurements = np.empty(0) self.spl_filter = FirstOrderFilter(0, 4, DT_MIC, initialized=False) - self.all_measurements = np.empty(0) + self.a_weighted_spl_filter = FirstOrderFilter(0, 4, DT_MIC, initialized=False) def update(self): - # self.measurements contains amplitudes from -1 to 1 which we use to - # calculate an uncalibrated sound pressure level + """ + Using amplitude measurements, calculate an uncalibrated sound pressure and sound pressure level. + Then apply A-weighting to the raw amplitudes and run the same calculations again. + + Logged A-weighted equivalents are rough approximations of the human-perceived loudness. + """ + if len(self.measurements) > 0: - self.all_measurements = np.concatenate((self.all_measurements, self.measurements)) - np.save('/data/test_recording', self.all_measurements) - # https://www.engineeringtoolbox.com/sound-pressure-d_711.html - sound_pressure = np.sqrt(np.mean(self.measurements ** 2)) # RMS of amplitudes - sound_pressure_level = 20 * np.log10(sound_pressure / REFERENCE_SPL) if sound_pressure > 0 else 0 # dB + sound_pressure, sound_pressure_level = calculate_spl(self.measurements) self.spl_filter.update(sound_pressure_level) + + measurements_weighted = apply_a_weighting(self.measurements) + sound_pressure_weighted, sound_pressure_level_weighted = calculate_spl(measurements_weighted) + self.a_weighted_spl_filter.update(sound_pressure_level_weighted) else: sound_pressure = 0 sound_pressure_level = 0 + sound_pressure_weighted = 0 + sound_pressure_level_weighted = 0 self.measurements = np.empty(0) @@ -117,7 +84,7 @@ def micd_thread(self, device=None): if device is None: device = "sysdefault" - with sd.InputStream(device=device, channels=1, samplerate=44100, callback=self.callback) as stream: + with sd.InputStream(device=device, channels=1, samplerate=SAMPLE_RATE, callback=self.callback) as stream: cloudlog.info(f"micd stream started: {stream.samplerate=} {stream.channels=} {stream.dtype=} {stream.device=}") while True: self.update() From 7131fe5712e05712933fe646dbea2d8ede85835d Mon Sep 17 00:00:00 2001 From: Shane Smiskol Date: Thu, 1 Dec 2022 16:30:20 -0800 Subject: [PATCH 05/15] some clean up --- system/micd.py | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/system/micd.py b/system/micd.py index 5ee28903d3b98a..0fa0c5e7deee7a 100755 --- a/system/micd.py +++ b/system/micd.py @@ -9,14 +9,17 @@ RATE = 10 DT_MIC = 1. / RATE -REFERENCE_SPL = 2 * 10 ** -5 # newtons/m^2 +REFERENCE_SPL = 2e-5 # newtons/m^2 SAMPLE_RATE = 44100 def calculate_spl(measurements): # https://www.engineeringtoolbox.com/sound-pressure-d_711.html sound_pressure = np.sqrt(np.mean(measurements ** 2)) # RMS of amplitudes - sound_pressure_level = 20 * np.log10(sound_pressure / REFERENCE_SPL) if sound_pressure > 0 else 0 # dB + if sound_pressure > 0: + sound_pressure_level = 20 * np.log10(sound_pressure / REFERENCE_SPL) # dB + else: + sound_pressure_level = 0 return sound_pressure, sound_pressure_level @@ -29,8 +32,7 @@ def apply_a_weighting(measurements: np.ndarray) -> np.ndarray: freqs = np.fft.fftfreq(measurements_windowed.size, d=1 / SAMPLE_RATE) # Calculate the A-weighting filter - # TODO: create global for 3.5041384e16 - A = 3.5041384e16 * freqs ** 4 / ((freqs ** 2 + 20.598997 ** 2) * (freqs ** 2 + 12194 ** 2) * np.sqrt((freqs ** 2 + 107.65265 ** 2) * (freqs ** 2 + 737.86223 ** 2))) + 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 From 1b48c9c1136b7d6b84be067e0eafeaea873f5336 Mon Sep 17 00:00:00 2001 From: Shane Smiskol Date: Thu, 1 Dec 2022 16:53:09 -0800 Subject: [PATCH 06/15] wishful tuning --- cereal | 2 +- selfdrive/ui/soundd/sound.cc | 2 +- system/micd.py | 16 +++++++--------- 3 files changed, 9 insertions(+), 11 deletions(-) diff --git a/cereal b/cereal index dbc9846ac9c9e7..e6a898e825de6e 160000 --- a/cereal +++ b/cereal @@ -1 +1 @@ -Subproject commit dbc9846ac9c9e735ee2f4a281ce079cfff7ea285 +Subproject commit e6a898e825de6e9c2f25b2cb7b689bcb8dce053e diff --git a/selfdrive/ui/soundd/sound.cc b/selfdrive/ui/soundd/sound.cc index 3deb6ceca0ca33..1ab08694d56400 100644 --- a/selfdrive/ui/soundd/sound.cc +++ b/selfdrive/ui/soundd/sound.cc @@ -48,7 +48,7 @@ void Sound::update() { // scale volume with speed if (sm.updated("microphone")) { - float volume = util::map_val(sm["microphone"].getMicrophone().getFilteredSoundPressureDb(), 58.f, 77.f, 0.f, 1.f); + float volume = util::map_val(sm["microphone"].getMicrophone().getFilteredSoundPressureDb(), 45.f, 60.f, 0.f, 1.f); volume = QAudio::convertVolume(volume, QAudio::LogarithmicVolumeScale, QAudio::LinearVolumeScale); Hardware::set_volume(volume); } diff --git a/system/micd.py b/system/micd.py index 0fa0c5e7deee7a..e6eb3d05b10954 100755 --- a/system/micd.py +++ b/system/micd.py @@ -45,8 +45,7 @@ def __init__(self, pm): self.rk = Ratekeeper(RATE) self.measurements = np.empty(0) - self.spl_filter = FirstOrderFilter(0, 4, DT_MIC, initialized=False) - self.a_weighted_spl_filter = FirstOrderFilter(0, 4, DT_MIC, initialized=False) + self.spl_filter_weighted = FirstOrderFilter(0, 4, DT_MIC, initialized=False) def update(self): """ @@ -57,15 +56,12 @@ def update(self): """ if len(self.measurements) > 0: - sound_pressure, sound_pressure_level = calculate_spl(self.measurements) - self.spl_filter.update(sound_pressure_level) - + sound_pressure, _ = calculate_spl(self.measurements) measurements_weighted = apply_a_weighting(self.measurements) sound_pressure_weighted, sound_pressure_level_weighted = calculate_spl(measurements_weighted) - self.a_weighted_spl_filter.update(sound_pressure_level_weighted) + self.spl_filter_weighted.update(sound_pressure_level_weighted) else: sound_pressure = 0 - sound_pressure_level = 0 sound_pressure_weighted = 0 sound_pressure_level_weighted = 0 @@ -73,8 +69,10 @@ def update(self): msg = messaging.new_message('microphone') msg.microphone.soundPressure = float(sound_pressure) - msg.microphone.soundPressureDb = float(sound_pressure_level) - msg.microphone.filteredSoundPressureDb = float(self.spl_filter.x) + msg.microphone.soundPressureWeighted = float(sound_pressure_weighted) + + msg.microphone.soundPressureWeightedDb = float(sound_pressure_level_weighted) + msg.microphone.filteredSoundPressureWeightedDb = float(self.spl_filter_weighted.x) self.pm.send('microphone', msg) self.rk.keep_time() From ab6a63f67d6e3026b261eada62bfa29df104fe0c Mon Sep 17 00:00:00 2001 From: Shane Smiskol Date: Thu, 1 Dec 2022 16:57:21 -0800 Subject: [PATCH 07/15] log pressure level (db) for debugging --- cereal | 2 +- system/micd.py | 4 +++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/cereal b/cereal index e6a898e825de6e..54b597470f1da1 160000 --- a/cereal +++ b/cereal @@ -1 +1 @@ -Subproject commit e6a898e825de6e9c2f25b2cb7b689bcb8dce053e +Subproject commit 54b597470f1da13246599502d91dd4e18f4aa11f diff --git a/system/micd.py b/system/micd.py index e6eb3d05b10954..136392777008c5 100755 --- a/system/micd.py +++ b/system/micd.py @@ -56,12 +56,13 @@ def update(self): """ if len(self.measurements) > 0: - sound_pressure, _ = calculate_spl(self.measurements) + sound_pressure, sound_pressure_level = calculate_spl(self.measurements) measurements_weighted = apply_a_weighting(self.measurements) sound_pressure_weighted, sound_pressure_level_weighted = calculate_spl(measurements_weighted) self.spl_filter_weighted.update(sound_pressure_level_weighted) else: sound_pressure = 0 + sound_pressure_level = 0 sound_pressure_weighted = 0 sound_pressure_level_weighted = 0 @@ -70,6 +71,7 @@ def update(self): msg = messaging.new_message('microphone') msg.microphone.soundPressure = float(sound_pressure) msg.microphone.soundPressureWeighted = float(sound_pressure_weighted) + msg.microphone.soundPressureDb = float(sound_pressure_level) msg.microphone.soundPressureWeightedDb = float(sound_pressure_level_weighted) msg.microphone.filteredSoundPressureWeightedDb = float(self.spl_filter_weighted.x) From 003be51f443c510ebc12b4764a689019d3e7e38c Mon Sep 17 00:00:00 2001 From: Shane Smiskol Date: Thu, 1 Dec 2022 17:05:24 -0800 Subject: [PATCH 08/15] fix --- selfdrive/ui/soundd/sound.cc | 2 +- system/micd.py | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/selfdrive/ui/soundd/sound.cc b/selfdrive/ui/soundd/sound.cc index 1ab08694d56400..30164f97c2dcba 100644 --- a/selfdrive/ui/soundd/sound.cc +++ b/selfdrive/ui/soundd/sound.cc @@ -48,7 +48,7 @@ void Sound::update() { // scale volume with speed if (sm.updated("microphone")) { - float volume = util::map_val(sm["microphone"].getMicrophone().getFilteredSoundPressureDb(), 45.f, 60.f, 0.f, 1.f); + float volume = util::map_val(sm["microphone"].getMicrophone().getFilteredSoundPressureWeightedDb(), 45.f, 60.f, 0.f, 1.f); volume = QAudio::convertVolume(volume, QAudio::LogarithmicVolumeScale, QAudio::LinearVolumeScale); Hardware::set_volume(volume); } diff --git a/system/micd.py b/system/micd.py index 4277eb51a0b838..399416db5fc855 100755 --- a/system/micd.py +++ b/system/micd.py @@ -33,6 +33,7 @@ def apply_a_weighting(measurements: np.ndarray) -> np.ndarray: 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 From aa5c34eabb3ba311dea2dd0124bec3cf26ffd0cb Mon Sep 17 00:00:00 2001 From: Cameron Clough Date: Thu, 1 Dec 2022 17:50:34 -0800 Subject: [PATCH 09/15] tuning --- selfdrive/ui/soundd/sound.cc | 2 +- system/micd.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/selfdrive/ui/soundd/sound.cc b/selfdrive/ui/soundd/sound.cc index 30164f97c2dcba..f039511ff8854a 100644 --- a/selfdrive/ui/soundd/sound.cc +++ b/selfdrive/ui/soundd/sound.cc @@ -48,7 +48,7 @@ void Sound::update() { // scale volume with speed if (sm.updated("microphone")) { - float volume = util::map_val(sm["microphone"].getMicrophone().getFilteredSoundPressureWeightedDb(), 45.f, 60.f, 0.f, 1.f); + float volume = util::map_val(sm["microphone"].getMicrophone().getFilteredSoundPressureWeightedDb(), 30.f, 52.f, 0.f, 1.f); volume = QAudio::convertVolume(volume, QAudio::LogarithmicVolumeScale, QAudio::LinearVolumeScale); Hardware::set_volume(volume); } diff --git a/system/micd.py b/system/micd.py index 399416db5fc855..abf8915ddcc870 100755 --- a/system/micd.py +++ b/system/micd.py @@ -47,7 +47,7 @@ def __init__(self, pm): self.rk = Ratekeeper(RATE) self.measurements = np.empty(0) - self.spl_filter_weighted = FirstOrderFilter(0, 4, DT_MIC, initialized=False) + self.spl_filter_weighted = FirstOrderFilter(0, 2.5, DT_MIC, initialized=False) def update(self): """ From 2f90200cb07cfc3405c6ad39cf4cd7a2b3c0a2ea Mon Sep 17 00:00:00 2001 From: Cameron Clough Date: Thu, 1 Dec 2022 18:22:57 -0800 Subject: [PATCH 10/15] ignore complex to real warning --- system/micd.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/system/micd.py b/system/micd.py index abf8915ddcc870..050d79bf98cde4 100755 --- a/system/micd.py +++ b/system/micd.py @@ -1,6 +1,7 @@ #!/usr/bin/env python3 import sounddevice as sd import numpy as np +import warnings from cereal import messaging from common.filter_simple import FirstOrderFilter @@ -95,7 +96,9 @@ def micd_thread(self, device=None): self.update() -def main(pm=None, sm=None): +def main(pm=None): + warnings.simplefilter('ignore', np.ComplexWarning) + if pm is None: pm = messaging.PubMaster(['microphone']) From 3f268f40738bff8d6b98c164af4f061495dc9d9a Mon Sep 17 00:00:00 2001 From: Shane Smiskol Date: Thu, 1 Dec 2022 19:01:44 -0800 Subject: [PATCH 11/15] remove this --- cereal | 2 +- system/micd.py | 4 +--- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/cereal b/cereal index 54b597470f1da1..45ac5a01eabe58 160000 --- a/cereal +++ b/cereal @@ -1 +1 @@ -Subproject commit 54b597470f1da13246599502d91dd4e18f4aa11f +Subproject commit 45ac5a01eabe582ffb1054e4a2ced421f5fb340b diff --git a/system/micd.py b/system/micd.py index 050d79bf98cde4..a822cec42e6af9 100755 --- a/system/micd.py +++ b/system/micd.py @@ -59,14 +59,13 @@ def update(self): """ if len(self.measurements) > 0: - sound_pressure, sound_pressure_level = calculate_spl(self.measurements) + sound_pressure, _ = calculate_spl(self.measurements) measurements_weighted = apply_a_weighting(self.measurements) sound_pressure_weighted, sound_pressure_level_weighted = calculate_spl(measurements_weighted) if not HARDWARE.is_sound_playing(): self.spl_filter_weighted.update(sound_pressure_level_weighted) else: sound_pressure = 0 - sound_pressure_level = 0 sound_pressure_weighted = 0 sound_pressure_level_weighted = 0 @@ -75,7 +74,6 @@ def update(self): msg = messaging.new_message('microphone') msg.microphone.soundPressure = float(sound_pressure) msg.microphone.soundPressureWeighted = float(sound_pressure_weighted) - msg.microphone.soundPressureDb = float(sound_pressure_level) msg.microphone.soundPressureWeightedDb = float(sound_pressure_level_weighted) msg.microphone.filteredSoundPressureWeightedDb = float(self.spl_filter_weighted.x) From ddeb5ac2aa6e041c9467e446d91f4bd6502eac13 Mon Sep 17 00:00:00 2001 From: Shane Smiskol Date: Thu, 1 Dec 2022 23:05:25 -0800 Subject: [PATCH 12/15] Update selfdrive/ui/soundd/sound.cc --- selfdrive/ui/soundd/sound.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/selfdrive/ui/soundd/sound.cc b/selfdrive/ui/soundd/sound.cc index f039511ff8854a..73d65eb1f7f939 100644 --- a/selfdrive/ui/soundd/sound.cc +++ b/selfdrive/ui/soundd/sound.cc @@ -48,7 +48,7 @@ void Sound::update() { // scale volume with speed if (sm.updated("microphone")) { - float volume = util::map_val(sm["microphone"].getMicrophone().getFilteredSoundPressureWeightedDb(), 30.f, 52.f, 0.f, 1.f); + float volume = util::map_val(sm["microphone"].getMicrophone().getFilteredSoundPressureWeightedDb(), 30.f, 55.f, 0.f, 1.f); volume = QAudio::convertVolume(volume, QAudio::LogarithmicVolumeScale, QAudio::LinearVolumeScale); Hardware::set_volume(volume); } From ba4fa89e5e658ff06c179f5e6635a8bc5e958227 Mon Sep 17 00:00:00 2001 From: Shane Smiskol Date: Thu, 1 Dec 2022 23:27:59 -0800 Subject: [PATCH 13/15] Update system/micd.py --- system/micd.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/system/micd.py b/system/micd.py index a822cec42e6af9..161eec1f79d49f 100755 --- a/system/micd.py +++ b/system/micd.py @@ -39,7 +39,7 @@ def apply_a_weighting(measurements: np.ndarray) -> np.ndarray: A /= np.max(A) # Normalize the filter # Apply the A-weighting filter to the signal - return np.fft.ifft(np.fft.fft(measurements_windowed) * A) + return np.abs(np.fft.ifft(np.fft.fft(measurements_windowed) * A)) class Mic: From 4cc19eb9873c25022d1f8fdfa927074c561f7803 Mon Sep 17 00:00:00 2001 From: Shane Smiskol Date: Thu, 1 Dec 2022 23:29:03 -0800 Subject: [PATCH 14/15] remove warning supp --- system/micd.py | 3 --- 1 file changed, 3 deletions(-) diff --git a/system/micd.py b/system/micd.py index 161eec1f79d49f..150dcb9cbe4103 100755 --- a/system/micd.py +++ b/system/micd.py @@ -1,7 +1,6 @@ #!/usr/bin/env python3 import sounddevice as sd import numpy as np -import warnings from cereal import messaging from common.filter_simple import FirstOrderFilter @@ -95,8 +94,6 @@ def micd_thread(self, device=None): def main(pm=None): - warnings.simplefilter('ignore', np.ComplexWarning) - if pm is None: pm = messaging.PubMaster(['microphone']) From 4308aa98bfadb2a5730a6279ca0f2b6727a06548 Mon Sep 17 00:00:00 2001 From: Shane Smiskol Date: Thu, 1 Dec 2022 23:33:25 -0800 Subject: [PATCH 15/15] bump cereal to master --- cereal | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cereal b/cereal index 45ac5a01eabe58..7765176413c0bb 160000 --- a/cereal +++ b/cereal @@ -1 +1 @@ -Subproject commit 45ac5a01eabe582ffb1054e4a2ced421f5fb340b +Subproject commit 7765176413c0bb14143fe2469d5390ea0ea61a1e