CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    May 2001
    Posts
    24

    circular buffers (delay lines)

    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

  2. #2
    Join Date
    Jun 2002
    Location
    Letchworth, UK
    Posts
    1,020
    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.

    Code:
    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++];
        }
    };
    Succinct is verbose for terse

  3. #3
    Join Date
    May 2001
    Posts
    24
    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]
    }

  4. #4
    Join Date
    Jun 2002
    Location
    Letchworth, UK
    Posts
    1,020
    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.

    Code:
        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++];
        }
    };
    Succinct is verbose for terse

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured