2015-11-08 25 views
0

我想使用正弦表查找方法來查找不同步長的音頻,但是當我將浮點轉換爲整數並使用oscicopte查看頻率時,它不能顯示任何屏幕上的東西。如何使用步長查找正弦表中的音頻? STM32

有誰知道這個問題的解決方案是什麼。任何幫助是apperaite。

下面是代碼:

// 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; 
      } 
} 

enter image description here

但是,如果我改變步長= 3;它顯示頻率:

enter image description here

回答

0

有你的代碼的幾個問題。但我會從你問到的那個開始。

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

之所以這個代碼不工作是v總是被設置爲0在循環的結束,因爲3575360更大。那麼你總是打電話sin(3),因爲3575/10003integer division

也許,你應該改寫你的最後一行爲if ((v/1000) >= 360) v = 0;。否則,我會改寫你的循環這樣

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

我也建議你宣佈你的查找表const。因此,它看起來像

const signed int sine_table[91] = { 

最後一項建議是選擇另一個名稱爲您sin功能,以免與sin library function混淆。即使在這種情況下,應該不會有問題。