Python 对一维 numpy 数组进行下采样

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20322079/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 20:12:04  来源:igfitidea点击:

Downsample a 1D numpy array

pythonnumpyscipysignal-processingresampling

提问by TheChymera

I have a 1-d numpy array which I would like to downsample. Any of the following methods are acceptable if the downsampling raster doesn't perfectly fit the data:

我有一个一维 numpy 数组,我想对其进行下采样。如果下采样栅格不完全适合数据,则可以接受以下任何方法:

  • overlap downsample intervals
  • convert whatever number of values remains at the end to a separate downsampled value
  • interpolate to fit raster
  • 重叠下采样间隔
  • 将末尾剩余的任意数量的值转换为单独的下采样值
  • 插值以适合光栅

basically if I have

基本上如果我有

1 2 6 2 1

and I am downsampling by a factor of 3, all of the following are ok:

并且我将采样率降低了 3 倍,以下所有情况都可以:

3 3

3 1.5

or whatever an interpolation would give me here.

或者任何插值会给我在这里。

I'm just looking for the fastest/easiest way to do this.

我只是在寻找最快/最简单的方法来做到这一点。

I found scipy.signal.decimate, but that sounds like it decimatesthe values (takes them out as needed and only leaves one in X). scipy.signal.resampleseems to have the right name, but I do not understand where they are going with the whole fourier thing in the description. My signal is not particularly periodic.

我发现了scipy.signal.decimate,但这听起来像是减少了值(根据需要将它们取出并只在 X 中留下一个)。 scipy.signal.resample似乎有正确的名称,但我不明白他们在描述中的整个傅立叶事物的去向。我的信号不是特别周期性。

Could you give me a hand here? This seems like a really simple task to do, but all these functions are quite intricate...

你能帮我一下吗?这似乎是一项非常简单的任务,但所有这些功能都非常复杂......

采纳答案by shx2

In the simple case where your array's size is divisible by the downsampling factor (R), you can reshapeyour array, and take the mean along the new axis:

在数组大小可被下采样因子 ( R)整除的简单情况下,您可以使用reshape数组,并沿新轴取平均值:

import numpy as np
a = np.array([1.,2,6,2,1,7])
R = 3
a.reshape(-1, R)
=> array([[ 1.,  2.,  6.],
         [ 2.,  1.,  7.]])

a.reshape(-1, R).mean(axis=1)
=> array([ 3.        ,  3.33333333])

In the general case, you can pad your array with NaNs to a size divisible by R, and take the mean using scipy.nanmean.

在一般情况下,您可以使用NaNs 将数组填充到可被 整除的大小R,并使用 取平均值scipy.nanmean

import math, scipy
b = np.append(a, [ 4 ])
b.shape
=> (7,)
pad_size = math.ceil(float(b.size)/R)*R - b.size
b_padded = np.append(b, np.zeros(pad_size)*np.NaN)
b_padded.shape
=> (9,)
scipy.nanmean(b_padded.reshape(-1,R), axis=1)
=> array([ 3.        ,  3.33333333,  4.])

回答by Manoj Singh

If array size is not divisible by downsampling factor (R), reshaping (splitting) of array can be done using np.linspace followed by mean of each subarray.

如果数组大小不能被下采样因子 (R) 整除,则可以使用 np.linspace 后跟每个子数组的平均值来完成数组的整形(拆分)。

input_arr = np.arange(531)

R = 150 (number of split)

split_arr = np.linspace(0, len(input_arr), num=R+1, dtype=int)

dwnsmpl_subarr = np.split(input_arr, split_arr[1:])

dwnsmpl_arr = np.array( list( np.mean(item) for item in dwnsmpl_subarr[:-1] ) )

回答by TimSC

Here are a few approaches using either linear interpolation or the Fourier method. These methods support upsampling as well as downsampling.

以下是使用线性插值或傅立叶方法的几种方法。这些方法支持上采样和下采样。

import numpy as np
import matplotlib.pyplot as plt
from scipy.signal import resample
from scipy.interpolate import interp1d

def ResampleLinear1D(original, targetLen):
    original = np.array(original, dtype=np.float)
    index_arr = np.linspace(0, len(original)-1, num=targetLen, dtype=np.float)
    index_floor = np.array(index_arr, dtype=np.int) #Round down
    index_ceil = index_floor + 1
    index_rem = index_arr - index_floor #Remain

    val1 = original[index_floor]
    val2 = original[index_ceil % len(original)]
    interp = val1 * (1.0-index_rem) + val2 * index_rem
    assert(len(interp) == targetLen)
    return interp

if __name__=="__main__":

    original = np.sin(np.arange(256)/10.0)
    targetLen = 100

    # Method 1: Use scipy interp1d (linear interpolation)
    # This is the simplest conceptually as it just uses linear interpolation. Scipy
    # also offers a range of other interpolation methods.
    f = interp1d(np.arange(256), original, 'linear')
    plt.plot(np.apply_along_axis(f, 0, np.linspace(0, 255, num=targetLen)))

    # Method 2: Use numpy to do linear interpolation
    # If you don't have scipy, you can do it in numpy with the above function
    plt.plot(ResampleLinear1D(original, targetLen))

    # Method 3: Use scipy's resample
    # Converts the signal to frequency space (Fourier method), then back. This
    # works efficiently on periodic functions but poorly on non-periodic functions.
    plt.plot(resample(original, targetLen))

    plt.show()