Skip to content
Merged

mpll #18

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
472 changes: 356 additions & 116 deletions Cargo.lock

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ socket2 = "0.6.0"
idsp = "0.20"
dsp-process = "0.1.0"
rustfft = "6.1.0"
rand = { version = "0.9.0", features = ["small_rng"] }
rand = "0.10.0"
derive_builder = "0.20.0"

[profile.release]
Expand Down
6 changes: 3 additions & 3 deletions src/bin/psd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -171,14 +171,14 @@ fn main() -> Result<()> {
loop {
match source.get() {
Ok(traces) => {
for (i, (name, trace)) in traces.iter().enumerate() {
for (i, (name, trace)) in traces.into_iter().enumerate() {
if dec.len() <= i {
let mut p = PsdCascade::<{ 1 << 9 }>::default();
p.set_detrend(acq.detrend);
p.set_avg(acq.avg_opts());
dec.push((*name, p));
dec.push((name, p));
}
dec[i].1.process(trace);
dec[i].1.process(&trace);
}
}
Err(e) => log::warn!("source: {}", e),
Expand Down
58 changes: 52 additions & 6 deletions src/de/data.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use super::Error;
use core::fmt::Debug;
use core::{f32, fmt::Debug};

pub trait Payload<'a>: Debug {
fn new(batches: usize, data: &'a [u8]) -> Result<Self, Error>
Expand All @@ -20,9 +20,7 @@ impl<'a> Payload<'a> for AdcDac<'a> {
/// * `batch_size` - The size of each batch in samples.
/// * `data` - The binary data composing the stream frame.
fn new(batches: usize, data: &'a [u8]) -> Result<Self, Error> {
const CHANNELS: usize = 4;
const BATCH_SIZE: usize = 8;
let data: &[[[[u8; 2]; BATCH_SIZE]; CHANNELS]] = bytemuck::try_cast_slice(data)?;
let data = bytemuck::try_cast_slice(data)?;
assert_eq!(data.len(), batches);
Ok(Self { data })
}
Expand Down Expand Up @@ -90,7 +88,7 @@ pub struct Fls<'a> {

impl<'a> Payload<'a> for Fls<'a> {
fn new(batches: usize, data: &'a [u8]) -> Result<Self, Error> {
let data: &[[[[u8; 4]; 7]; 2]] = bytemuck::try_cast_slice(data)?;
let data = bytemuck::try_cast_slice(data)?;
// demod_re, demod_im, phase[2], ftw, pow_amp, pll
assert_eq!(batches, data.len());
Ok(Self { data })
Expand Down Expand Up @@ -148,7 +146,7 @@ pub struct ThermostatEem<'a> {

impl<'a> Payload<'a> for ThermostatEem<'a> {
fn new(batches: usize, data: &'a [u8]) -> Result<Self, Error> {
let data: &[[[u8; 4]; 16 + 4]] = bytemuck::try_cast_slice(data)?;
let data = bytemuck::try_cast_slice(data)?;
assert_eq!(batches, data.len());
Ok(Self { data })
}
Expand All @@ -164,3 +162,51 @@ impl<'a> Payload<'a> for ThermostatEem<'a> {
.collect())
}
}

#[derive(Clone, Debug)]
pub struct Mpll<'a> {
data: &'a [[[u8; 4]; 6]],
}

impl<'a> Payload<'a> for Mpll<'a> {
fn new(batches: usize, data: &'a [u8]) -> Result<Self, Error> {
let data = bytemuck::try_cast_slice(data)?;
assert_eq!(batches, data.len());
Ok(Self { data })
}

fn traces(&self) -> Result<Vec<(&'static str, Vec<f32>)>, Error> {
Ok(vec![
(
"phase (rad)",
self.data
.iter()
.map(|b| {
i32::from_le_bytes(b[4]) as f32 * (f32::consts::TAU / (1u64 << 32) as f32)
})
.collect(),
),
(
"frequency (kHz)",
self.data
.iter()
.map(|b| {
i32::from_le_bytes(b[5]) as f32 * (1.0 / 1.28e-3 / (1u64 << 32) as f32)
})
.collect(),
),
(
"amplitude (V/G10)",
self.data
.iter()
.map(|b| {
((i32::from_le_bytes(b[0]) as f32).powi(2)
+ (i32::from_le_bytes(b[1]) as f32).powi(2))
.sqrt()
* (10.24 / 10.0 * 2.0 * 2.0 / (1u64 << 32) as f32)
})
.collect(),
),
])
}
}
1 change: 1 addition & 0 deletions src/de/frame.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ impl<'a> Frame<'a> {
Format::AdcDac => Box::new(data::AdcDac::new(batches, data)?),
Format::Fls => Box::new(data::Fls::new(batches, data)?),
Format::ThermostatEem => Box::new(data::ThermostatEem::new(batches, data)?),
Format::Mpll => Box::new(data::Mpll::new(header.batches as _, data)?),
};
Ok(Self { header, payload })
}
Expand Down
1 change: 1 addition & 0 deletions src/de/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ pub enum Format {
AdcDac = 1,
Fls = 2,
ThermostatEem = 3,
Mpll = 4,
}

#[derive(Debug, Clone, Error)]
Expand Down
2 changes: 1 addition & 1 deletion src/loss.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ impl Loss {
self.dropped += missing;
if missing > 0 {
log::warn!(
"Lost {} batches: {:#08X} -> {:#08X}",
"Lost {} batches {:#08X}..{:#08X}",
missing,
seq,
frame.header.seq,
Expand Down
20 changes: 8 additions & 12 deletions src/source.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::{Frame, Loss};
use anyhow::Result;
use clap::Parser;
use dsp_process::Process;
use rand::{rngs::SmallRng, Rng, SeedableRng};
use rand::{rngs::SmallRng, RngExt, SeedableRng};
use socket2::{Domain, Protocol, Socket, Type};
use std::{
fs::File,
Expand Down Expand Up @@ -106,7 +106,7 @@ impl Source {
"noise",
rng.sample_iter(rand::distr::Open01)
.map(|mut x| {
x = (x - 0.5) * 12.0f32.sqrt(); // zero mean, RMS = 1
x = (x - 0.5) * 12f32.sqrt(); // zero mean, RMS = 1
state.iter_mut().fold(x, |mut x, s| {
// TODO: branch optimization barrier
(x, *s) = if *diff { (x - *s, x) } else { (*s, x + *s) };
Expand Down Expand Up @@ -148,17 +148,13 @@ impl Source {
},
Data::Raw(fil) => loop {
let mut buf = [0u8; 2048];
match fil.read(&mut buf[..]) {
Ok(len) => {
if len == 0 && self.opts.repeat {
fil.seek(std::io::SeekFrom::Start(0))?;
continue;
}
let v: &[[u8; 4]] = bytemuck::cast_slice(&buf[..len / 4 * 4]);
break vec![("raw", v.iter().map(|b| f32::from_le_bytes(*b)).collect())];
}
Err(e) => Err(e)?,
let len = fil.read(&mut buf[..])?;
if len == 0 && self.opts.repeat {
fil.seek(std::io::SeekFrom::Start(0))?;
continue;
}
let v: &[[u8; 4]] = bytemuck::cast_slice(&buf[..len / 4 * 4]);
break vec![("raw", v.iter().map(|b| f32::from_le_bytes(*b)).collect())];
},
Data::Udp(socket) => {
let mut buf = [0u8; 2048];
Expand Down