Click to See Complete Forum and Search --> : circular buffers (delay lines)
scottincz
July 4th, 2002, 05:08 AM
Hello there
I need to implement a circular buffer (for an FIR digital audio filter)
What is the simplese way to implement circular buffers (for delay lines)
This will be just a simple console ap so the simpler the better. (prefer c not c++)
Best regards and thanks in advance
Scott
cup
July 4th, 2002, 07:09 AM
I don't know how big your buffer needs to be or what delay lines are. If the items to be posted are very small, you could use the fact that unsigned chars wrap after 255. This is one I posted on an earlier thread. This is in C++ but it can be easily modified for C. It is fast in that there is no need to check for overflows/wrapping etc, as long as the write position does not catch up with the read position.
class TrulyCyclic
{
public:
char mBuffer[256];
unsigned char mReadPosn; // Must be unsigned chars otherwise it will not wrap
unsigned char mWritePosn;
TrulyCyclic ()
: mReadPosn (0)
, mWritePosn (0)
{
}
/*
Usage:
Single item
char* start = 0;
Push (item, &start, itemsize);
Multiple items
char* start = 0;
Push (itemPart1, &start, itemPart1Size);
Push (itemPart2, &start, itemPart2Size);
...
Push (itemPartn, &start, itemPartnSize);
*/
void Push (char* iObject, char** vObjectStart, unsigned char iObjectSize)
{
if (*vObjectStart == 0)
{
// Not formed yet. Set the start position
*vObjectStart = &mBuffer[mWritePosn];
mBuffer[mWritePosn++] = 0;
}
unsigned char* oldsize = reinterpret_cast<unsigned char*> (*vObjectStart);
for (int i = 0; i < iObjectSize; i++)
mBuffer[mWritePosn++] = iObject[i];
// Increase the size
(*oldsize) += iObjectSize;
}
void Pop (char* oObject, unsigned char* oObjectSize)
{
*oObjectSize = mBuffer[mReadPosn++];
for (int i = 0; i < *oObjectSize; i++)
oObject[i] = mBuffer[mReadPosn++];
}
};
scottincz
July 4th, 2002, 07:43 AM
I need to be able to take a buffer of length x and then write to position 0 each time.
int buffer[length];
process(int value)
{
buffer[0]=value;
..
..// need to be able to make buffer [1] equal to buffer [0]
and so on up to buffer[length] = buffer[length-1].
//buffer[length] falls off the end of the buffer.
//need to do this effeciently with a pointer? (circular buffer)
// rather than for looping and relocating each value to [x-1]
}
cup
July 4th, 2002, 09:08 AM
You could consider a queue or singly linked list. They use pointers to objects instead of arrays. That way there is no shuffling.
Does it always have to be item 0? In the code I posted, the start is always at mWritePosn. If it just integers, then the code is a lot simpler. If the write never moves, the read position will catch up with it eventually and start overwriting.
int mBuffer[256]; // 256 of the desired storage item
unsigned char mReadPosn = 0; // Must be unsigned chars otherwise it will not wrap
unsigned char mWritePosn = 0;
// Add an item
void Push (int iObject)
{
mBuffer[mWritePosn++] = iObject;
}
// Remove item
void Pop (int* oObject)
{
*oObject = mBuffer[mReadPosn++];
}
};
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.