确定平均过零

determine mean zero crossing

我使用 numpy 提取了信号的零交叉点。

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

如果我在检查过零之前过滤数据,则需要证明滤波器的各个方面(增益相位裕度)是合理的,而对零交叉点取平均值稍微更容易证明是合理的

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

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

[125, 1048, 2149]

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

此代码采用一种简单的方法来寻找转换之间的间隙 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]

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

我决定:

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()

这很好用。