-1

This is my first question (quite new to coding) so will try and include as much info as possible 'cos right now I'm stumped!

I'm attempting to write code that will create a butterworth filter to fit user input specifications. I'm using Code Composer 4.

Having google away 54 errors I have a persistent 1 left:

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

I have triple checked curly brackets, any ideas?

EDIT:

Hi again, thanks for all the help. Have sorted old problems and more since then but have hit a brick wall again; the code won't write over (or create if the file is deleted) the coffic.cof file. No errors appear but the file just remains the same. Any idea?

P.S. sorry about the previous code layout - hopefully this is better:

#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

3 Answers 3

1

What is that curly bracket doing after n=ceil(o)?

You are ending the main function, which leaves your if statements outside function scope.

Remove that curly bracket and the code should work.

Additionally, use == instead of = in if statements. = will assign values to your variables, incidentally, they always return true, == actually compares them.

0

Also, it looks like your program lacks a main() function, having code at the top level (outside any function) which is not allowed.

The main program should be in a function called:

int main(void)

since that is where execution will start.

Also, the comparison operator in C is spelled ==. Single = is assignment.

0
0

You are trying to execute some of your code outside of main and not declared as functions. You will need to put all your code in main. On line 27, you had closed main and not declared your next statements as a function. So

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

And you are not comparing, but assigning. Change to

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

Just as little hint.

If you perform

if( n = 1 )

this is a valid expression, but an assignment and will return true in this particular case, but it will always return true. Whereas

if ( n == 1)

is a comparison and will either yield true if n is equal to 1 or false if n is not equal to 1.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.