Consider the design of a Savitzky-Golay filter of window length 101 and (high) polynomial order 20. Using scipy version 1.10.1, the filter coefficients can be obtained in python as:

from scipy import signal
h = signal.savgol_coeffs(window_length=101, polyorder=20)

leading to

enter image description here

For reference, in Matlab r2022b

order=20; framelen=101; b = sgolay(order,framelen); h = b((framelen+1)/2,:);

gives enter image description here

which looks more reasonable. For lower order polynomials, the results coincide much more closely.

It looks like scipy's implementation solves a least squares problem where the design matrix is computed as:

 order = np.arange(polyorder + 1).reshape(-1, 1)
 A = x ** order

leading to a condition number > 1e29!

Is there some other way to get these coefficients in python?

  • 1
    $\begingroup$ Do you need to compute these at run-time? If not, you could simply store the Matlab-computed coefficients in a file and use these in your Python application $\endgroup$
    – Jdip
    Commented Aug 8, 2023 at 9:25
  • $\begingroup$ Indeed, that's the approach that I've wound up taking. $\endgroup$
    – rhz
    Commented Aug 8, 2023 at 16:25

1 Answer 1


Some languages offers higher accuracy data types.
For instance, on Python, you may use the BigFloat package.
There is always enough bits you can throw at it and get something working.

Yet a better approach is to use a different approach.
Since you're after a polynomial like model, you could use Kalman Filter to get similar results with better numerical properties.
The trick here will be tweaking the ratio between $ \boldsymbol{Q} $ and $ \boldsymbol{R} $ to have similar results.

With some tweaking and hyper parameters grid search you will be able to easily do so.

Remark: If you share some signals we can try to implement the approach.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.