Skip to main content

lib_jsl_electromagnetics/algorithms/
cfm_beamformer.rs

1//! The conjugate field match (CFM) beamformer.
2//!
3//! See the book entry [Arrays, scenes and beamformers](https://jwkunz.github.io/lib_jsl/book/em-arrays/beamforming.html).
4
5use super::beamforming::normalize;
6use crate::arrays::SensorArray;
7use crate::conventions::C64;
8use crate::linalg::{check_hermitian, hermitian_eigen};
9use lib_jsl_core::ErrorsJSL;
10use ndarray::{Array1, Array2};
11
12/// The conjugate field match: the matched filter for a known harmonic array excitation `a`
13/// (the response the desired field produces across the elements), `w = a / ‖a‖`. With `y = wᴴ x`
14/// this conjugates the excitation's phases, adding the desired contributions coherently; it
15/// maximises the output for the desired field per unit weight norm and is the max-SNR solution in
16/// spatially white noise.
17pub fn cfm_weights(excitation: &Array1<C64>) -> Result<Array1<C64>, ErrorsJSL> {
18    normalize(excitation)
19}
20
21/// The CFM weights for a plane wave from `(θ, φ)` with Jones polarization `p`.
22pub fn cfm_for_direction(array: &SensorArray, theta: f64, phi: f64, polarization: [C64; 2]) -> Result<Array1<C64>, ErrorsJSL> {
23    cfm_weights(&array.steering_vector(theta, phi, polarization)?)
24}
25
26/// The CFM for a *distributed* desired field described by its covariance `R_d`: the weights
27/// maximising `wᴴ R_d w / wᴴ w`, i.e. the dominant eigenvector of `R_d`, returned with that
28/// maximum (the eigenvalue). For a single point source this reduces to [`cfm_weights`].
29pub fn cfm_beamformer(desired_covariance: &Array2<C64>) -> Result<(Array1<C64>, f64), ErrorsJSL> {
30    check_hermitian(desired_covariance, desired_covariance.nrows())?;
31    let (vals, vecs) = hermitian_eigen(desired_covariance)?;
32    Ok((normalize(&vecs.column(0).to_owned())?, vals[0]))
33}
34
35#[cfg(test)]
36mod tests {
37    use super::*;
38    use crate::algorithms::beamforming::{output_power, response};
39    use crate::antenna_models::Isotropic;
40    use crate::conventions::TimeConvention;
41    use crate::coordinates::transforms::RigidTransform;
42    use crate::signal_models::{Coherency, PointSource};
43    use lib_jsl_core::constants::em::C0;
44    use std::sync::Arc;
45
46    fn array() -> SensorArray {
47        let f = 1e9;
48        SensorArray::from_antenna(&SensorArray::linear_positions(8, 0.5 * C0 / f, [1.0, 0.0, 0.0]), Arc::new(Isotropic::theta_polarized()), &RigidTransform::identity(), f, TimeConvention::PlusJ, 1.0).unwrap()
49    }
50
51    #[test]
52    fn matched_filter_adds_the_desired_signal_coherently() {
53        let a = array();
54        let theta_pol = [C64::new(1.0, 0.0), C64::new(0.0, 0.0)];
55        let w = cfm_for_direction(&a, 1.2, 0.3, theta_pol).unwrap();
56        let s = a.steering_vector(1.2, 0.3, theta_pol).unwrap();
57        // |wᴴa| = ‖a‖ = √N for unit-gain elements: full coherent gain.
58        assert!((response(&w, &s).unwrap().norm() - 8f64.sqrt()).abs() < 1e-9);
59        assert!((w.iter().map(|v| v.norm_sqr()).sum::<f64>() - 1.0).abs() < 1e-12);
60        // Any other unit-norm weights collect less.
61        let other = cfm_for_direction(&a, 0.9, 0.3, theta_pol).unwrap();
62        assert!(response(&other, &s).unwrap().norm() < response(&w, &s).unwrap().norm());
63    }
64
65    #[test]
66    fn eigen_form_reduces_to_the_matched_filter_for_a_point_source() {
67        let a = array();
68        let src = PointSource::new(1.0, 0.0, 1e-12, Coherency::theta()).unwrap();
69        let rd = src.covariance(&a).unwrap();
70        let (w, lambda) = cfm_beamformer(&rd).unwrap();
71        let direct = cfm_for_direction(&a, 1.0, 0.0, [C64::new(1.0, 0.0), C64::new(0.0, 0.0)]).unwrap();
72        assert!(w.iter().zip(direct.iter()).all(|(x, y)| (x - y).norm() < 1e-9), "same up to the normalised phase");
73        assert!((output_power(&w, &rd).unwrap() - lambda).abs() < 1e-9 * lambda);
74        assert!(cfm_weights(&Array1::zeros(3)).is_err());
75    }
76}