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
11 changes: 8 additions & 3 deletions sedpy/smoothing.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -624,7 +629,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


Expand Down
60 changes: 60 additions & 0 deletions tests/test_smoothing.py
Original file line number Diff line number Diff line change
@@ -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)
Loading