Computation of Root Mean Square (RMS) for a series of values

Page 1 of 1

8 Replies - 22963 Views - Last Post: 27 December 2007 - 01:04 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=40055&amp;s=1bee67fe1f78e60df71751647f4e198c&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

Reputation: 0
• Posts: 5
• Joined: 21-December 07

Computation of Root Mean Square (RMS) for a series of values

Posted 25 December 2007 - 03:55 PM

/* I have manage to compute the rms value for all 256 numbers, however i also wish to compute the rms value from the 127th number onwards and subsequently the rest...ie shifting the procedure until all the numbers in the array are computed */
```#include <stdio.h>
#include<math.h>

#define novalues 256 /* max. number of entered values */
#define nofile 0

int get_values(int maxvals, char fname[], int *n, float arr[]);
float calc_average(int nval, float arr[]);
void display_average(int nval,float arr[], float aver);

int main(void)
{
float values[novalues], average;
int nvalues;

if (get_values(novalues,"IN.DAT",&nvalues,values)==nofile)
return(1);
average=calc_average(nvalues,values);
display_average(nvalues,values,average);
return(0);
}

int get_values(int maxvals, char fname[], int *n,float arr[])
{
FILE *in;
int i=0, rtn;

if((in=fopen(fname,"r"))==NULL)
{
printf("Cannot Open %s\n",fname);
return(nofile);
}

while(!feof(in))
{
rtn=fscanf(in,"%f",&arr[i]);
if(rtn!=EOF) i++;
if(i==maxvals) break;
}
fclose(in);

*n=i;
return(!nofile);
}

float calc_average(int nval,float arr[])
{
int i;
float N=1;

for(i=1;i<nval;i++)
N+=(arr[i]*arr[i]);

/* Note there is no test for a divide by zero */
return(sqrt(N/nval));
}

void display_average(int nval,float arr[],float aver)
{
int i;

puts("Input Values Are:");

for(i=0;i<nval;i++)
printf("%8.0f",arr[i]);

printf("\n\nThe Root Mean Square is %8.3f\n",aver);
}
```

MOD EDIT: Use tags

Is This A Good Question/Topic? 0

Replies To: Computation of Root Mean Square (RMS) for a series of values

#2 Nayana

• DIC Hawk - 나야나 नयन:

Reputation: 32
• Posts: 824
• Joined: 14-November 07

Re: Computation of Root Mean Square (RMS) for a series of values

Posted 26 December 2007 - 06:51 PM

```float calc_average(int nval,float arr[])
```

to
```float calc_average(int first, int last, float arr[])
```

Or something like that. Then make your for loop just loop from the first to the last value. To use one value, you'd obviously make them the same number.

Reputation: 0
• Posts: 5
• Joined: 21-December 07

Re: Computation of Root Mean Square (RMS) for a series of values

Posted 27 December 2007 - 07:38 AM

I'm pretty confuse on how to make the for loop from the first to the last value.

#4 baavgai

• Dreaming Coder

Reputation: 7151
• Posts: 14,894
• Joined: 16-October 07

Re: Computation of Root Mean Square (RMS) for a series of values

Posted 27 December 2007 - 07:55 AM

AddyP, on 27 Dec, 2007 - 09:38 AM, said:

I'm pretty confuse on how to make the for loop from the first to the last value.

e.g
```float calc_average_for_range(int startNum, int endNum, float arr[]) {
int i;
float N=1;
int nval = startNum - endNum + 1;

for(i=startNum;i<=endNum;i++)
N+=(arr[i]*arr[i]);

/* Note there is no test for a divide by zero */
return(sqrt(N/nval));
}

float calc_average(int nval,float arr[]) {
int startNum = 0;
int endNum = nval-1;
return calc_average_for_range(startNum, endNum, arr);
}

```

Hope this helps.

Reputation: 0
• Posts: 5
• Joined: 21-December 07

Re: Computation of Root Mean Square (RMS) for a series of values

Posted 27 December 2007 - 10:58 AM

I was able to compute the root mean square using the previous code, however when i edited it, i was able to execute the program but not computing the equation.

#6 baavgai

• Dreaming Coder

Reputation: 7151
• Posts: 14,894
• Joined: 16-October 07

Re: Computation of Root Mean Square (RMS) for a series of values

Posted 27 December 2007 - 11:23 AM

Oops, spotted a bug! Make int nval = startNum - endNum + 1; into int nval = (endNum - startNum) + 1; and see how it goes. Sorry about that, I couldn't test the code without sample data.

Reputation: 0
• Posts: 5
• Joined: 21-December 07

Re: Computation of Root Mean Square (RMS) for a series of values

Posted 27 December 2007 - 11:51 AM

Thanks Baavgai, really appreciated the help...

Reputation: 0
• Posts: 5
• Joined: 21-December 07

Re: Computation of Root Mean Square (RMS) for a series of values

Posted 27 December 2007 - 12:46 PM

I was able to run and compute the rms value, however when i wish to compute...say the rms for 10 samples i will have to manually edit the code i.e. int nval - 246 but how can i loop the program in such a way that i can calculate the rms for example 11 samples that is int nval - 245, 12 samples, 13 samples....and so on without having to manually change the equation.

float calc_average(int startNum, int endNum, float arr[])
{
int i;
float N=1;
int nval = (endNum - startNum) + 1;

for(i=startNum;i<=endNum;i++)
N+=(arr[i]*arr[i]);

/* Note there is no test for a divide by zero */
return(sqrt(N/nval));
}

float calc_average(int nval,float arr[])
{
int startNum = 0;
int endNum = nval-246
return calc_average(startNum, endNum, arr);
}

#9 baavgai

• Dreaming Coder

Reputation: 7151
• Posts: 14,894
• Joined: 16-October 07

Re: Computation of Root Mean Square (RMS) for a series of values

Posted 27 December 2007 - 01:04 PM

Rewriting float calc_average(int nval,float arr[]) is just to proof the thing. Why not just call calc_average_for_range(0, nval - 246, arr);?

I'm not quite following the difficulty. It's your program, make another function to handle the math as you like.