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

How to read the 127th sample from a file of 256 samples and compute it

Page 1 of 1

8 Replies - 16067 Views - Last Post: 27 December 2007 - 01:04 PM Rate Topic: -----

#1 AddyP  Icon User is offline

  • New D.I.C Head

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

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

Post icon  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 :code: tags

Is This A Good Question/Topic? 0
  • +

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

#2 Nayana  Icon User is offline

  • DIC Hawk - 나야나 नयन:
  • member icon

Reputation: 31
  • View blog
  • 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

Try changing the function head
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.
Was This Post Helpful? 0
  • +
  • -

#3 AddyP  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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.
Was This Post Helpful? 0
  • +
  • -

#4 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5833
  • View blog
  • Posts: 12,687
  • Joined: 16-October 07

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

Posted 27 December 2007 - 07:55 AM

View PostAddyP, 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.
Was This Post Helpful? 0
  • +
  • -

#5 AddyP  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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

Posted Image

Posted Image

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.
Was This Post Helpful? 0
  • +
  • -

#6 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5833
  • View blog
  • Posts: 12,687
  • 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.
Was This Post Helpful? 0
  • +
  • -

#7 AddyP  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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...
Was This Post Helpful? 0
  • +
  • -

#8 AddyP  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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);
}
Was This Post Helpful? 0
  • +
  • -

#9 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5833
  • View blog
  • Posts: 12,687
  • 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. :P
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1