网址:https://ww2.mathworks.cn/help/signal/ug/rms-value-of-periodic-waveforms.html
描述:本案例由1个示例构成。
-
针对以上案例,采用Python语言实现。
-
本例中的波形是连续时间对应物的离散时间版本。
rms创建频率为p/4 rad/sample。信号的长度为16个样本,等于正弦波的两个周期。
python
import numpy as np
import math
from sympy import *
from scipy import fftpack,signal
import matplotlib.pyplot as plt
%matplotlib inline
python
n = np.arange(16)
x = np.cos(np.pi / 4 * n)
python
def get_rms(records):
return math.sqrt(np.sum(np.power(records, 2)) / len(records))
"""
均方根值 反映的是有效值而不是平均值
"""
'\n均方根值 反映的是有效值而不是平均值\n'
python
def rect_square_wave(origin=0, size=10, ratio=0.25, x_unit=0.1, y0=0, y1=1):
d = []
for start in np.arange(origin-0.0125, origin + x_unit * size, x_unit):
d.append((start, y0))
middle = start + x_unit * ratio
d.append((middle, y0))
d.append((middle, y1))
end = start + x_unit
d.append((end, y1))
return d
python
rmsval = get_rms(x)
python
t = np.linspace(0, 1, 100, endpoint=False)
x = 2 * signal.square(2 * np.pi * 10 * t)
fig = plt.figure()
ax1 = fig.add_subplot(111)
ax1.stem(t, x, markerfmt='o')
plt.xlim(0, 1)
plt.ylim(-2.5, 2.5)
(-2.5, 2.5)
python
d = np.array(rect_square_wave(origin=0, size=10, ratio=0.25, x_unit=0.1, y0=1, y1=0))
figure = plt.figure()
plt.plot(d[:, 0], d[:, 1])
plt.xlim(0, 1)
plt.show()
python