Skip to content

Commit

Permalink
change from u8 to i8 for microspeech
Browse files Browse the repository at this point in the history
  • Loading branch information
meelislootus committed Mar 18, 2021
1 parent f594667 commit ed8547b
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 13 deletions.
6 changes: 3 additions & 3 deletions examples/microspeech/Runefile
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
FROM runicos/base

CAPABILITY<I16[24000]> audio SOUND --hz 16000 --samples 150 --sample-size-ms 1500
PROC_BLOCK<I16[24000],U8[1960]> fft hotg-ai/rune#proc_blocks/fft
PROC_BLOCK<I16[24000],I8[1960]> fft hotg-ai/rune#proc_blocks/fft


MODEL<U8[1960],U8[4]> model ./model.tflite
MODEL<I8[1960],I8[4]> model ./model.tflite

# PROC_BLOCK<U8[4], UTF8> label hotg-ai/rune#proc_blocks/ohv_label --labels=unknown,silence,yes,no
PROC_BLOCK<U8[4], UTF8> microspeech_agg hotg-ai/rune#proc_blocks/microspeech_agg --labels=unknown,silence,yes,no
PROC_BLOCK<I8[4], UTF8> microspeech_agg hotg-ai/rune#proc_blocks/microspeech_agg --labels=unknown,silence,yes,no

OUT serial

Expand Down
Binary file modified examples/microspeech/microspeech.rune
Binary file not shown.
8 changes: 4 additions & 4 deletions proc_blocks/fft/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ impl Fft {
}

impl<const N: usize> runic_types::Transform<[i16; N]> for Fft {
type Output = [u8; 1960];
type Output = [i8; 1960];

fn transform(&mut self, input: [i16; N]) -> Self::Output {
// Build the spectrogram computation engine
Expand All @@ -67,10 +67,10 @@ impl<const N: usize> runic_types::Transform<[i16; N]> for Fft {
let max_value =
result_f32.iter().fold(f32::NEG_INFINITY, |a, &b| a.max(b));

let res: Vec<u8> = result_f32
let res: Vec<i8> = result_f32
.into_iter()
.map(|freq| 255.0 * (freq - min_value) / (max_value - min_value))
.map(|freq| freq as u8)
.map(|freq| 255.0 * (freq - min_value) / (max_value - min_value) - 128.0)
.map(|freq| freq as i8)
.collect();
let mut out = [0; 1960];

Expand Down
12 changes: 6 additions & 6 deletions proc_blocks/microspeech_agg/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use alloc::collections::VecDeque;
#[derive(Debug, Clone, PartialEq)]
pub struct MicrospeechAgg<const N: usize> {
labels: [&'static str; N],
history: VecDeque<[u8; N]>,
history: VecDeque<[i8; N]>,
max_capacity: usize,
unknown: &'static str,
throttle_interval: usize,
Expand Down Expand Up @@ -47,7 +47,7 @@ impl<const N: usize> MicrospeechAgg<N> {
}
}

fn add_history(&mut self, input: [u8; N]) {
fn add_history(&mut self, input: [i8; N]) {
self.history.push_back(input);

while self.history.len() > self.max_capacity {
Expand All @@ -62,9 +62,9 @@ impl<const N: usize> MicrospeechAgg<N> {

(0..N)
.fold(None, |previous_most_likely, microspeech_index| {
let sum: u8 =
let sum: i8 =
self.history.iter().map(|input| input[microspeech_index]).sum();
let avg = sum / self.history.len() as u8;
let avg = sum / self.history.len() as i8;

match previous_most_likely {
Some((_, previous_avg)) if previous_avg >= avg => {
Expand All @@ -81,10 +81,10 @@ impl<const N: usize> MicrospeechAgg<N> {
}
}

impl<const N: usize> Transform<[u8; N]> for MicrospeechAgg<N> {
impl<const N: usize> Transform<[i8; N]> for MicrospeechAgg<N> {
type Output = &'static str;

fn transform(&mut self, input: [u8; N]) -> Self::Output {
fn transform(&mut self, input: [i8; N]) -> Self::Output {
// This is a rust port of https://github.com/andriyadi/MagicWand-TFLite-ESP32/blob/00fd15f0861b27437236689ceb642a05cf5fb028/src/microspeech_predictor.cpp#L35-L101

self.add_history(input);
Expand Down
1 change: 1 addition & 0 deletions runic-types/src/buffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,3 +83,4 @@ impl_buffer!(f32, A, B, C, D);
impl_buffer!(i32, A, B, C, D);
impl_buffer!(i16, A, B, C, D);
impl_buffer!(u8, A, B, C, D);
impl_buffer!(i8, A, B, C, D);
4 changes: 4 additions & 0 deletions runic-types/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,3 +112,7 @@ impl AsParamType for i16 {
impl AsParamType for u8 {
const VALUE: PARAM_TYPE = PARAM_TYPE::BINARY;
}

impl AsParamType for i8 {
const VALUE: PARAM_TYPE = PARAM_TYPE::BINARY;
}

0 comments on commit ed8547b

Please sign in to comment.