frequenz_resampling/
lib.rs

1// License: MIT
2// Copyright © 2024 Frequenz Energy-as-a-Service GmbH
3
4/*!
5# frequenz-resampling-rs
6
7This project is the rust resampler for resampling a stream of samples to a
8given interval.
9
10## Usage
11
12An instance of the [`Resampler`] can be created with the
13[`new`][Resampler::new] method.
14Raw data can be added to the resampler either through the
15[`push`][Resampler::push] or [`extend`][Resampler::extend] methods, and the
16[`resample`][Resampler::resample] method resamples the data that was added to
17the buffer.
18
19```rust
20use chrono::{DateTime, TimeDelta, Utc};
21use frequenz_resampling::{Resampler, ResamplingFunction, Sample};
22
23#[derive(Debug, Clone, Default, Copy, PartialEq)]
24pub(crate) struct TestSample {
25    timestamp: DateTime<Utc>,
26    value: Option<f64>,
27}
28
29impl Sample for TestSample {
30    type Value = f64;
31
32    fn new(timestamp: DateTime<Utc>, value: Option<f64>) -> Self {
33        Self { timestamp, value }
34    }
35
36    fn timestamp(&self) -> DateTime<Utc> {
37        self.timestamp
38    }
39
40    fn value(&self) -> Option<f64> {
41        self.value
42    }
43}
44
45let start = DateTime::from_timestamp(0, 0).unwrap();
46let mut resampler: Resampler<f64, TestSample> =
47    Resampler::new(TimeDelta::seconds(5), ResamplingFunction::Average, 1, start, false);
48
49let step = TimeDelta::seconds(1);
50let data = vec![
51    TestSample::new(start + step, Some(1.0)),
52    TestSample::new(start + step * 2, Some(2.0)),
53    TestSample::new(start + step * 3, Some(3.0)),
54    TestSample::new(start + step * 4, Some(4.0)),
55    TestSample::new(start + step * 5, Some(5.0)),
56    TestSample::new(start + step * 6, Some(6.0)),
57    TestSample::new(start + step * 7, Some(7.0)),
58    TestSample::new(start + step * 8, Some(8.0)),
59    TestSample::new(start + step * 9, Some(9.0)),
60    TestSample::new(start + step * 10, Some(10.0)),
61];
62
63resampler.extend(data);
64
65let resampled = resampler.resample(start + step * 10);
66
67let expected = vec![
68    TestSample::new(DateTime::from_timestamp(5, 0).unwrap(), Some(3.0)),
69    TestSample::new(DateTime::from_timestamp(10, 0).unwrap(), Some(8.0)),
70];
71
72assert_eq!(resampled, expected);
73```
74*/
75
76mod resampler;
77
78#[cfg(test)]
79mod tests;
80
81#[cfg(feature = "python")]
82mod python;
83
84mod resampling_function;
85pub use resampling_function::ResamplingFunction;
86
87pub use resampler::{Resampler, Sample};