⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 sine.c

📁 dtmf generator c6713
💻 C
字号:
/*****************************************************************************/
/*                                                                           */
/* FILENAME                                                                  */
/*   sine.c                                                                  */
/*                                                                           */
/* DESCRIPTION                                                               */
/*   TMS320C6713 DSK.                                                        */
/*   Generation of sine wave using polynomial evaluation.                    */
/*   This is a fixed-point implementation and can be used with C62x/C64x     */ 
/*                                                                           */
/* NOTE: DSPLIB does not contain this function.                              */
/*                                                                           */
/* REVISION                                                                  */
/*   Revision: 1.00                                                          */
/*   Author  : Richard Sikora                                                */
/*---------------------------------------------------------------------------*/
/*                                                                           */
/* HISTORY                                                                   */
/*   Revision: 1.00                                                          */
/*   6th January 2006. Created by Richard Sikora.                            */
/*                                                                           */
/*****************************************************************************/

static const int coefficients[] = {0x00003240,  /*  3.140625   */
                                   0x00000053,  /*  0.02026367 */
                                   0xFFFFAACC,  /* -5.325196   */
                                   0x000008B7,  /*  0.5446776  */
                                   0x00001CCE}; /*  1.800293   */

/*****************************************************************************/
/* sine()                                                                    */
/*---------------------------------------------------------------------------*/
/*                                                                           */
/* Generate sine wave using polynomial expansion                             */
/*                                                                           */
/* sin(x) = 3.1406 x + 0.020263 x^2 -5.3252 x^3 + 0.54468 x^4 + 1.8000 x^5   */
/*                                                                           */
/* INPUT:  0x3FFF (0.500) represents 90 degrees.                             */
/*         0x7FFF (1.000) represents 180 degrees.                            */
/*                                                                           */
/* RETURNS: sine in range -32767 to +32767.                                  */
/*                                                                           */
/*****************************************************************************/

signed int sine(signed int input)
{
 int x;
 int adjusted_input;
 int product;
 int output;
 int i;
 
 /* Check if in 2nd or 4th quadrant */
 if ( input & 0x4000)
  {
   adjusted_input = -input; /* Invert */
  } 
 else
  {
   adjusted_input = input;
  } 

 adjusted_input &= 0x7FFF;  /* Remove sign bits */
    
 output = 0;
 x = adjusted_input;
 
 for ( i = 0 ; i < 5 ; i++)
  {
   /* x*coeff[0] + x*x*coeff[1] + x*x*x*coeff[2] etc */
   product = (x * coefficients[i]);    
   output += product;
   
   /* Generate x^2, x^3, x^4 etc with rounding up */
   x *= adjusted_input;
   x = (((x >> 14) + 1 ) >> 1);
  } 
 
 /* Convert from Q4:12 to Q1:15 format. Round up final value */
 output >>= 11;
 output += 1;
 output >>= 1;
 
 if ( output > 32767)
  {
   output = 32767; /* Saturate if required */
  }
 
 /* Third and fourth quadrant. Invert */
 if ( input & 0x8000)
  {
   output = - output;
  }
 
 return(output);
}

/*****************************************************************************/
/* End of sine.c                                                             */
/*****************************************************************************/

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -