DSP & Software

Circular Buffer

/ser-kyoo-ler buf-er/ (ring buffer)
A fixed-size FIFO data structure implemented as a contiguous memory array with wrap-around read and write pointers. When the write pointer reaches the array end, it wraps to the beginning, overwriting oldest data. Circular buffers provide O(1) insertion and removal with zero dynamic memory allocation, making them essential for real-time RF signal processing: ADC sample streaming in SDR systems, FIR filter delay lines, FFT overlap-save windowing, radar pulse history buffers, and producer-consumer pipelines between DSP processing stages.
Category: DSP & Software
Complexity: O(1) read/write
Allocation: Zero runtime

Understanding Circular Buffer

Real-time RF signal processing requires continuous data flow between hardware (ADC, DAC, DMA) and software (DSP algorithms, demodulators, decoders). Standard dynamic arrays and linked lists are unsuitable because memory allocation and deallocation introduce unpredictable latency (microseconds to milliseconds for malloc/free), which at sample rates of 10 to 100 Msps would cause buffer overruns and lost samples. The circular buffer solves this by pre-allocating a fixed memory block at initialization and using modular arithmetic on read/write indices to cycle through it indefinitely.

The implementation uses two pointers: a write (head) pointer where new samples are inserted, and a read (tail) pointer where samples are consumed. The available data is the difference (write - read) mod N, where N is the buffer size. For single-producer single-consumer (SPSC) scenarios common in RF pipelines, the buffer is inherently lock-free because the producer only modifies the write pointer and the consumer only modifies the read pointer. This guarantees deterministic sub-microsecond latency. Power-of-two buffer sizes (256, 1024, 4096, ...) are preferred because the modulo operation reduces to a bitwise AND (index & (N-1)), saving 10 to 30 CPU cycles per access compared to integer division. In FPGA-based RF systems, circular buffers are implemented as dual-port block RAM with binary counters, achieving single-clock-cycle read/write at ADC sampling rates up to 6.4 Gsps.

Circular Buffer Parameters

Available Data:
count = (write_ptr - read_ptr) mod N

Available Space:
free = N - 1 - count   [one slot reserved to distinguish full from empty]

Fast Modulo (power-of-two N):
index = ptr & (N - 1)   [bitwise AND, 1 clock cycle]

Where N = buffer size (power of two), ptr = read or write pointer. Required buffer size: N ≥ Tmax · fs where Tmax = max processing latency, fs = sample rate. For 100 Msps with 10 ms max latency: N = 1M samples.

Circular Buffer Sizing for RF Applications

ApplicationSample RateMax LatencyBuffer SizeMemory
SDR USB streaming2.4 Msps50 ms128K samples512 KB (I/Q 16-bit)
5G NR baseband122.88 Msps1 ms128K samples512 KB
Radar pulse buffer500 Msps100 μs64K samples256 KB
Spectrum analyzer1 Gsps10 ms16M samples64 MB
FIR delay line (256 taps)AnyN/A256 samples1 KB
Common Questions

Frequently Asked Questions

How is a circular buffer used in SDR?

The ADC/DMA produces a continuous I/Q sample stream; a circular buffer sits between hardware and DSP threads. The DMA writes blocks at the hardware rate while the DSP reads at its processing rate. GNU Radio uses circular buffers (4K to 64K samples) between every connected block. Buffer size must accommodate max processing jitter to prevent data loss.

Why use power-of-two buffer sizes?

The modulo wrap operation becomes a bitwise AND (1 cycle vs 10 to 30 for division). At 100 Msps, this saves millions of cycles per second. Power-of-two sizes also align with FFT lengths, DMA transfer sizes, and cache lines, reducing pipeline stalls and memory access overhead.

What is a lock-free circular buffer?

With single producer and single consumer, atomic pointer updates replace mutexes. The producer only writes the write pointer (after data), the consumer only writes the read pointer (after reading). Each reads the other's pointer atomically. This eliminates lock latency and priority inversion, achieving consistent sub-microsecond latency critical for real-time RF processing.

RF Test & Development

Request a Quote

Need precision waveguide components for SDR test benches or radar signal processing development? Contact our engineering team.

Get in Touch