TRESADERN J.M wrote: > I am working on a final year project for my degree, using a PIC > controller as a data logger. At the moment a predefined amount of > data is logged for any given session time. What I want to do it > allow the system to be run for any length of time and still have the > same amount of data logged (e.g 60 points). > > Does anybody have an algorithm or know where to find one, that > dynamically junks data as the time goes on, such that I have a set > of data with fairly equal spaces in time between them. Jon: Jennifer Wilson and I have come up with the following: 1. Set INTERVAL = 1 and take enough samples (at one sample every INTERVAL seconds) to fill your buffer. 2. Set N = 1. 3. Wait INTERVAL seconds, then take a sample. 4. Throw out the sample at position [N+1], then shift all the following samples left one position, so you end up with an empty spot at the end of your buffer. Store your newest sample in that empty spot. 5. If N = 1 then INTERVAL = INTERVAL * 2. 6. N = N + 1. 7. If N > (buffer size / 2) then go to Step 2. Otherwise, go to Step 3. Using a 10-sample buffer for simplicity, this algorithm produces the following: Average Time Time Buffer Contents Between Samples ---- --------------------------------------- ---------------- 10 1 2 3 4 5 6 7 8 9 10 1 11 1 3 4 5 6 7 8 9 10 11 1.111 13 1 3 5 6 7 8 9 10 11 13 1.333 15 1 3 5 7 8 9 10 11 13 15 1.555 17 1 3 5 7 9 10 11 13 15 17 1.777 19 1 3 5 7 9 11 13 15 17 19 2 21 1 5 7 9 11 13 15 17 19 21 2.222 25 1 5 9 11 13 15 17 19 21 25 2.666 29 1 5 9 13 15 17 19 21 25 29 3.111 33 1 5 9 13 17 19 21 25 29 33 3.555 37 1 5 9 13 17 21 25 29 33 37 4 etc... With this algorithm: The buffer is always full. The first sample in the buffer is always the very first one you took. The last sample in the buffer is always the most-recent one you took. All the samples are in the correct order. The distance between samples is always as close as possible to equal. No extra storage is required. There's no "processing": Each of the samples in your buffer is accurate; none are averaged or modified in any way. -Andy P.S. The algorithm is for buffers with an even number of slots. If your buffer-size is odd, you need to modify the algorithm slightly, as follows: 1. Set INTERVAL = 1 and take enough samples (at one sample every INTERVAL seconds) to fill your buffer. 2. Set N = 1. INTERVAL = INTERVAL * 2. 3. Wait INTERVAL seconds, then take a sample. 4. Throw out the sample at position [N+1], then shift all the following samples left one position, so you end up with an empty spot at the end of your buffer. Store your newest sample in that empty spot. 5. N = N + 1. 6. If N > (buffer size / 2) then go to Step 2. Otherwise, go to Step 3. P.P.S. If anyone's interested, I have a QuickBASIC program that nicely illustrates the operation of the algorithm... Request it in PRIVATE e-mail and I'll send you a copy. === Andrew Warren - fastfwd@ix.netcom.com === Fast Forward Engineering - Vista, California === http://www.geocities.com/SiliconValley/2499