0

我正在尝试使用正弦表查找方法来查找不同步长的音调频率,但是当我将浮点数转换为整数并使用 oscicopte 查看频率时,它无法在屏幕上显示任何内容.

有谁知道这个问题的解决方案是什么。任何帮助都是合适的。

下面是代码:

// use the formula: StepSize = 360/(Fs/f) Where Fs is the Sample frequency 44.1 kHz and f is the tone frequency.
// example: StepSize = 360/(44100/440) = 3.576, since the STM32 doesn't support the floating point, therefore, we have to use the fixed-point format which multiply it by 1000 to be 3575

int StepSize = 3575;  
unsigned int v=0;

signed int sine_table[91] = {
          0x800,0x823,0x847,0x86b,
          0x88e,0x8b2,0x8d6,0x8f9,
          0x91d,0x940,0x963,0x986,
          0x9a9,0x9cc,0x9ef,0xa12,
          0xa34,0xa56,0xa78,0xa9a,
          0xabc,0xadd,0xaff,0xb20,
           0xb40,0xb61,0xb81,0xba1,
           0xbc1,0xbe0,0xc00,0xc1e,
           0xc3d,0xc5b,0xc79,0xc96,
           0xcb3,0xcd0,0xcec,0xd08,
            0xd24,0xd3f,0xd5a,0xd74,
            0xd8e,0xda8,0xdc1,0xdd9,
            0xdf1,0xe09,0xe20,0xe37,
            0xe4d,0xe63,0xe78,0xe8d,
            0xea1,0xeb5,0xec8,0xedb,
            0xeed,0xeff,0xf10,0xf20,
            0xf30,0xf40,0xf4e,0xf5d,
            0xf6a,0xf77,0xf84,0xf90,
            0xf9b,0xfa6,0xfb0,0xfba,
            0xfc3,0xfcb,0xfd3,0xfda,
            0xfe0,0xfe6,0xfec,0xff0,
            0xff4,0xff8,0xffb,0xffd,
            0xffe,0xfff,0xfff};

unsigned int sin(int x){
   x = x % 360;
   if(x <= 90)
      return sine_table[x];
    else if ( x <= 180){
      return sine_table[180 - x];
    }else if ( x <= 270){
      return 4096 - sine_table[x - 180];
    }else{
      return 4096 - sine_table[360 - x];
     }
}

void main(void)
{
while(1){
            v+=StepSize;                // Don't know why it doesn't work in this way. not display anything on screen.
           DAC->DHR12R2 = sin(v/1000);      // DAC channel-2 12-bit Right aligned data
           if (v >= 360) v = 0;
           }
}

在此处输入图像描述

但是,如果我改变 StepSize = 3; 它显示频率:

在此处输入图像描述

4

1 回答 1

0

您的代码存在一些问题。但我将从你问的那个开始。

int StepSize = 3575;  
unsigned int v=0;
while(1){
       v+=StepSize;
       DAC->DHR12R2 = sin(v/1000);
       if (v >= 360) v = 0;
}

这段代码不起作用的原因v是总是0在循环结束时设置为,因为3575大于360。所以你总是打电话,sin(3)因为3575/1000整数除法3

也许,您应该将最后一行重写为if ((v/1000) >= 360) v = 0;. 否则,我会像这样重写你的循环

while(1){
       v+=StepSize;
       v/=1000;
       DAC->DHR12R2 = sin(v);
       if (v >= 360) v = 0;
}    

我还建议您将查找表声明为 a const。所以它看起来像

const signed int sine_table[91] = {

最后的建议是为您的sin函数选择另一个名称,以免与sin 库函数混淆。即使在这种情况下也不应该有问题。

于 2015-11-08T12:45:54.440 回答