File: mod.rs

package info (click to toggle)
rust-rustdct 0.7.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 460 kB
  • sloc: makefile: 2
file content (41 lines) | stat: -rw-r--r-- 1,159 bytes parent folder | download | duplicates (6)
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
use rand::{distributions::Uniform, prelude::Distribution, rngs::StdRng, SeedableRng};
use rustdct::num_traits::{Float, FromPrimitive};

pub mod known_data;
pub mod reference_impls;

#[macro_use]
pub mod macros;

pub fn fuzzy_cmp<T: Float>(a: T, b: T, tolerance: T) -> bool {
    a >= b - tolerance && a <= b + tolerance
}

pub fn compare_float_vectors<T: Float + FromPrimitive>(expected: &[T], observed: &[T]) -> bool {
    assert_eq!(expected.len(), observed.len());

    let tolerance = T::from_f64(0.001).unwrap();

    for i in 0..expected.len() {
        if !fuzzy_cmp(observed[i], expected[i], tolerance) {
            return false;
        }
    }
    true
}

pub fn random_signal<T: Float + FromPrimitive>(length: usize) -> Vec<T> {
    let mut sig = Vec::with_capacity(length);
    let distribution = Uniform::new(0.0, 10.0);

    let seed: [u8; 32] = [
        1, 5, 6, 7, 1, 5, 3, 7, 4, 2, 6, 2, 6, 1, 5, 6, 7, 1, 5, 3, 7, 4, 2, 6, 2, 6, 1, 5, 1, 0,
        1, 7,
    ];
    let mut rng: StdRng = SeedableRng::from_seed(seed);

    for _ in 0..length {
        sig.push(T::from_f64(distribution.sample(&mut rng)).unwrap());
    }
    return sig;
}