首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Python中的浮动RMS

Python中的浮动RMS
EN

Stack Overflow用户
提问于 2020-01-21 18:45:14
回答 1查看 154关注 0票数 0

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

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

代码:

代码语言:javascript
复制
import matplotlib.pyplot as plot
import numpy as np
import math


if __name__ == '__main__':
    # sine generation
    time_array = []
    value_array = []
    start = 0
    end = 6*math.pi
    steps = 100000
    amplitude = 10

    #rms calc
    acc_load_current = 0
    sample_size = 1000

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

        # rms calc
        acc_load_current -= (acc_load_current/sample_size)
        # square here
        sq_value = actual_value * actual_value
        acc_load_current += sq_value
    # mean and then root here

    floating_rms = np.sqrt(acc_load_current/sample_size)
    fixed_rms = np.sqrt(np.mean(np.array(value_array)**2))
    math_rms = 1/math.sqrt(2) * amplitude

    print(floating_rms)
    print(fixed_rms)
    print(math_rms)

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

结果:

代码语言:javascript
复制
2.492669969708522
7.071032456438027
7.071067811865475

EN

回答 1

Stack Overflow用户

发布于 2020-01-21 23:23:58

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

代码语言:javascript
复制
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()
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59839333

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档