Why am I getting different results using savgol filter in python compared to smoothdata in MATLAB?
Show older comments
Hi so, as the title says - I am moving code from MATLAB to python for a test. The code is to process spectral data and then run a series of preprocessing techniques on it to then perform a PCA, the component values are what i'm recording, currently.
I am comparing a few things, so the code compares two smoothing windows and using both the first and second derriviate after Savitzky Golay smoothing. Then i apply either Standard normal variate (SNV) or multiplative signal correctino (MSC), and then mean center the data.
When running the process with MSC, i get identical PCA component results in Python as i did in MATLAB. WHen i run the SNV process, i get a difference of around 4 for component 1 and 0.6 for component 2 over my four results (Different smoothing and derrivates).
As nothing else changes, my guess is that there is a slight difference in methods that gets exacerbated in SNV but not MSC, but would appreciate help and advice if there is anything wrong with my code!
MATLAB:
polynomialOrder = 2;
frameSize = 19;
% Calculate Savitzky-Golay filter coefficients
[b, g] = sgolay(polynomialOrder, frameSize);
% Compute the sampling interval in energy
x = Energy;
dt = x(2) - x(1); % Sampling interval in keV
% First derivative after Savitzky-Golay smoothing
p = 1;
dKernel = factorial(p) / dt^p * g(:, p+1)'; % 1 x frameSize kernel
Counts_sm19 = conv2(Counts_sm19_prediff, dKernel, 'same');
PYTHON:
# Parameters for Savitzky-Golay filter
polynomialOrder = 2
frameSize = 19
# Calculate Savitzky-Golay filter coefficients
# [b, g] = sgolay(polynomialOrder, frameSize);
# Compute the sampling interval in energy
x = Energy
dt = x[1] - x[0] # Sampling interval in keV
Counts_sm19 = savgol_filter(Counts_sm19_prediff, frameSize, polynomialOrder, deriv=1, delta=dt, axis=1, mode="interp")
1 Comment
Harald
3 minutes ago
As a side note in case you were not aware, you can call MATLAB from Python and vice versa, e.g., via
Thus you have something something that works fine in MATLAB and something that works in Python, you can bring both pieces together without having to rewrite one way or another.
Answers (0)
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!