# Filter1D — 1-dimensional spectral filtering.¶

This module defines the 1D filter methods.

class admit.util.filter.Filter1D.Filter1D(spec, method, **keyval)[source]

This class defines and runs 1D spectral filters. The currently available filters are Gaussian, Hanning, Triangle, Welch, Boxcar, and Savitzky Golay. The output spectrum will be of the same length as the input spectrum, however some edge channels may be zeroed by some methods, depending on the input paramters.

Parameters: spec : numpy array 1D numpy array of the input spectrum (just the amplitudes). method : str The smoothing filter to apply: boxcar, gaussian, welch, hanning, triangle, or savgol. No default. Minimum matching is enabled with a minimum of 3 characters, i.e. box = boxcar. keyval : various Any keyword value pairs for the specific method chosen, see the notes for specific keywords.

Notes

Details of the different filter keywords and defaults:

Filter Keyword Def. Description
“boxcar” “width” 3 Number of channels to average together
“gaussian” “width” 7 Number of channels to span with the gaussian
“hanning” “width” 5 Number of channels to include in the cos
“triangle” “width” 5 Number of channels to span with the triangle
“welch” “width” 5 Number of channels to use in the function
“savgol” “window_size” 7 Number of channels to use in the calculation
“order” 3 Order of the poynomial fit (must be odd)
“deriv” 0 The number of the derivative to compute (0 = just smooth)

Attributes

 spec (numpy array) The spectrum. len (int) The length of the spectrum. methods (list) A list of the available filters. [method]_args (dict) A dictionary for each method giving its keywords and defaults (e.g. boxcar_args). method (str) The method being used.

Methods

 boxcar(width) Method to apply a boxcar filter to a spectrum. buffer(nchan) Method to buffer/pad an array so that filters can work all the way to the edge. checkmethod(method) Method to interpret the input method and determine the full method convertargs(args) Method to convert a tuple of arguments into a dictionary of arguments for the specified method. gaussian(width) Method to apply a Gaussian filter to a spectrum. hanning(width) Method to apply a Hanning filter to a spectrum. isodd(value) Method to determine if a number is odd run() Method to run the selected filter on the data savgol(window_size, order[, deriv, rate]) Smooth (and optionally differentiate) data with a Savitzky-Golay filter. triangle(width) Method to apply a Triangular filter to a spectrum. welch(width) Method to apply a Welch filter to a spectrum.
boxcar(width)[source]

Method to apply a boxcar filter to a spectrum. The filter for point x[i] is defined as:

$x[i] = \frac{1}{N} \sum_{n=0}^{N} x[i + n - \frac{N - 1}{2}]$

where N is the width of the filter.

Parameters: width : int The width of the box to use in channels, must be odd numpy array The smoothed spectrum, (width - 1)/2 edge channels will be zeroed
boxcar_args = OrderedDict([('width', 3)])
buffer(nchan)[source]

Method to buffer/pad an array so that filters can work all the way to the edge. Uses np.pad with mode=’reflect’

Parameters: nchan : int The number of channels to add to each end of the array Numpy array containing the buffered input array
checkmethod(method)[source]

Method to interpret the input method and determine the full method name

Parameters: method : str The method to use, minimal matching is possible, with a minimum of 3 characters (e.g. “box” will be interpreted to be “boxcar”) None
static convertargs(args)[source]

Method to convert a tuple of arguments into a dictionary of arguments for the specified method. The first item of the tuple must be the method name. The remaining items are the arguments to the method in the order the method lists. To see which arguments a method takes call getargs(method) or getargs() to list the arguments for all methods.

Parameters: args : tuple Tuple containing the method as the first item and any arguments for that method in the order specified by the method. Dictionary containing the converted arguments.
gaussian(width)[source]

Method to apply a Gaussian filter to a spectrum. The filter for point x[i] is defined as:

$x[i] = \sum_{n=0}^{N} x[i + n - \frac{N - 1}{2}] e^{-\frac{1}{2}\left(\frac{n-(N-1)/2}{\sigma(N-1)/2}\right)^2}$

