# Calulating RMS value using a PIC

#### mik3

Joined Feb 4, 2008
4,843
Hi guys,

I have two varying amplitude sine waves and each one is superimposed on a 2VDC source individually. I will sample them with a PIC and find which one has greatest RMS value at any instance in time.

Do you have any suggestions on how to implement this?

I have some ideas in my mind but maybe someone else can give a more clever algorithm.

#### beenthere

Joined Apr 20, 2004
15,819
I would tend towards making life simpler by some external analog processing. Get rid of the offset - pretty easy if the signals have any frequency with just a capacitor.

Then use the National Semiconductor absolute value circuit to rectify each waveform and convert to a nice DC voltage. That way you can sample without worrying about peaks, and stil be able to compare magnitudes. You can even put in some gain to match the PIC A to D range.

#### mik3

Joined Feb 4, 2008
4,843
Beenthere,

This is one solution I want to avoid as to minimize the components.

#### Alexei Smirnov

Joined Jan 7, 2009
43
What is
RMS value at any instance in time
?
I thought that RMS of periodic signal makes sense only on integer number of cycles?
Can you do math in the PIC? You can accumulate Sum(x) and Sum(x^2) continuously on the fly. Then, on any integer number of cycles calculate RMS (don't remember the formula, but it is easy to find out).

Thanks.

#### mik3

Joined Feb 4, 2008
4,843
I can sample the signal for a time of one period of the signal, square each sample, add them and then divide by the number of samples. This is the formula you was talking about. However, this process needs a considerable amount of time to be implemented by the PIC. Thus I was looking for a better algorithm.

#### Alexei Smirnov

Joined Jan 7, 2009
43
This is not exactly what I suggested. There is an algorithm when you calculate sum of samples and sum of samples squared during sampling. For each sample you'll do 3 operations: add new sample to an accumulator, squre sample, add squared sample to another accumulator. When you think You've got one or more cycles, just calculate RMS from accumulators:
Sum(x^2)/N - Sum(x), than take sqrt if needed.
I think this is a best algorithm where you don't need a lot of calculations at a time, everything is on the fly.
Thanks.

#### mik3

Joined Feb 4, 2008
4,843
Why do you subtract by sum(x)?

Shouldn't be sqrt{Sum(x^2)/N}? where x are the discrete values of the signal

#### Alexei Smirnov

Joined Jan 7, 2009
43
Yes, if there is no offset. This is calculation of "AC" power of a signal with AC and DC components. This is the same as calculating DC component first, subtracting it from the signal and then calculating RMS.

#### mik3

Joined Feb 4, 2008
4,843
Then you should divide sum(x) by N, shouldn't you?

Where did you find this formula? I think its wrong.

#### atferrari

Joined Jan 6, 2004
4,470
He is taking the average. Is he?

#### Dutar

Joined Jan 27, 2009
13
Do you want to find the RMS value or just compare the two signals for largest RMS value?

In the case of compare, if the signal frequency of both the signals are the same, and the signals are pure sine waves then you can compare the peak to peak value. keep 4 (MIN, MAX pair for each signal) and load them with the initial sampled value. After each sampling compare the current with its MIN, MAX pair and update them accordingly.

#### Alexei Smirnov

Joined Jan 7, 2009
43
Then you should divide sum(x) by N, shouldn't you?

Where did you find this formula? I think its wrong.
Sorry, I missed square of sum(x):

Sum(x^2)/N - Sum(x)^2

Here is one of the ways to derive it (source):

Thanks.

#### Dutar

Joined Jan 27, 2009
13
Continuing my earlier post, once you know the peak value of Sine wave if you want to find it's RMS value,

RMS of Sine wave = peak * 1/SQRT(2)

If you want the RMS of your original signal (Sine wave + DC component), you can use the principle of superposition.

#### Alexei Smirnov

Joined Jan 7, 2009
43
Dutar is right, if you just need
find which one has greatest RMS value at any instance in time.
simply measure min and max of each signal, and the one with larger peak-to-peak has larger RMS: if there are sin waves, and you need RMS of sine only, excluding DC offset.

#### mik3

Joined Feb 4, 2008
4,843
Continuing my earlier post, once you know the peak value of Sine wave if you want to find it's RMS value,

RMS of Sine wave = peak * 1/SQRT(2)

If you want the RMS of your original signal (Sine wave + DC component), you can use the principle of superposition.
I have thought of that but what if the signals are noisy?

I have a bandpass filter but still noise occurs.

By calculating the RMS, a spike in the signal won't affect much the final value.

#### Alexei Smirnov

Joined Jan 7, 2009
43
If you know sine frequency, you can do Fourier at that frequency (DFT), but it's almost the same calculations as I've suggested for RMS, just multiplying twice on sin and cos.

#### Dutar

Joined Jan 27, 2009
13
I have thought of that but what if the signals are noisy?
Thats actually a big possibility. If you have a high sampling rate, you can try to use the moving average of reasonable number of samples to reduce the noise effect. If you like to find the exact RMS value you can try to use a dsPIC with the DSP library. I guess you can use the "vector power", and although the computing cost depends on the sampling frequency, with a high instruction rate (like 80MIPS) it still might be possible.
The idea of using square in RMS is to eliminate the negative part which otherwise sums up to zero due to the symmetry of the Sine wave. So if you tell us more about your signals and the requirement (like whether the DC level is constant or not, frequencies are the same or not, requirement is comparing or finding the RMS value etc.) we might be able to come up with an efficient solution.

#### mik3

Joined Feb 4, 2008
4,843
The DC is constant and the signals are 10Khz, the only thing which varies is the amplitude of the sine waves.

I thought to subtract the DC value from each sample as to recover only the AC signal and then use the abs(x) function (finds the absolute value of x) to convert the sine wave into a full wave rectified signal. Finally, add the samples and divide by the number of samples to get the average value of the 'rectified' signal. This process does require squaring, which needs time, but uses the abs(x) function which I don't know how long it takes to be executed.

#### Dutar

Joined Jan 27, 2009
13
Hi mik3,
You can eliminate few steps from your last proposal. You can omit the part of subtracting DC value and instead compare your sampled value with the DC value (for abs. you'll have to check the signal with zero so this step wont change the number of calculations). If it is more than DC take as it is and if it is less than DC take {2*DC-[ADC reading]}. Also you can omit the final division isn't it?

Going back to your original method, the RMS comparison over a full period of signals as it is would yield to:

Ʃ[aSin(nT) + DC]^2 -Ʃ[bSin(nT-θ) + DC]^2
= a^2ƩSin^2(nT) - b^2ƩSin^2(nT-θ)
= (a^2 -b^2)*π/2

So RMS comparison should work even with the DC component (I hope my math is correct)

#### mik3

Joined Feb 4, 2008
4,843
I know RMS calculation works with DC but the processing time will be greater because it makes squaring of numbers.

I think that the abs(x) function will be faster.

If don't make the division it will have the same disadvantage with spikes. However, if I take for example 50 samples and one of them is a spike and make the division the spike amplitude will reduce a lot because addition of samples and then division acts like a low pass filter.