Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

ODC-1.1 — FIR filters (Lanczos + Kaiser) and along-track wavelength bandpass

Source survey item: ocean-data-challenges-survey.md §1.1 Status: proposed Maps to upstream: mod_filter.py from 2024_DC_SSH_mapping_SWOT_OSE (and equivalents in the 2023 / 2024c repos).


1. Motivation

The ocean-data-challenges evaluation pipeline filters along-track satellite altimetry (SLA / SSH) before scoring against gridded reconstructions. Two operations recur in every notebook:

  1. Low-pass an along-track segment to remove wavelengths shorter than λmin (instrument noise, sub-mesoscale variability the gridded product cannot resolve).

  2. Band-pass an along-track segment to (λmin, λmax) — keeping mesoscale energy while suppressing both the short-wavelength noise and the large-scale geoid/MDT residual.

The community reaches for Lanczos (windowed-sinc) FIR filters because:

We currently expose only Butterworth IIR (xrtoolz.interpolate.lowpass_filter). That covers gridded data well but is the wrong default for along-track SLA: gentle roll-off, opaque cutoff units, and IIR phase compensation relies on sosfiltfilt’s reverse-pass which is sensitive to short segments.

This proposal adds the FIR family alongside Butterworth — Lanczos as the community standard, Kaiser as the tunable engineering default — plus a domain-aware wrapper that takes cutoffs in kilometres rather than normalized Nyquist.

2. User stories

2.1 Single-segment along-track bandpass (primary)

As an SSH-mapping researcher, I have a DUACS L4 reconstruction and a SWOT along-track product. I want to bandpass-filter the SWOT SLA to (65 km, 500 km) before colocating my reconstruction onto it, so that the residual I score is in the mesoscale band the reconstruction claims to resolve.

import xarray as xr
from xrtoolz.geo import bandpass_wavelength

ds_track = xr.open_dataset("swot_along_track.nc")
ds_band  = bandpass_wavelength(
    ds_track,
    dim="num_lines",
    lambda_min_km=65.0,
    lambda_max_km=500.0,
    method="lanczos",
)

spacing_km is auto-derived from ds_track.longitude / ds_track.latitude via geodesic distance (pyproj.Geod, ellipsoidal — more accurate than haversine).

2.2 Tunable engineering default

I’m not in the SSH-mapping community and I just want a clean linear-phase low-pass with a 60 dB stop-band on a regular time series. I don’t want to hand-pick FIR coefficients.

from xrtoolz.interpolate import fir_filter

ds_smooth = fir_filter(
    ds, dim="time",
    cutoff=0.05,          # normalized Nyquist
    method="kaiser",
    attenuation_db=60.0,  # auto-picks β and tap count
)

2.3 Reproducible Lanczos for community comparability

I’m reproducing the 2024 SSH-mapping-SWOT-OSE leaderboard. I need a Lanczos low-pass with cutoff at λ = 65 km on a 7 km-spaced track and I need the result to match the reference notebooks bit-for-bit modulo floating-point.

ds_lp = bandpass_wavelength(
    ds, dim="num_lines",
    lambda_min_km=65.0, lambda_max_km=None,   # low-pass only
    method="lanczos", num_taps=129,
)

2.4 As a Layer-1 Operator inside a Sequential

I’m building a reusable along-track scoring pipeline and want the bandpass step to be a configurable, serializable Operator I can drop into a Sequential.

from xrtoolz.geo import BandpassWavelength
from xrtoolz.core import Sequential

pipeline = Sequential([
    BandpassWavelength(
        dim="num_lines", var="ssha",
        lambda_min_km=65.0, lambda_max_km=500.0,
        method="lanczos",
    ),
    AlongTrackColocate(...),     # ODC-1.2, future
    SegmentedPSDScore(...),      # ODC-1.3, future
])

3. What we already have / what’s missing

CapabilityCurrent stateThis proposal
Butterworth IIR low/high/band/stop_smooth_kernels.lowpass_filterUnchanged
Generic IIR family (Cheby, Ellip, Bessel)—Out of scope
FIR Lanczos—Add
FIR Kaiser (tunable atten ↔ taps)—Add
FIR application machinery—Use scipy.signal.filtfilt with FIR taps
Cutoff in normalized NyquistYesYes, for fir_filter
Cutoff in wavelength (km)—Add via bandpass_wavelength
Median along-track spacing_haversine_km private helperAdd median_dx_km (uses pyproj.Geod, public)
Operator wrapper—Add BandpassWavelength Layer-1 operator

