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.

Problem in sine wave generator

Hello friends ,

                      I am trying to generate sine wave using digital resonator as mentioned in application note - SPRA708. In that note C-code is given as,

short output;
main()
{
    int i;
    const short A=0x7e66; /* A=(1.975/2 * 32768) */
    short y[3]={0,0x1209,0}; /* (y0,y1,y2), y1=(0.1409*32768) */
    for (i=0; i<40; i++)

   {
             y[0] = (((A*y[1])>>15) + ((A*y[1])>>15)) – y[2];
             y[2] = y[1]; /* y2 <–– y1 */
             y[1] = y[0]; /* y1 <–– y0 */
             output = y[0];
    }
}

Problem is that,

1) why author suggests to divide A=1.975 by 2 ?

2) why they multiply A with y[1] twice followed by 15 bit right shift ?

Sinewave generation(708).pdf
  • Dhaval,

    If A were not divided by 2, what hex value would you use for the signed 16-bit variable?

    If you agree that A should be divided by 2, does that help you understand the answer to your second question?

    Others on the forum will benefit from your answers or your additional questions, please please reply back.

    Regards,
    RandyP

  • thanks Randy ,

                 I am not sure but I think, divides A by 2 term is to keep its value in range in 16-bit and twice right shifting to nullify effect of multiplication by 32768 and by 2 . We can also use ' int ' datatype instead of ' short ' ,then we do not require divide by 2 and twice right shifting terms. But I am not sure about these. Do you have any other idea?

    thanks...

  • Dhaval,

    Your answers make good sense. 1.975 cannot be stored in a 16-bit fixed point format using Q15.

    If you move to int instead of short datatypes, you would have more bits for accuracy. You would need to choose carefully which Q-format you use in order to use the greater accuracy while generating the same 16-bit result.

    Regards,
    RandyP

     

    If you need more help, please reply back. If this answers the question, please click  Verify Answer  , below.