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
use Error;
use PI;
use ;
/// BPSK (Binary Phase-Shift Keying) modem/modem implementation.
///
/// In BPSK:
/// - Bit 0 is represented by a sine wave with phase 0.
/// - Bit 1 is represented by a sine wave with phase π (i.e. inverted sine wave).
// impl ModemTrait for BPSK {
// /// Encodes raw data into a BPSK modulated signal.
// ///
// /// Each bit in the input data is converted into a BPSK-modulated sine wave.
// fn modulate(&self, data: &[bool]) -> Result<Vec<f32>, Box<dyn Error>> {
// let mut signal = Vec::new();
// // Generate the corresponding BPSK wave for each bit
// for &bit in data {
// signal.extend(self.gen_wave(bit));
// }
// Ok(signal)
// }
// /// Decodes a BPSK modulated signal back into raw data.
// ///
// /// The signal is processed in chunks of `samples_per_bit` and a correlation with a reference
// /// sine wave (phase 0) is computed. A negative correlation implies a bit value of 1,
// /// while a positive correlation implies 0.
// fn demodulate(&self, samples: &[f32]) -> Result<Vec<bool>, Box<dyn Error>> {
// let mut decoded_data = Vec::new();
// let mut current_bits = Vec::new();
// // Process the signal chunk by chunk (each chunk represents one bit)
// for chunk in samples.chunks(self.samples_per_bit as usize) {
// let correlation = self.correlate(chunk);
// // Determine the bit: if correlation is negative, we treat it as bit 1.
// current_bits.push(correlation < 0.0);
// // Once we have 8 bits, convert them into a byte.
// if current_bits.len() == 8 {
// decoded_data.push(super::bits_to_byte(¤t_bits));
// current_bits.clear();
// }
// }
// Ok(decoded_data.iter().map(|&b| b != 0).collect())
// }
// // todo: Test this method
// fn samples_per_bit(&self) -> usize {
// self.samples_per_bit as usize
// }
// // todo: Test this method
// fn get_bit_metrics(&self, samples: &[f32]) -> ((f32, f32), (f32, f32)) {
// let mark_energy = self.correlate(samples);
// let space_energy = self.correlate(&samples.iter().map(|&s| -s).collect::<Vec<_>>());
// ((mark_energy, space_energy), (mark_energy, space_energy))
// }
// }