#NEWLINE# def savitzky_golay( y , window_size , order , deriv = 0 ) : #NEWLINE# #INDENT# "Smooth (and optionally differentiate) data with a Savitzky-Golay\n    filter.  The Savitzky-Golay filter removes high frequency noise\n    from data.  It has the advantage of preserving the original shape\n    and features of the signal better than other types of filtering\n    approaches, such as moving averages techhniques.\n    \n    Parameters\n    ----------\n    y : array_like, shape (N,)\n        the values of the time history of the signal.\n    window_size : int\n                  the length of the window. Must be an odd integer\n                  number.\n    order : int\n            the order of the polynomial used in the filtering. Must be\n            less then `window_size` - 1.\n    deriv: int\n           the order of the derivative to compute (default = 0 means\n           only smoothing)\n\n    Returns\n    -------\n    ys : ndarray, shape (N)\n         the smoothed signal (or it's n-th derivative).\n\n    Notes\n    -----\n    The Savitzky-Golay is a type of low-pass filter, particularly\n    suited for smoothing noisy data. The main idea behind this\n    approach is to make for each point a least-square fit with a\n    polynomial of high order over a odd-sized window centered at the\n    point.\n\n    Examples\n    --------\n    t = np.linspace(-4, 4, 500)\n    y = np.exp( -t**2 ) + np.random.normal(0, 0.05, t.shape)\n    ysg = savitzky_golay(y, window_size=31, order=4)\n    import matplotlib.pyplot as plt\n    plt.plot(t, y, label='Noisy signal')\n    plt.plot(t, np.exp(-t**2), 'k', lw=1.5, label='Original signal')\n    plt.plot(t, ysg, 'r', label='Filtered signal')\n    plt.legend()\n    plt.show()\n    \n    References\n    ----------\n    .. [1] A. Savitzky, M. J. E. Golay, Smoothing and Differentiation\n           of Data by Simplified Least Squares Procedures. Analytical\n           Chemistry, 1964, 36 (8), pp 1627-1639.\n\n    .. [2] Numerical Recipes 3rd Edition: The Art of Scientific\n           Computing W.H. Press, S.A. Teukolsky, W.T. Vetterling,\n           B.P. Flannery Cambridge University Press ISBN-13:\n           9780521880688\n    " #NEWLINE# try : #NEWLINE# #INDENT# window_size = abs ( int ( window_size ) ) #NEWLINE# order = abs ( int ( order ) ) #NEWLINE# #UNINDENT# except ValueError ( msg ) : #NEWLINE# #INDENT# raise ValueError ( 'window_size and order have to be of type int' ) #NEWLINE# #UNINDENT# if ( ( ( window_size % 2 ) != 1 ) or ( window_size < 1 ) ) : #NEWLINE# #INDENT# raise TypeError ( 'window_size size must be a positive odd number' ) #NEWLINE# #UNINDENT# if ( window_size < ( order + 2 ) ) : #NEWLINE# #INDENT# raise TypeError ( 'window_size is too small for the polynomials order' ) #NEWLINE# #UNINDENT# order_range = range ( ( order + 1 ) ) #NEWLINE# half_window = ( ( window_size - 1 ) // 2 ) #NEWLINE# b = np . mat ( [ [ ( k ** i ) for i in order_range ] for k in range ( ( - half_window ) , ( half_window + 1 ) ) ] ) #NEWLINE# m = np . linalg . pinv ( b ) . A [ deriv ] #NEWLINE# firstvals = ( y [ 0 ] - np . abs ( ( y [ 1 : ( half_window + 1 ) ] [ : : ( - 1 ) ] - y [ 0 ] ) ) ) #NEWLINE# lastvals = ( y [ ( - 1 ) ] + np . abs ( ( y [ ( ( - half_window ) - 1 ) : ( - 1 ) ] [ : : ( - 1 ) ] - y [ ( - 1 ) ] ) ) ) #NEWLINE# y = np . concatenate ( ( firstvals , y , lastvals ) ) #NEWLINE# return np . convolve ( m , y , mode = 'valid' )