Skip to main content

lib_jsl_electromagnetics/algorithms/
lcmv_beamformer.rs

1//! Linearly constrained minimum variance (LCMV) and MVDR beamformers.
2//!
3//! See the book entry [Arrays, scenes and beamformers](https://jwkunz.github.io/lib_jsl/book/em-arrays/beamforming.html).
4
5use crate::conventions::C64;
6use crate::linalg::{check_hermitian, hermitian_transpose};
7use lib_jsl_core::ErrorsJSL;
8use ndarray::{Array1, Array2};
9use lib_jsl_linalg::{Factorize, Solve};
10
11/// The weights minimising the output power `wᴴ R w` subject to the linear constraints
12/// `wᴴ c_k = f_k`: the beamformer's response to each constraint vector `c_k` (a column of `C`,
13/// e.g. a steering vector) is the prescribed `f_k` (1 for a direction to pass undistorted, 0 for
14/// one to null). Equivalently `Cᴴ w = f*`, giving
15///
16/// `w = R⁻¹ C (Cᴴ R⁻¹ C)⁻¹ f*`.
17///
18/// `R` must be Hermitian positive definite and the constraints linearly independent (at most `N`).
19pub fn lcmv_beamformer(covariance: &Array2<C64>, constraints: &Array2<C64>, response: &Array1<C64>) -> Result<Array1<C64>, ErrorsJSL> {
20    let n = covariance.nrows();
21    check_hermitian(covariance, n)?;
22    let m = constraints.ncols();
23    if constraints.nrows() != n || m == 0 || m > n || response.len() != m {
24        return Err(ErrorsJSL::InvalidInputRange("Need an N×M constraint matrix with 1 <= M <= N and M responses."));
25    }
26    // ANCHOR: lcmv
27    let lu = covariance.factorize().map_err(|_| ErrorsJSL::InvalidInputRange("The covariance is singular."))?;
28    let mut rinv_c = Array2::<C64>::zeros((n, m));
29    for k in 0..m {
30        let col = lu.solve(&constraints.column(k).to_owned()).map_err(|_| ErrorsJSL::InvalidInputRange("The covariance is singular."))?;
31        rinv_c.column_mut(k).assign(&col);
32    }
33    let gram = hermitian_transpose(constraints).dot(&rinv_c);
34    let g = gram.solve(&response.mapv(|v| v.conj())).map_err(|_| ErrorsJSL::InvalidInputRange("The constraints are linearly dependent."))?;
35    Ok(rinv_c.dot(&g))
36    // ANCHOR_END: lcmv
37}
38
39/// The minimum-variance distortionless response beamformer: LCMV with the single constraint
40/// `wᴴ a = 1`, i.e. `w = R⁻¹ a / (aᴴ R⁻¹ a)`. Its output power is `1 / (aᴴ R⁻¹ a)` (the Capon
41/// spectrum).
42pub fn mvdr_beamformer(covariance: &Array2<C64>, steering: &Array1<C64>) -> Result<Array1<C64>, ErrorsJSL> {
43    let c = steering.clone().insert_axis(ndarray::Axis(1));
44    lcmv_beamformer(covariance, &c, &Array1::from_elem(1, C64::new(1.0, 0.0)))
45}
46
47#[cfg(test)]
48mod tests {
49    use super::*;
50    use crate::algorithms::beamforming::{output_power, response, sinr};
51    use crate::algorithms::max_snr_beamformer::max_snr_beamformer;
52    use crate::antenna_models::Isotropic;
53    use crate::arrays::SensorArray;
54    use crate::conventions::TimeConvention;
55    use crate::coordinates::transforms::RigidTransform;
56    use crate::linalg::quadratic_form;
57    use crate::signal_models::{Coherency, PointSource, Scene};
58    use lib_jsl_core::constants::em::C0;
59    use std::sync::Arc;
60
61    const POL: [C64; 2] = [C64 { re: 1.0, im: 0.0 }, C64 { re: 0.0, im: 0.0 }];
62
63    fn array() -> SensorArray {
64        let f = 1e9;
65        SensorArray::from_antenna(&SensorArray::linear_positions(10, 0.5 * C0 / f, [1.0, 0.0, 0.0]), Arc::new(Isotropic::theta_polarized()), &RigidTransform::identity(), f, TimeConvention::PlusJ, 1.0).unwrap()
66    }
67
68    #[test]
69    fn mvdr_is_distortionless_and_nulls_a_jammer() {
70        let a = array();
71        // A jammer about 40 dB above the receiver noise.
72        let scene = Scene::new(1e6).unwrap().with_point_source(PointSource::new(0.8, 0.0, 1e-8, Coherency::theta()).unwrap()).with_receiver_temperature(300.0);
73        let r = scene.covariance(&a).unwrap();
74        let look = a.steering_vector(1.5, 0.0, POL).unwrap();
75        let w = mvdr_beamformer(&r, &look).unwrap();
76        assert!((response(&w, &look).unwrap() - C64::new(1.0, 0.0)).norm() < 1e-9);
77        let jam = a.steering_vector(0.8, 0.0, POL).unwrap();
78        assert!(response(&w, &jam).unwrap().norm() < 1e-4, "{}", response(&w, &jam).unwrap().norm());
79        // Output power equals the Capon value 1 / (aᴴ R⁻¹ a), and no distortionless weights do better.
80        let rinv_a = r.solve(&look).unwrap();
81        let capon = 1.0 / look.iter().zip(rinv_a.iter()).map(|(x, y)| (x.conj() * y).re).sum::<f64>();
82        assert!((output_power(&w, &r).unwrap() - capon).abs() < 1e-9 * capon);
83        let naive = look.mapv(|v| v / 10.0); // the conventional beamformer, also distortionless
84        assert!(quadratic_form(&naive, &r) > 10.0 * capon);
85    }
86
87    #[test]
88    fn lcmv_honours_every_constraint_and_matches_max_snr_for_one_constraint() {
89        let a = array();
90        let scene = Scene::new(1e6).unwrap().with_point_source(PointSource::new(1.0, 0.0, 1e-13, Coherency::theta()).unwrap()).with_receiver_temperature(300.0);
91        let r = scene.covariance(&a).unwrap();
92        let dirs = [1.5, 2.0, 0.6];
93        let c = Array2::from_shape_fn((10, 3), |(i, k)| a.steering_vector(dirs[k], 0.0, POL).unwrap()[i]);
94        let f = Array1::from(vec![C64::new(1.0, 0.0), C64::new(0.0, 0.0), C64::new(0.5, -0.5)]);
95        let w = lcmv_beamformer(&r, &c, &f).unwrap();
96        for k in 0..3 {
97            assert!((response(&w, &c.column(k).to_owned()).unwrap() - f[k]).norm() < 1e-9, "constraint {k}");
98        }
99        // MVDR on the interference-plus-noise covariance maximises SINR for a point target.
100        let target = a.steering_vector(1.5, 0.0, POL).unwrap();
101        let rs = PointSource::new(1.5, 0.0, 1e-16, Coherency::theta()).unwrap().covariance(&a).unwrap();
102        let w_mvdr = mvdr_beamformer(&r, &target).unwrap();
103        let (_, best) = max_snr_beamformer(&rs, &r).unwrap();
104        assert!((sinr(&w_mvdr, &rs, &r).unwrap() - best).abs() < 1e-6 * best);
105        // Validation.
106        assert!(lcmv_beamformer(&r, &Array2::zeros((10, 0)), &Array1::zeros(0)).is_err());
107        assert!(lcmv_beamformer(&r, &c, &Array1::zeros(2)).is_err());
108        let dup = Array2::from_shape_fn((10, 2), |(i, _)| target[i]);
109        assert!(lcmv_beamformer(&r, &dup, &Array1::from_elem(2, C64::new(1.0, 0.0))).is_err());
110    }
111}