lib_jsl_dsp/discrete/sources/
square.rs1use super::nco::{threshold_from_duty, threshold_from_ratio, Nco, NcoSource, Phase, Waveform};
6use lib_jsl_core::ErrorsJSL;
7
8#[derive(Clone, Debug)]
11pub struct SquareWave {
12 threshold: u128,
13}
14
15impl SquareWave {
16 pub fn new(duty: f64) -> Result<SquareWave, ErrorsJSL> {
18 Ok(SquareWave { threshold: threshold_from_duty(duty)? })
19 }
20}
21
22impl Waveform for SquareWave {
23 type Output = f64;
24
25 fn sample(&mut self, phase: Phase) -> f64 {
26 if (phase.0 as u128) < self.threshold { 1.0 } else { -1.0 }
27 }
28}
29
30impl NcoSource<SquareWave> {
31 pub fn square(frequency_hz: f64, sample_rate: f64) -> Result<Self, ErrorsJSL> {
33 Ok(NcoSource::from_parts(Nco::new(frequency_hz, sample_rate)?, SquareWave::new(0.5)?))
34 }
35
36 pub fn square_with_duty(frequency_hz: f64, sample_rate: f64, duty: f64) -> Result<Self, ErrorsJSL> {
38 Ok(NcoSource::from_parts(Nco::new(frequency_hz, sample_rate)?, SquareWave::new(duty)?))
39 }
40
41 pub fn square_with_period(period: u64, high: u64) -> Result<Self, ErrorsJSL> {
43 let wave = SquareWave { threshold: threshold_from_ratio(high, period)? };
44 Ok(NcoSource::from_parts(Nco::from_period(period)?, wave))
45 }
46}
47
48pub fn square(period: usize, length: usize) -> Result<Vec<f64>, ErrorsJSL> {
51 let high = (period as u64).div_ceil(2);
52 Ok(NcoSource::square_with_period(period as u64, high)?.generate(length))
53}
54
55#[cfg(test)]
56mod tests {
57 use super::*;
58
59 #[test]
60 fn exact_patterns() {
61 assert_eq!(square(4, 8).unwrap(), vec![1.0, 1.0, -1.0, -1.0, 1.0, 1.0, -1.0, -1.0]);
62 assert_eq!(square(6, 12).unwrap(), [1.0, 1.0, 1.0, -1.0, -1.0, -1.0].repeat(2));
63 assert_eq!(square(5, 10).unwrap(), [1.0, 1.0, 1.0, -1.0, -1.0].repeat(2));
64 assert_eq!(square(2, 4).unwrap(), vec![1.0, -1.0, 1.0, -1.0]);
65 assert!(square(1, 4).is_err());
66 }
67
68 #[test]
69 fn every_period_and_high_count_is_exact_for_many_cycles() {
70 for period in 2..60u64 {
71 for high in 0..=period {
72 let mut s = NcoSource::square_with_period(period, high).unwrap();
73 for cycle in 0..20 {
74 for n in 0..period {
75 let want = if n < high { 1.0 } else { -1.0 };
76 assert_eq!(s.next_sample(), want, "period {period} high {high} cycle {cycle} n {n}");
77 }
78 }
79 }
80 }
81 }
82
83 #[test]
84 fn frequency_form_and_duty_cycle() {
85 let mut s = NcoSource::square(1.0, 8.0).unwrap();
86 assert_eq!(s.generate(8), vec![1.0, 1.0, 1.0, 1.0, -1.0, -1.0, -1.0, -1.0]);
87 let mut d = NcoSource::square_with_duty(1.0, 10.0, 0.3).unwrap();
88 let x = d.generate(10);
89 assert_eq!(x.iter().filter(|&&v| v > 0.0).count(), 3);
90 assert!(NcoSource::square_with_duty(1.0, 10.0, 1.2).is_err());
91 assert!(NcoSource::square_with_period(4, 5).is_err());
92 }
93
94 #[test]
95 fn mean_follows_the_duty_cycle() {
96 let mut s = NcoSource::square_with_duty(7.0, 1000.0, 0.25).unwrap();
97 let x = s.generate(10_000);
98 let mean = x.iter().sum::<f64>() / x.len() as f64;
99 assert!((mean - (2.0 * 0.25 - 1.0)).abs() < 0.01, "mean {mean}");
100 }
101}