nirs4all.operators.transforms.wavelet_denoise module
Wavelet denoising transforms for spectral data.
- class nirs4all.operators.transforms.wavelet_denoise.WaveletDenoise(wavelet: str = 'db4', level: int = 5, mode: str = 'periodization', threshold_mode: str = 'soft', noise_estimator: str = 'median', *, copy: bool = True)[source]
Bases:
TransformerMixin,BaseEstimatorWavelet denoising transformer for spectral data.
Applies multi-level discrete wavelet decomposition with soft or hard thresholding on detail coefficients, then reconstructs the signal to the original length. This preprocessing step reduces noise while preserving spectral features, making it ideal for NIRS analysis with PLS regression or other methods.
- Parameters:
wavelet (str, default="db4") – Wavelet family to use. Common choices: - ‘db4’, ‘db8’: Daubechies wavelets (good for general signals) - ‘sym8’: Symlets (nearly symmetric, smooth reconstruction) - ‘coif3’: Coiflets (good for edge preservation) - ‘haar’: Haar wavelet (simple, fast)
level (int, default=5) – Decomposition level. Higher values capture lower frequency components. Typical range: 3-7 for NIRS data (~1000 features).
mode (str, default="periodization") – Signal extension mode for boundary handling: - ‘periodization’: Assumes signal is periodic (recommended) - ‘symmetric’: Mirror extension (good for edge preservation) - ‘smooth’: Smooth extension (minimal boundary artifacts)
threshold_mode (str, default="soft") – Thresholding method: - ‘soft’: Shrinks coefficients continuously (smoother, more denoising) - ‘hard’: Zeroes coefficients below threshold (preserves peaks)
noise_estimator (str, default="median") – Method to estimate noise level from finest detail coefficients: - ‘median’: Robust to outliers (recommended for real data) - ‘std’: Standard deviation (assumes Gaussian noise)
copy (bool, default=True) – Whether to copy input data before transformation.
- Variables:
transformer) (None (stateless)
Examples
>>> from nirs4all.operators.transforms import WaveletDenoise >>> from sklearn.cross_decomposition import PLSRegression >>> pipeline = [ ... WaveletDenoise(wavelet='db8', level=5), ... {"model": PLSRegression(n_components=10)} ... ]
Notes
The transformer is stateless (does not learn from training data)
Output shape matches input shape exactly
The universal threshold is data-dependent: sigma * sqrt(2 * log(L))
References
Donoho, D. L., & Johnstone, I. M. (1994). Ideal spatial adaptation by wavelet shrinkage. Biometrika, 81(3), 425-455.
- fit(X, y=None)[source]
Fit the transformer (no-op for stateless wavelet denoising).
- Parameters:
X (array-like of shape (n_samples, n_features)) – Input spectral data.
y (None) – Ignored.
- Returns:
self – Returns self.
- Return type:
- Raises:
ValueError – If input is a sparse matrix.
- transform(X, y=None)[source]
Apply wavelet denoising to input data.
- Parameters:
X (array-like of shape (n_samples, n_features)) – Input spectral data.
y (None) – Ignored.
- Returns:
X_denoised – Denoised spectral data with original length preserved.
- Return type:
np.ndarray of shape (n_samples, n_features)
- nirs4all.operators.transforms.wavelet_denoise.wavelet_denoise(spectra: ndarray, wavelet: str = 'db4', level: int = 5, mode: str = 'periodization', threshold_mode: str = 'soft', noise_estimator: str = 'median') ndarray[source]
Apply wavelet denoising to spectral data while preserving original length.
Uses multi-level discrete wavelet decomposition with thresholding on detail coefficients, followed by reconstruction to maintain the original signal length. This is ideal for NIRS preprocessing before PLS or other regression methods.
- Parameters:
spectra (np.ndarray of shape (n_samples, n_features)) – Input spectral data.
wavelet (str, default="db4") – Wavelet family to use (e.g., ‘db4’, ‘sym8’, ‘coif3’, ‘haar’).
level (int, default=5) – Decomposition level. Higher levels capture lower frequency components.
mode (str, default="periodization") – Signal extension mode for boundary handling. Options: ‘zero’, ‘constant’, ‘symmetric’, ‘periodic’, ‘smooth’, ‘periodization’.
threshold_mode (str, default="soft") – Thresholding method: ‘soft’ (continuous) or ‘hard’ (discontinuous).
noise_estimator (str, default="median") – Method to estimate noise level: - ‘median’: sigma = median(
|cD_finest|) / 0.6745 (robust to outliers) - ‘std’: sigma = std(cD_finest) (assumes Gaussian noise)
- Returns:
Denoised spectra with original length preserved.
- Return type:
np.ndarray of shape (n_samples, n_features)
Notes
The universal threshold is computed as: uthresh = sigma * sqrt(2 * log(L)) where L is the signal length and sigma is estimated from the finest detail coefficients.
References
Donoho, D. L., & Johnstone, I. M. (1994). Ideal spatial adaptation by wavelet shrinkage. Biometrika, 81(3), 425-455.