Equidistant interpolation

Interpolating tabulated data (x, y) onto an evenly sampled, equidistant axis is a frequent problem.

PyAstronomy.pyasl.equidistantInterpolation(x, y, dxmode, ifct=<Mock name='mock.interpolate.interp1d' id='140554583538560'>)

Construct evenly sampled data set by interpolation

Parameters
xarray

Input x-axis

yarray or list/tuple of arrays

The y axis or axes

dxmodestring {2x, mean} or float

If a float is given, it specifies the spacing of the new x axis. If ‘2x’ is given, an equidistant x axis with twice as many points as the input one will be used. If ‘mean’ is given, the spacing of the new x axis corresponds to the mean of that of the input axis. If dxmode is an array, it is used as the new x-axis (without check for equidistant sampling).

ifctinterpolation callable

A callable taking x and y as arguments and returning a callable, which takes the new equidistant wavelength axis as argument. The default is interp1d from scipy called by ny = ifct(x, y)(nx).

Returns
x, yarrays

New equidistant x axis and linearly interpolated y axis. If input y is a list or tuple of arrays, a list of interpolated arrays is returned.

Example

import numpy as np
import matplotlib.pylab as plt
from PyAstronomy import pyasl

x = np.array([0.1, 0.2, 0.5,0.87,1.5,2])
y = np.array([1,2,3,1,4,1.1])

w1, f1 = pyasl.equidistantInterpolation(x, y, "2x")
w2, f2 = pyasl.equidistantInterpolation(x, y, "mean")
w3, f3 = pyasl.equidistantInterpolation(x, y, 0.05)

plt.plot(x,y,'bo', label="Data")
plt.plot(w1, f1, 'r.-', label="2x")
plt.plot(w2, f2, 'g.-', label="mean")
plt.plot(w3, f3, 'm.-', label="0.02")
plt.legend()
plt.show()

Example (list of y axes and explicit x-axis)

x = np.array([0.1, 0.2, 0.5,0.87,1.5,2])
yy = [np.array([1,2,3,1,4,1.1]), np.array([10,20,30,1,10,-4.5])]

# Apply interpolation to list of arrays for y
w, ff = pyasl.equidistantInterpolation(x, yy, "2x")

for f in ff:
    plt.plot(w, f, '.-')

# Specify new x-axis explicitly
_, g = pyasl.equidistantInterpolation(x, yy[0], w)
plt.plot(w, g, 'r--')
plt.show()