0
# Smoothing-Based Methods
1
2
Algorithms that use smoothing operations and iterative filtering to separate baseline from signal components. These methods are effective for noisy data and spectra with complex peak structures, using various windowing and filtering approaches to identify and preserve baseline regions while suppressing peak contributions.
3
4
## Capabilities
5
6
### Noise-Median Method
7
8
Uses median filtering combined with smoothing to estimate baseline from noisy data.
9
10
```python { .api }
11
def noise_median(data, half_window=None, smooth_half_window=None, sigma=None, x_data=None, pad_kwargs=None, **kwargs):
12
"""
13
Noise-median smoothing baseline correction.
14
15
Parameters:
16
- data (array-like): Input y-values to fit baseline
17
- half_window (int, optional): Half-window size for median filtering
18
- smooth_half_window (int, optional): Half-window for smoothing operation
19
- sigma (float, optional): Standard deviation for noise estimation
20
- x_data (array-like, optional): Input x-values
21
- pad_kwargs (dict, optional): Padding parameters for edge handling
22
23
Returns:
24
tuple: (baseline, params)
25
"""
26
```
27
28
### SNIP Algorithm
29
30
Statistical Sensitive Non-linear Iterative Peak algorithm for baseline estimation.
31
32
```python { .api }
33
def snip(data, max_half_window=None, decreasing=False, smooth_half_window=None, filter_order=2, x_data=None, pad_kwargs=None, **kwargs):
34
"""
35
Statistical Sensitive Non-linear Iterative Peak algorithm.
36
37
Parameters:
38
- data (array-like): Input y-values to fit baseline
39
- max_half_window (int, optional): Maximum half-window size for iterations
40
- decreasing (bool): Whether to use decreasing window sizes
41
- smooth_half_window (int, optional): Half-window for smoothing
42
- filter_order (int): Order of smoothing filter
43
- x_data (array-like, optional): Input x-values
44
- pad_kwargs (dict, optional): Padding parameters
45
46
Returns:
47
tuple: (baseline, params)
48
"""
49
```
50
51
### Small-Window Moving Average (SWIMA)
52
53
Uses moving averages with adaptive window sizing for baseline estimation.
54
55
```python { .api }
56
def swima(data, min_half_window=3, max_half_window=None, smooth_half_window=None, x_data=None, pad_kwargs=None, **kwargs):
57
"""
58
Small-window moving average baseline correction.
59
60
Parameters:
61
- data (array-like): Input y-values to fit baseline
62
- min_half_window (int): Minimum half-window size
63
- max_half_window (int, optional): Maximum half-window size
64
- smooth_half_window (int, optional): Half-window for final smoothing
65
- x_data (array-like, optional): Input x-values
66
- pad_kwargs (dict, optional): Padding parameters
67
68
Returns:
69
tuple: (baseline, params)
70
"""
71
```
72
73
### Iterative Polynomial Smoothing Algorithm (IPSA)
74
75
Combines polynomial fitting with iterative smoothing for robust baseline estimation.
76
77
```python { .api }
78
def ipsa(data, half_window=None, max_iter=500, tol=None, roi=None, original_criteria=False, x_data=None, pad_kwargs=None, **kwargs):
79
"""
80
Iterative Polynomial Smoothing Algorithm.
81
82
Parameters:
83
- data (array-like): Input y-values to fit baseline
84
- half_window (int, optional): Half-window size for smoothing
85
- max_iter (int): Maximum iterations
86
- tol (float, optional): Convergence tolerance
87
- roi (array-like, optional): Region of interest for processing
88
- original_criteria (bool): Whether to use original convergence criteria
89
- x_data (array-like, optional): Input x-values
90
- pad_kwargs (dict, optional): Padding parameters
91
92
Returns:
93
tuple: (baseline, params)
94
"""
95
```
96
97
### Range Independent Algorithm (RIA)
98
99
Adapts to varying signal ranges and provides range-independent baseline correction.
100
101
```python { .api }
102
def ria(data, x_data=None, half_window=None, max_iter=500, tol=1e-2, side='both', width_scale=0.1, height_scale=1., sigma_scale=1./12., pad_kwargs=None, **kwargs):
103
"""
104
Range Independent Algorithm for baseline correction.
105
106
Parameters:
107
- data (array-like): Input y-values to fit baseline
108
- x_data (array-like, optional): Input x-values
109
- half_window (int, optional): Half-window size
110
- max_iter (int): Maximum iterations
111
- tol (float): Convergence tolerance
112
- side (str): Side for range extension ('left', 'right', 'both')
113
- width_scale (float): Width scaling factor
114
- height_scale (float): Height scaling factor
115
- sigma_scale (float): Sigma scaling factor
116
- pad_kwargs (dict, optional): Padding parameters
117
118
Returns:
119
tuple: (baseline, params)
120
"""
121
```
122
123
### 4S Peak Filling
124
125
Peak filling algorithm using iterative smoothing with controlled filling operations.
126
127
```python { .api }
128
def peak_filling(data, x_data=None, half_window=None, sections=None, max_iter=5, lam_smooth=None):
129
"""
130
4S Peak Filling algorithm for baseline correction.
131
132
Parameters:
133
- data (array-like): Input y-values to fit baseline
134
- x_data (array-like, optional): Input x-values
135
- half_window (int, optional): Half-window size for operations
136
- sections (int, optional): Number of sections for processing
137
- max_iter (int): Maximum iterations for filling
138
- lam_smooth (float, optional): Smoothing parameter
139
140
Returns:
141
tuple: (baseline, params)
142
"""
143
```
144
145
## Usage Examples
146
147
### SNIP baseline correction:
148
149
```python
150
import numpy as np
151
from pybaselines.smooth import snip
152
153
# Sample noisy data with multiple peaks
154
x = np.linspace(0, 1000, 2000)
155
baseline = 5 + 0.01 * x
156
peaks = (100 * np.exp(-((x - 200) / 30)**2) +
157
80 * np.exp(-((x - 500) / 50)**2) +
158
60 * np.exp(-((x - 800) / 40)**2))
159
noise = np.random.normal(0, 2, len(x))
160
data = baseline + peaks + noise
161
162
# Apply SNIP algorithm
163
baseline_est, params = snip(data, max_half_window=50, x_data=x)
164
corrected = data - baseline_est
165
```
166
167
### Iterative smoothing with IPSA:
168
169
```python
170
from pybaselines.smooth import ipsa
171
172
# Robust baseline estimation with iterative smoothing
173
baseline_est, params = ipsa(data, half_window=20, max_iter=100, x_data=x)
174
corrected = data - baseline_est
175
176
print(f"Converged in {len(params.get('tol_history', []))} iterations")
177
```