Skip to main content

lib_jsl_dsp/discrete/transformations/
channel_transmitter.rs

1//! Transmitter front-end that resamples then frequency shifts.
2//!
3//! Processing order:
4//! 1) Arbitrary polyphase resample (`PolyphaseArbitraryResampler`)
5//! 2) Frequency shift (`FrequencyMixer`)
6//!
7//! The internal path is always complex and output is always complex.
8//! The input can be either real or complex, but will be converted to complex if it is real. 
9//! This allows the transmitter to handle both real and complex baseband inputs, while still providing a consistent complex output that can be directly fed into the channel model and receiver front-end.
10//!
11//! See the book entry [Channels and equalisation](https://jwkunz.github.io/lib_jsl/book/comms/equalisation.html#a-channel-in-software).
12
13use num_complex::Complex;
14
15use crate::{
16    dsp::discrete::{
17        resampling::polyphase_arbitrary_resampling::PolyphaseArbitraryResampler,
18        stream_operator::{StreamOperator, StreamOperatorManagement},
19        transformations::frequency_mixer::FrequencyMixer,
20    },
21    prelude::ErrorsJSL,
22};
23
24
25pub struct ChannelTransmitter {
26    resampler_complex: PolyphaseArbitraryResampler<Complex<f64>>,
27    mixer: FrequencyMixer,
28}
29
30impl ChannelTransmitter {
31    pub fn new(
32        frequency_shift_hz: f64,
33        sample_rate_hz: f64,
34        up_rate: usize,
35        down_rate: f64,
36        fir_taps: Option<&[f64]>,
37    ) -> Result<Self, ErrorsJSL> {
38        if !sample_rate_hz.is_finite() || sample_rate_hz <= 0.0 {
39            return Err(ErrorsJSL::InvalidInputRange(
40                "sample_rate_hz must be finite and > 0",
41            ));
42        }
43
44        Ok(Self {
45            resampler_complex: PolyphaseArbitraryResampler::new(up_rate, down_rate, fir_taps)?,
46            mixer: FrequencyMixer::new(frequency_shift_hz, sample_rate_hz, None),
47        })
48    }
49}
50
51impl StreamOperatorManagement for ChannelTransmitter {
52    fn reset(&mut self) -> Result<(), ErrorsJSL> {
53        self.resampler_complex.reset()?;
54        self.mixer.reset()?;
55        Ok(())
56    }
57
58    fn finalize(&mut self) -> Result<(), ErrorsJSL> {
59        self.resampler_complex.finalize()?;
60        self.mixer.finalize()?;
61        Ok(())
62    }
63}
64
65impl StreamOperator<f64, Complex<f64>> for ChannelTransmitter {
66    fn process(&mut self, data_in: &[f64]) -> Result<Option<Vec<Complex<f64>>>, ErrorsJSL> {
67        if data_in.is_empty() {
68            return Ok(None);
69        }
70        let x = data_in
71            .iter()
72            .map(|&v| Complex::new(v, 0.0))
73            .collect::<Vec<_>>();
74        let y = match self.resampler_complex.process(&x)? {
75            Some(v) => v,
76            None => return Ok(None),
77        };
78        self.mixer.process(&y)
79    }
80
81    fn flush(&mut self) -> Result<Option<Vec<Complex<f64>>>, ErrorsJSL> {
82        let y = match self.resampler_complex.flush()? {
83            Some(v) => v,
84            None => return Ok(None),
85        };
86        self.mixer.process(&y)
87    }
88}
89
90impl StreamOperator<Complex<f64>, Complex<f64>> for ChannelTransmitter {
91    fn process(&mut self, data_in: &[Complex<f64>]) -> Result<Option<Vec<Complex<f64>>>, ErrorsJSL> {
92        if data_in.is_empty() {
93            return Ok(None);
94        }
95        let y = match self.resampler_complex.process(data_in)? {
96            Some(v) => v,
97            None => return Ok(None),
98        };
99        self.mixer.process(&y)
100    }
101
102    fn flush(&mut self) -> Result<Option<Vec<Complex<f64>>>, ErrorsJSL> {
103        let y = match self.resampler_complex.flush()? {
104            Some(v) => v,
105            None => return Ok(None),
106        };
107        self.mixer.process(&y)
108    }
109}
110
111#[cfg(test)]
112mod tests {
113    use super::*;
114
115    #[test]
116    fn test_channel_transmitter_f64_to_complex() {
117        let taps = [1.0];
118        let mut dut = ChannelTransmitter::new(0.0, 48_000.0, 1, 2.0, Some(&taps)).unwrap();
119        let x = (0..8).map(|v| v as f64).collect::<Vec<_>>();
120        let mut y =
121            <ChannelTransmitter as StreamOperator<f64, Complex<f64>>>::process(&mut dut, &x)
122                .unwrap()
123                .unwrap_or_default();
124        y.extend(
125            <ChannelTransmitter as StreamOperator<f64, Complex<f64>>>::flush(&mut dut)
126                .unwrap()
127                .unwrap_or_default(),
128        );
129        let expected = vec![
130            Complex::new(0.0, 0.0),
131            Complex::new(2.0, 0.0),
132            Complex::new(4.0, 0.0),
133            Complex::new(6.0, 0.0),
134            Complex::new(0.0, 0.0),
135        ];
136        assert_eq!(y, expected);
137    }
138
139    #[test]
140    fn test_channel_transmitter_complex_to_complex() {
141        let taps = [1.0];
142        let mut dut = ChannelTransmitter::new(0.0, 48_000.0, 1, 2.0, Some(&taps)).unwrap();
143        let x = (0..8)
144            .map(|v| Complex::new(v as f64, -(v as f64)))
145            .collect::<Vec<_>>();
146        let mut y = <ChannelTransmitter as StreamOperator<Complex<f64>, Complex<f64>>>::process(
147            &mut dut, &x,
148        )
149        .unwrap()
150        .unwrap_or_default();
151        y.extend(
152            <ChannelTransmitter as StreamOperator<Complex<f64>, Complex<f64>>>::flush(&mut dut)
153                .unwrap()
154                .unwrap_or_default(),
155        );
156        let expected = vec![
157            Complex::new(0.0, -0.0),
158            Complex::new(2.0, -2.0),
159            Complex::new(4.0, -4.0),
160            Complex::new(6.0, -6.0),
161            Complex::new(0.0, 0.0),
162        ];
163        assert_eq!(y, expected);
164    }
165}
166