从np.vector绘制RMS

时间:2018-10-29 10:26:14

标签: python arrays numpy rms

我有一个数组,需要将其切成许多其他数组,从切成的数组计算均方根,然后在图中绘制均方根的结果。 我写了下面的代码,尽管我不能“存储”此结果以作图,但可以在其中打印数组中的所有rms值。谁能帮我吗?

import numpy as np
import matplotlib.pyplot as plt

# Number of samplepoints
N = 10000
# sample spacing
T = 1.0 / 800.0
t = np.linspace(0.0, N*T, N)
y1 = np.sin(50.0 * 2.0*np.pi*t)

plt.figure(1)
plt.plot(t,y1)
plt.show()

i = 0

while (i < len(y1)):
    i1 = i
    i2 = i1+1000
    x = y1[i:i2]
    rms = np.sqrt(np.mean(x**2))
    i = i2
    print (rms)

else:
    print("finish")

1 个答案:

答案 0 :(得分:1)

您当然可以创建一个列表l,并用l.append(rms)添加每个均方根值。但是您已经有了一个不错的numpy数组,为什么不使用它:

#reshape y1 as 10 columns with 1000 rows 
arr = y1.reshape(10, 1000).T
#square each value, calculate mean for each column (axis 0), then calculate the sqrt for each result
rms = np.sqrt(np.mean(np.square(arr), axis = 0))
print(rms)

示例输出:

[0.70707275 0.70707187 0.70707121 0.70707076 0.70707054 0.70707054
 0.70707076 0.70707121 0.70707187 0.70707275]

这也是您在循环中计算出的结果。现在,如果将绘图功能移到脚本的末尾,您现在可以将其绘制到图形中。