Skip to content

Commit

Permalink
Add a lighter downsampler option
Browse files Browse the repository at this point in the history
  • Loading branch information
bugadani committed Nov 6, 2023
1 parent 305e42a commit 7fdca85
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 6 deletions.
3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,9 @@ esp32s3 = [
"esp-wifi/esp32s3",
]

# Signal processing
downsampler-light = [] # uses IIR-based filtering and less memory

# Print options
uart = ["esp-backtrace/print-uart", "esp-println/uart"]
jtag_serial = ["esp-backtrace/print-jtag-serial", "esp-println/jtag_serial"]
Expand Down
1 change: 0 additions & 1 deletion src/board/wifi/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ use crate::{
use alloc::{boxed::Box, rc::Rc};
use embassy_executor::Spawner;
use embassy_net::{Config, Stack, StackResources};
use embassy_time::{Duration, Timer as DelayTimer};
use esp_wifi::{
wifi::{WifiApDevice, WifiDevice, WifiDeviceMode, WifiStaDevice},
EspWifiInitFor, EspWifiInitialization,
Expand Down
63 changes: 58 additions & 5 deletions src/states/measure.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ use object_chain::{chain, Chain, ChainElement, Link};
use signal_processing::{
compressing_buffer::CompressingBuffer,
filter::{
downsample::DownSampler,
iir::{precomputed::ALL_PASS, HighPass, Iir, LowPass},
pli::{adaptation_blocking::AdaptationBlocking, PowerLineFilter},
Filter,
Expand All @@ -31,6 +30,9 @@ use signal_processing::{
moving::sum::EstimatedSum,
};

#[cfg(not(feature = "downsampler-light"))]
use signal_processing::filter::downsample::DownSampler;

type MessageQueue = Channel<CriticalSectionRawMutex, Sample, 32>;

// FIXME: avoid this allow
Expand All @@ -48,13 +50,66 @@ pub type EcgFilter = chain! {
Iir<'static, HighPass, 2>
};

#[cfg(feature = "downsampler-light")]
pub struct DownsamplerLight {
filter: Iir<'static, LowPass, 2>,
counter: u8,
}

#[cfg(feature = "downsampler-light")]
impl Filter for DownsamplerLight {
fn clear(&mut self) {
self.filter.clear();
self.counter = 0;
}

fn update(&mut self, sample: f32) -> Option<f32> {
let filtered = self.filter.update(sample)?;
if self.counter == 0 {
self.counter = 7;
Some(filtered)
} else {
self.counter -= 1;
None
}
}
}

// Downsample by 8 to display around 1 second
pub type EcgDownsampler = chain! {
#[cfg(not(feature = "downsampler-light"))]
pub type DownsamplerChain = chain! {
DownSampler,
DownSampler,
DownSampler
};

#[cfg(not(feature = "downsampler-light"))]
type EcgDownsampler = DownsamplerChain;

#[cfg(not(feature = "downsampler-light"))]
fn create_downsampler() -> DownsamplerChain {
Chain::new(DownSampler::new())
.append(DownSampler::new())
.append(DownSampler::new())
}

#[cfg(feature = "downsampler-light")]
type EcgDownsampler = DownsamplerLight;

#[cfg(feature = "downsampler-light")]
fn create_downsampler() -> DownsamplerLight {
DownsamplerLight {
#[rustfmt::skip]
filter: macros::designfilt!(
"lowpassiir",
"FilterOrder", 2,
"HalfPowerFrequency", 35,
"SampleRate", 1000
),
counter: 7,
}
}

pub const ECG_BUFFER_SIZE: usize = 90_000;

// Two filter chains:
Expand All @@ -72,9 +127,7 @@ impl EcgObjects {
fn new(hpf: Iir<'static, HighPass, 2>) -> Self {
Self {
filter: Chain::new(PowerLineFilter::new_1ksps([50.0])).append(hpf),
downsampler: Chain::new(DownSampler::new())
.append(DownSampler::new())
.append(DownSampler::new()),
downsampler: create_downsampler(),
heart_rate_calculator: HeartRateCalculator::new(1000.0),

#[rustfmt::skip]
Expand Down

0 comments on commit 7fdca85

Please sign in to comment.