8 years, 11 months ago.

What's wrong of my code ver2

Hi guys, glad to see you are concerned about my question.

This is my code, every function are proved to work well but this code cannot work. I think maybe there are something wrong about the switch sentences but i dont know.

/media/uploads/Oneoftherulingparty/mixed_program.cpp

thank you so much!

2 Answers

8 years, 11 months ago.

You are calling the function that asks the user to pick a filter type and waiting for a reply at 20kHz. Few people can enter their choices at 20kHz and normally you wouldn't want to change filter type every sample. ;-)

Move your filter option variables to being globals. Move all of the code asking the user to select the filter type into a new function and call that directly from main before you set the ticker. All that should remain in the ticker are the switch statements to call to the correct filter code.

Warning - more complex but more efficient method follows. Feel free to ignore.

You could save a few extra CPU cycles in the ticker function by using a function pointer. When the user sets the filter type you set the function pointer to point to the correct filter operation. Then in the ticker code there is no need for any switch statements or conditional code which can slow things down, you simply call the function that the pointer indicates.

That's assuming you wanted to stick to c code. In c++ you could create a filter base class and each filter type becomes a child of that base class that overloads the addSample method. When the user selects a filter you create an instance of the correct filter but store it as a pointer to the base class. The ticker would then use that pointer and call it's addSample method which would automatically call the correct filter implementation.

Accepted Answer

thank you andy, but i still wondering about one question. I think if i move my filter option variables to being globals, i have to create 12 function with the content :"sample20kHz_tick.attach_us(&sample20kHz_task, 50);" to point to different function "void sample20kHz_task(void)", then each function sample20kHz_task will point to each filter function. I think it is so complicated so do you have any other diea?

posted by Joe Davis 22 Apr 2015

Why would you need to do that?

