Smoothing and background filters

This module provides background extraction functions and smoothing functions. These functions are extracted from PyMca module SpecFitFuns.

Index of background extraction functions:

  • strip()
  • snip1d()
  • snip2d()
  • snip3d()

Smoothing functions:

  • savitsky_golay()
  • smooth1d()
  • smooth2d()
  • smooth3d()

References:

[Morhac97](1, 2, 3) Miroslav Morháč et al. Background elimination methods for multidimensional coincidence γ-ray spectra. Nucl. Instruments and Methods in Physics Research A401 (1997) 113-132. https://doi.org/10.1016/S0168-9002(97)01023-1
[Ryan88]C.G. Ryan et al. SNIP, a statistics-sensitive background treatment for the quantitative analysis of PIXE spectra in geoscience applications. Nucl. Instruments and Methods in Physics Research B34 (1988) 396-402*. https://doi.org/10.1016/0168-583X(88)90063-8

API documentation:

smooth1d(data)

Simple smoothing for 1D data.

For a data array \(y\) of length \(n\), the smoothed array \(ys\) is calculated as a weighted average of neighboring samples:

\(ys_0 = 0.75 y_0 + 0.25 y_1\)

\(ys_i = 0.25 (y_{i-1} + 2 y_i + y_{i+1})\) for \(0 < i < n-1\)

\(ys_{n-1} = 0.25 y_{n-2} + 0.75 y_{n-1}\)

Parameters:data (numpy.ndarray) – 1D data array
Returns:Smoothed data
Return type:numpy.ndarray(dtype=numpy.float64)
smooth2d(data)

Simple smoothing for 2D data: smooth1d() is applied succesively along both axis

Parameters:data (numpy.ndarray) – 2D data array
Returns:Smoothed data
Return type:numpy.ndarray(dtype=numpy.float64)
smooth3d(data)

Simple smoothing for 3D data: smooth2d() is applied on each 2D slice of the data volume along all 3 axis

Parameters:data (numpy.ndarray) – 2D data array
Returns:Smoothed data
Return type:numpy.ndarray(dtype=numpy.float64)
savitsky_golay(data, npoints=5)

Smooth a curve using a Savitsky-Golay filter.

Parameters:
  • data (1D numpy array) – Input data
  • npoints – Size of the smoothing operator in number of samples Must be between 3 and 100.
Returns:

Smoothed data

snip1d(data, snip_width)

Estimate the baseline (background) of a 1D data vector by clipping peaks.

Implementation of the algorithm SNIP in 1D is described in [Morhac97]. The original idea for 1D and the low-statistics-digital-filter (lsdf) comes from [Ryan88].

Parameters:
  • data (numpy.ndarray) – Data array, preferably 1D and of type numpy.float64. Else, the data array will be flattened and converted to dtype=numpy.float64 prior to applying the snip filter.
  • snip_width – Width of the snip operator, in number of samples. A sample will be iteratively compared to it’s neighbors up to a distance of snip_width samples. This parameters has a direct influence on the speed of the algorithm.
Returns:

Baseline of the input array, as an array of the same shape.

Return type:

numpy.ndarray

snip2d(data, snip_width)

Estimate the baseline (background) of a 2D data signal by clipping peaks.

Implementation of the algorithm SNIP in 2D described in [Morhac97].

Parameters:
  • data (numpy.ndarray) – 2D array
  • width (int) – Width of the snip operator, in number of samples. A wider snip operator will result in a smoother result (lower frequency peaks will be clipped), and a longer computation time.
Returns:

Baseline of the input array, as an array of the same shape.

Return type:

numpy.ndarray

snip3d(data, snip_width)

Estimate the baseline (background) of a 3D data signal by clipping peaks.

Implementation of the algorithm SNIP in 3D described in [Morhac97].

Parameters:
  • data (numpy.ndarray) – 3D array
  • width (int) – Width of the snip operator, in number of samples. A wider snip operator will result in a smoother result (lower frequency peaks will be clipped), and a longer computation time.
Returns:

Baseline of the input array, as an array of the same shape.

Return type:

numpy.ndarray

strip(data, w=1, niterations=1000, factor=1.0, anchors=None)

Extract background from data using the strip algorithm, as explained at http://pymca.sourceforge.net/stripbackground.html.

In its simplest implementation it is just as an iterative procedure depending on two parameters. These parameters are the strip background width w, and the number of iterations. At each iteration, if the contents of channel i, y(i), is above the average of the contents of the channels at w channels of distance, y(i-w) and y(i+w), y(i) is replaced by the average. At the end of the process we are left with something that resembles a spectrum in which the peaks have been stripped.

Parameters:
  • data (numpy.ndarray) – Data array
  • w – Strip width
  • niterations – number of iterations
  • factor – scaling factor applied to the average of y(i-w) and y(i+w) before comparing to y(i)
  • anchors – Array of anchors, indices of points that will not be modified during the stripping procedure.
Returns:

Data with peaks stripped away