| 1 |
extern crate rand; |
| 2 |
|
| 3 |
use std::f32; |
| 4 |
|
| 5 |
use rand::{distributions::Uniform, prelude::Distribution, rngs::StdRng, SeedableRng}; |
| 6 |
|
| 7 |
pub fn fuzzy_cmp(a: f32, b: f32, tolerance: f32) -> bool { |
| 8 |
a >= b - tolerance && a <= b + tolerance |
| 9 |
} |
| 10 |
|
| 11 |
pub fn compare_float_vectors(expected: &[f32], observed: &[f32]) -> bool { |
| 12 |
assert_eq!(expected.len(), observed.len()); |
| 13 |
|
| 14 |
let tolerance: f32 = 0.001; |
| 15 |
|
| 16 |
for i in 0..expected.len() { |
| 17 |
if !fuzzy_cmp(observed[i], expected[i], tolerance) { |
| 18 |
return false; |
| 19 |
} |
| 20 |
} |
| 21 |
true |
| 22 |
} |
| 23 |
|
| 24 |
pub fn random_signal(length: usize) -> Vec<f32> { |
| 25 |
let mut sig = Vec::with_capacity(length); |
| 26 |
let normal_dist = Uniform::new(0.0, 10.0); |
| 27 |
|
| 28 |
let seed: [u8; 32] = [ |
| 29 |
1, 5, 6, 4, 1, 5, 3, 7, 4, 2, 6, 2, 6, 4, 5, 6, 7, 1, 5, 3, 7, 4, 2, 6, 2, 6, 1, 5, 3, 0, |
| 30 |
1, 0, |
| 31 |
]; |
| 32 |
let mut rng: StdRng = SeedableRng::from_seed(seed); |
| 33 |
|
| 34 |
for _ in 0..length { |
| 35 |
sig.push(normal_dist.sample(&mut rng) as f32); |
| 36 |
} |
| 37 |
return sig; |
| 38 |
} |