diff options
author | Matthias P. Braendli <matthias.braendli@mpb.li> | 2024-12-16 16:53:54 +0100 |
---|---|---|
committer | Matthias P. Braendli <matthias.braendli@mpb.li> | 2024-12-16 16:53:54 +0100 |
commit | 8daccb896619f054c49a65a76b73e92fcce330e9 (patch) | |
tree | 407c96c7fdd2571b1623e8c32ee99b05fe7d779c /mictoled/pdmgen | |
parent | 959edc17ff8f4b71a258c3c66d25bd904fb98bf0 (diff) | |
download | iceFUN-8daccb896619f054c49a65a76b73e92fcce330e9.tar.gz iceFUN-8daccb896619f054c49a65a76b73e92fcce330e9.tar.bz2 iceFUN-8daccb896619f054c49a65a76b73e92fcce330e9.zip |
mictoled: improve sim test data
Diffstat (limited to 'mictoled/pdmgen')
-rw-r--r-- | mictoled/pdmgen/.gitignore | 3 | ||||
-rw-r--r-- | mictoled/pdmgen/Cargo.lock | 7 | ||||
-rw-r--r-- | mictoled/pdmgen/Cargo.toml | 6 | ||||
-rw-r--r-- | mictoled/pdmgen/plot.py | 16 | ||||
-rw-r--r-- | mictoled/pdmgen/src/main.rs | 63 |
5 files changed, 95 insertions, 0 deletions
diff --git a/mictoled/pdmgen/.gitignore b/mictoled/pdmgen/.gitignore new file mode 100644 index 0000000..6e712d8 --- /dev/null +++ b/mictoled/pdmgen/.gitignore @@ -0,0 +1,3 @@ +pdm.hex +pdm.npy +target/ diff --git a/mictoled/pdmgen/Cargo.lock b/mictoled/pdmgen/Cargo.lock new file mode 100644 index 0000000..db8ba76 --- /dev/null +++ b/mictoled/pdmgen/Cargo.lock @@ -0,0 +1,7 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "pdmgen" +version = "0.1.0" diff --git a/mictoled/pdmgen/Cargo.toml b/mictoled/pdmgen/Cargo.toml new file mode 100644 index 0000000..d93909a --- /dev/null +++ b/mictoled/pdmgen/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "pdmgen" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/mictoled/pdmgen/plot.py b/mictoled/pdmgen/plot.py new file mode 100644 index 0000000..daa238c --- /dev/null +++ b/mictoled/pdmgen/plot.py @@ -0,0 +1,16 @@ +#!/usr/bin/env python +import sys +import numpy as np +import matplotlib.pyplot as pp + +pdm = np.fromfile("pdm.npy", np.int8) + +pdm_decim = np.sum(pdm.reshape(-1, 64), axis=1) + +fig = pp.figure() +fig.suptitle("PDM decimated") +ax1 = fig.add_subplot(1, 1, 1) +ax1.plot(pdm_decim) + +pp.show() + diff --git a/mictoled/pdmgen/src/main.rs b/mictoled/pdmgen/src/main.rs new file mode 100644 index 0000000..873f3ea --- /dev/null +++ b/mictoled/pdmgen/src/main.rs @@ -0,0 +1,63 @@ +use std::{io::Write, slice}; + +fn main() { + let freq_ampl_pairs : Vec<String> = std::env::args() + .skip(1) + .collect(); + + let freq_ampl : Vec<(f32, f32)> = freq_ampl_pairs + .chunks_exact(2) + .map(|c| { + (c[0].parse().expect("argument should convert to 32"), + c[1].parse().expect("argument should convert to 32")) + }) + .collect(); + + println!("PDM gen with:"); + for (f, a) in &freq_ampl { + println!("Freq: {} Ampl: {}", f, a); + } + + const AUDIO_RATE : usize = 31250; + const INTERP : usize = 64; + //const PDM_RATE : usize = AUDIO_RATE * INTERP; + + const AUDIO_LEN : usize = 2 * AUDIO_RATE; + + let mut audio_signal = Vec::new(); + audio_signal.resize(AUDIO_LEN, 0f32); + use std::f32::consts::PI; + for (f, a) in &freq_ampl { + for n in 0..AUDIO_LEN { + audio_signal[n] += a * f32::sin(2.0 * PI * f / (AUDIO_RATE as f32) * (n as f32)); + } + } + + let mut pdm_signal = Vec::with_capacity(audio_signal.len() * INTERP); + for samp in audio_signal { + let mut quant = 0i8; + let mut sigma = 0.0; + for _i in 0..INTERP { + let delta = samp - quant as f32; + sigma += delta; + quant = if sigma >= 0.0 { 1 } else { -1 }; + pdm_signal.push(quant); + } + } + + let fd = std::fs::File::create("pdm.npy").expect("pdm.npy should be created"); + let mut fd = std::io::BufWriter::new(fd); + let slice_u8: &[u8] = unsafe { + slice::from_raw_parts( + pdm_signal.as_ptr() as *const u8, + pdm_signal.len(), + ) + }; + fd.write(slice_u8).expect("write to pdm.npy"); + + let fd = std::fs::File::create("pdm.hex").expect("pdm.hex should be created"); + let mut fd = std::io::BufWriter::new(fd); + for samp in pdm_signal { + writeln!(&mut fd, "{}", if samp > 0 { 1 } else { 0 }).expect("write to pdm.hex"); + } +} |