Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 48 additions & 7 deletions sedpy/smoothing.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -270,20 +271,43 @@ 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)

# 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)
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:
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)
invRgrid = np.diff(np.log(wave))
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)
Expand Down Expand Up @@ -624,10 +648,27 @@ 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


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
Expand Down
113 changes: 113 additions & 0 deletions tests/test_smoothing.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
# -*- 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)


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


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)
Loading