我们如何通过使用 float 和 int window 大小的平均方法对一维数组值进行下采样?

How we can down sample a 1D array values by averaging method using float and int window sizes?

我正在尝试使用平均法将固定的 [Mx1] 向量向下采样到任何给定的 [Nx1] 维度。我有一个动态的 window 大小,每次都会根据所需的输出数组进行更改。所以,在某些情况下,我很幸运,得到的 window 大小的整数完全符合 window 的大小,有时我得到的浮点数是 windows 的大小。但是,我如何使用浮动大小 windows 从固定的 [Mx1] 向量中生成 [Nx1] 大小的向量?

下面是我试过的代码:

chunk = 0.35
def fixed_meanVector(vec, chunk):
   size = (vec.size*chunk) #size of output according to the chunk
   R    = (vec.size/size) #windows size to transform array into chunk size
   pad_size = math.ceil(float(vec.size)/R)*R - vec.size
   vec_padded = np.append(vec, np.zeros(pad_size)*np.NaN)

   print "Org Vector: ",vec.size, "output Size: ",size, "Windows Size: ",R, "Padding size", pad_size
   newVec = scipy.nanmean(vec_padded.reshape(-1,R), axis=1)
   print "New Vector shape: ",newVec.shape
   return newVec

print "Word Mean of N values Similarity: ",cosine(fixed_meanVector(vector1, chunk)
                                                      ,fixed_meanVector(vector2, chunk))

输出:

New Vector shape:  (200,)
Org Vector:  400 output Size:  140.0 Windows Size:  2.85714285714 Padding  size 0.0
New Vector shape:  (200,)
0.46111661289

在上面的例子中,我需要向下采样 [Mx1] ([400x1]) vector in Nx1 ([140x1]) 尺寸。因此,动态 window size [2.857x1] 可用于下采样 [Mx1] vector 。但是,在这种情况下,我得到一个 [200x1] 的向量作为我的输出而不是 [140x1] 由于浮动 window 它提高到 flour(2.85) 使用 -> [2x1] 进行下采样。 填充为零,因为我的 window 尺寸非常适合新的 [Nx1] 尺寸。那么,有什么方法可以使用这种类型的 windows 大小来对 [Mx1] 向量进行下采样?

一旦 M%N>0 就可以对其进行矢量化,但并不自然。因为用于构建结果数组的单元格数量不是恒定的,在您的情况下在 3 到 4 之间。

自然的方法是 运行 遍历数组,在每个 bin 处调整:

想法是填满每个箱子直到溢出。然后切断溢出(进位)并将其保留在下一个垃圾箱中。使用 int 算术,最后一个进位始终为 null。

代码:

def resized(data,N):
    M=data.size
    res=empty(N,data.dtype)
    carry=0
    m=0
    for n in range(N):
        sum = carry
        while m*N - n*M < M :
            sum += data[m]
            m += 1
        carry = (m-(n+1)*M/N)*data[m-1]
        sum -= carry
        res[n] = sum*N/M
    return res

测试:

In [5]: resized(np.ones(7),3)
Out[5]: array([ 1.,  1.,  1.])

In [6]: %timeit resized(rand(400),140)
    1000 loops, best of 3: 1.43 ms per loop

它有效,但不是很快。幸运的是,您可以使用 numba 加速它:

from numba import jit
resized2=jit(resized)             

In [7]: %timeit resized2(rand(400),140)
1 loops, best of 3: 8.21 µs per loop

可能比任何纯 numpy 解决方案都快(此处为 N=3*M):

IN [8]: %timeit rand(402).reshape(-1,3).mean(1)
10000 loops, best of 3: 39.2 µs per loop

请注意,如果 M>N.

它也有效
In [9]: resized(arange(4.),9)
Out[9]: array([ 0.  ,  0.  ,  0.75,  1.  ,  1.5 ,  2.  ,  2.25,  3.  ,  3.  ])

你做错了,你为你需要的抽取建立了一个window,而不是相反。

Mr Nyquist 说你的 BW 不能超过 fs/2,否则你会有讨厌的别名。

所以要解决这个问题,你不仅要 "average",还要低通,这样 fs/2 以上的频率低于你可接受的本底噪声。

MA's 是一种有效的低通滤波器类型,您只是将其应用于错误的数组。

arbitrary decimation 的通常情况是。

 Upsample -> Lowpass -> Downsample

因此,为了能够从 N 到 M 个样本中任意抽取,算法是:

  • find LCM between your current samples your target samples.
  • upsample by LCM/N
  • design a filter using a stop frequency ws<= M/LCM
  • downsample by LCM/M

你所谓的平均法,是一个具有rectangular window

的FIR滤波器

如果您使用 frequency response in that window 的第一个零作为阻带,那么您可以计算出所需的 window 尺寸 K ,如

2/K <= M/LCM

因此您必须使用 windows 大小:

ceil(2*LCM/M) = K

显然,您不需要实现所有这些。只需使用 ws<= M/LCM 设计一个合适的 window 并使用 scipy.signal.resample.

应用它

如果应用于 window 的 ceil 弄乱了你的结果,请不要使用矩形 windows,你可以使用很多更好的过滤器。