ChemometricsTools.jl icon indicating copy to clipboard operation
ChemometricsTools.jl copied to clipboard

Savitzky Golay filter

Open lnacquaroli opened this issue 4 years ago • 2 comments

Hi there,

Please see here. Apparently the implementation of the SG filter from Qlab is not fully correct. I check with yours here and I got the following:

julia> x = [1,2,3,4,5,6,7,8,9,10]
julia> SavitzkyGolay(x, 0, 1, 5)
1×10 LinearAlgebra.Adjoint{Float64,Array{Float64,1}}:
 1.2  2.0  3.0  4.0  5.0  6.0  7.0  8.0  6.8  5.4

It seems that the SG should produce the same input x for this case. I checked with Matlab sgolayfilt and it does give [1,2,3,4,5,6,7,8,9,10]. Looks like the edges are distorted. In the Qlab implementation we have the same problem:

savitzkyGolay2(x,5,1)
10-element Array{Float64,1}:
 1.6000000000000003
 2.200000000000001 
 3.0               
 4.0               
 5.000000000000001 
 6.000000000000001 
 7.0               
 8.0               
 8.8               
 9.400000000000002 

lnacquaroli avatar Nov 26 '19 15:11 lnacquaroli

Not sure what QLab is?

I've been having a bugger of a time with Sgolay, using DSP.jl broke my package a few weeks ago, and I had to write my own Conv1D function etc.

Yes the edges do appear distorted relative to Matlabs implementation. The tricky part is this, savitsky golay loses information at the edges by doing the convolution, it's inevitable. Basically it's an FIR filter if I recall correctly. So some implementations, I think, enforce different boundary conditions, IE: mirror, repeating, set to 0, etc. I haven't looked at your example yet - but I can try to make it match more closely to matlab if you want.

Thank you again for the heads up!

caseykneale avatar Nov 30 '19 14:11 caseykneale

Hi thanks for the reply.

Yeah, it was someone who actually posted that the data filter did not match that one from Matlab's. But, in any case, are just the edges, as you said. I actually found another SG implementation a bit old, using generated functions, that has the same issue.

The DSP.jl seems to work for me.

lnacquaroli avatar Dec 02 '19 15:12 lnacquaroli