Savitzky-Golay filters
Table of Contents
1 Introduction
In the spectrometry community, Savitzky-Golay filters were a common approach to smooth signal (before the Wavelets era).
Full API documentation is described in the Savitzky-Golay Filters manual section.
2 Savitzky-Golay filters
2.1 Filter coefficients
Creates a set of Savitzky-Golay filters. In this example, window width is \(11=2*5+1\) and polynomial degree is \(3\).
sg = SG_Filter(Float64,halfWidth=5,degree=3);
This can be checked with
length(sg) polynomialOrder(sg)
11 3
Savitzky-Golay filters can be used to smooth or to compute smoothed
derivatives of a signal. The associated filter is obtained thanks to
the filter
function.
d0filter=filter(sg,derivativeOrder=0); d1filter=filter(sg,derivativeOrder=1); d2filter=filter(sg,derivativeOrder=2); p=plot(range(d0filter),fcoef(d0filter), markershape = :hexagon,label="smoothing filter") p=plot!(range(d1filter),fcoef(d1filter),markershape = :hexagon,label="d_1 filter") p=plot!(range(d2filter),fcoef(d2filter),markershape = :hexagon,label="d_2 filter")
Figure 1: Savitzky-Golay filters
Maximum derivative order is equal to the polynomial order, however there is a dedicated function:
maxDerivativeOrder(sg)
3
2.2 Smoothing example
First load a signal
signal=readcsv(joinpath(DirectConvolution.RootDir,"data/signal_1.csv"));
signal=signal[:,2];
then compute a smoothed version
sg = SG_Filter(Float64,halfWidth=5,degree=3); smoothed=apply_SG_filter(signal,sg,derivativeOrder=0) p=plot(signal,label="signal") p=plot!(smoothed, label="smoothed",linewidth=2) p=plot!(signal-smoothed, label="residue")
Figure 2: Savitzky-Golay smoothing
2.3 First order smoothed derivative
Smoothed first order derivative example:
smoothed_d1=apply_SG_filter(signal,sg,derivativeOrder=1) p=plot(signal,label="signal") p=plot!(smoothed_d1, label="smoothed derivative",linewidth=2)
Figure 3: Savitzky-Golay smoothed first order derivative
2.4 2D example
Signal is:
signal2D=readdlm(joinpath(DirectConvolution.RootDir,"data/surface.data")); surface(signal2D,label="2D signal");
Following Smoothing example we can perform 2D smoothing as follows:
sg_I = SG_Filter(Float64,halfWidth=5,degree=3); sg_J = SG_Filter(Float64,halfWidth=3,degree=3); smoothed2D = apply_SG_filter2D(signal2D, sg_I, sg_J, derivativeOrder_I=0, derivativeOrder_J=0) surface(smoothed2D,label="Smoothed 2D signal");
Caveat: the function name is apply_SG_filter2D
and not apply_SG_filter