Smoothing data¶
- PyAstronomy.pyasl.smooth(x, windowLen, window='flat')¶
Smooth data using a window function.
This method is based on the convolution of a window function with the signal. The window function is normalized so that the sum of its entries amounts to one. The signal is prepared by adding reflected copies of the signal (with the window size) to both ends of the input array, so that the output array can have the same length as the input. Consequently the smoothing at the edges is actually based on extrapolation.
Note
This algorithm was adopted from the scipy cookbook (http://www.scipy.org/Cookbook/SignalSmooth). The copyright of the original algorithm belongs to the authors of that cookbook algorithm.
- Parameters
- xarray
The input signal
- windowLenint
The dimension of the smoothing window. It must be an odd integer.
- windowstring, {‘flat’, ‘hanning’, ‘hamming’, ‘bartlett’, ‘blackman’}
The window function to be used. A flat window will produce a moving average smoothing.
- Returns
- Smoothed signalarray
The smoothed signal. Same length as input array.
Example of usage¶
from PyAstronomy import pyasl
import numpy as np
import matplotlib.pylab as plt
# Create same "data" using a sine
# and random noise
x = np.linspace(0, 10, 100)
y = 0.5*np.sin(x/3.0*2.0*np.pi + 1.7)
y += np.random.normal(0.0, 0.2, len(y))
# Use two smoothing windows with the same
# window size
sm1 = pyasl.smooth(y, 11, 'flat')
sm2 = pyasl.smooth(y, 11, 'hamming')
# Plot the outcome
plt.title("Data and smoothed curves: flat (blue) and hamming window (red)")
plt.plot(x, y, 'bp')
plt.plot(x, sm1, 'b--')
plt.plot(x, sm2, 'r--')
plt.show()