2013-05-02 140 views
0

這是我的第一個問題(編碼相當新穎),所以會嘗試儘可能多地包含信息,因爲我現在很難過!預期聲明

我正在嘗試編寫代碼,該代碼將創建butterworth過濾器以適合用戶輸入規範。我使用的Code Composer 4.

讓Google有54個錯誤,我有一個持續1個左:

"expected a declaration" on line 27: if (n=1 && hpf=0) 

我有三重檢查大括號,任何想法?

編輯:

嗨,再次感謝所有的幫助。自那時起就排序了很多舊問題,但又一次撞上了一堵磚牆;代碼將不會寫入coffic.cof文件(或者在文件被刪除時創建)。沒有錯誤出現,但文件保持不變。任何想法?

P.S.遺憾前面的代碼佈局 - 希望這是更好的:

#include "dsk6713_aic23.h"  //codec-DSK support file 
Uint32 fs=DSK6713_AIC23_FREQ_8KHZ; //set sampling rate 
# include <stdio.h> 
# include <math.h> 
# include <stdlib.h> 
#define pi 3.1415927 
#include "coffic.cof" 

void main() 
{ 
    double hpf, fs, fco, atn, fat, tp, k, ad, m, n, o, da, db, dc; 
    FILE *fp; 
    int c, d, e, f, g, h, i, q, r, s, t, u, v; 
    hpf = 0;   //for a high-pass filter input 1, for a low-pass filter input 0 
    fs = 8000;   //input samping frequency here 
    fco = 2400;   //input cut-off frequency here 
    atn = 17;   //input attenuation (dB) here 
    fat = 3500;   //input the frequency of attenuation here 
    tp = 1/fs; 
    k = tan(pi*fco*tp); 
    ad = tan(pi*fat*tp); 
    m = (log10((pow(10,(atn/10)))-1))/(2*(log10(ad/k))); 
    o = abs(m); 
    n = ceil(o); 

    da = 1.414; 
    c = (pow(2,15)*k*k/(1+da*k+k*k)); 
    d = (pow(2,15)*2*k*k/(1+da*k+k*k)); 
    e = (pow(2,15)*k*k/(1+da*k+k*k)); 
    q = (pow(2,15)*(2-2*k*k)/(1+da*k+k*k)); 
    r = (pow(2,15)*(-1+k-k*k)/(1+da*k+k*k)); 

    fp = fopen("U:\DSK6713\Ivo\CSP\coffic.cof", "w"); 
    if (fp == NULL) 
    { 
     printf("Error. Unable to open coffic.cof"); 
     exit(0); 
    } 

    fprintf(fp, "int a[3]={%d, d%, %d};\n", c, d, e); 
    fprintf(fp, "int b[3]={1, d%, %d};\n", q ,r); 
    fprintf(fp, "int x[3]={0,0,0};\nint y[3]={0,0,0};\n"); 

    fflush(fp); 

    fclose(fp); 

    comm_intr();     //init DSK, codec, McBSP 
    while(1);      //infinite loop 
} 

interrupt void c_int11()   //interrupt service routine 
{ 
    short input; 
    FILE *fp; 
    fp = fopen("U:\DSK6713\Ivo\CSP\coffic.cof", "r"); 
    if (fp == NULL) 
    { 
     printf("Error. Unable to open coffic.cof"); 
     exit(0); 
    } 

    fclose(fp); 

    x[2]=x[1]; 
    x[1]=x[0]; 
    y[2]=y[1]; 
    y[1]=y[0]; 

    input=input_sample(); 
    x[0]=input; 

    y[0]=a[0]*x[0]+a[1]*x[1]+a[2]*x[2]+b[1]*y[1]+b[2]*x[2];  

    y[0]=y[0]>>15;      
    input=(short)y[0]; 
    output_sample(input); //output data 
    return; 
} 
+0

您的所有代碼都必須在函數內。你不能在頂層有這樣的if。並請縮進您的代碼。 – Mat 2013-05-02 11:01:28

+0

你應該縮進和格式化你的代碼,這很可怕,不可讀。此外,你沒有指出哪一行是第27行。 – 2013-05-02 11:01:36

