![]() |
|
#1
|
|||
|
|||
average filter program using circular bufferHi.
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 Thanks! |
|
#2
|
|||
|
|||
Re: average filter program using circular bufferQuote:
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:
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
|
|||
|
|||
Re: average filter program using circular buffermany thanks!
|
|
#4
|
|||
|
|||
Re: average filter program using circular bufferquick 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
|
|||
|
|||
Re: average filter program using circular bufferQuote:
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:
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:
Regards, Dave |
|
#6
|
|||
|
|||
Re: average filter program using circular bufferwhats 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
|
|||
|
|||
Re: average filter program using circular bufferQuote:
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:
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:
Regards, Dave |
Recent GIDBlog
Last Week of IA Training by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
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