GIDForums  

Go Back   GIDForums > Computer Programming Forums > C Programming Language
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 20-Nov-2006, 05:38
tellthatmatt tellthatmatt is offline
New Member
 
Join Date: Nov 2006
Posts: 10
tellthatmatt is on a distinguished road

average filter program using circular buffer


Hi.

I've got an assignment for which I have to read in data from a TEXT file (the TEXT file contains one integer per line and may have up to 1000 lines) and use a circular buffer to provide a moving average filter, i.e it takes the average of the entire TEXT file.
The circular buffer can use the commands INIT, PUSH and AVERAGE but not POP.

The result then has to be displayed on screen & written to a seperate file.

I dont have a clue about circular buffers please help!
Thanks!
  #2  
Old 20-Nov-2006, 07:53
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,623
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: average filter program using circular buffer


Quote:
Originally Posted by tellthatmatt
Hi.

I dont have a clue about circular buffers

In terms of C language in the context of your assignment: a "circular buffer" is an array. You start putting things into element zero and increment the index for the next access. When the index becomes larger than the largest allowable value (that is: the next access would go beyond the end of the array) just set the index back to zero.

This means that, after the buffer is full (the program has written something in every element of the array) further accesses will overwrite the previous value at each address.

This has the effect of replacing the oldest value by the newest value each time, and the average of all elements of the array always uses the most recent sample values.

For example for a circular buffer of ints:

CPP / C++ / C Code:

int buffer[BUFFER_SIZE]

/* define buffer maximum value here
 * 
 */
int MAX_INDEX = BUFFER_SIZE - 1;
.
.
.
int current_index = 0; /* this always points to the next element to be written */
.
.
.
/* Each time you push a new value into the
 * array, write the new value at buffer[current_index]
 * then increment increment current_index to get ready
 * for the next write. If current_index becomes greater than
 * MAX_INDEX, then set current_index = 0
 */

Of course, you don't actually need a separate variable named MAX_INDEX, but I spelled it out for emphasis. (That is: simply compare current_index with BUFFER_SIZE-1.)



Regards,

Dave
  #3  
Old 20-Nov-2006, 08:23
tellthatmatt tellthatmatt is offline
New Member
 
Join Date: Nov 2006
Posts: 10
tellthatmatt is on a distinguished road

Re: average filter program using circular buffer


many thanks!
  #4  
Old 23-Nov-2006, 11:53
tellthatmatt tellthatmatt is offline
New Member
 
Join Date: Nov 2006
Posts: 10
tellthatmatt is on a distinguished road

Re: average filter program using circular buffer


quick question, is there any dedicated function to let me take the average of an array? or would i have to code it by adding all the elements together & then dividing.
  #5  
Old 23-Nov-2006, 12:15
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,623
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: average filter program using circular buffer


Quote:
Originally Posted by tellthatmatt
quick question, is there any dedicated function to let me take the average of an array? or would i have to code it by adding all the elements together & then dividing.

It's called a loop. (Not a function, but there are several built-in control constructs for creating loops.)

If you know that you are going to do something exactly n times, then the loop can look like this:

CPP / C++ / C Code:
    int i;
    for (i = 0; i < n; i++) {
        /* statements here that will be executed n times */
    }

Suppose you have an array, x, of ten double precision numbers and you want to find the sum of their values (that is: you want the sum x[0] + x[1] + x[2] ... + x[9]).

Then you could do it like this:

CPP / C++ / C Code:
    int i;
    double x[10];
    int n = 10;
    double sum;
    double average;
.
.

/* do whatever you have to do to get values into the array x */

    sum = 0; /* start with the total equal to zero */
    for (i = 0; i < n; i++) {
        sum += x[i];
    }
    average = sum / n;


Regards,

Dave
  #6  
Old 24-Nov-2006, 04:34
tellthatmatt tellthatmatt is offline
New Member
 
Join Date: Nov 2006
Posts: 10
tellthatmatt is on a distinguished road

Re: average filter program using circular buffer


whats the best way to read the integers from the file in this case? we havnt yet covered this in class & my textbook doesnt explain it very well at all. i think its something to do with the fgetsc() function but im not sure.

the rest of my program works fine, i just dont know how to read the integers from the TEXT file!
  #7  
Old 24-Nov-2006, 08:23
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,623
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: average filter program using circular buffer


Quote:
Originally Posted by tellthatmatt
whats the best way to read the integers from the file in this case? we havnt yet covered this in class & my textbook doesnt explain it very well at all. i think its something to do with the fgetsc() function but im not sure.

the rest of my program works fine, i just dont know how to read the integers from the TEXT file!
If the text file contains integers then fscanf() is probably the easiest way and it is, in my opinion, the most appropriate. (But there are other ways.)

When using fscanf(), you give it the address(es) of whatever variable(s) you are reading into. The return value of fscanf is the number of items that were successfully converted. You should always (always) test the return value.

For example if you want to read the value into one value:

CPP / C++ / C Code:
    int ivalue;
.
.
.
    if (fscanf(infile, "%d", &ivalue) == 1) {
        printf("value read for ivalue = %d\n", ivalue);
    }
    else {
        printf("Can't read ivalue\n");
        return 0;
    }


This is just a test to show what tried to explain.

fscanf() will fail (that is, its value will be zero) if it can't convert stuff to an int --- that is it encounters a char that is not whitespace and is not a decimal digit from '0' to '9'. It also fails if it tries to read past the end of the file.

If you want to read a value into an array element, then just give scanf the address of the element. If you want to read the entire file's contents into an integer array used as a circular buffer:

CPP / C++ / C Code:
#define BUFFER_SIZE 10
.
.
.
    int iarray[BUFFER_SIZE];
    int i;
.
.
.


    i = 0;
    while (fscanf(infile, "%d", &iarray[i]) == 1) {
        printf("value read for iarray[%d] = %d\n", i, iarray[i]);
    /* do whatever you want to do with the contents of iarray */
        if (++i >= BUFFER_SIZE) {
            i = 0;
        }
    }

Regards,

Dave
 
 

Recent GIDBlogLast Week of IA Training by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
BOOKEEPING program, HELP!! yabud C Programming Language 10 17-Nov-2006 03:48
Small problem Krc784 CPP / C++ Forum 1 05-Nov-2004 08:34
Need help with my programs, please help. agentxx04 C Programming Language 1 23-Sep-2004 18:02
Can someone check to see if my program is correct tommy69 C Programming Language 2 12-Apr-2004 20:36

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 19:52.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.