def astropy_smooth(data, vel, linearize=False, kernel=convolution.Gaussian1DKernel, **kern_args):
"""
Smooth using a gaussian filter, using astropy.
Parameters:
===========
- data: kglib.utils.DataStructures.xypoint instance
The data to smooth.
- vel: float
The velocity scale to smooth out.
Can either by an astropy quantity or a float in km/s
- linearize: boolean
If True, we will put the data in a constant
log-wavelength spacing grid before smoothing.
The output has the same spacing as the input
regardless of this variable.
- kernel: astropy.convolution kernel
The astropy kernel to use. The default is the
Gaussian1DKernel.
- kern_args: Additional kernel arguments beyond width
Returns:
========
A smoothed version of the data, on the same wavelength grid as the data
"""
if linearize:
original_data = data.copy()
datafcn = spline(data.x, data.y, k=3)
linear = DataStructures.xypoint(data.x.size)
linear.x = np.logspace(np.log10(data.x[0]), np.log10(data.x[-1]), linear.size())
linear.y = datafcn(linear.x)
data = linear
# Figure out feature size in pixels
if not isinstance(vel, u.quantity.Quantity):
vel *= u.km / u.second
featuresize = (vel / constants.c).decompose().value
dlam = np.log(data.x[1] / data.x[0])
Npix = featuresize / dlam
# Make kernel and smooth
kern = kernel(Npix, **kern_args)
smoothed = convolution.convolve(data.y, kern, boundary='extend')
if linearize:
fcn = spline(data.x, smoothed)
return fcn(original_data.x)
return smoothed
评论列表
文章目录