Maker Pro
Maker Pro

1024 Samples

Rob_K

Sep 20, 2013
59
Joined
Sep 20, 2013
Messages
59
Hi there,

I am not sure if I am posting in the right place for this so please bear with me. I am being asked to write a small routine to compute the RMS of 1024 samples of 16 bit two's complement data.

Now I understand RMS (Root Mean Square) commonly used to describe AC mains power for example and I understand to a degree, two's complement, as a way of providing negative binary numbers. However, I am a little lost as to the significance of 1024 Samples. Is this referring to a amount of data refreshed per second maybe. I am a little lost. To me the significance of 1024 is that it is the maximum amount of binary configurations in 8 bits.

Any help on this would be much appreciated.

kind regards
 

jpanhalt

Nov 12, 2013
426
Joined
Nov 12, 2013
Messages
426
To me the significance of 1024 is that it is the maximum amount of binary configurations in 8 bits.

2^8 is 256; 2^10 is 1024

I believe the 1024 to which you refer is related to the ready availability of 10-bit analog-to-digital converters (ADC's).

John
 
Last edited:

Harald Kapp

Moderator
Moderator
Nov 17, 2011
13,700
Joined
Nov 17, 2011
Messages
13,700
RMS of 1024 samples of 16 bit two's complement data.
1024 samples usually means that you have a set of 1024 values. Each value is a 16 bit integer in 2's complement form (which is typically used in computers to represent positive as well as negative numbers).

For your RMS routine it is totally insignificant where these 1024 samples come from. You'll have to assume that these are samples taken within one period of an AC signal (e.g. 20ms at 50Hz). Or from an integer number of periods (e.g. 2 full periods) to smooth out noise. Since the discrete RMS calculation contains the term 1/N (here N=1024) but no time variable it is irrelevant whether these 1024 samples were taken within 1ms or 1s or whatever time frame.
It is the responsibility of the provider of the data to ensure that full periods are covered by the samples.

The nice side of using a power of 2 (2^10) for the number of samples is the easy division by that number. Divide by 2^10 is equal to shifting the result by 10 digits to the right.

A note on the side: you are aware that you'll have to do the computation with much higher resolution than 16 bit to avoid overflow, are you?



Here's some interesting literature.
 

Rob_K

Sep 20, 2013
59
Joined
Sep 20, 2013
Messages
59
Thank you Harald, this has cleared up the definition for me perfectly and, thankfully, was what I had suspected, as it is linked to a schematic I posted the other day, which now understand to be an ADC comprising of a series of op amps.

It does now make perfect sense why 1024 samples would be used, though that was my real question, as I associate 1024 with binary (and yes I am aware I got my figures wrong in my original post, it is of course 10 bit not 8 bit).

On your last note, are you referring to stack overflow? and is it right to assume that if I only use the 16 bit resolution, when I come to do the RMS calculation, the numbers are going to expand out of that range due to squaring the numbers and adding the squares together? If so, the maximum number would be something in the range of 1.1 x 10^12, which is larger than a 32 bit number is it not, so I would need to use a signed long long int?
 

Harald Kapp

Moderator
Moderator
Nov 17, 2011
13,700
Joined
Nov 17, 2011
Messages
13,700
Rob, I'm not referring to stack overflow. That is another matter.
I'm referring to overflow of the max. range of numbers representable by a given number of bits.

The relevant equation for RMS calculation is: RMS=sqrt(1/N*sum(V²)).
Now V is a 16 bit number in 2's complement. If you look at the MSB as a sign (which it isn't exactly) then each 16 bit sample has 15 effective bits for the magnitude.
A 15 bit number, when squared, gives a 30 bit result!
Adding 30 bit numbers rapidly increases the number of required bits. If, for example, you add 4 full-scale 30 bit numbers, 32 bits are used up. Adding another number will require more than 32 bits. But your 32 bit integer has only so many bits. Therefore adding one more number will lead to an overflow and, worst case, the result will look like a negative number. Your sums will end up completely wrong.
Adding 1024 30 bit numbers will in the worst case require 40 bits. So your computation should use 64 bit integers.

Avoid the pitfall of using floating point numbers. They van represent very small and very big numbers, but are prone to rounding errors when it comes to adding numbers of very different exponents, as will be the case in an RMS computation.

Here's another article with code snippets.
 

Rob_K

Sep 20, 2013
59
Joined
Sep 20, 2013
Messages
59
Ok, yes, I'm fairly sure that I have that now, so I was right to a certain extent just got terms mixed up, but essentially, I think in C a long long int is indeed 64 bit.

Thank you very much for your help, I have a good grasp of the question at hand now.

Kind regards

Rob
 
Top