Scipy implementation of Savitzky-Golay filter

后端 未结 1 1029
时光说笑
时光说笑 2021-01-21 10:36

I was looking at the scipy cookbook implementation of the Savitzky-Golay algorithm:

#!python
def savitzky_golay(y, window_size, order, deriv=0, rate=1):
    r\"\         


        
相关标签:
1条回答
  • 2021-01-21 11:37

    Indeed, this logic isn't right, which can be best seen by considering the case of y[0] and y[-1] being 0. I believe the intent was to achieve odd reflection, so that the first derivative would be continuous at the reflection point. The correct form for that is

    firstvals = 2*y[0] - y[1:half_window+1][::-1]
    lastvals = 2*y[-1] - y[-half_window-1:-1][::-1]
    

    or, combining reversing and slicing in one step,

    firstvals = 2*y[0] - y[half_window:0:-1]
    lastvals = 2*y[-1] - y[-2:-half_window-2:-1]
    

    I should emphasize this is just some code contributed by a user. The actual Scipy implementation of Savitzky-Golay filter is entirely different.

    0 讨论(0)
提交回复
热议问题