(Some printf's removed from getOption() code in order to keep within the post size limit.)

    int Option = 0;
    int type1 = 0;
    int type2 = 0;
    int type3 = 0;
    int type4 = 0;

void getOption() {
    pc.printf("1. FIR_LPF; 2. FIR_HPF;3. IIR_LPF; 4. IIR_HPF;\n\r");
    pc.printf("Please enter the type you want: \n\r");
    pc.scanf("%d", &Option);
    pc.printf("Your choice is: %d;\n\r", Option);
    switch (Option)//The options of the filter types;
    {
    case 1:
        pc.printf("You choice is: FIR_LPF\n\r");
        pc.scanf("%d", &type1);
        break;
    case 2:
        pc.printf("You choice is: FIR_HPF\n\r");
        pc.scanf("%d", &type2);
        break;
    case 3:
        pc.printf("You choice is: IIR_LPF\n\r");
        pc.scanf("%d", &type3);
        break;
    case 4:
        pc.printf("You choice is: IIR_HPF\n\r");
        pc.scanf("%d", &type4);
        break;
    default:printf("Please type effective number!\n\r");
    }
}

int main()
{
    getOption();
    sample20kHz_tick.attach_us(&sample20kHz_task, 50);//attach task to 50us tick  
}

void sample20kHz_task(void)
{
    data_in = Ain - 0.5;
    switch (Option)
    {
    case 1:
        switch (type1)
        {
        case 1:data_out = FIRLPF1(data_in); break;
        case 2:data_out = FIRLPF2(data_in); break;
        case 3:data_out = FIRLPF3(data_in); break;
        default:break;
        }
        break;
    case 2:
        switch (type2)//The options of function types;
        {
        case 1: data_out = FIRHPF1(data_in); break;
        case 2:data_out = FIRHPF2(data_in); break;
        case 3:data_out = FIRHPF3(data_in); break;
        default:break;
        }
        break;
    case 3:
        switch (type3)//The options of function types;
        {
        case 1:data_out = IIRLPF1(data_in); break;
        case 2:data_out = IIRLPF2(data_in); break;
        case 3:data_out = IIRLPF3(data_in); break;
        default:break;
        }
        break;
    case 4:
        switch (type4)//The options of function types;  <<<<<<<<--------- your code has type 3 here
        {
        case 1:data_out = IIRHPF1(data_in); break;
        case 2: data_out = IIRHPF2(data_in); break;
        case 3: data_out = IIRHPF3(data_in); break;
        default:break;
        }
        break;
    default: break;
    }
    Aout = data_out + 0.5;
}
posted by Andy A 22 Apr 2015

Or the function pointer version:

float (*filterFunctionPtr)(float);

void getOption() {
    int Option = 0;
    int type = 0;
    pc.printf("1. FIR_LPF; 2. FIR_HPF;3. IIR_LPF; 4. IIR_HPF;\n\r");
    pc.printf("Please enter the type you want: \n\r");
    pc.scanf("%d", &Option);
    switch (Option)//The options of the filter types;
    {
    case 1:
        pc.printf("You choice is: FIR_LPF\n\r");
        pc.scanf("%d", &type);
        switch (type)//The options of function types;
        {
        case 1:pc.printf("Your choice is: equiripple.\n\r"); filterFunctionPtr = &FIRLPF1; break;
        case 2:pc.printf("Your choice is: hamming.\n\r"); filterFunctionPtr = &FIRLPF2; break;
        case 3:pc.printf("Your choice is: kaiser.\n\r"); filterFunctionPtr = &FIRLPF3; break;
        default:printf("Please type effective number!\n\r");
        }
        break;
    case 2:
        pc.printf("You choice is: FIR_HPF\n\r");
        pc.scanf("%d", &type);
        switch (type)//The options of function types;
        {
        case 1:pc.printf("Your choice is: equiripple.\n\r"); filterFunctionPtr = &FIRHPF1; break;
        case 2:pc.printf("Your choice is: hamming.\n\r"); filterFunctionPtr = &FIRHPF2; break;
        case 3:pc.printf("Your choice is: kaiser.\n\r"); filterFunctionPtr = &FIRHPF3; break;
        default:printf("Please type effective number!\n\r");
        }
        break;
    case 3:
        pc.printf("You choice is: IIR_LPF\n\r");
        pc.scanf("%d", &type);
        switch (type)//The options of function types;
        {
        case 1:pc.printf("Your choice is: butterworth.\n\r"); filterFunctionPtr = &IIRLPF1; break;
        case 2:pc.printf("Your choice is: chebyshev I.\n\r"); filterFunctionPtr = &IIRLPF2; break;
        case 3:pc.printf("Your choice is: chebyshev II.\n\r"); filterFunctionPtr = &IIRLPF3; break;
        default:printf("Please type effective number!\n\r");
        }
        break;
    case 4:
        pc.printf("You choice is: IIR_HPF\n\r");
        pc.scanf("%d", &type);
        switch (type)//The options of function types;
        {
        case 1:pc.printf("Your choice is: butterworth.\n\r"); filterFunctionPtr = &IIRHPF1; break;
        case 2:pc.printf("Your choice is: chebyshev I.\n\r"); filterFunctionPtr = &IIRHPF2; break;
        case 3:pc.printf("Your choice is: chebyshev II.\n\r"); filterFunctionPtr = &IIRHPF3; break;
        default:printf("Please type effective number!\n\r");
        }
        break;
    default:printf("Please type effective number!\n\r");
    }

int main()
{
    getOption();
    sample20kHz_tick.attach_us(&sample20kHz_task, 50);//attach task to 50us tick  
}

//------------------------------------------------------------------[sampling and control function]
void sample20kHz_task(void)
{
    data_in = Ain - 0.5;
    data_out = (*filterFunctionPtr)(data_in);
    Aout = data_out + 0.5;
}
posted by Andy A 22 Apr 2015

Thanks a lot, but if i want to add another menu to let user type a number and return to the main menu for another choice,what should I do?

posted by Joe Davis 23 Apr 2015
8 years, 11 months ago.

Bit too much code for a full review. Check at least :

case 4:
		pc.printf("You choice is: IIR_HPF\n\r");
		pc.printf("Please enter your pereference of the function type: \n\r");
		pc.printf("1. butterworth; 2. chebyshev I; 3. chebyshev II;\n\r");
		pc.scanf("%d", &type4);
		pc.printf("Your choice is: %d\n\r", type4);

==>> switch (type3)//The options of function types;   TEST on type3, you want to test on type4
		{
		case 1:pc.printf("Your choice is: butterworth.\n\r"); data_out = IIRHPF1(data_in); break;
		case 2:pc.printf("Your choice is: chebyshev I.\n\r"); data_out = IIRHPF2(data_in); break;
		case 3:pc.printf("Your choice is: chebyshev II.\n\r"); data_out = IIRHPF3(data_in); break;
		default:printf("Please type effective number!\n\r");
		}
		break;