lib_jsl_electromagnetics/
signal_models.rs1pub mod covariance;
10pub mod distributed;
11pub mod point_source;
12
13pub use covariance::{Scene, sample_covariance, synthesize_snapshots};
14pub use distributed::{BrightnessScene, SkyPolarization};
15pub use point_source::PointSource;
16
17use crate::conventions::{C64, TimeConvention};
18use std::f64::consts::FRAC_1_SQRT_2;
19
20#[derive(Debug, Clone, Copy, PartialEq)]
23pub struct Coherency(pub [[C64; 2]; 2]);
24
25impl Coherency {
26 pub fn from_jones(p: [C64; 2]) -> Self {
28 let n = p[0].norm_sqr() + p[1].norm_sqr();
29 let p = p.map(|v| v / n.sqrt().max(1e-300));
30 Coherency([[p[0] * p[0].conj(), p[0] * p[1].conj()], [p[1] * p[0].conj(), p[1] * p[1].conj()]])
31 }
32
33 pub fn unpolarized() -> Self {
35 let h = C64::new(0.5, 0.0);
36 let z = C64::new(0.0, 0.0);
37 Coherency([[h, z], [z, h]])
38 }
39
40 pub fn theta() -> Self {
42 Self::from_jones([C64::new(1.0, 0.0), C64::new(0.0, 0.0)])
43 }
44
45 pub fn phi() -> Self {
47 Self::from_jones([C64::new(0.0, 0.0), C64::new(1.0, 0.0)])
48 }
49
50 pub fn rhcp_incoming(convention: TimeConvention) -> Self {
53 Self::from_jones([C64::new(FRAC_1_SQRT_2, 0.0), convention.unit() * FRAC_1_SQRT_2])
55 }
56
57 pub fn lhcp_incoming(convention: TimeConvention) -> Self {
59 Self::from_jones([C64::new(FRAC_1_SQRT_2, 0.0), -convention.unit() * FRAC_1_SQRT_2])
60 }
61
62 pub fn trace(&self) -> f64 {
63 self.0[0][0].re + self.0[1][1].re
64 }
65
66 pub fn degree_of_polarization(&self) -> f64 {
68 let det = (self.0[0][0] * self.0[1][1] - self.0[0][1] * self.0[1][0]).re;
69 (1.0 - 4.0 * det).max(0.0).sqrt()
70 }
71}
72
73#[cfg(test)]
74mod tests {
75 use super::*;
76
77 #[test]
78 fn coherency_states() {
79 assert!((Coherency::unpolarized().degree_of_polarization()).abs() < 1e-12);
80 assert!((Coherency::theta().degree_of_polarization() - 1.0).abs() < 1e-12);
81 let r = Coherency::rhcp_incoming(TimeConvention::PlusJ);
82 assert!((r.trace() - 1.0).abs() < 1e-12 && (r.degree_of_polarization() - 1.0).abs() < 1e-12);
83 let l = Coherency::lhcp_incoming(TimeConvention::PlusJ);
85 let tr: C64 = (0..2).flat_map(|i| (0..2).map(move |k| (i, k))).map(|(i, k)| r.0[i][k] * l.0[k][i]).sum();
86 assert!(tr.norm() < 1e-12);
87 assert!((Coherency::from_jones([C64::new(3.0, 0.0), C64::new(0.0, 4.0)]).trace() - 1.0).abs() < 1e-12);
88 }
89}