lib_jsl_dsp/discrete/radar/
pulse_compression.rs1use crate::discrete::convolve::ConvolveMode;
11use crate::discrete::fft_convolution::fft_convolve;
12use lib_jsl_core::ErrorsJSL;
13use num_complex::Complex;
14use std::f64::consts::PI;
15
16type C64 = Complex<f64>;
17
18pub fn linear_chirp(bandwidth: f64, duration: f64, fs: f64) -> Result<Vec<C64>, ErrorsJSL> {
21 if !(bandwidth > 0.0 && duration > 0.0 && fs > 0.0) || bandwidth > fs {
22 return Err(ErrorsJSL::InvalidInputRange("Need positive bandwidth, duration and rate, with bandwidth <= fs."));
23 }
24 let n = (duration * fs).round() as usize;
25 if n == 0 {
26 return Err(ErrorsJSL::InvalidInputRange("The pulse is shorter than one sample."));
27 }
28 let rate = bandwidth / duration;
29 Ok((0..n).map(|i| { let t = i as f64 / fs - duration / 2.0; C64::from_polar(1.0, PI * rate * t * t) }).collect())
30}
31
32pub fn matched_filter(pulse: &[C64], weights: Option<&[f64]>) -> Result<Vec<C64>, ErrorsJSL> {
36 if let Some(w) = weights
37 && w.len() != pulse.len()
38 {
39 return Err(ErrorsJSL::IncompatibleArraySizes((w.len(), pulse.len())));
40 }
41 Ok((0..pulse.len()).rev().map(|i| pulse[i].conj() * weights.map_or(1.0, |w| w[i])).collect())
42}
43
44pub fn pulse_compress(received: &[C64], pulse: &[C64], weights: Option<&[f64]>) -> Result<Vec<C64>, ErrorsJSL> {
48 if pulse.is_empty() || received.is_empty() {
49 return Err(ErrorsJSL::InvalidInputRange("The pulse and the received signal must not be empty."));
50 }
51 let filter = matched_filter(pulse, weights)?;
53 let full = fft_convolve(received, &filter, ConvolveMode::Full)?;
54 let offset = pulse.len() - 1;
55 Ok((0..received.len()).map(|k| full[k + offset]).collect())
56 }
58
59pub fn peak_sidelobe_level_db(compressed: &[C64]) -> f64 {
62 let magnitude: Vec<f64> = compressed.iter().map(|c| c.norm()).collect();
63 let Some(peak) = (0..magnitude.len()).max_by(|&i, &j| magnitude[i].total_cmp(&magnitude[j])) else { return f64::NEG_INFINITY };
64 let mut right = peak;
65 while right + 1 < magnitude.len() && magnitude[right + 1] < magnitude[right] {
66 right += 1;
67 }
68 let mut left = peak;
69 while left > 0 && magnitude[left - 1] < magnitude[left] {
70 left -= 1;
71 }
72 let sidelobe = magnitude[..left].iter().chain(&magnitude[right + 1..]).fold(0.0f64, |m, &v| m.max(v));
73 20.0 * (sidelobe / magnitude[peak]).log10()
74}
75
76#[cfg(test)]
77mod tests {
78 use super::*;
79 use crate::discrete::windows::{WindowType, generate_window};
80
81 fn echo_scene(pulse: &[C64], delays: &[(usize, f64)], length: usize) -> Vec<C64> {
82 let mut r = vec![C64::new(0.0, 0.0); length];
83 for &(d, a) in delays {
84 r[d..d + pulse.len()].iter_mut().zip(pulse).for_each(|(v, p)| *v += a * p);
85 }
86 r
87 }
88
89 #[test]
90 fn resolves_close_targets_with_the_expected_gain() {
91 let (b, t, fs) = (10e6, 20e-6, 40e6);
93 let pulse = linear_chirp(b, t, fs).unwrap();
94 assert_eq!(pulse.len(), 800);
95 let single = pulse_compress(&echo_scene(&pulse, &[(300, 1.0)], 2000), &pulse, None).unwrap();
98 assert!((single[300].norm() - 800.0).abs() < 1e-6);
99 let r = echo_scene(&pulse, &[(300, 1.0), (320, 0.5)], 2000);
100 let y = pulse_compress(&r, &pulse, None).unwrap();
101 let magnitude: Vec<f64> = y.iter().map(|v| v.norm()).collect();
102 assert!((magnitude[300] / 800.0 - 1.0).abs() < 0.03 && (magnitude[320] / 400.0 - 1.0).abs() < 0.06, "{} {}", magnitude[300], magnitude[320]);
104 let gap = magnitude[302..319].iter().fold(f64::INFINITY, |m, &v| m.min(v));
105 assert!(gap < 0.25 * magnitude[320], "{gap}");
106 let half_power = magnitude[300] / 2f64.sqrt();
108 let width = magnitude[290..311].iter().filter(|&&m| m >= half_power).count();
109 assert!((3..=5).contains(&width), "{width}");
110 }
111
112 #[test]
113 fn weighting_trades_sidelobes_for_resolution_and_validates() {
114 let pulse = linear_chirp(5e6, 40e-6, 20e6).unwrap();
115 let r = echo_scene(&pulse, &[(1000, 1.0)], 3000);
116 let plain = peak_sidelobe_level_db(&pulse_compress(&r, &pulse, None).unwrap());
117 assert!((plain + 13.3).abs() < 0.8, "{plain}");
119 let window = generate_window(WindowType::Hamming, pulse.len(), true);
120 let weighted = peak_sidelobe_level_db(&pulse_compress(&r, &pulse, Some(&window)).unwrap());
121 assert!(weighted < -35.0, "{weighted}");
122 assert!(linear_chirp(5e6, 1e-6, 1e6).is_err() && matched_filter(&pulse, Some(&[1.0])).is_err());
123 assert!(pulse_compress(&r, &[], None).is_err());
124 }
125}