Morphological Baselines

The contents of pybaselines.morphological contain algorithms that use morphological operations for estimating the baseline.

Introduction

Morphological operations include dilation, erosion, opening, and closing. Morphological operators use moving windows and compute the maximum, minimum, or a combination of the two within each window.

Note

All morphological algorithms use a half_window parameter to define the size of the window used for the morphological operators. half_window is index-based, rather than based on the units of the data, so proper conversions must be done by the user to get the desired window size.

Algorithms

mpls (Morphological Penalized Least Squares)

mpls() uses both morphological operations and Whittaker-smoothing to create the baseline. First, a morphological opening is performed on the data. Then, the index of the minimum data value between each flat region of the opened data is selected as a baseline anchor point and given a weighting of \(1 - p\), while all other points are given a weight of \(p\). The data and weights are then used to calculate the baseline, similar to the asls() method.

(Source code, png)

../_images/morphological-1.png

mor (Morphological)

mor() performs a morphological opening on the data and then selects the element-wise minimum between the opening and the average of a morphological erosion and dilation of the opening to create the baseline.

Note

The baseline from the mor method is not smooth. Smoothing is left to the user to perform, if desired.

(Source code, png)

../_images/morphological-2.png

imor (Improved Morphological)

imor() is an attempt to improve the mor method, and iteratively selects the element-wise minimum between the original data and the average of a morphological erosion and dilation of the opening of either the data (first iteration) or previous iteration's baseline to create the baseline.

(Source code, png)

../_images/morphological-3.png

mormol (Morphological and Mollified Baseline)

mormol() iteratively convolves the erosion of the data with a mollifying (smoothing) kernel, to produce a smooth baseline.

(Source code, png)

../_images/morphological-4.png

amormol (Averaging Morphological and Mollified Baseline)

amormol() iteratively convolves a mollifying (smoothing) kernel with the element-wise minimum of the data and the average of the morphological closing and opening of either the data (first iteration) or previous iteration's baseline.

(Source code, png)

../_images/morphological-5.png

rolling_ball (Rolling Ball)

rolling_ball() performs a morphological opening on the data and then smooths the result with a moving average, giving a baseline that resembles rolling a ball across the data.

(Source code, png)

../_images/morphological-6.png

mwmv (Moving Window Minimum Value)

mwmv() performs a morphological erosion on the data and then smooths the result with a moving average.

(Source code, png)

../_images/morphological-7.png

tophat (Top-hat Transformation)

tophat() performs a morphological opening on the data.

Note

The baseline from the tophat method is not smooth. Smoothing is left to the user to perform, if desired.

(Source code, png)

../_images/morphological-8.png

mpspline (Morphology-Based Penalized Spline)

mpspline() uses both morphological operations and penalized splines to create the baseline. First, the data is smoothed by fitting a penalized spline to the closing of the data with a window of 3. Then baseline points are identified where the smoothed data is equal to the element-wise minimum between the opening of the smoothed data and the average of a morphological erosion and dilation of the opening. The baseline points are given a weighting of \(1 - p\), while all other points are given a weight of \(p\), similar to the mpls() method. Finally, a penalized spline is fit to the smoothed data with the assigned weighting.

(Source code, png)

../_images/morphological-9.png

jbcd (Joint Baseline Correction and Denoising)

jbcd() uses regularized least-squares fitting combined with morphological operations to simultaneously obtain the baseline and denoised signal.

Minimized function:

\[\frac{1}{2} \sum\limits_{i = 1}^N (s_i + b_i - y_i)^2 + \alpha \sum\limits_{i = 1}^N (b_i - Op_i)^2 + \beta \sum\limits_{i = 1}^{N - d} (\Delta^d b_i)^2 + \gamma \sum\limits_{i = 1}^{N - d} (\Delta^d s_i)^2\]

where \(y_i\) is the measured data, \(b_i\) is the estimated baseline, \(s_i\) is the estimated signal, \(\Delta^d\) is the forward-difference operator of order d, \(Op_i\) is the morphological opening of the measured data, and \(\alpha\), \(\beta\), and \(\gamma\) are regularization parameters.

Linear systems:

The initial signal, \(s^0\), and baseline, \(b^0\), are set equal to \(y\), and \(Op\), respectively. Then the signal and baseline at iteration \(n\), \(s^n\) and \(b^n\), are solved for sequentially using the following two linear equations:

\[(I + 2 \gamma D_d^{\top} D_d) s^n = y - b^{n-1}\]
\[(I + 2 \alpha I + 2 \beta D_d^{\top} D_d) b^n = y - s^n + 2 \alpha Op\]

where \(I\) is the identity matrix and \(D_d\) is the matrix version of \(\Delta^d\), which is also the d-th derivative of the identity matrix. After each iteration, \(\beta\), and \(\gamma\) are updated by user-specified multipliers.

(Source code, png)

../_images/morphological-10.png

The signal with the baseline removed and noise decreased can also be obtained from the output of the jbcd function.

(Source code, png)

../_images/morphological-11.png