确定平均过零点

时间:2015-10-02 10:44:59

标签: python smooth

使用numpy我已经提取了信号的过零点。

不幸的是,数据源是有噪声的,因此存在多个过零点。

如果我在检查过零点之前过滤数据,则需要证明滤波器的某些方面(增益相位裕度)是合理的,而平均过零点更容易证明

[123,125,127,1045,1049,1050,2147,2147,2151,2155]

考虑以上列表。什么是合适的创建方式:

[125, 1048, 2149]

目的是找到两个正弦波之间的相移

2 个答案:

答案 0 :(得分:0)

这段代码采用了一种简单的方法来寻找转换之间的间隙THRESHOLD - 超过这标志着信号转换的结束。

xings = [123,125,127,1045,1049,1050,2147,2147,2151,2155]

THRESHOLD = 100

xlast = -1000000
tot = 0
n = 0
results = []
i = 0
while i < len(xings):
    x = xings[i]
    if x-xlast > THRESHOLD:
        # emit a transition, averaged for the
        if n > 0:
            results.append(tot/n)
        tot = 0
        n = 0
    tot += x
    n += 1
    xlast = x
    i += 1
if n > 0:
    results.append(tot/n)

print results

打印:

[125, 1048, 2150]

答案 1 :(得分:0)

我希望有一个更优雅的解决方案来迭代过零列表,但似乎这是唯一的解决方案。

我决定:

def zero_crossing_avg(data):
    output = []
    running_total = data[0]
    count = 1
    for i in range(1,data.size):
        val = data[i]
        if val - data[i-1] < TOL:
            running_total += val
            count += 1
        else:
            output.append(round(running_total/count))
            running_total = val
            count = 1
    return output

使用它的示例代码:

#!/usr/bin/env python

import numpy as np
from matplotlib import pyplot as plt

dt = 5e-6
TOL = 50


class DCfilt():
    def __init__(self,dt,freq):
        self.alpha = dt/(dt + 1/(2*np.pi*freq))
        self.y = [0,0]
    def step(self,x):
        y = self.y[-1] + self.alpha*(x - self.y[-1])
        self.y[-1] = y
        return y

def zero_crossing_avg(data):
    output = []
    running_total = data[0]
    count = 1
    for i in range(1,data.size):
        val = data[i]
        if val - data[i-1] < TOL:
            running_total += val
            count += 1
        else:
            output.append(round(running_total/count))
            running_total = val
            count = 1
    return output





t = np.arange(0,2,dt)
print(t.size)
rng = (np.random.random_sample(t.size) - 0.5)*0.1
s = 10*np.sin(2*np.pi*t*10  + np.pi/12)+rng
c = 10*np.cos(2*np.pi*t*10)+rng

filt_s = DCfilt(dt,16000)
filt_s.y[-1] =s[0]
filt_c = DCfilt(dt,1600)
filt_c.y[-1] =c[0]

# filter the RAW data first
for i in range(s.size):
    s[i] = filt_s.step(s[i])
    c[i] = filt_c.step(c[i])

# determine the zero crossings
s_z = np.where(np.diff(np.sign(s)))[0]
c_z = np.where(np.diff(np.sign(c)))[0]

sin_zc = zero_crossing_avg( np.where(np.diff(np.sign(s)))[0] )  
cos_zc = zero_crossing_avg( np.where(np.diff(np.sign(c)))[0] )  


HALF_PERIOD = (sin_zc[1] - sin_zc[0])
for i in range([len(sin_zc),len(cos_zc)][len(sin_zc) > len(cos_zc)]):
    delta  = abs(cos_zc[i]-sin_zc[i])
    print(90 - (delta/HALF_PERIOD)*180)


plt.hold(True)
plt.grid(True)

plt.plot(s)
plt.plot(c)
plt.show()

这很好用。