如何从 numpy 数组中删除仅噪声行?

How to remove noise-only rows from a numpy array?

我想从 Numpy 数组中删除行,其中只有噪声和随机常量偏移量。

我的数据与此类似:

offset = np.array([0.2, 3.2])
signal = np.sin(np.arange(0, 2 * np.pi, 0.1))
null = np.zeros_like(signal)
data_block = np.array([signal, null]).T
padding = np.zeros((10, 2))
block = np.vstack((padding, data_block, padding)) + offset
# add noise
shape = block.shape
noise = np.random.rand(shape[0], shape[1]) * .01
everything = noise + block

实际上,没有任何步骤,而是从偏移量到数据的平滑过渡。就好像先有一个偏移量,一旦数据块开始,它就开始移动,停止时就变成另一个偏移量。噪声幅度远小于数据幅度。

我想从everything中检索带有数据块的行,最好基于数据块中连续、平滑的变化。我该怎么做?

这是我识别 data_block 的最大努力。如果可以改进我会很高兴!

import numpy as np

offset = np.array([0.2, 3.2])
signal = np.sin(np.arange(0, 2 * np.pi, 0.1))
null = np.zeros_like(signal)
data_block = np.array([signal, null]).T
padding = np.zeros((10, 2))
block = np.vstack((padding, data_block, padding)) + offset
# add noise
shape = block.shape
noise = np.random.rand(shape[0], shape[1]) * .01
everything = noise + block

from matplotlib import pyplot as plt

x = np.arange(shape[0])
plt.plot(x, everything[:, 0])
plt.plot(x, everything[:, 1])
plt.show()

diff_everything = np.diff(everything, axis=0)
x = np.arange(shape[0] - 1)
plt.plot(x, diff_everything[:, 0])
plt.plot(x, diff_everything[:, 1])
plt.show()

mask = (np.linalg.norm(diff_everything[:, :], axis=1) > 0.01)
mask = np.append(mask, False)
data = everything[mask, :]
shape = data.shape
x=np.arange(0,shape[0])
plt.plot(x, data[:, 0])
plt.plot(x, data[:, 1])

plt.show()