Python Notes: RMS Demo
- The information presented here is intended for educational use.
- The information presented here is provided free of charge, as-is, with no warranty of any kind.
- Edit: 2025-04-05
RMS Demo (the code)
#!/bin/python3.9
'''
title : rms_demo_100.py
author : Neil Rieck
created: 2025-04-06
notes :
1) just playing around with some electrical stuff here
2) RMS (root-mean-square) is similar to area under a curve
which is necessary when comparing AC to DC
3) a simpler program could be written without a numpy array,
but I did it this way just to show how to do it.
4) np.empty creates a uninitialized array
5) np.zeros creates an initialized array
'''
import math
import numpy as np
import sys
angles = 360
# arr1d = np.empty([angles], dtype = float)
arr1d = np.zeros([angles], dtype = float)
def method1():
# no sqare or square root here (so not RMS)
global arr1d
for x in range(angles):
y = math.sin(math.radians(x))
arr1d[x] = abs(y)
# print(arr1d)
total = np.sum(arr1d)
print("-i-method1 : ", total / angles)
def method2():
global arr1d
for x in range(angles):
y = math.sin(math.radians(x))
a = abs(y)
arr1d[x] = a * a # square
# print(arr1d)
total1 = np.sum(arr1d)
total2 = total1 / angles
total3 = math.sqrt(total2) # square root
print("-i-method2 : ", total3)
def ref():
print("-i-reference: ", math.sin(math.radians(45)))
def main():
script = sys.argv[0]
print("-i-script: ", script)
method1()
method2()
ref()
print("-i-exiting")
#
# catch-all
#
if __name__ == '__main__':
main()
# this is the last line
Output on Linux
$ ./rms_demo_100.py
-i-script: ./rms_demo_100.py
-i-method1 : 0.6366036118294979
-i-method2 : 0.7071067811865476
-i-reference: 0.7071067811865475
-i-exiting
$
Links

Back to
Home
Neil Rieck
Waterloo, Ontario, Canada.