diff --git a/.gitignore b/.gitignore index 2a06d4c..d90130a 100644 --- a/.gitignore +++ b/.gitignore @@ -9,3 +9,7 @@ ml/sample_input.* ml/data ml/venv src/models +__pycache__ +venv +tools/*.wav +tools/*.log diff --git a/src/dfsdm.c b/src/dfsdm.c index f767596..a68e2ff 100644 --- a/src/dfsdm.c +++ b/src/dfsdm.c @@ -45,11 +45,11 @@ void dfsdm_init(void) { hdfsdm1_filter0.Instance = DFSDM1_Filter0; hdfsdm1_filter0.Init.RegularParam.Trigger = DFSDM_FILTER_SW_TRIGGER; - hdfsdm1_filter0.Init.RegularParam.FastMode = ENABLE; + hdfsdm1_filter0.Init.RegularParam.FastMode = DISABLE; hdfsdm1_filter0.Init.RegularParam.DmaMode = ENABLE; - hdfsdm1_filter0.Init.FilterParam.SincOrder = DFSDM_FILTER_SINC3_ORDER; - hdfsdm1_filter0.Init.FilterParam.Oversampling = 200; - hdfsdm1_filter0.Init.FilterParam.IntOversampling = 1; + hdfsdm1_filter0.Init.FilterParam.SincOrder = DFSDM_FILTER_SINC2_ORDER; + hdfsdm1_filter0.Init.FilterParam.Oversampling = 25; + hdfsdm1_filter0.Init.FilterParam.IntOversampling = 4; if (HAL_DFSDM_FilterInit(&hdfsdm1_filter0) != HAL_OK) { ERR("Failed to initialize DFSDM filter 0\n"); } @@ -67,8 +67,8 @@ void dfsdm_init(void) DFSDM_CHANNEL_SPI_CLOCK_INTERNAL; hdfsdm1_channel2.Init.Awd.FilterOrder = DFSDM_CHANNEL_FASTSINC_ORDER; hdfsdm1_channel2.Init.Awd.Oversampling = 1; - hdfsdm1_channel2.Init.Offset = -190; - hdfsdm1_channel2.Init.RightBitShift = 8; + hdfsdm1_channel2.Init.Offset = 0; + hdfsdm1_channel2.Init.RightBitShift = 2; if (HAL_DFSDM_ChannelInit(&hdfsdm1_channel2) != HAL_OK) { ERR("Failed to initialize DFSDM channel 2\n"); } diff --git a/src/mic.cc b/src/mic.cc index 1039745..9720a89 100644 --- a/src/mic.cc +++ b/src/mic.cc @@ -53,19 +53,6 @@ void speech::mic::init() ERR("malloc failed.\n"); } - /* Memory test */ - memset(this->buf, 42, this->buf_size * sizeof(int32_t)); - for (size_t i = 0; i < this->buf_size; i++) { - this->buf[i] = 42; - } - - for (size_t i = 0; i < this->buf_size; i++) { - if (this->buf[i] != 42) { - printf("mismatch on %u\n", i); - } - assert(this->buf[i] == 42); - } - if (HAL_DFSDM_FilterRegularStart_DMA(&hdfsdm1_filter0, this->buf, this->buf_size) != HAL_OK) { ERR("Failed to start conversion.\n"); diff --git a/tools/plot.py b/tools/plot.py new file mode 100755 index 0000000..8bb0c32 --- /dev/null +++ b/tools/plot.py @@ -0,0 +1,32 @@ +#!/usr/bin/env python3 +import matplotlib.pyplot as plt + +def read_data(file_path): + data = [] + with open(file_path, 'r') as file: + for line_number, line in enumerate(file, start=1): + if line_number >= 5: + try: + data.append(float(line.strip())) + except ValueError: + print(f"Error parsing line {line_number}: {line}") + return data + +def plot_data(data): + x = list(range(1, len(data) + 1)) # Sample numbers from 1 to 1600 + plt.figure(figsize=(12, 6)) # Wide figure + x = [ i*0.0625 for i in x ] + plt.plot(x, data, color='black', linewidth=0.5) # Thin black line + plt.scatter(x, data, color='red', s=3) # Little red points at each sample + #plt.xlabel('Sample Number') + plt.xlabel('Time [ms]') + plt.ylabel('Amplitude') + plt.title('Microphone (16 kS/s)') + plt.grid(True) + #plt.savefig('plot.pdf') # Save plot to PDF + plt.show() + +if __name__ == "__main__": + file_path = "data.log" + data = read_data(file_path) + plot_data(data) diff --git a/tools/replay.py b/tools/replay.py new file mode 100755 index 0000000..32d9055 --- /dev/null +++ b/tools/replay.py @@ -0,0 +1,58 @@ +#!/usr/bin/env python3 + +from plot import plot_data + +import simpleaudio as sa +import numpy as np +import wave +import math + +sampling_rate = 16000 +resolution = 255 + +def read_data(file_path): + data = [] + with open(file_path, 'r') as file: + for line_number, line in enumerate(file, start=1): + if line_number >= 5: + try: + data.append(float(line.strip())) + except ValueError: + print(f"Error parsing line {line_number}: {line}") + # Strip first ~100 ms + start_time = 0.100 + data = data[int(start_time/(1/sampling_rate)):] + return data + +def record_file(file_path, data, sampling_rate): + with wave.open(file_path, mode="wb") as wav_file: + wav_file.setnchannels(1) + wav_file.setsampwidth(1) + wav_file.setframerate(sampling_rate) + wav_file.writeframes(bytes(data)) + +def play_file(file_path): + wave_obj = sa.WaveObject.from_wave_file(file_path) + play_obj = wave_obj.play() + play_obj.wait_done() + +if __name__ == "__main__": + file_path = "data.log" + data = read_data(file_path) + + # Known-good signal + #data = [] + #for i in np.arange(6400): + # time = 1/sampling_rate * i + # amplitude = math.sin(2 * math.pi * 440 * time) + # data.append(amplitude) + + # Normalize data + min_val = np.min(data) + max_val = np.max(data) + data = (data - min_val) / (max_val - min_val) + data = [ int(i) for i in (data * resolution) ] + + plot_data(data) + record_file("output.wav", data, sampling_rate) + play_file("output.wav")