steve_bank
Diabetic retinopathy and poor eyesight. Typos ...
Root mean square - Wikipedia
en.wikipedia.org
The average value of a function can be problematic because it can have a zero mean value. Put a zero mean sine current through a resistor and the resistor heats up, but the average value is not meaningful in calculating the power dissipation.
The solution is Root Mean Square or RMS. RMS squares the values so both positive and negate areas under the curve do not cancel each other out.
Any function r curve with the same RMS is said to represent the same thermal energy, at least in ;electric circuits.
The Numpy trapz() integration function makes it easy to code.
Sometimes it is required to separate the RMS value of a waveform and the average value.
I looked up the integral of sin()^2 in a table of integrals and compared to the numerical solution.
Results
Total RMS 1.422729023749446
AC RMS 0.707106781186547 Average -1.234567890000000
Average Value -1.234567890000000
From Table 0.707106781186548
1/sqrt(2) 0.707106781186547
Code:
import numpy as np
import math as ma
import array as ar
import matplotlib.pyplot as plt
#--------------------------------------------------------------------------
def plotxy(xlo,xhi,ylo,yhi,x,y,title,xlabel,ylabel):
font1 = {'family': 'arial',
'color': 'black',
'weight': 'heavy',
'size': 15,
}
[fig, p1] = plt.subplots(1)
p1.plot(x,y,linewidth=2.0,color="k")
p1.grid(color='k', linestyle='-', linewidth=1)
p1.grid(which='major', color='k',linestyle='-', linewidth=0.8)
p1.grid(which='minor', color='k', linestyle='-', linewidth=0.3)
p1.set_xlim(xlo,xhi)
p1.set_ylim(ylo,yhi)
p1.set_title(title, fontdict = font1)
p1.set_xlabel(xlabel, fontdict = font1)
p1.set_ylabel(ylabel, fontdict = font1)
p1.minorticks_on()
plt.show()
#-----------------------------------------------------------------
def rmsacdc(yin,t):
# RMS Root Mean Squared
# ac dc components
dc = np.trapz(yin,t)/(max(t)-min(t))
#remove the DC(average) component
yac = abs((yin - dc)**2)
rms_ac = np.sqrt(np.trapz(yac,t)/(max(t)-min(t)) )
return rms_ac,dc
#-------------------------------------------------------------------
def rms(t,yin):
# the rms integral
rms = np.sqrt( np.trapz(y**2,t)/(max(t)-min(t)) )
return rms
#-----------------------------------------------------------------------
def dc_ave(t,y):return np.trapz(y,t)/(max(t)-min(t))
#-------------------------------------------------------------------------
t = np.linspace(0,1,100);
f = 1
y = np.sin(2*np.pi*f*t)
y = y + -1.23456789 #dd avdc to waveform
rm = rms(t,y)
print("Total RMS %.15f" %rm)
ac,dc = rmsacdc(y,t)
print("AC RMS %.15f Average %.15f" %(ac,dc))
dcav = dc_ave(t,y)
print("Average Value %.15f" %dcav)
b = np.pi*2 #integral 0 - 2*pi
a = 0
integ = (.5*b - .25*np.sin(2*b))-(.5*a - .25*np.sin(2*a))
rmstable = np.sqrt(abs(integ)/(b-a))
print("From Table %.15f" %rmstable)
print("1/sqrt(2) %.15f" %(1/np.sqrt(2)))
plotxy(min(t),max(t),-5,5,t,y," ","t","volts")