I am trying to program a DSP (TMSF28335) using writing C codes in Control Studio V6.02.
In this project, I need to make a 90 degrees phase offset on an AC signal that I measure by the sensors. I was advised to use a circular buffer in order to make such phase shift. But unfortunately, I am not very familiar how to write a circular buffer in C language. According to the concept, I know that the "head" of the buffer should be the input signal (measured AC signal) and the "tail" is the shifted input signal that is used as the output signal of the circular buffer.
The sampling time of the system is set to 3.84599989e-5 (s) and one period is 0.02 (s) (50 Hz). So 1/4 of one period constitutes (0.02/4)/3.84599989e-5=130 samples. In the other word, I need to make 130 samples delay.
I would be grateful if you can tell me how to write the circular buffer in C for my controller and accordingly I can make phase delay.
What you need is special implementation of a circular buffer called a delay line. Here's a simple (quick and dirty, and somewhat naive) implementation.
Note that this can give you a fixed phase shift only if the input frequency is constant.
typedef short Sample; // change to whatever your sample data is...
#define DELAY (130)
struct DelayLine // <- make sure you initialize the entire struct
{ // with zeroes before use !
Sample buffer[DELAY + 1];
int next;
};
Sample tick(DelayLine* effect, Sample sampleIn)
{
// call for each sample received, returns the signal after delay
int nextOut = effect->next + DELAY; // note that this delay could be anything
// shorter than the buffer size.
if (nextOut >= DELAY + 1) // <-- but not this one!!!
nextOut -= DELAY + 1;
effect->buffer[effect->next] = sampleIn;
if (++(effect->next) >= DELAY + 1)
effect->next = 0;
return effect->buffer[nextOut];
}