4. Design

4.1 Tap design (single helper, two windows)

# src/xrtoolz/interpolate/_src/_smooth_kernels.py
def _fir_taps(
    *,
    cutoff: float | tuple[float, float],
    method: str,                    # "lanczos" | "kaiser"
    btype: str,                     # "low" | "high" | "bandpass" | "bandstop"
    num_taps: int | None = None,    # FIR length, must be odd
    attenuation_db: float | None = None,  # Kaiser only
) -> NDArray[np.floating]:
    """Design symmetric FIR taps via windowed-sinc."""

Lanczos taps (low-pass case):

M = (num_taps - 1) // 2
n = np.arange(-M, M + 1)
taps = np.sinc(2 * cutoff * n) * np.sinc(n / M)
taps /= taps.sum()

Band-pass = low-pass(fhi) − low-pass(flo); high-pass = identity − low-pass. Same construction for Kaiser, swap the window for scipy.signal.kaiser(num_taps, beta) where β is derived from attenuation_db via scipy.signal.kaiser_beta. Tap-count auto-pick uses scipy.signal.kaiserord(attenuation_db, transition_width) when num_taps is None for Kaiser; for Lanczos the default is 2 * ceil(2 / cutoff) + 1.

4.2 Private numpy/scipy kernel

# src/xrtoolz/interpolate/_src/_smooth_kernels.py
def fir_filter(
    arr: ArrayLike, *,
    axis: int = -1,
    cutoff: float | Sequence[float],
    method: Literal["lanczos", "kaiser"] = "lanczos",
    btype: str = "low",                   # "low" | "high" | "bandpass" | "bandstop"
    num_taps: int | None = None,
    attenuation_db: float | None = None,  # Kaiser-only
) -> NDArray[np.floating]:
    """Zero-phase FIR filter via symmetric windowed-sinc taps."""

Implementation: _fir_taps(...) → scipy.signal.filtfilt(taps, [1.0], arr, axis=axis, padtype='odd', method='gust'). The 'gust' method handles short segments better than the default. Complex inputs filtered component-wise (existing _as_floating helper).

4.3 Layer 0 — xarray wrapper

# src/xrtoolz/interpolate/_src/smooth.py
def fir_filter(
    ds: xr.Dataset, *,
    dim: str,
    cutoff: float | Sequence[float],
    method: str = "lanczos",
    btype: str = "low",
    num_taps: int | None = None,
    attenuation_db: float | None = None,
) -> xr.Dataset

Mirrors existing lowpass_filter exactly — same _apply_along_dim plumbing, same Dataset-pass-through semantics for non-numeric / no-dim variables.

4.4 Domain-aware bandpass

# src/xrtoolz/geo/_src/along_track.py — new module
def median_dx_km(lon: ArrayLike, lat: ArrayLike) -> float:
    """Median geodesic spacing between consecutive points (km, WGS-84).

    Uses pyproj.Geod(ellps='WGS84').line_lengths.
    """

def bandpass_wavelength(
    ds: xr.Dataset, *,
    dim: str,
    lambda_min_km: float | None,    # high-pass edge; None → no high-pass
    lambda_max_km: float | None,    # low-pass edge;  None → no low-pass
    spacing_km: float | None = None,
    method: str = "lanczos",
    num_taps: int | None = None,
    attenuation_db: float | None = None,
    lon: str = "longitude",
    lat: str = "latitude",
) -> xr.Dataset:
    """Bandpass along ``dim`` with cutoffs in wavelength (km)."""

Internals:

  1. If spacing_km is None, derive from ds[lon] / ds[lat] via median_dx_km.

  2. Translate λ → cutoff_norm = 2 * spacing_km / λ_km.

  3. Validate 0 < cutoff_norm < 1; raise with a helpful message if a wavelength is below 2 × spacing_km (Nyquist).

  4. Resolve btype from which of (λmin, λmax) are set — at least one required:

    λminλmaxbtypecutoff
    Noneset"low"2·dx/λ_max
    setNone"high"2·dx/λ_min
    setset"bandpass"(2·dx/λ_max, 2·dx/λ_min)
  5. Delegate to fir_filter.

4.5 Layer-1 Operator

