我问这个问题是因为我不确定我应该使用哪个过滤器。

地雷仅仅是由离散值(例如s = [1 2 2 2 3 4 2 4 3 4 5 3 2 3 3])组成的信号。然后,我想按窗口大小,对信号进行滤波。所以例如如果我对s使用5的窗口大小,那么我会得到; s_filtered = [2 2 2 2 2 4 4 4 4 4 3 3 3 3 3]。因此,我想保留每个块中频率最高的值。对于索引0:4(窗口大小5),最高频率的值为2,所以我希望我的“滤波”信号(如果确实是正确的术语)在所有“滤波”信号的索引0:4中都具有2 。

目前,我仅使用中值滤波器,但我认为这不是正确的方法。

这是一些python代码来演示我在做什么(但是正如我所说,这是错误的)。

import numpy as np
import pylab *
from scipy.signal import medfilt

test = np.random.randint(10, size=1000)

fig, ax1 = plt.subplots(1,sharey=True, sharex=True, figsize=(15,5))
ax1.plot(test)
ax1.plot(medfilt(test,[99]),'r')
plt.show()


python - 我需要什么过滤器?只想保留高频值-LMLPHP

红线是窗口大小为99的滤波信号。

解:

import itertools
import collections

def grouper(iterable, n, fillvalue=None):
    args = [iter(iterable)] * n
    return itertools.izip_longest(*args, fillvalue=fillvalue)

s = [1, 2, 2, 2, 3, 4, 2, 4, 3, 4, 5, 3, 2, 3, 3]

list(chain.from_iterable(repeat(collections.Counter(x).most_common(1)[0][0],5) for x in grouper(s,5)))

最佳答案

您可以使用itertools recipes中的grouper函数根据指定的长度对数组进行分组,然后使用collections.Counter.most_common()方法找到最常见的项目,并使用itertools.repeat重复您的项目5次,最后链接重复的对象与itertools.chain.from_iterable

def grouper(iterable, n, fillvalue=None):
    args = [iter(iterable)] * n
    return zip_longest(*args, fillvalue=fillvalue)


演示:

>>> list(chain.from_iterable(repeat(Counter(x).most_common(1)[0][0],5) for x in grouper(s,5)))
[2, 2, 2, 2, 2, 4, 4, 4, 4, 4, 3, 3, 3, 3, 3]

关于python - 我需要什么过滤器?只想保留高频值,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/32890967/

10-10 01:44