Ringbuffer

Implements a ringbuffer to maintain past samples in memory for real-time DSP

One of the main goals of the LuckyLoop real-time-process is to calculate phase and power of a specific frequency for a specific channel. To calculate phase and power, we need more than a current sample. To achieve this, data is collected by one of EtherCat slave devices and continually read in the EtherCat main process into a ringbuffer.

Defines

BUFFERSIZE

a sensible default for the buffersize

Functions

ringbuf *ringbuf_new(int buffersize)

creates a new buffer, returns a pointer to it

Parameters
  • buffersize – the size of the ringbuf in samples

Returns

a new ringbuf

void ringbuf_destroy(ringbuf *ringbuffer)

destroy a ringbuf and free the allocated memory

Parameters
  • ringbuffer – the ringbuf to be destroyed

int write_to_buffer(ringbuf *ringbuffer, double sample)

writes a new sample into the ringbuff

Parameters
  • ringbuffer – the ringbuffer to be written into

  • sample – a double value written into the the head of the buffer

int write_array_to_buffer(ringbuf *ringbuffer, int chunklen, double *chunk)

write an array of samples into the ringbuf

Parameters
  • chunk – the array of samples to write into the buffer

  • chunklen – the size of the array in samples

  • ringbuffer – the ringbuffer to be written into The last value in the array will be the newest in the ringbuffer

Returns

int will return 2 if the array is larger than the buffersize, suggesting an overflow

void print_rbuf(ringbuf *ringbuffer)

print the whole buffer in circular fashion starting at the current head

Parameters
  • ringbuffer – the ringbuffer to print

double read_ringbuf_pos(ringbuf *ringbuffer, int position)

return a sample relative to the current head

Parameters
  • ringbuffer – the ringbuffer to read from

  • position – int the newest value is at 0, the oldest at buffersize-1

struct ringbuf
#include <ringbuf.h>

A structure to represent a ringbuffer

Public Members

double *buffer

the dynamically allocated buffer array

int buffersize

the size of the buffer in samples

int head

the current position of the buffer