-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Stefan Gloor <code@stefan-gloor.ch>
- Loading branch information
Showing
5 changed files
with
100 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,3 +9,7 @@ ml/sample_input.* | |
ml/data | ||
ml/venv | ||
src/models | ||
__pycache__ | ||
venv | ||
tools/*.wav | ||
tools/*.log |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") |