1
$\begingroup$

I am trying to implement a Savitzky-Golay filter by following this Wikipedia page and in the first instance I have written the following Octave software code to create the convolution coefficients

clear all

m = input( 'Enter number of bars (must be an odd number): ' ) ;

% check no_bars is odd
if mod( m , 2 ) == 0 % is even, report error

fprintf( '\nYou have entered an even number: try again.\n\n' ) ;

else % is odd

% create z 
z = ( ( 1 - m ) / 2 : 1 : ( m - 1 ) / 2 )' ;

% create Jacobian matrix J for a cubic polynomial
J = ones( m , 4 ) ;
J( : , 2 ) = z ;
J( : , 3 ) = z .* z ;
J( : , 4 ) = z .* z .* z ;

% create convolution matrix C & show in terminal
C = inv( J' * J ) * J'

end

which gives a matrix output where the first line are the coefficients for the smooth, second line are coefficients for the 1st derivative, third line are coefficients for the 2nd derivative etc. However, when I check this program's output with the selected coefficients given in the Wikipedia page appendix there is disagreement.

To do this check I take the normalisation figures given in the tables, e.g. 35 for smoothing coefficients for a window of length 5 for a cubic fit, and multiply the matrix by this number, which will then transform the relevant row of the matrix into the coefficients given in the tables. Following this procedure I see that for the actual smoothing coefficients and the coefficients for the 1st derivative for window lengths 5, 7 and 9 for a cubic fit there is agreement. However, for the 2nd and 3rd derivatives there is no agreement. In fact to get agreement the normalisation figures in the table would need to be changed thus:

  • window length 5 - 2nd deriv = 14, 3rd deriv = 12
  • window length 7 - 2nd deriv = 84, 3rd deriv = 36
  • window length 9 - 2nd deriv = 924, 3rd deriv = 1188

Therefore I am now uncertain whether I have understood the Wikipedia page and have correctly implemented the above code, or whether in fact the Wikipedia page is wrong in this regard. Can anyone point out where the problem is?

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.