This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

CCS/OMAPL138B-EP: Help with sine wave generation.

Part Number: OMAPL138B-EP

Tool/software: Code Composer Studio

Hi,

I am using OMAPL138B  LCDK version for generation of sine wave using Code Composer Studio 5.5.0. I am facing problem in generation of sine wave. I have tried generation using look up table and using 'sin' function by using the following codes.

USING LOOK UP TABLE CODE:

#include "L138_LCDK_aic3106_init.h"
#define LOOPLENGTH 100
#define BUFLENGTH 256
int16_t sine_table[LOOPLENGTH] =
{0,63,125,187,249,309,368,426,482,536,588,637,685,729,771,809,844,876,905,930,951,969,982,992,998,1000,998,992,982,969,951,930,905,876,844,809,771,729,685,637,588,536,482,426,368,309,249,187,125,63,0,-63,-125,-187,-249,-309,-368,-426,-482,-536,-588,-637,-685,-729,-771,-809,-844,-876,-905,-930,-951,-969,-982,-992,-998,-1000,-998,-992,-982,-969,-951,-930,-905,-876,-844,-809,-771,-729,-685,-637,-588,-536,-482,-426,-368,-309,-249,-187,-125,-63};
int16_t sine_ptr = 0; // pointer into lookup table
int32_t buffer[BUFLENGTH];
int16_t buf_ptr = 0;
interrupt void interrupt4(void) // interrupt service routine
{
int16_t sample;
sample = sine_table[sine_ptr]; // read sample from table
output_left_sample(sample); // output sample
sine_ptr = (sine_ptr+1) %LOOPLENGTH; // increment table index
buffer[buf_ptr] = (int32_t)(sample); // store sample in buffer
buf_ptr = (buf_ptr+1) %BUFLENGTH; // increment buffer index
return;
}
int main(void)
{
L138_initialise_intr(FS_48000_HZ,ADC_GAIN_0DB,DAC_ATTEN_0DB,LCDK_LINE_INPUT);
while(1);
}

USING SINE FUNCTION:

#define PI 3.14159265358979
float frequency = 1000.0;
float amplitude = 20000.0;
float theta_increment;
float theta = 0.0;
interrupt void interrupt4(void) // interrupt service routine
{
 theta_increment = 2*PI*frequency/SAMPLING_FREQ;
 theta += theta_increment;
 if (theta > 2*PI) theta -= 2*PI;
 output_left_sample((int16_t)(amplitude*sin(theta)));
 return;
}
int main(void)
{
 L138_initialise_intr(FS_8000_HZ,ADC_GAIN_0DB,DAC_ATTEN_0DB,LCDK_LINE_INPUT);
 while(1);
}

OUTPUT:


Output for both the code is coming the same. The output has been attached. Please help! Where am I going wrong? Because both the codes are running fine in MATLAB. Also I need help in the area of setting the parameters of graph tool.

Thank You in advance. 


  • Hi Gunturu,

    I've forwarded this to the SW experts. Their feedback should be posted here.

    BR
    Tsvetolin Shulev
  • Hi Gunturu

    I am not sure if the graph that you show is the result of the first code or the second.

    The first code is simple and I am not sure what is the problem with it.  (by the way, why do you need the pointers to be 16-bit?  it is faster if the pointers are the native size of the registers namely 32-bit)

    For the second code here is my suggestion -

    First define the theta_increment and theta as double precision and then increase the value of sampling frequency by a lot,  (I am not sure what is your sampling frequency)  

    Do it, show the graph again and tell us what code generated the graph

     

    Regards

     

    Ran

  • Hi Ran,

    Thank you for the reply. I have modified the code as per your suggestions.
    Here is the first one :

    #include "L138_LCDK_aic3106_init.h"
    #define LOOPLENGTH 48
    #define BUFLENGTH 256
     int32_t sine_table[LOOPLENGTH] =    {0,131,259,383,500,609,707,793,866,924,966,991,1000,991,966,924,866,793,707,609,500,383,259,131,0,-131,-259,-383,-500,-609,-707,-793,-866,-924,-966,-991,-1000,-991,-966,-924,-866,-793,-707,-609,-500,-383,-259,-131};
     int32_t sine_ptr = 0; // pointer into lookup table
     int32_t buffer[BUFLENGTH];
    int32_t buf_ptr = 0;
    interrupt void interrupt4(void) // interrupt service routine
     {
    int32_t sample;
    sample = sine_table[sine_ptr]; // read sample from table
     output_left_sample(sample); // output sample
     sine_ptr = (sine_ptr+1) %LOOPLENGTH; // increment table index
    buffer[buf_ptr] = sample; // store sample in buffer
     buf_ptr = (buf_ptr+1) %BUFLENGTH; // increment buffer index
     return;
     }
    int main(void)
    {
    L138_initialise_intr(FS_48000_HZ,ADC_GAIN_0DB,DAC_ATTEN_0DB,LCDK_LINE_INPUT);
    while(1);
     }
    

    Here is the second one :

    I increased the sampling frequency from 8GHz to 48GHz.

    /*
     * Sine Wave with function call
     */
    #include "L138_LCDK_aic3106_init.h"
    #include "math.h"
    #define SAMPLING_FREQ 48000
    #define PI 3.14159265358979
    float frequency = 1000.0;
    float amplitude = 20000.0;
    double theta_increment;
    double theta = 0.0;
    interrupt void interrupt4(void) // interrupt service routine
    {
     theta_increment = 2*PI*frequency/SAMPLING_FREQ;
     theta += theta_increment;
     if (theta > 2*PI) theta -= 2*PI;
     output_left_sample((int32_t)(amplitude*sin(theta)));
     return;
    }
    int main(void)
    {
     L138_initialise_intr(FS_48000_HZ,ADC_GAIN_0DB,DAC_ATTEN_0DB,LCDK_LINE_INPUT);
     while(1);
    }
    

    Thanks in advance.

  • OK  Let's start with the table sin(x) and try to understand what is going on

    1. get the code out of the interrupt.  Make it a simple sin generation code with a long for loop

    2. Add a print out of the output vector  (run it for 1000 times)

    3. Look at the output print and verify that you see the values that are in the table one by one.

    4. If you see the correct values, the problem is with the interrupt function OR the graphic program that is used to display the results

    5. If you do not see the correct values,  re-compile the code with debug option and debug the code,  see how the index is changed from one run to another

    Report what you see

    Ran