### Finding RMS value of arrays.

Hi, I'm very new to C++ and can't seem to work out why this piece of code wont work as i expected.

I have three arrays representing wave functions and i am trying to find out the averages and RMS values for each waves. The RMS seems to be eluding me.

 ``1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283`` `````` #include #include using namespace std; #define SIZE1 80 #define SIZE2 62 #define SIZE3 29 double getAverage(short arr[], int size) { int i, sum = 0; double avg; for (i = 0; i < size; ++i) { sum += arr[i]; } avg = double(sum) / size; return avg; } double getRMS (short arr[], int size) { int i; double sumsq; double RMS; sumsq = 0; for (i = 0; i< size; i++) { sumsq += arr[i]*arr[i]; RMS = (1/size)*(sqrt(sumsq)); return RMS; } } int main() { short cosine[SIZE1] = { 32767, 32666, 32364, 31862, 31163, 30273, 29196, 27938, 26509, 24916, 23170, 21280, 19260, 17121, 14876, 12539, 10126, 7649, 5126, 2571, 0, -2571, -5126, -7649, -10126, -12539, -14876, -17121, -19260, -21280, -23170, -24916, -26509, -27938, -29196, -30273, -31163, -31862, -32364, -32666, -32767, -32666, -32364, -31862, -31163, -30273, -29196, -27938, -26509, -24916, -23170, -21280, -19260, -17121, -14876, -12539, -10126, -7649, -5126, -2571, 0, 2571, 5126, 7649, 10126, 12539, 14876, 17121, 19260, 21280, 23170, 24916, 26509, 27938, 29196, 30273, 31163, 31862, 32364, 32666 }; short triangle[SIZE2] = { 15500, 14500, 13500, 12500, 11500, 10500, 9500, 8500, 7500, 6500, 5500, 4500, 3500, 2500, 1500, 500, -500, -1500, -2500, -3500, -4500, -5500, -6500, -7500, -8500, -9500, -10500, -11500, -12500, -13500, -14500, -15500, -14500, -13500, -12500, -11500, -10500, -9500, -8500, -7500, -6500, -5500, -4500, -3500, -2500, -1500, -500, 500, 1500, 2500, 3500, 4500, 5500, 6500, 7500, 8500, 9500, 10500, 11500, 12500, 13500, 14500 }; short sawtooth[SIZE3] = { -24000, -22000, -20000, -18000, -16000, -14000, -12000, -10000, -8000, -6000, -4000, -2000, 0, 2000, 4000, 6000, 8000, 10000, 12000, 14000, 16000, 18000, 20000, 22000, 24000, 26000, 28000, 30000, 32000 }; cout<<"Average of cosine: "<

The values for averages are what i would expect, but the value for RMS cosine is found to be 0. Having gone through the array using a calculator i expect a value of roughly 408.

Any tips would be hugely appreciated.

edit: Equation to find RMS is ( 1/size of array * (sum of squares))^1/2
Last edited on
Hi there,

I believe you problem lies here:

 `` `` ``RMS = (1/size)*(sqrt(sumsq)); //line 39 ``

You probably mean:

`RMS = (static_cast<double>(1)/size)*(sqrt(sumsq)); `

If you don't cast 1 (or size) to a double, 1/size will be an integer by integer division, returning an integer result (0 in this case, because 1 divided by size will be 0. something).

Hope that helps.

All the best,
NwN
if your formula for RMS is correct, then RMS shouldn't be within the body of the for...loop, you should have:
 ``12345678`` ``````sumsq=0; for(int x=0; x

closed account (D80DSL3A)
In addition to the points above, shouldn't it be
`RMS = static_cast<double>(sqrt(sumsq/size));` ?

You may have an overflow issue with the product of 2 shorts on line 38
`sumsq += arr[i]*arr[i];`
Consider building up the product in an int variable first:
 ``123`` ``````int prod = arr[i]; prod *= arr[i]; sumsq += prod;``````
Thank you all for your replies. Having taken your suggestions into account and changed the code to:

 ``123456789101112131415161718`` `````` double getRMS (short arr[], int size) { int i; double sumsq; double RMS; sumsq = 0; for (i = 0; i< size; i++) { sumsq += arr[i]*arr[i]; } RMS = sqrt((static_cast(1)/size)*(sumsq)); return RMS; } ``````

I am finding RMScosine to be 23169 and as I said above i think it should be around 408.
closed account (D80DSL3A)
You may not have seen my post when you last posted. Try fixing the overflow problem mentioned there.
The RMS value of a pure sine wave (or cosine wave) is equal to the peak value divided by sqrt(2).

Thus, if the peak value is 32767, the RMS value should be 32767/sqrt(2) = 23169.8

http://en.wikipedia.org/wiki/Root_mean_square
Last edited on