filtering - How does the sgolay function work in Matlab R2013a? -
i have question sgolay
function in matlab r2013a. database has 165 spectra 2884 variables , take first , second derivatives of them. how might define inputs k
, f
sgolay
?
below example:
sgolay
used smooth noisy sinusoid , compare resulting first , second derivatives first , second derivatives computed using diff
. notice how using diff
amplifies noise , generates useless results.
k = 4; % order of polynomial fit f = 21; % window length [b,g] = sgolay(k,f); % calculate s-g coefficients dx = .2; xlim = 200; x = 0:dx:xlim-1; y = 5*sin(0.4*pi*x)+randn(size(x)); % sinusoid noise halfwin = ((f+1)/2) -1; n = (f+1)/2:996-(f+1)/2, % zero-th derivative (smoothing only) sg0(n) = dot(g(:,1), y(n - halfwin: n + halfwin)); % 1st differential sg1(n) = dot(g(:,2), y(n - halfwin: n + halfwin)); % 2nd differential sg2(n) = 2*dot(g(:,3)', y(n - halfwin: n + halfwin))'; end sg1 = sg1/dx; % turn differential derivative sg2 = sg2/(dx*dx); % , 2nd derivative % scale "diff" results diffd1 = (diff(y(1:length(sg0)+1)))/ dx; diffd2 = (diff(diff(y(1:length(sg0)+2)))) / (dx*dx); subplot(3,1,1); plot([y(1:length(sg0))', sg0']) legend('noisy sinusoid','s-g smoothed sinusoid') subplot(3, 1, 2); plot([diffd1',sg1']) legend('diff-generated 1st-derivative', 's-g smoothed 1st-derivative') subplot(3, 1, 3); plot([diffd2',sg2']) legend('diff-generated 2nd-derivative', 's-g smoothed 2nd-derivative')
taking derivatives in inherently noisy process. thus, if have noise in data, indeed, magnified take higher order derivatives. savitzky-golay useful way of combining smoothing , differentiation 1 operation. it's general method , computes derivatives arbitrary order. there trade-offs, though. other special methods exist data structure.
in terms of application, don't have concrete answers. depends on nature of data (sampling rate, noise ratio, etc.). if use smoothing, you'll smear data or produce aliasing. same thing if over-fit data using high order polynomial coefficients, k
. in demo code should plot analytical derivatives of sin
function. play different amounts of input noise , smoothing filters. such tool know exact answers may helpful if can approximate aspects of real data. in practice, try use little smoothing possible in order produce derivatives aren't noisy. means third-order polynomial (k = 3
) , window size, f
, small possible.
so yes, many suggest use eyes tune these parameters. however, there has been recent research on choosing coefficients automatically: on selection of optimum savitzky-golay filters (2013). there alternatives savitzky-golay, e.g., this paper based on regularization, may need implement them in matlab.
by way, while wrote little replacement sgolay
. you, needed second output, differentiation filters, g
, that's calculates. function faster (by 2–4 times):
function g=sgolayfilt(k,f) %sgolayfilt savitzky-golay differentiation filters s = vander(0.5*(1-f):0.5*(f-1)); s = s(:,f:-1:f-k); [~,r] = qr(s,0); g = s/r/r';
a full version of function input validation available on github.
Comments
Post a Comment