autd3_core/acoustics/directivity/
t4010a1.rs

1use super::*;
2
3#[allow(clippy::excessive_precision, clippy::unreadable_literal)]
4static DIR_COEF_A: &[f32] = &[
5    1.0,
6    1.0,
7    1.0,
8    0.891250938,
9    0.707945784,
10    0.501187234,
11    0.354813389,
12    0.251188643,
13    0.199526231,
14];
15
16#[allow(clippy::excessive_precision, clippy::unreadable_literal)]
17static DIR_COEF_B: &[f32] = &[
18    0.,
19    0.,
20    -0.00459648054721,
21    -0.0155520765675,
22    -0.0208114779827,
23    -0.0182211227016,
24    -0.0122437497109,
25    -0.00780345575475,
26    -0.00312857467007,
27];
28
29#[allow(clippy::excessive_precision, clippy::unreadable_literal)]
30static DIR_COEF_C: &[f32] = &[
31    0.,
32    0.,
33    -0.000787968093807,
34    -0.000307591508224,
35    -0.000218348633296,
36    0.00047738416141,
37    0.000120353137658,
38    0.000323676257958,
39    0.000143850511,
40];
41
42#[allow(clippy::excessive_precision, clippy::unreadable_literal)]
43static DIR_COEF_D: &[f32] = &[
44    0.,
45    0.,
46    1.60125528528e-05,
47    2.9747624976e-06,
48    2.31910931569e-05,
49    -1.1901034125e-05,
50    6.77743734332e-06,
51    -5.99548024824e-06,
52    -4.79372835035e-06,
53];
54
55/// [T4010A1/B4](https://www.nicera.co.jp/en/products/ultrasonic-sensor/open-aperture-type) directivity model.
56#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
57pub struct T4010A1 {}
58
59impl Directivity for T4010A1 {
60    fn directivity(theta: Angle) -> f32 {
61        let theta_deg = theta.degree().abs() % 180.0;
62        let theta_deg = 90.0 - (theta_deg - 90.0).abs();
63        let i = (theta_deg / 10.0).ceil() as usize;
64        if i == 0 {
65            1.0
66        } else {
67            let x = theta_deg - (i as f32 - 1.0) * 10.0;
68            ((DIR_COEF_D[i - 1] * x + DIR_COEF_C[i - 1]) * x + DIR_COEF_B[i - 1]) * x
69                + DIR_COEF_A[i - 1]
70        }
71    }
72}
73
74#[cfg(test)]
75mod tests {
76    use crate::defined::deg;
77
78    use super::*;
79
80    #[rstest::rstest]
81    #[test]
82    #[case::deg_0(1.0, 0.0)]
83    #[case::deg_10(1.0, 10.0)]
84    #[case::deg_20(1.0, 20.0)]
85    #[case::deg_30(0.891251, 30.0)]
86    #[case::deg_40(0.70794576, 40.0)]
87    #[case::deg_50(0.5011872, 50.0)]
88    #[case::deg_60(0.35481337, 60.0)]
89    #[case::deg_70(0.25118864, 70.0)]
90    #[case::deg_80(0.19952622, 80.0)]
91    #[case::deg_90(0.17783181, 90.0)]
92    #[case::deg_100(0.19952622, 100.0)]
93    fn test_directivity(#[case] expected: f32, #[case] theta_deg: f32) {
94        approx::assert_abs_diff_eq!(expected, T4010A1::directivity(theta_deg * deg));
95    }
96}