Window overlap in Pandas

后端 未结 2 1561
庸人自扰
庸人自扰 2021-01-03 00:30

In pandas, there are several methods to manipulate data in a given window (e.g. pd.rolling_mean or pd.rolling_std.) However, I would like to set a

2条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2021-01-03 01:02

    I am not familiar with pandas, but in numpy you would do it something like this (untested):

    def overlapped_windows(x, nwin, noverlap = None):
        if noverlap is None:
            noverlap = nwin // 2
        step = nwin - noverlap
        for i in range(0, len(x) - nwin + 1, step):
            window = x[i:i+nwin] #this is a view, not a copy
            y = window * hann(nwin)
            #your code here with y
    

    This is ripped from some old code to calculate an averaged PSD, which you typically process with half-overlapping windows. Note that window is a 'view' into array x, which means it does not do any copying of data (very fast, so probably good) and that if you modify window you also modify x (so dont do window = hann * window).

提交回复
热议问题