Skip to main content

lib_jsl_interpolation/two_d/
lanczos.rs

1//! Lanczos resampling on a uniform grid.
2
3use super::{Interpolation2D, SeparableResampler, sinc, uniform_axis, validate_grid};
4use lib_jsl_core::*;
5
6/// Separable Lanczos-`a` interpolation: kernel `sinc(d) sinc(d / a)` for `|d| < a`, a windowed sinc
7/// that preserves constants (weights are renormalised) and passes through the grid samples.
8#[derive(Debug, Clone)]
9pub struct Lanczos2D {
10    x0: f64,
11    dx: f64,
12    y0: f64,
13    dy: f64,
14    a: usize,
15    z: R2D,
16}
17
18impl Lanczos2D {
19    /// `a` is the lobe count (2 or 3 are typical); both axes must be uniformly spaced.
20    pub fn new(x: VR1D, y: VR1D, z: VR2D, a: usize) -> Result<Self, ErrorsJSL> {
21        validate_grid(x, y, z, 2)?;
22        if a == 0 {
23            return Err(ErrorsJSL::InvalidInputRange("lobe count must be positive"));
24        }
25        let (x0, dx) = uniform_axis(x)?;
26        let (y0, dy) = uniform_axis(y)?;
27        Ok(Lanczos2D { x0, dx, y0, dy, a, z: z.to_owned() })
28    }
29}
30
31impl Interpolation2D for Lanczos2D {
32    fn interpolate_at(&self, x: f64, y: f64) -> Result<f64, ErrorsJSL> {
33        let a = self.a as f64;
34        let kernel = |d: f64| if d.abs() < a { sinc(d) * sinc(d / a) } else { 0.0 };
35        let r = SeparableResampler { x0: self.x0, dx: self.dx, y0: self.y0, dy: self.dy, support: self.a as isize, kernel: &kernel };
36        Ok(r.eval(&self.z, x, y))
37    }
38}
39
40#[cfg(test)]
41mod tests {
42    use super::*;
43
44    #[test]
45    fn interpolates_samples_and_preserves_constants() {
46        let x = R1D::from_iter((0..12).map(|k| 1.0 + 0.5 * k as f64));
47        let y = R1D::from_iter((0..10).map(|k| -2.0 + 0.25 * k as f64));
48        let z = R2D::from_shape_fn((12, 10), |(i, j)| ((i * 7 + j * 3) % 5) as f64);
49        let l = Lanczos2D::new(x.view(), y.view(), z.view(), 3).unwrap();
50        for i in 0..12 {
51            for j in 0..10 {
52                assert!((l.interpolate_at(x[i], y[j]).unwrap() - z[[i, j]]).abs() < 1e-12);
53            }
54        }
55        let c = Lanczos2D::new(x.view(), y.view(), R2D::from_elem((12, 10), 4.5).view(), 2).unwrap();
56        assert!((c.interpolate_at(3.17, -1.1).unwrap() - 4.5).abs() < 1e-12);
57    }
58
59    #[test]
60    fn accurate_on_a_smooth_oversampled_signal() {
61        let ax = R1D::from_iter((0..40).map(|k| k as f64 * 0.1));
62        let f = |a: f64, b: f64| (1.5 * a).sin() * (1.0 * b).cos();
63        let z = R2D::from_shape_fn((40, 40), |(i, j)| f(ax[i], ax[j]));
64        let l = Lanczos2D::new(ax.view(), ax.view(), z.view(), 3).unwrap();
65        for &(px, py) in &[(1.23, 2.34), (2.05, 1.77), (3.01, 0.55)] {
66            assert!((l.interpolate_at(px, py).unwrap() - f(px, py)).abs() < 2e-3, "{px},{py}");
67        }
68    }
69
70    #[test]
71    fn rejects_nonuniform_axes() {
72        let x = R1D::from_vec(vec![0.0, 1.0, 3.0]);
73        assert!(Lanczos2D::new(x.view(), x.view(), R2D::zeros((3, 3)).view(), 2).is_err());
74    }
75}