Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/346.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python中的浮动RMS_Python_Math - Fatal编程技术网

Python中的浮动RMS

Python中的浮动RMS,python,math,Python,Math,我试图用python实现一个浮动窗口RMS。我通过简化迭代时间和计算正弦波来模拟输入的测量数据流。因为它是一个完美的正弦波,很容易用数学来比较结果。我还添加了一个numpy计算来确认数组是否正确填充 但是,我的浮动RMS没有返回正确的值,这与我的样本大小无关 代码: 结果: 2.492669969708522 7.071032456438027 7.071067811865475 我通过使用带过零检测的递归平均解决了这个问题: import matplotlib.pyplot as plot

我试图用python实现一个浮动窗口RMS。我通过简化迭代时间和计算正弦波来模拟输入的测量数据流。因为它是一个完美的正弦波,很容易用数学来比较结果。我还添加了一个numpy计算来确认数组是否正确填充

但是,我的浮动RMS没有返回正确的值,这与我的样本大小无关

代码:

结果:

2.492669969708522
7.071032456438027
7.071067811865475

我通过使用带过零检测的递归平均解决了这个问题:

import matplotlib.pyplot as plot
import numpy as np
import math


def getAvg(prev_avg, x, n):
    return (prev_avg * n + x) / (n+1)

if __name__ == '__main__':
    # sine generation
    time_array = []
    value_array = []
    used_value_array = []
    start = 0
    end = 6*math.pi + 0.5
    steps = 10000
    amplitude = 325

    #rms calc
    rms_stream = 0
    stream_counter = 0
    #zero crossing
    in_crossing = 0
    crossing_counter = 0
    crossing_limits = [-5,5]
    left_crossing = 0

    for time in np.linspace(0, end, steps):
        time_array.append(time)
        actual_value = amplitude * math.sin(time) + 4 * np.random.rand()
        value_array.append(actual_value)

        # detect zero crossing, by checking the first time we reach the limits
        # and then not counting until we left it again
        is_crossing = crossing_limits[0] < actual_value < crossing_limits[1]
        # when we are at amp/2 we can be sure the noise is not causing zero crossing
        left_crossing = abs(actual_value) > amplitude/2
        if is_crossing and not in_crossing:
            in_crossing = 1
            crossing_counter += 1
        elif not is_crossing and in_crossing and left_crossing:
            in_crossing = 0

        # rms calc
        # square here
        if 2 <= crossing_counter <= 3:
            sq_value = actual_value * actual_value
            rms_stream = getAvg(rms_stream, sq_value, stream_counter)
            stream_counter += 1
            # debugging by recording the used values
            used_value_array.append(actual_value)
        else:
            used_value_array.append(0)

    # mean and then root here
    stream_rms_sqrt = np.sqrt(rms_stream)

    fixed_rms_sqrt = np.sqrt(np.mean(np.array(value_array)**2))
    math_rms_sqrt = 1/math.sqrt(2) * amplitude

    print(stream_rms_sqrt)
    print(fixed_rms_sqrt)
    print(math_rms_sqrt)

    plot.plot(time_array, value_array, time_array, used_value_array)
    plot.show()
将matplotlib.pyplot导入为绘图
将numpy作为np导入
输入数学
def getAvg(上一个平均值,x,n):
返回值(上一个平均值*n+x)/(n+1)
如果uuuu name uuuuuu='\uuuuuuu main\uuuuuuu':
#正弦产生
时间数组=[]
值_数组=[]
使用的_值_数组=[]
开始=0
结束=6*math.pi+0.5
步数=10000
振幅=325
#rms计算
rms_流=0
流_计数器=0
#过零
in_交叉=0
交叉计数器=0
交叉限制=[-5,5]
左交叉=0
对于np.linspace中的时间(0,结束,步):
时间\数组.append(时间)
实际_值=振幅*math.sin(时间)+4*np.rand.rand()
值\u数组.append(实际值)
#通过检查第一次达到极限来检测过零
#然后不算,直到我们再次离开
是否交叉=交叉限制[0]<实际值<交叉限制[1]
#当我们处于amp/2时,我们可以确定噪声不会导致过零
左交叉=abs(实际值)>振幅/2
如果是交叉口而不是交叉口:
in_交叉=1
交叉_计数器+=1
elif不在交叉口、在交叉口和左交叉口:
in_交叉=0
#rms计算
#这里是正方形

若2,我希望在某个地方有一个窗口大小的数组。那将是
样本大小
?此外,在数值计算中,我会先用零填充它,以查看瞬态行为。
import matplotlib.pyplot as plot
import numpy as np
import math


def getAvg(prev_avg, x, n):
    return (prev_avg * n + x) / (n+1)

if __name__ == '__main__':
    # sine generation
    time_array = []
    value_array = []
    used_value_array = []
    start = 0
    end = 6*math.pi + 0.5
    steps = 10000
    amplitude = 325

    #rms calc
    rms_stream = 0
    stream_counter = 0
    #zero crossing
    in_crossing = 0
    crossing_counter = 0
    crossing_limits = [-5,5]
    left_crossing = 0

    for time in np.linspace(0, end, steps):
        time_array.append(time)
        actual_value = amplitude * math.sin(time) + 4 * np.random.rand()
        value_array.append(actual_value)

        # detect zero crossing, by checking the first time we reach the limits
        # and then not counting until we left it again
        is_crossing = crossing_limits[0] < actual_value < crossing_limits[1]
        # when we are at amp/2 we can be sure the noise is not causing zero crossing
        left_crossing = abs(actual_value) > amplitude/2
        if is_crossing and not in_crossing:
            in_crossing = 1
            crossing_counter += 1
        elif not is_crossing and in_crossing and left_crossing:
            in_crossing = 0

        # rms calc
        # square here
        if 2 <= crossing_counter <= 3:
            sq_value = actual_value * actual_value
            rms_stream = getAvg(rms_stream, sq_value, stream_counter)
            stream_counter += 1
            # debugging by recording the used values
            used_value_array.append(actual_value)
        else:
            used_value_array.append(0)

    # mean and then root here
    stream_rms_sqrt = np.sqrt(rms_stream)

    fixed_rms_sqrt = np.sqrt(np.mean(np.array(value_array)**2))
    math_rms_sqrt = 1/math.sqrt(2) * amplitude

    print(stream_rms_sqrt)
    print(fixed_rms_sqrt)
    print(math_rms_sqrt)

    plot.plot(time_array, value_array, time_array, used_value_array)
    plot.show()