Expand description
§frequenz-resampling-rs
This project is the rust resampler for resampling a stream of samples to a given interval.
§Usage
An instance of the Resampler can be created with the
new method.
Raw data can be added to the resampler either through the
push or extend methods, and the
resample method resamples the data that was added to
the buffer.
use chrono::{DateTime, TimeDelta, Utc};
use frequenz_resampling::{Closed, Label, Resampler, ResamplingFunction, Sample};
#[derive(Debug, Clone, Default, Copy, PartialEq)]
pub(crate) struct TestSample {
timestamp: DateTime<Utc>,
value: Option<f64>,
}
impl Sample for TestSample {
type Value = f64;
fn new(timestamp: DateTime<Utc>, value: Option<f64>) -> Self {
Self { timestamp, value }
}
fn timestamp(&self) -> DateTime<Utc> {
self.timestamp
}
fn value(&self) -> Option<f64> {
self.value
}
}
let start = DateTime::from_timestamp(0, 0).unwrap();
let mut resampler: Resampler<f64, TestSample> =
Resampler::new(
TimeDelta::seconds(5),
ResamplingFunction::Average,
1,
start,
Closed::Left,
Label::Right,
);
let step = TimeDelta::seconds(1);
// Data starts at t=0 with values 1-10
// Interval [0, 5): t=0,1,2,3,4 with values 1,2,3,4,5 → avg = 3.0
// Interval [5, 10): t=5,6,7,8,9 with values 6,7,8,9,10 → avg = 8.0
let data = vec![
TestSample::new(start, Some(1.0)),
TestSample::new(start + step, Some(2.0)),
TestSample::new(start + step * 2, Some(3.0)),
TestSample::new(start + step * 3, Some(4.0)),
TestSample::new(start + step * 4, Some(5.0)),
TestSample::new(start + step * 5, Some(6.0)),
TestSample::new(start + step * 6, Some(7.0)),
TestSample::new(start + step * 7, Some(8.0)),
TestSample::new(start + step * 8, Some(9.0)),
TestSample::new(start + step * 9, Some(10.0)),
];
resampler.extend(data);
let resampled = resampler.resample(start + step * 10);
let expected = vec![
TestSample::new(DateTime::from_timestamp(5, 0).unwrap(), Some(3.0)),
TestSample::new(DateTime::from_timestamp(10, 0).unwrap(), Some(8.0)),
];
assert_eq!(resampled, expected);Structs§
- Resampler
- The Resampler struct is used to resample a time series of samples. It stores the samples in a buffer and resamples the samples in the buffer when the resample method is called. A resampler can be configured with a resampling function and a resampling interval.
- Simple
Sample - A simple sample type for use with the
resamplefunction.
Enums§
- Closed
- Controls which edge of an interval is closed for sample membership.
- Label
- Controls which edge of an interval is used as the output timestamp label.
- Resampling
Function - The ResamplingFunction enum represents the different resampling functions that can be used to resample a channel.
Traits§
- Sample
- The Sample trait represents a single sample in a time series.
Functions§
- epoch_
align - Aligns a timestamp to the epoch of the resampling interval.
- resample
- Resamples a list of timestamp/value pairs in a single call.