Calculating RMS

For alternating current wave forms (e.g. sine wave) there are several ways of characterizing the voltage. One is the RMS (root mean square) which is the equivalent DC voltage that would have the same heating effect on a resistor. The calculation of RMS involves taking the average voltage over some time range. To generate a low noise result it is important to align these time ranges with the cycles of the wave form. This can be achieved by the following calculations.

  • An example sine wave (use periodic 100 ms raster):

    voltage = sin(Master())

  • Detecting the zero crossings (used to finish the integral):

    positive = voltage > 0

    windowDetect = positive && !State_Register(positive, !1)

  • Also provide the detected signal delayed by one sample (used to restart the integral):

    windowDetect2 = State_Register(windowDetect, !1)

  • Remember start of window on each crossing:

    windowStart = Latch(Master(), windowDetect2)

  • Calculate the RMS since the last zero crossing:

    RMSTemp = sqrt(Rolling_Accumulate_Integral(voltage ** 2, windowStart))

  • Hold the RMS value from the end of the last window:

    RMS = Latch(RMSTemp, windowDetect)

Overall the calculated RMS will be one period delayed.

See also  

Extracting Bits or Bit Fields from an Integer

Using Enumeration Signals

Applying Calculations to Specific Samples