1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
//! Allan provides variance and deviation tools for stability analysis
//!
//! # Goals
//! * provide streaming variance and deviations from series data
//! * pre-allocated datastructures
//!
//! # Usage
//!
//! Create a new instance, add records, retrieve statistic
//!
//! # Example
//!
//! Create a new instance, add a few records, retrieve allan deviation
//!
//! ```
//!
//! use allan::*;
//!
//! // a default Allan
//! let mut allan = Allan::new();
//! for _ in 0..100 {
//!     allan.record(1.0);
//! }
//! assert_eq!(allan.get(1).unwrap().deviation().unwrap(), 0.0);
//!
//! // a configured Allan
//! let mut allan = Allan::configure().max_tau(10_000).build().unwrap();

#![crate_type = "lib"]

use std::collections::VecDeque;

/// the main datastructure for Allan
#[derive(Clone)]
pub struct Allan {
    samples: usize,
    config: Config,
    taus: Vec<Tau>,
    buffer: VecDeque<f64>,
}

/// a duration-based bucket for the stability metric
#[derive(Copy, Clone)]
pub struct Tau {
    value: f64,
    count: u64,
    tau: usize,
}

impl Tau {
    // construct a new `Tau`
    fn new(tau: usize) -> Tau {
        Tau {
            value: 0.0_f64,
            count: 0_u64,
            tau: tau,
        }
    }

    /// returns the time value of the `Tau`
    pub fn tau(self) -> usize {
        self.tau
    }

    // add a value to the `Tau`
    fn add(&mut self, value: f64) {
        self.value += value;
        self.count += 1;
    }

    /// returns the count of samples at `Tau`
    pub fn count(self) -> u64 {
        self.count
    }

    // return the sum at `Tau`
    pub fn value(self) -> f64 {
        self.value
    }

    /// returns the Allan Variance at `Tau`
    pub fn variance(self) -> Option<f64> {
        if self.count == 0 {
            return None;
        }
        Some(self.value() / (2.0_f64 * self.count() as f64) / self.tau() as f64)
    }

    /// returns the Allan Deviation at `Tau`
    pub fn deviation(self) -> Option<f64> {
        if self.count == 0 {
            return None;
        }
        Some((self.value() / (2.0_f64 * self.count() as f64)).powf(0.5) / self.tau() as f64)
    }
}

/// describes the gaps between `Tau` and impacts space and computational costs
#[derive(Copy, Clone)]
pub enum Style {
    SingleTau(usize), // single specified Tau
    AllTau, // all Tau from 1 ... Tau (inclusive)
    Decade, // 1,10,100, ... Tau (inclusive)
    DecadeDeci, // 1, 2, 3, .., 9, 10, 20, 30, .. Tau (inclusive)
    Decade124, // 1, 2, 4, 10, 20, 40, ... Tau (inclusive)
    Decade1248, // 1, 2, 4, 8, 10, 20, 40, ... Tau (inclusive)
    Decade125, // 1, 2, 5, 10, 20, 50, ... Tau (inclusive)
}

/// used to configure an `Allan`
#[derive(Copy, Clone)]
pub struct Config {
    max_tau: usize,
    style: Style,
}

impl Default for Config {
    fn default() -> Config {
        Config {
            max_tau: 1_000,
            style: Style::DecadeDeci,
        }
    }
}

impl Config {
    pub fn new() -> Config {
        Default::default()
    }

    pub fn style(mut self, style: Style) -> Self {
        self.style = style;
        self
    }

    pub fn max_tau(mut self, max_tau: usize) -> Self {
        self.max_tau = max_tau;
        self
    }

    pub fn build(self) -> Option<Allan> {
        Allan::configured(self)
    }
}

impl Default for Allan {
    fn default() -> Allan {
        Config::default().build().unwrap()
    }
}

impl Allan {
    /// create a new Allan
    pub fn new() -> Allan {
        Default::default()
    }

    fn decade_tau(max: usize, steps: Vec<usize>) -> Vec<Tau> {
        let mut p = 0;
        let mut t = 1;
        let mut taus: Vec<Tau> = Vec::new();

        while t <= max {
            for i in &steps {
                t = i * 10_u32.pow(p) as usize;
                if t <= max {
                    taus.push(Tau::new(t));
                }
            }
            p += 1;
        }
        taus
    }

    pub fn configure() -> Config {
        Config::default()
    }

    fn configured(config: Config) -> Option<Allan> {
        let samples = config.max_tau * 2 + 1; // this will vary by type

        let buffer = VecDeque::with_capacity(samples as usize);

        let mut taus: Vec<Tau> = Vec::new();

        match config.style {
            Style::SingleTau(t) => {
                taus.push(Tau::new(t));
            }
            Style::AllTau => {
                for t in 1..(config.max_tau + 1) {
                    taus.push(Tau::new(t));
                }
            }
            Style::Decade125 => taus = Allan::decade_tau(config.max_tau, vec![1, 2, 5]),
            Style::Decade124 => taus = Allan::decade_tau(config.max_tau, vec![1, 2, 4]),
            Style::Decade1248 => taus = Allan::decade_tau(config.max_tau, vec![1, 2, 4, 8]),
            Style::DecadeDeci => {
                taus = Allan::decade_tau(config.max_tau, vec![1, 2, 3, 4, 5, 6, 7, 8, 9])
            }
            Style::Decade => taus = Allan::decade_tau(config.max_tau, vec![1]),
        }

        Some(Allan {
            buffer: buffer,
            config: config,
            samples: samples,
            taus: taus,
        })
    }

    /// add a record
    pub fn record(&mut self, value: f64) {
        self.buffer.push_front(value);
        self.calculate();
        if self.buffer.len() == self.samples {
            let _ = self.buffer.pop_back();
        }
    }

    // recalculate values
    fn calculate(&mut self) {
        for tau in &mut self.taus {
            let t = tau.tau() as usize;
            if (2 * t) < self.buffer.len() {
                let var: f64 = self.buffer[(2 * t)] - 2.0_f64 * self.buffer[t] + self.buffer[0];
                tau.add(var.powf(2.0_f64));
            }
        }
    }

    /// print deviations for all `Tau`
    pub fn print(&self) {
        for tau in &self.taus {
            if tau.count() >= 3 {
                println!("{} {}", tau.variance().unwrap_or(0.0), tau.tau());
            } else {
                println!("0.0 {}", tau.tau())
            }
        }
    }

    /// get a single `Tau` from the `Allan`
    pub fn get(&self, tau: usize) -> Option<Tau> {
        if tau > self.config.max_tau {
            return None;
        }
        for t in &self.taus {
            if t.tau() == tau {
                return Some(*t);
            }
        }
        None
    }
}