lockin: borrow adc samples and timestamps as slices

This commit is contained in:
Matt Huszagh 2020-11-28 13:20:42 -08:00
parent f259d6cf65
commit 90ef9f1e6a
2 changed files with 15 additions and 34 deletions

View File

@ -154,9 +154,6 @@ impl Lockin {
/// * `timestamps` - Counter values corresponding to the edges of
/// an external reference signal. The counter is incremented by a
/// fast internal clock.
/// * `valid_timestamps` - The number of valid timestamps in
/// `timestamps`. Only `&timestamps[..valid_timestamps]` are used;
/// every other value in the `timestamps` array is ignored.
///
/// # Returns
///
@ -172,9 +169,8 @@ impl Lockin {
/// at the end of the ADC batch sampling period.
pub fn demodulate(
&mut self,
adc_samples: [i16; ADC_SAMPLE_BUFFER_SIZE],
timestamps: [u16; TIMESTAMP_BUFFER_SIZE],
valid_timestamps: u16,
adc_samples: &[i16],
timestamps: &[u16],
) -> Result<
([f32; ADC_SAMPLE_BUFFER_SIZE], [f32; ADC_SAMPLE_BUFFER_SIZE]),
&str,
@ -195,7 +191,7 @@ impl Lockin {
// record new timestamps
timestamps
.iter()
.take(valid_timestamps as usize)
.take(timestamps.len())
.rev()
.take(2)
.rev()
@ -449,11 +445,7 @@ mod tests {
},
);
assert_eq!(
lockin.demodulate(
[0; ADC_SAMPLE_BUFFER_SIZE],
[0; TIMESTAMP_BUFFER_SIZE],
0
),
lockin.demodulate(&[0; ADC_SAMPLE_BUFFER_SIZE], &[],),
Err("insufficient timestamps")
);
}
@ -472,11 +464,7 @@ mod tests {
},
);
assert_eq!(
lockin.demodulate(
[0; ADC_SAMPLE_BUFFER_SIZE],
[0; TIMESTAMP_BUFFER_SIZE],
1
),
lockin.demodulate(&[0; ADC_SAMPLE_BUFFER_SIZE], &[0],),
Err("insufficient timestamps")
);
}
@ -499,15 +487,9 @@ mod tests {
[-8, 7, -7, 6, -6, 5, -5, 4, -4, 3, -3, 2, -2, -1, 1, 0];
let reference_period: u16 = 2800;
let initial_phase_integer: u16 = 200;
let timestamps: [u16; TIMESTAMP_BUFFER_SIZE] = [
let timestamps: &[u16] = &[
initial_phase_integer,
initial_phase_integer + reference_period,
0,
0,
0,
0,
0,
0,
];
let initial_phase: f32 =
-(initial_phase_integer as f32) / reference_period as f32 * 2. * PI;
@ -527,7 +509,7 @@ mod tests {
*q = cosine * adc_samples[n] as f32;
}
let (result_in_phase, result_quadrature) =
lockin.demodulate(adc_samples, timestamps, 2).unwrap();
lockin.demodulate(&adc_samples, timestamps).unwrap();
assert!(
array_within_tolerance(&result_in_phase, &in_phase, 0., 1e-5),
"\nin_phase computed: {:?},\nin_phase expected: {:?}",

View File

@ -1,7 +1,7 @@
use dsp::iir::IIR;
use dsp::lockin::{
decimate, magnitude_phase, Lockin, ADC_SAMPLE_BUFFER_SIZE,
DECIMATED_BUFFER_SIZE, TIMESTAMP_BUFFER_SIZE,
DECIMATED_BUFFER_SIZE,
};
use std::f64::consts::PI;
@ -159,15 +159,14 @@ fn adc_sampled_signal(
/// batch period, followed by an array of the timestamp values.
fn adc_batch_timestamps(
reference_frequency: f64,
timestamps: &mut [u16],
timestamp_start: u64,
timestamp_stop: u64,
internal_frequency: f64,
) -> (usize, [u16; TIMESTAMP_BUFFER_SIZE]) {
) -> &[u16] {
let reference_period = internal_frequency / reference_frequency;
let start_count = timestamp_start as f64 % reference_period;
let mut valid_timestamps: usize = 0;
let mut timestamps: [u16; TIMESTAMP_BUFFER_SIZE] =
[0; TIMESTAMP_BUFFER_SIZE];
let mut timestamp = (reference_period - start_count) % reference_period;
while timestamp < (timestamp_stop - timestamp_start) as f64 {
@ -176,7 +175,7 @@ fn adc_batch_timestamps(
valid_timestamps += 1;
}
(valid_timestamps, timestamps)
&timestamps[..valid_timestamps]
}
/// Lowpass biquad filter using cutoff and sampling frequencies.
@ -589,8 +588,10 @@ fn lowpass_test(
internal_frequency,
adc_frequency,
);
let (valid_timestamps, timestamps) = adc_batch_timestamps(
let mut timestamps_array = [0_u16; ADC_SAMPLE_BUFFER_SIZE / 2];
let timestamps = adc_batch_timestamps(
reference_frequency,
&mut timestamps_array,
timestamp_start,
timestamp_start + sample_count - 1,
internal_frequency,
@ -598,9 +599,7 @@ fn lowpass_test(
let mut in_phase: [f32; ADC_SAMPLE_BUFFER_SIZE];
let mut quadrature: [f32; ADC_SAMPLE_BUFFER_SIZE];
let lockin_demodulate =
lockin.demodulate(signal, timestamps, valid_timestamps as u16);
match lockin_demodulate {
match lockin.demodulate(&signal, timestamps) {
Ok((i, q)) => {
in_phase = i;
quadrature = q;