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()

API documentation:

silx.math.fit.smooth1d(data) → numpy.ndarray

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)
silx.math.fit.smooth2d(data) → numpy.ndarray

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)
silx.math.fit.smooth3d(data) → numpy.ndarray

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)
silx.math.fit.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

silx.math.fit.snip1d(data, snip_width) → numpy.ndarray

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

Implementation of the algorithm SNIP in 1D is described in Miroslav Morhac et al. Nucl. Instruments and Methods in Physics Research A401 (1997) 113-132.

The original idea for 1D and the low-statistics-digital-filter (lsdf) come from C.G. Ryan et al. Nucl. Instruments and Methods in Physics Research B34 (1988) 396-402.

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

silx.math.fit.snip2d(data, width, nrows=None, ncolumns=None) → numpy.ndarray

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

Implementation of the algorithm SNIP in 2D described in Miroslav Morhac et al. Nucl. Instruments and Methods in Physics Research A401 (1997) 113-132.

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

silx.math.fit.snip3d(data, width, nx=None, ny=None, nz=None) → numpy.ndarray

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

Implementation of the algorithm SNIP in 2D described in Miroslav Morhac et al. Nucl. Instruments and Methods in Physics Research A401 (1997) 113-132.

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

silx.math.fit.strip(data, w, niterations, 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

Table Of Contents

Previous topic

Fit functions

Next topic

fitmanager: Fit functions manager

This Page