[問題] DSK/DSP/CCS 用差分方程產生sine
開發平台(Platform): (Ex: VC++, GCC, Linux, ...)
軟體: CCS(code composer studio v3)
硬體: DSK6146 (數位訊號處裡用的板子)
問題(Question):
我想用差分方程來產生兩個sine訊號相加。
然後用switch去選擇相關的頻率。
方程式: y[n] = 2*cos(wT)*y[n-1] - y[n-2]
初始值: y[-2] = 0 , y[-1] = A*sin(wT) // A只是震幅大小
錯誤結果(Wrong Output):
No warning , no error
我看了看,也問過助教,但還是弄不出來。也參閱過google上面的寫法。
但是 就是沒辦法產生出正確的訊號。只是產生出一團類似雜訊的東西。
這語言不是典型的C,有點冷門。
不知道怎樣解決....
程式碼(Code):
#include "DSK6416_AIC23.h" //codec support
Uint32 fs=DSK6416_AIC23_FREQ_16KHZ; //set sampling rate
#define DSK6416_AIC23_INPUT_LINE 0x0011
Uint16 inputsource=DSK6416_AIC23_INPUT_LINE; // select input
#include <math.h>
#define PI 3.14159265358979
#define TABLESIZE 512 // size of look up table
#define SAMPLING_FREQ 16000
#define STEP_770 (float) 2*PI*770/SAMPLING_FREQ
#define STEP_1336 (float) 2*PI*1336/SAMPLING_FREQ
#define STEP_941 (float) 2*PI*941/SAMPLING_FREQ
#define STEP_1209 (float) 2*PI*1209/SAMPLING_FREQ
#define BUFLENGTH 512
#define IO_PORT 0x60000000
#define Get_Switches ((*(int *)0x60000000>>4) & 0x0F)
float inc_high=0; // look up table index
float inc_low=0;
float tmp;
short i=0,A=1000;
short buf[BUFLENGTH],bufi=0;
float y1[3],y2[3];
int Dipsw, old_Dipsw=100;
int tone_enable=1;
float c1,c2;
interrupt void c_int11() //interrupt service routine
{
y1[2] = 2*c1*y1[1] - y1[0];
y2[2] = 2*c2*y2[1] - y2[0];
tmp = y1[2] + y2[2];
output_left_sample((short) tmp);
buf[bufi++] = (short) tmp;
bufi %= BUFLENGTH;
for (i=2;i>0;i--)
{
y1[i-1] = y1[i]; //update buffer , preparing for y1[1] <= y1[2]
y2[i-1] = y2[i]; //update buffer , preparing for y1[0] <= y1[0]
}
return; //return from interrupt
}
void main()
{
// if (tone_enable != 0)
comm_intr(); // initialise DSK
while(1)
{
Dipsw = 15 - Get_Switches ; //Dipsw = 0 (all down) ~ 15 (all up)
*(unsigned volatile int *)IO_PORT = Dipsw; //LEDs off
if(Dipsw !=old_Dipsw)
{
old_Dipsw = Dipsw; // update switch
switch(Dipsw) {
case 0:
inc_low = STEP_770;
inc_high = STEP_1366;
break;
case 1:
inc_low = STEP_941;
inc_high = STEP_1209;
break;
default:
inc_low = 0;
inc_high = 0; //tone_enable
}
y1[0] = 0.0; // initial value
y1[1] = A * sin(inc_low);
y2[0] = 0.0;
y2[1] = A * sin(inc_high);
c1 = cos(inc_low);
c2 = cos(inc_high);
}
}
}
// 去掉部分不重要的程式碼了
補充說明(Supplement):
我已經想了一整周了...
強者解答出,500p 奉上。
跪謝。
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 36.224.252.126
※ 編輯: ex61999 來自: 36.224.252.126 (04/27 02:45)
C_and_CPP 近期熱門文章
PTT數位生活區 即時熱門文章