+0

我的眼睛......他們在流血。 – holgac 2013-05-02 12:14:45

回答

0

此外,它看起來像你的程序缺乏main()功能,其在頂層(任何函數之外)代碼這是不允許的。

主程序應在調用的函數:

int main(void) 

,因爲這是執行將啓動。

另外,C中的比較運算符拼寫爲==。單一=被分配。

+0

這是一個很好的觀點,但這並不能解決這個錯誤。 – Mat 2013-05-02 11:02:12

0

您正嘗試在main之外執行一些代碼,而不是聲明爲函數。您需要將所有代碼放在主要位置。在第27行,您已關閉main,並且未將您的下一個陳述聲明爲函數。所以

#include "dsk6713_aic23.h"  //codec-DSK support file 
Uint32 fs=DSK6713_AIC23_FREQ_8KHZ; //set sampling rate 
# include <stdio.h> 
# include <math.h> 
# include <stdlib.h> 
#define pi 3.1415927 

int main(int argc, char *argv[]) 
{ 
double hpf, fs, fco, atn, fat, tp, k, ad, m, n, o, da, db, dc; 
int c, d, e, f, g, h, i, q, r, s, t, u, v; 
hpf = 0;   //for a high-pass filter input 1, for a low-pass filter input 0 
fs = 8000;   //input samping frequency here 
fco = 2400;   //input cut-off frequency here 
atn = 17;   //input attenuation (dB) here 
fat = 3500;   //input the frequency of attenuation here 
tp = 1/fs; 
k = tan(pi*fco*tp); 
ad = tan(pi*fat*tp); 
m = (log10((pow(10,(atn/10)))-1))/(2*(log10(ad/k))); 
o = abs(m); 
n = ceil(o); 


if (n == 1 && hpf == 0) 
{ 
    int  a[2]={c,d}; 
    int  b[2]={1,q}; 
    int  x[2]={0,0}; 
    int  y[2]={0,0}; 
    c = (pow(2,15)*k/(k+1)); 
    d = (pow(2,15)*k/(k+1)); 
    q = (pow(2,15)*(1-k)/(k+1)); 
} 

else if (n == 2 && hpf == 0) 
{ 
    da = 1.414; 
    int  a[3]={c,d,e}; 
    int  b[3]={1,q,r}; 
    int  x[3]={0,0,0}; 
    int  y[3]={0,0,0}; 
    c = (pow(2,15)*k*k/(1+da*k+k*k)); 
    d = (pow(2,15)*2*k*k/(1+da*k+k*k)); 
    e = (pow(2,15)*k*k/(1+da*k+k*k)); 
    q = (pow(2,15)*(2-2*k*k)/(1+da*k+k*k)); 
    r = (pow(2,15)*(-1+k-k*k)/(1+da*k+k*k)); 
} 

else 
{ 
    puts("Sorry, the parameters you have entered cannot be met by a Butterworth Filter of order 6 or less."); 
} 

    comm_intr();     //init DSK, codec, McBSP 
    while(1);      //infinite loop 

} // closing brace of main 

interrupt void c_int11()   //interrupt service routine 
{ 
short input; 

x[1]=x[0]; 
y[1]=y[0]; 

input=input_sample(); 
x[0]=input; 

y[0]=a[0]*x[0]+a[1]*x[1]+b[1]*y[1];  

y[0]=y[0]>>15;      
input=(short)y[0]; 
output_sample(input); //output data 
return; 
} 

而你沒有比較,但分配。更改爲

if (n == 1 && hpf == 0){ } 

正如小hint

如果執行

if(n = 1) 

這是一個有效的表達,而是一種分配,並會在這種特殊情況下返回true,但它總是返回true。 鑑於

if (n == 1) 

是一個比較和將或者產生true如果n等於1或假,如果n不等於1

0

那是什麼大括號經過n =小區(O)做?

您正在結束主函數,它將if語句留在函數作用域之外。

刪除大括號,代碼應該工作。

此外,在if語句中使用==代替=。 =將賦值給你的變量,順便說一句,它們總是返回true,==實際上會比較它們。