Resample a numpy array

前端 未结 5 1250
梦如初夏
梦如初夏 2020-12-14 03:06

It\'s easy to resample an array like

 a = numpy.array([1,2,3,4,5,6,7,8,9,10])

with an integer resampling factor. For instance, wi

5条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-14 03:40

    In signal processing, you can think of resampling as basically rescaling the array and interpolating the missing values or values with non-integer index using nearest, linear, cubic, etc methods.

    Using scipy.interpolate.interp1d, you can achieve one dimensional resampling using the following function

    def resample(x, factor, kind='linear'):
        n = np.ceil(x.size / factor)
        f = interp1d(np.linspace(0, 1, x.size), x, kind)
        return f(np.linspace(0, 1, n))
    

    e.g.:

    a = np.array([1,2,3,4,5,6,7,8,9,10])
    resample(a, factor=1.5, kind='linear')
    

    yields

    array([ 1. ,  2.5,  4. ,  5.5,  7. ,  8.5, 10. ])
    

    and

    a = np.array([1,2,3,4,5,6,7,8,9,10])
    resample(a, factor=1.5, kind='nearest')
    

    yields

    array([ 1.,  2.,  4.,  5.,  7.,  8., 10.])
    

提交回复
热议问题