[Scipy-tickets] [SciPy] #1706: Error in Cookbook "Savitzky Golay"

SciPy Trac scipy-tickets@scipy....
Sun Jul 22 06:58:04 CDT 2012

#1706: Error in Cookbook "Savitzky Golay"
 Reporter:  thomas.haslwanter        |       Owner:  pv         
     Type:  defect                   |      Status:  new        
 Priority:  normal                   |   Milestone:  Unscheduled
Component:  Documentation            |     Version:  0.10.0     
 Keywords:  Cookbook Savitzky-Golay  |  

Comment(by thomas.haslwanter):

 The Cookbook entry http://www.scipy.org/Cookbook/SavitzkyGolay has a
 number of problems:

 1) As it is, it does in general not run. The line

     "import numpy as np"

 should be inserted before the first "try" in line 49

 In addition, there are a few mistakes which only shows up for derivatives.
 The attached code sample should fix that.

 Since the Savitzky-Golay filter is a very commonly used filter, I regard
 this mistake as significant.

 I would make those changes myself, but have not been able to figure out
 how exactly to proceed. Any tips how I have to proceed to contribute would
 be appreciated!


 def savitzky_golay(y, window_size, order, deriv=0, rate=1):
     import numpy as np
     from math import factorial

         window_size = np.abs(np.int(window_size))
         order = np.abs(np.int(order))
     except ValueError, msg:
         raise ValueError("window_size and order have to be of type int")
     if window_size % 2 != 1 or window_size < 1:
         raise TypeError("window_size size must be a positive odd number")
     if window_size < order + 2:
         raise TypeError("window_size is too small for the polynomials
     order_range = range(order+1)
     half_window = (window_size -1) // 2
     # precompute coefficients
     b = np.mat([[k**i for i in order_range] for k in range(-half_window,
     m = np.linalg.pinv(b).A[deriv] * rate**deriv * factorial(deriv)
     # pad the signal at the extremes with
     # values taken from the signal itself
     firstvals = y[0] - np.abs( y[1:half_window+1][::-1] - y[0] )
     lastvals = y[-1] + np.abs(y[-half_window-1:-1][::-1] - y[-1])
     y = np.concatenate((firstvals, y, lastvals))
     return np.convolve( m[::-1], y, mode='valid')

Ticket URL: <http://projects.scipy.org/scipy/ticket/1706#comment:1>
SciPy <http://www.scipy.org>
SciPy is open-source software for mathematics, science, and engineering.

More information about the Scipy-tickets mailing list