Thread: Moving Average (from and into an array)

  1. #1
    Registered User
    Join Date
    Nov 2009
    Posts
    59

    Moving Average (from and into an array)

    I am trying to create a boxcar moving average where the windows do not overlap.

    I have window size as win_size and the number of data entries is line_num

    Code:
    for(p=0; p<(win_size/line_num); p++)
    	{
    		for(i=win_size*p; i<((win_size*p)+win_size); i++)
    		{
    			tsum = tsum + tdata[i];
    			ysum = ysum + ydata[i];
    			tmean[q] = tsum/win_size;
    			ymean[q] = ysum/win_size;
    		}
    	}
    What I am trying to do is compute the sum of the tdata and ydata arrays between 0 and the win_size. I will then divide that by the win_size (i.e. number of points) and write the results to new arrays tmean and ymean.

    I want to then move the window to the end of the previous one and repeat until I get my final full win_size of data.

    Am I even close? Thanks

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    If your window size is 5, and you want non-overlapping windows, then try a test with an array that looks like this
    1,1,1,1,1,2,2,2,2,2,3,3,3,3,3,4,4,4,4,4,5,5,5,5,5

    The answer should be obvious. If your code produces that answer, you might be onto something.
    If it doesn't, it's broke.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Nov 2009
    Posts
    59
    I am getting the mean value as 0 for some reason...

    I am thinking there is something wrong with my code...

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Post a small complete example we can just copy, paste, compile and run.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Nov 2009
    Posts
    59
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    #include <string.h>
    
    
    int main(void)
    {
    	FILE*		input;
    	const char	input_fn[]="proj3input.dat";
    	char 		        buf[1025];
    	char 		        *tokptr, *strptr = buf;
    	int			col=0, line_num=0, win_min, win_max, win_size, i=0, j=0, x=0, record=0, curr_col, initial, tmp2, p, q;
    	double		*tdata, *ydata, *tmean, *ymean, tmp, tsum, ysum;
    	
    	input=fopen("proj3input.dat","r");
    	
    	/*Verify file*/
    	if (input==(FILE*) NULL)
            {
    	    printf("Cannot open required file!\n");
                printf("Please ensure your data file\n");
    	    printf("is in the correct directory\n");
    	    exit(EXIT_FAILURE);
    	}
    	else
    	
    	/*Count number of lines*/
    	{
    		while((fgets(buf, 1024, input) != NULL))
    		{
    			line_num++;
    		}
    		printf("Number of lines is %d \n", line_num);
    	}
    	
    	rewind(input);
    	
    	fgets(buf, 1024, input);
           while((tokptr=(char*)strtok(strptr, " \t")) != NULL)
           {
                   strptr = NULL;
                   col++;
           }
    	
    	printf("Number of columns is %d \n", col);
    	
    	rewind(input);
    	
    	/* Find minimum and maximum sizes of window */
    
            win_min = 10;
            win_max = line_num;
    	
    	/* Read user input for window size */
    
    	printf("Please enter your desired window size.\n");
    	printf("This should be between %d and %d.\n", win_min, win_max);
            scanf("%s", buf);
            win_size = atoi(buf);
    	
    	printf("Your window size will be %d\n", win_size);
    	
    	/* Allocate memory space */
    	
    	tdata = (double*) malloc((line_num)*sizeof(double));
            ydata = (double*) malloc((line_num)*sizeof(double));
    	tmean = (double*) malloc((win_size)*sizeof(double));
    	ymean = (double*) malloc((win_size)*sizeof(double));
            printf("Allocated memory for %d records\n",line_num);
    	
    	rewind(input);
    	
    	while(fscanf(input, "%lf", &tmp) == 1)
    	{
    		curr_col = (record % col) + 1;
    		if(curr_col==1)
    	        {
    			tdata[i] = tmp;
    		}
    		else
    		{
    			ydata[i] = tmp;
    			i++;
    		}
    		record++;
    	}
    	fclose(input);
    	for(x=9990; x<10000; x++)
    	{
    		printf("(%lf %lf)\n", tdata[x], ydata[x]);
    	}
    	i=0;
    	for(p=0; p<(win_size/line_num); p++)
    	{
    		for(i=win_size*p; i<((win_size*p)+win_size); i++)
    		{
    			tsum=0;
    			ysum=0;
    			tsum = tsum + tdata[i];
    			ysum = ysum + ydata[i];
                            tmean[q] = tsum/win_size;
    		        ymean[q] = ysum/win_size;
    		}
    
    	}
    	printf("Tmean is %lf, Ymean is %lf\n", tmean[0], ymean[0]);
    }
    data file in 2 columns named proj3input.dat

    Thanks
    Last edited by browser; 01-24-2010 at 03:42 PM.

Popular pages Recent additions subscribe to a feed