6.2. CIRCULAR BUFFERING 95
6.2 CIRCULAR BUFFERING
In many signal processing algorithms, including filtering, adaptive filtering, and spectral analysis,
one requires to shift data or update samples, or to deal with a moving window. For example, a Fi-
nite Impulse Response (FIR) filter basically consists of a list of coefficients which are multiplied
with current and past sampled input signal values. FIR filtering does not involve dependency
on previous output values. For a filter with N coefficients, the output y.n/ based on the input
signal x.n/ gets computed via an FIR filter, that is y.n/ D
P
N 1
kD0
B
k
x.n k/. is equation
can be realized by using a circular buffer where a moving window of the past N 1 sampled
values and the current sampled value is established to compute the output corresponding to the
current input sample.
e direct method of shifting data is normally not efficient and uses many cycles. Circular
buffering is an addressing mode by which a moving-window effect can be created without the
overhead associated with data shifting. In a circular buffer, if a pointer pointing to the last ele-
ment of the buffer is incremented, it is automatically wrapped around and pointed back to the
first element of the buffer. is provides an easy mechanism to exclude the oldest sample while
including the newest sample, creating a moving-window effect as illustrated in Figure 6.1.
Some processors such as DSP processor have specialized hardware for circular buffering.
However, on ARM processors, such hardware is not available. Here let us consider a C imple-
mentation of a circular buffer which could be used on an ARM processor. First, let us establish
the data structure of a circular buffer:
typedef struct CircularBuffer {
short* buff;
int writeCount;
int bitMask;
} CircularBuffer;
In the CircularBuffer structure, buff contains the data and writeCount stores the num-
ber of times the circular buffer has been written to. e rest of the code for the circular buffer
appears below.
CircularBuffer* newCircularBuffer(int size) {
CircularBuffer* newCircularBuffer =
(CircularBuffer*)malloc(sizeof(CircularBuffer));
int pow2Size = 0x01;
while (pow2Size < size) {
pow2Size = pow2Size << 1;
}