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
use num_complex::{Complex, Complex64};
use rand;
use rand::distributions::{Normal, Distribution};
use std::f64;
use std::f64::consts::PI;
use crate::signals::Signal;
pub struct SignalGen<F>
where
F: Fn(f64) -> Complex64,
{
gen: F,
}
impl<F> SignalGen<F>
where
F: Fn(f64) -> Complex64,
{
pub fn new(f: F) -> SignalGen<F> {
SignalGen { gen: f }
}
pub fn generate(&self, points: Vec<f64>) -> Signal {
let data = points.iter().map(|&i| (self.gen)(i)).collect();
Signal::new(data)
}
}
pub fn impulse() -> SignalGen<impl Fn(f64) -> Complex64> {
SignalGen::new(|i| {
if i == 0. {
Complex::new(1., 0.)
} else {
Complex::new(0., 0.)
}
})
}
pub fn step() -> SignalGen<impl Fn(f64) -> Complex64> {
SignalGen::new(|i| {
if i >= 0. {
Complex::new(1., 0.)
} else {
Complex::new(0., 0.)
}
})
}
pub fn complex(freq: f64, offset: f64) -> SignalGen<impl Fn(f64) -> Complex64> {
let w = 2.0 * PI * freq;
SignalGen::new(move |i| Complex::new(0., w * (i + offset / 2.)).exp())
}
pub fn sine(freq: f64, offset: f64) -> SignalGen<impl Fn(f64) -> Complex64> {
let w = 2.0 * PI * freq;
SignalGen::new(move |i| Complex::new(f64::sin(w * (i + offset / 2.)), 0.))
}
pub fn cosine(freq: f64, offset: f64) -> SignalGen<impl Fn(f64) -> Complex64> {
let w = 2.0 * PI * freq;
SignalGen::new(move |i| Complex::new(f64::cos(w * (i + offset / 2.)), 0.))
}
pub fn triangle(freq: f64) -> SignalGen<impl Fn(f64) -> Complex64> {
let w = 2.0 * freq;
SignalGen::new(move |i| Complex::new((w * (i + 0.5)) % 2. - 1., 0.))
}
pub fn square(freq: f64) -> SignalGen<impl Fn(f64) -> Complex64> {
let w = freq;
SignalGen::new(move |i| {
let a = w * i % 1.;
let b = if a < -0.5 || (a > 0.0 && a < 0.5) {
1.0
} else {
-1.0
};
Complex::new(b, 0.)
})
}
pub fn chirp(start_freq: f64, end_freq: f64, time: f64) -> SignalGen<impl Fn(f64) -> Complex64> {
let slope = (end_freq - start_freq) / time;
SignalGen::new(move |i| {
if i < 0. || i > time {
Complex::new(0., 0.)
} else {
let f = slope * i + start_freq;
let w = 2.0 * PI * f * i;
Complex::new(0., w).exp()
}
})
}
pub fn noise(std: f64) -> SignalGen<impl Fn(f64) -> Complex64> {
let normal = Normal::new(0.0, std);
SignalGen::new(move |_| {
Complex::new(normal.sample(&mut rand::thread_rng()), 0.0)
})
}
#[cfg(test)]
mod tests {
use super::*;
use num_complex::Complex;
#[test]
fn test_impulse() {
let signal = impulse().generate(vec![-4.0, 0.0, 42.0]);
assert_eq!(signal.get(0), Complex::new(0., 0.));
assert_eq!(signal.get(1), Complex::new(1., 0.));
assert_eq!(signal.get(2), Complex::new(0., 0.));
}
#[test]
fn test_step() {
let signal = step().generate(vec![-4.0, 0.0, 42.0]);
assert_eq!(signal.get(0), Complex::new(0., 0.));
assert_eq!(signal.get(1), Complex::new(1., 0.));
assert_eq!(signal.get(2), Complex::new(1., 0.));
}
}