0

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.

Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
Foad
  • 3
  • 1
  • 2
    Find a tutorial on circular buffers. Learn to implement and use them on simple examples. Then find out the exact period of your signal. Then maintain a quarter of that as the distance between input and output. Alternatively (in case of non-constant period) you will have to dynamically determine what "90 degrees" means for your signal at any given time. – Yunnosch Aug 11 '17 at 07:38
  • 1
    You need a [digital filter](https://en.wikipedia.org/wiki/Digital_filter) (either FIR or IIR) and to implement that you should use a ring buffer that's right so far. I would use a hilbert filter as this would transform `cos(ωt)` to `sin(ωt)` which is a shift of 90°. See: https://en.wikipedia.org/wiki/Hilbert_transform for the mathematical explanation, you need knowledge about complex numbers for that. This question should be better here: https://dsp.stackexchange.com/. Further I would recomment to train such things as digital signal processing in Matlab. It's great for that purpose! – Andre Kampling Aug 11 '17 at 07:39
  • 1
    But to answer your question about circular buffers this is a very general one: https://stackoverflow.com/a/827749/8051589. Here in one for a FIR filter implementation: https://stackoverflow.com/questions/22749058/circular-buffer-implementation-for-fir-filter-in-c. There is much stuff to find on the internet. – Andre Kampling Aug 11 '17 at 07:48

1 Answers1

0

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];
}
Michaël Roy
  • 6,338
  • 1
  • 15
  • 19
  • Thanks for the reply. I tried the codes in CC6. My sample data is float. So I changed the type of int to float. The error that I received were on the lines "effect->buffer[effect->next] = sampleIn;" and "return effect->buffer[nextOut]" saying that "expression must have integral or enum type". Can you please help me how I should rearrange the codes so that the input signal of the buffer (sampleIn) is accepted as a float type file? – Foad Aug 12 '17 at 02:34
  • the indices `next``nextOut` and `nextIn` are `int` types no matter what. for float samples you need only change the `typedef Sample int` to `typedef Sample float` in the example above. – Michaël Roy Aug 12 '17 at 14:23
  • Sorry, I spotted an error in the code. I've corrected it. – Michaël Roy Aug 13 '17 at 03:51