Logo
MSP DSP Library
FIR Circular Buffer

The FIR functions in DSPLib require past sample history to compute output samples. The most straight forward approach is to copy a block of samples from the end of the previous sample buffer to the beginning of the next sample buffer but is generally inefficient and requires additional time and energy. The FIR functions include a circular input buffer feature where the end of the input buffer wraps around to the beginning of the same buffer. This feature is supported by LEA and enables FIR filtering with less memory usage and less sample copy overhead.

When using the circular buffer feature the input buffer must be twice the FIR length and aligned to an address equal to it's total byte size. The diagram below shows an example allocation of a 128 sample circular buffer aligned to a 256-byte address and separated into two 64-sample buffers for FIR filtering.

circular_buffer.svg
Example circular buffer alignment for 64-sample Q15 FIR filter

The code snippet below from example filter_ex1_fir_q15 demonstrates how to allocate the above buffer with the correct alignment using the included macros.

/* Filter parameters */
#define FIR_LENGTH 64
/*
* Circular buffer of size 2*FIR_LENGTH, aligned to 4*FIR_LENGTH in order to
* use the circular buffer feature.
*/
DSPLIB_DATA(circularBuffer,MSP_ALIGN_FIR_Q15(FIR_LENGTH))
_q15 circularBuffer[2*FIR_LENGTH];

The following steps demonstrate how to use the same 128-sample circular buffer to run a continuous FIR filter with 64 samples and 4 coefficient taps.

circular_buffer_usage.svg
Example circular buffer usage with 64 sample Q15 FIR and tap size of 4
  • Step 1: Zero initialize the entire circular buffer.
  • Step 2: Fill Buffer 1 with the first 64 samples (x[0] through x[63]) and invoke the FIR function with source pointing to the end of Buffer 2 minus the coefficient length (4 in this example).
  • Step 3: Fill Buffer 2 with the next 64 samples (x[64] through x[127]) and invoke the FIR function with source pointing to the end of Buffer 1 minus the coefficient length.
  • Step 4: Fill Buffer 1 with the next 64 samples (x[128] through x[191]) and invoke the FIR function with source pointing to the end of Buffer 2 minus the coefficient length.
  • Alternate steps 3 and 4 for each new 64 samples.

See examples filter_ex1_fir_q15 and filter_ex2_fir_iq31 for example code using the circular buffer feature with Q15 and IQ31 FIR filters.