Skip to content

Commit

Permalink
Merge branch 'stgloor/mic'
Browse files Browse the repository at this point in the history
Signed-off-by: Stefan Gloor <code@stefan-gloor.ch>
  • Loading branch information
stgloorious committed Jun 10, 2024
2 parents 9a43666 + e2afed1 commit e9dcb48
Show file tree
Hide file tree
Showing 5 changed files with 100 additions and 19 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,7 @@ ml/sample_input.*
ml/data
ml/venv
src/models
__pycache__
venv
tools/*.wav
tools/*.log
12 changes: 6 additions & 6 deletions src/dfsdm.c
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
Expand All @@ -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");
}
Expand Down
13 changes: 0 additions & 13 deletions src/mic.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down
32 changes: 32 additions & 0 deletions tools/plot.py
Original file line number Diff line number Diff line change
@@ -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)
58 changes: 58 additions & 0 deletions tools/replay.py
Original file line number Diff line number Diff line change
@@ -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")

0 comments on commit e9dcb48

Please sign in to comment.