From 9ee33c125909372d127a8689b0f17135ae92e5c0 Mon Sep 17 00:00:00 2001 From: Nikhil Garuda Date: Fri, 6 Nov 2026 21:27:26 -0600 Subject: [PATCH 1/7] keep input length for smooth_fft for odd length spectra --- sedpy/smoothing.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sedpy/smoothing.py b/sedpy/smoothing.py index 62c4fd8..5f6683d 100644 --- a/sedpy/smoothing.py +++ b/sedpy/smoothing.py @@ -624,7 +624,7 @@ def smooth_fft(dx, spec, sigma): # Multiply in fourier space ff_tapered = spec_ff * taper # Fourier transform back - spec_conv = np.fft.irfft(ff_tapered) + spec_conv = np.fft.irfft(ff_tapered, n=len(spec)) return spec_conv From 40d2364268f38fddee20095d1932dbbc48b44ea7 Mon Sep 17 00:00:00 2001 From: Nikhil Garuda Date: Fri, 6 Nov 2026 21:30:17 -0600 Subject: [PATCH 2/7] smooth native samples in smooth_vel_fft when grid is log-uniform --- sedpy/smoothing.py | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/sedpy/smoothing.py b/sedpy/smoothing.py index 5f6683d..dc0c069 100644 --- a/sedpy/smoothing.py +++ b/sedpy/smoothing.py @@ -273,8 +273,13 @@ def smooth_vel_fft(wavelength, spectrum, outwave, sigma_out, inres=0.0, if sigma <= 0: return np.interp(outwave, wavelength, spectrum) - # make length of spectrum a power of 2 by resampling - wave, spec = resample_wave(wavelength, spectrum) + # smooth native samples when they are already uniform in log wavelength + lnw = np.log(wavelength) + dln = np.diff(lnw) + if np.allclose(dln, dln[0]): + wave, spec = wavelength, spectrum + else: + wave, spec = resample_wave(wavelength, spectrum) # get grid resolution (*not* the resolution of the input spectrum) and make # sure it's nearly constant. It should be, by design (see resample_wave) From d76ecb7b8e32c69b772c010166ca127e04b22c46 Mon Sep 17 00:00:00 2001 From: Nikhil Garuda Date: Fri, 6 Nov 2026 21:43:23 -0600 Subject: [PATCH 3/7] Add tests for FFT smoothing on log-uniform grids --- tests/test_smoothing.py | 60 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100644 tests/test_smoothing.py diff --git a/tests/test_smoothing.py b/tests/test_smoothing.py new file mode 100644 index 0000000..b801bba --- /dev/null +++ b/tests/test_smoothing.py @@ -0,0 +1,60 @@ +# -*- coding: utf-8 -*- + +import numpy as np +from numpy.testing import assert_allclose + +from sedpy.smoothing import smooth_fft, smoothspec + +CKMS = 2.998e5 + + +def _log_grid(lo=4700.0, hi=5300.0, dv=10.0): + """A grid uniform in log wavelength with steps of ``dv`` km/s.""" + return np.exp(np.arange(np.log(lo), np.log(hi), dv / CKMS)) + + +def _absorption_spectrum(wave, width=12.7, nline=60, seed=0): + """Unit continuum with Gaussian absorption lines of ``width`` km/s, barely sampled.""" + rng = np.random.default_rng(seed) + v = CKMS * np.log(wave[:, None] / rng.uniform(4800.0, 5200.0, nline)[None, :]) + return 1.0 - np.sum(rng.uniform(0.05, 0.6, nline) * np.exp(-0.5 * (v / width) ** 2), axis=1) + + +def test_smooth_fft_keeps_length(): + """Odd and even inputs come back with their own length.""" + for n in (1024, 1023): + spec = np.random.default_rng(1).normal(size=n) + assert smooth_fft(1.0, spec, 3.0).size == n + + +def test_vel_fft_matches_direct_on_log_uniform_grid(): + """On a log-uniform grid the FFT path matches the direct convolution of smooth_vel.""" + wave = _log_grid() + spec = _absorption_spectrum(wave) + core = (wave > 4850.0) & (wave < 5150.0) + for sigma in (30.0, 150.0): + direct = smoothspec(wave, spec, sigma, outwave=wave, smoothtype="vel", fftsmooth=False) + fft = smoothspec(wave, spec, sigma, outwave=wave, smoothtype="vel", fftsmooth=True) + assert_allclose(fft[core], direct[core], rtol=0, atol=1e-6) + + +def test_vel_fft_odd_length_log_uniform_grid(): + """An odd number of log-uniform samples is smoothed and returned at full length.""" + wave = _log_grid() + wave = wave[: wave.size - (1 - wave.size % 2)] + assert wave.size % 2 == 1 + out = smoothspec(wave, _absorption_spectrum(wave), 60.0, outwave=wave, smoothtype="vel") + assert out.size == wave.size and np.all(np.isfinite(out)) + + +def test_vel_fft_non_uniform_grid_is_resampled(): + """A grid that is not log-uniform still goes through the resampled FFT path.""" + rng = np.random.default_rng(2) + wave = np.sort(rng.uniform(4700.0, 5300.0, 4000)) + spec = 1.0 + 0.1 * np.sin(wave / 3.0) + out = smoothspec(wave, spec, 60.0, outwave=wave, smoothtype="vel", fftsmooth=True) + direct = smoothspec(wave, spec, 60.0, outwave=wave, smoothtype="vel", fftsmooth=False) + core = (wave > 4850.0) & (wave < 5150.0) + assert out.size == wave.size + # the resampled path interpolates first, so it agrees only to a few 1e-3 + assert_allclose(out[core], direct[core], rtol=0, atol=5e-3) From d7942ead7b1ff17b2825bce268e68b5e65c5eb04 Mon Sep 17 00:00:00 2001 From: Nikhil Garuda Date: Fri, 6 Nov 2026 21:57:58 -0600 Subject: [PATCH 4/7] Upsample in Fourier space when outwave is finer than a native log grid --- sedpy/smoothing.py | 39 ++++++++++++++++++++++++++++++++++++--- 1 file changed, 36 insertions(+), 3 deletions(-) diff --git a/sedpy/smoothing.py b/sedpy/smoothing.py index dc0c069..f49c5d2 100644 --- a/sedpy/smoothing.py +++ b/sedpy/smoothing.py @@ -14,7 +14,8 @@ from numpy.fft import fft, ifft, fftfreq, rfftfreq __all__ = ["smoothspec", "smooth_wave", "smooth_vel", "smooth_lsf", - "smooth_wave_fft", "smooth_vel_fft", "smooth_fft", "smooth_lsf_fft", + "smooth_wave_fft", "smooth_vel_fft", "smooth_fft", "smooth_fft_upsample", + "smooth_lsf_fft", "mask_wave", "resample_wave"] ckms = 2.998e5 @@ -276,7 +277,8 @@ def smooth_vel_fft(wavelength, spectrum, outwave, sigma_out, inres=0.0, # smooth native samples when they are already uniform in log wavelength lnw = np.log(wavelength) dln = np.diff(lnw) - if np.allclose(dln, dln[0]): + native = np.allclose(dln, dln[0]) + if native: wave, spec = wavelength, spectrum else: wave, spec = resample_wave(wavelength, spectrum) @@ -287,8 +289,22 @@ def smooth_vel_fft(wavelength, spectrum, outwave, sigma_out, inres=0.0, assert invRgrid.max() / invRgrid.min() < 1.05 dv = ckms * np.median(invRgrid) + # on native samples, upsample in Fourier space when outwave is finer than + # the input grid, so the final interpolation does not alter narrow features + upsample = 1 + if native and outwave is not None and len(outwave) > 1: + dout = np.diff(np.log(outwave)) + dout = dout[dout > 0] + if dout.size: + upsample = max(1, int(np.ceil(np.median(invRgrid) / dout.min()))) + # Do the convolution - spec_conv = smooth_fft(dv, spec, sigma) + if upsample > 1: + spec_conv = smooth_fft_upsample(dv, spec, sigma, upsample) + dln_fine = np.median(invRgrid) / upsample + wave = np.exp(np.log(wave[0]) + np.arange(spec_conv.size) * dln_fine) + else: + spec_conv = smooth_fft(dv, spec, sigma) # interpolate onto output grid if outwave is not None: spec_conv = np.interp(outwave, wave, spec_conv) @@ -633,6 +649,23 @@ def smooth_fft(dx, spec, sigma): return spec_conv +def smooth_fft_upsample(dx, spec, sigma, upsample=1): + """Same as smooth_fft, but return the result on a grid + `upsample` times finer than the input grid (zero padding).""" + n = len(spec) + spec_ff = np.fft.rfft(spec) + + if n % 2 == 0 and upsample > 1: + spec_ff[-1] *= 0.5 # Nyquist frequency + + spec_ff *= np.exp(-2 * (np.pi ** 2) * (sigma ** 2) * (rfftfreq(n, d=dx) ** 2)) + + n_out = upsample * n + spec_fine = np.fft.irfft(spec_ff, n=n_out) * upsample + + return spec_fine[: upsample * (n - 1) + 1] + + def mask_wave(wavelength, width=1, wlo=0, whi=np.inf, outwave=None, nsigma_pad=20.0, linear=False, **extras): """Restrict wavelength range (for speed) but include some padding based on From 7af90c800a199c49c69d405d31f560982c200bb3 Mon Sep 17 00:00:00 2001 From: Nikhil Garuda Date: Fri, 6 Nov 2026 21:57:58 -0600 Subject: [PATCH 5/7] Add tests for Fourier upsampling in smooth_vel_fft --- tests/test_smoothing.py | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/tests/test_smoothing.py b/tests/test_smoothing.py index b801bba..02bdd73 100644 --- a/tests/test_smoothing.py +++ b/tests/test_smoothing.py @@ -58,3 +58,44 @@ def test_vel_fft_non_uniform_grid_is_resampled(): assert out.size == wave.size # the resampled path interpolates first, so it agrees only to a few 1e-3 assert_allclose(out[core], direct[core], rtol=0, atol=5e-3) + + +def test_smooth_fft_upsample_keeps_samples(): + """upsample=1 equals smooth_fft; with upsample=4 every 4th point is a smoothed input sample.""" + from sedpy.smoothing import smooth_fft_upsample + + for n in (1024, 1023): + spec = np.random.default_rng(3).normal(size=n) + base = smooth_fft(10.0, spec, 20.0) + assert_allclose(smooth_fft_upsample(10.0, spec, 20.0, 1), base, rtol=0, atol=1e-14) + fine = smooth_fft_upsample(10.0, spec, 20.0, 4) + assert fine.size == 4 * (n - 1) + 1 + assert_allclose(fine[::4], base, rtol=0, atol=1e-12) + + +def _analytic(wave, sigma, width=12.7, nline=60, seed=0): + """The spectrum of _absorption_spectrum smoothed analytically by ``sigma`` km/s.""" + rng = np.random.default_rng(seed) + total = np.hypot(width, sigma) + v = CKMS * np.log(wave[:, None] / rng.uniform(4800.0, 5200.0, nline)[None, :]) + depth = rng.uniform(0.05, 0.6, nline) * width / total + return 1.0 - np.sum(depth * np.exp(-0.5 * (v / total) ** 2), axis=1) + + +def test_vel_fft_fine_outwave_is_upsampled(): + """On a finer outwave, Fourier upsampling keeps narrow lines; linear interpolation does not. + + Lines of 12.7 km/s on 10 km/s pixels, smoothed by 13-25 km/s (a stellar LOSVD of 18-28 km/s + with the 12.7 km/s library resolution removed), evaluated on a 0.05 A grid. + """ + wave = _log_grid(4500.0, 5500.0) + spec = _absorption_spectrum(wave) + fine = np.arange(4850.0, 5150.0, 0.05) + for losvd in (18.0, 23.0, 28.0): + sigma = np.sqrt(losvd**2 - 12.7**2) + truth = _analytic(fine, sigma) + out = smoothspec(wave, spec, sigma, outwave=fine, smoothtype="vel") + native = smoothspec(wave, spec, sigma, outwave=wave, smoothtype="vel") + linear = np.interp(fine, wave, native) + assert np.max(np.abs(out - truth)) < 2e-3 + assert np.max(np.abs(linear - truth)) > 5e-3 From 135da3d81ecc8a46f06cfc9ca94c8ee30999506f Mon Sep 17 00:00:00 2001 From: Nikhil Garuda Date: Fri, 6 Nov 2026 22:09:56 -0600 Subject: [PATCH 6/7] Fourier upsample native grids in smooth_vel_fft also when the kernel is zero --- sedpy/smoothing.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/sedpy/smoothing.py b/sedpy/smoothing.py index f49c5d2..85ccd87 100644 --- a/sedpy/smoothing.py +++ b/sedpy/smoothing.py @@ -271,13 +271,16 @@ def smooth_vel_fft(wavelength, spectrum, outwave, sigma_out, inres=0.0, """ # The kernel width for the convolution. sigma = np.sqrt(sigma_out**2 - inres**2) - if sigma <= 0: - return np.interp(outwave, wavelength, spectrum) # smooth native samples when they are already uniform in log wavelength lnw = np.log(wavelength) dln = np.diff(lnw) native = np.allclose(dln, dln[0]) + + # with no kernel, a native grid still goes through the Fourier upsampling + # below, so a finer outwave is not filled by linear interpolation + if sigma <= 0 and not native: + return np.interp(outwave, wavelength, spectrum) if native: wave, spec = wavelength, spectrum else: From 247f2d4eef5e421ce282b43238f5c20ba8e5f7bf Mon Sep 17 00:00:00 2001 From: Nikhil Garuda Date: Fri, 6 Nov 2026 22:09:56 -0600 Subject: [PATCH 7/7] Test smooth_vel_fft with a zero kernel on a native grid --- tests/test_smoothing.py | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/tests/test_smoothing.py b/tests/test_smoothing.py index 02bdd73..98b4e1f 100644 --- a/tests/test_smoothing.py +++ b/tests/test_smoothing.py @@ -99,3 +99,15 @@ def test_vel_fft_fine_outwave_is_upsampled(): linear = np.interp(fine, wave, native) assert np.max(np.abs(out - truth)) < 2e-3 assert np.max(np.abs(linear - truth)) > 5e-3 + + +def test_vel_fft_zero_kernel_native_grid_is_upsampled(): + wave = _log_grid(4500.0, 5500.0) + spec = _absorption_spectrum(wave) + fine = np.arange(4850.0, 5150.0, 0.05) + out = smoothspec(wave, spec, 30.0, outwave=fine, smoothtype="vel", inres=30.0) + truth = _analytic(fine, 0.0) + assert np.max(np.abs(out - truth)) < 5e-3 + assert np.max(np.abs(np.interp(fine, wave, spec) - truth)) > 2e-2 + same = smoothspec(wave, spec, 30.0, outwave=wave, smoothtype="vel", inres=30.0) + assert_allclose(same, spec, rtol=0, atol=1e-12)