where N is the width of the filter.

Parameters: width : int The number of channels to span with the gaussian for each iteration, must be odd numpy array The smoothed spectrum, (width - 1)/2 edge channels will be zeroed
gaussian_args = OrderedDict([('width', 7)])
hanning(width)[source]

Method to apply a Hanning filter to a spectrum. The filter for point x[i] is defined as:

$x[i] = \sum_{n=0}^{N} x[i + n - \frac{N - 1}{2}] 0.5 \left(1 - \cos\left(\frac{2\pi n}{N-1}\right)\right)$

where N is the width of the filter.

Parameters: width : int The number of channels to span with the function for each iteration, must be odd numpy array The smoothed spectrum, (width - 1)/2 edge channels will be zeroed
hanning_args = OrderedDict([('width', 5)])
isodd(value)[source]

Method to determine if a number is odd

Parameters: value : int The number to check bool, True if the number is odd, False if it is even
methods = ['boxcar', 'gaussian', 'welch', 'hanning', 'triangle', 'savgol']
run()[source]

Method to run the selected filter on the data

Parameters: None The smoothed spectrum
savgol(window_size, order, deriv=0, rate=1)[source]

Smooth (and optionally differentiate) data with a Savitzky-Golay filter. The Savitzky-Golay filter removes high frequency noise from data. It has the advantage of preserving the original shape and features of the signal better than other types of filtering approaches, such as moving averages techniques. Adapted from http://wiki.scipy.org/Cookbook/SavitzkyGolay

Parameters: window_size : int the length of the window. Must be an odd integer number. order : int the order of the polynomial used in the filtering. Must be less then window_size - 1. deriv: int the order of the derivative to compute (default = 0 means only smoothing) ndarray the smoothed signal (or it’s n-th derivative).

Notes

The Savitzky-Golay is a type of low-pass filter, particularly suited for smoothing noisy data. The main idea behind this approach is to make for each point a least-square fit with a polynomial of high order over a odd-sized window centered at the point.

References

 [R1] A. Savitzky, M. J. E. Golay, Smoothing and Differentiation of Data by Simplified Least Squares Procedures. Analytical Chemistry, 1964, 36 (8), pp 1627-1639.
 [R2] Numerical Recipes 3rd Edition: The Art of Scientific Computing W.H. Press, S.A. Teukolsky, W.T. Vetterling, B.P. Flannery Cambridge University Press ISBN-13: 9780521880688
savgol_args = OrderedDict([('window_size', 7), ('order', 3), ('deriv', 0), ('rate', 1)])
triangle(width)[source]

Method to apply a Triangular filter to a spectrum. The filter for point x[i] is defined as:

$x[i] = \sum_{n=0}^{N} x[i + n - \frac{N - 1}{2}] \left(1 - \left|\frac{n-\frac{N-1}{2}}{\frac{N}{2}}\right|\right)$

where N is the width of the filter.

Parameters: width : int The number of channels to span with the function for each iteration, must be odd numpy array The smoothed spectrum, (width - 1)/2 edge channels will be zeroed
triangle_args = OrderedDict([('width', 5)])
welch(width)[source]

Method to apply a Welch filter to a spectrum. The filter for point x[i] is defined as:

$x[i] = \sum_{n=0}^{N} x[i + n - \frac{N - 1}{2}] \left(1 - \left(\frac{n - \frac{N-1}{2}}{\frac{N-1}{2}}\right)^2\right)$

where N is the width of the filter.

Parameters: width : int The number of channels to span with the function for each iteration, must be odd numpy array The smoothed spectrum, (width - 1)/2 edge channels will be zeroed
welch_args = OrderedDict([('width', 5)])
admit.util.filter.Filter1D.getargs(method=None)[source]

Method to report the keywords and default values for smoothing algorithms

Parameters: method : str The name of the method to report the keywords and default values for. If no method is given then all methods are reported on. Default: None None