-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpreambles.rs
57 lines (48 loc) · 1.46 KB
/
preambles.rs
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
use crate::helper::chirp;
use crate::traits::{Sample, FP};
use super::{traits::FramePreamble, PreambleGen};
/// an chirp signal preamble sequence, the frequency goes up then down.
/// **NOTE** Due to lack of rustc features, the following parameters are not configurable.
pub struct ChirpUpDown {
samples: Vec<FP>,
norm: FP,
}
impl ChirpUpDown {
/// the lowest frequency
pub const FA: f32 = if cfg!(feature = "wired") { 2000.0 } else { 3000.0 };
/// the highest frequency
pub const FB: f32 = if cfg!(feature = "wired") { 12000.0 } else { 6000.0 };
/// number of samples
pub const N: usize = if cfg!(feature = "wired") { 80 } else { 440 };
/// the sampling frequency
pub const FS: usize = 48000;
pub fn new() -> ChirpUpDown {
let fa = FP::from_f32(ChirpUpDown::FA);
let fb = FP::from_f32(ChirpUpDown::FB);
let m = ChirpUpDown::N / 2;
let fs = ChirpUpDown::FS;
let samples: Vec<FP> = chirp(fa, fb, m, fs).chain(chirp(fb, fa, m, fs)).collect();
let norm = samples.iter().fold(FP::ZERO, |s, &x| s + x * x).sqrt();
Self { samples, norm }
}
}
impl Default for ChirpUpDown {
fn default() -> Self {
Self::new()
}
}
impl PreambleGen for ChirpUpDown {
const PREAMBLE_LEN: usize = ChirpUpDown::N;
fn samples(&self) -> FramePreamble {
self.samples.clone()
}
fn iter(&self) -> std::slice::Iter<FP> {
self.samples.iter()
}
fn norm(&self) -> FP {
self.norm
}
fn generate() -> Self {
Self::new()
}
}