# src/xrtoolz/geo/operators.py
class BandpassWavelength(Operator):
    """Apply a wavelength-domain bandpass to a single variable."""

    def __init__(
        self, *, dim, lambda_min_km=None, lambda_max_km=None,
        spacing_km=None, method="lanczos", num_taps=None,
        attenuation_db=None, lon="longitude", lat="latitude",
    ): ...

    def __call__(self, ds: xr.Dataset) -> xr.Dataset: ...
    def get_config(self) -> dict[str, Any]: ...
    def __repr__(self) -> str: ...

Standard split-config / __call__ shape; nothing stateful.

5. Library leverage

NeedLibrary callNotes
FIR design (Kaiser tap count)scipy.signal.kaiserord, kaiser_beta, windows.kaiserβ-from-attenuation built in
Tap application (zero-phase)scipy.signal.filtfilt(taps, [1.0], …, method='gust')Robust on short segments
Sincnumpy.sincNormalized (π·x) variant — matches our derivation
Geodesic distancepyproj.Geod(ellps='WGS84').line_lengthsAlready a dependency. More accurate than haversine; vectorised; consecutive-only (no quadratic blow-up).
Lanczos window—1-line: np.sinc(n / M). No scipy primitive.
Existing IIRscipy.signal.butter, sosfiltfiltUntouched

No new top-level dependencies.

6. API summary (public surface)

# Private kernels (numpy/scipy)
xrtoolz.interpolate._src._smooth_kernels.fir_filter(arr, *, axis, cutoff, method, btype,
                                      num_taps, attenuation_db)

# Layer 0 — xarray
xrtoolz.interpolate.fir_filter(ds, *, dim, cutoff, method, btype,
                                num_taps, attenuation_db)

# Domain-aware (geo)
xrtoolz.geo.median_dx_km(lon, lat)
xrtoolz.geo.bandpass_wavelength(ds, *, dim, lambda_min_km, lambda_max_km,
                                 spacing_km, method, num_taps,
                                 attenuation_db, lon, lat)

# Operator
xrtoolz.geo.BandpassWavelength(...)

Existing lowpass_filter (Butterworth) is unchanged. fir_filter is the new sibling for FIR methods.

7. Tests

TestAsserts
Lanczos low-pass on a known sum-of-sinesOut-of-band amplitude < 1% of in-band
Kaiser low-pass with attenuation_db=60Stop-band peak ≤ −60 dB (FFT of impulse response)
Tap count auto-picknum_taps is odd; passes through _fir_taps without error
fir_filter zero-phasePhase response (FFT) is purely real within tol
bandpass_wavelength round-tripCutoff translation matches manual 2·dx/λ
bandpass_wavelength Nyquist guardRaises ValueError for λ < 2·dx
bandpass_wavelength lambda-boundsAt least one of (min, max) required; min < max
median_dx_km against known segmentWithin 0.1% of pyproj reference
Operator round-trip via get_configReconstructed operator produces identical output
Dataset pass-throughNon-numeric / no-dim variables untouched

Target: ~12 new test cases.

8. Out of scope

9. Effort

≈100 LOC implementation + ≈100 LOC tests + docstring examples. Single PR.

SliceLOCNotes
_fir_taps + fir_filter (private kernel)50scipy does the heavy lifting
fir_filter (Layer 0 xarray wrapper)15Reuse _apply_along_dim
median_dx_km + bandpass_wavelength35New geo/_src/along_track.py
BandpassWavelength operator20Standard pattern
Tests~10012 cases
Docs / re-exports10__init__.py updates, validation docs page

10. Risks / open questions

  1. Default num_taps for Lanczos. The 2 * ceil(2 / cutoff) + 1 heuristic gives ~2 main-lobe widths of taps, which matches the community implementations but is conservative. We should document explicitly and let users override.

  2. filtfilt method='gust' vs 'pad'. 'gust' is more robust on short segments but slower. Pick 'pad' as the default to match scipy and document method as a future parameter only if needed.

  3. bandpass_wavelength longitude/latitude lookup. Hard-coded default names ("longitude", "latitude") match CF conventions but not e.g. "lon" / "lat". Solution: expose lon= / lat= kwargs (above), default to the CF names.

  4. NaN handling. filtfilt does not skip NaNs — they propagate across the whole convolution kernel. The upstream notebooks pre-segment along-track data into NaN-free chunks before filtering. We document this limitation; an interpolate_nans=True convenience flag (using xrtoolz.interpolate.gap_fill) is a future enhancement, not blocking.

  5. Operator placement. BandpassWavelength could equally live in interpolate.operators (mirrors the smoother) or geo.operators (along-track domain). Choosing geo because the wavelength-in-km semantics and lon/lat lookup are geo-domain concerns.