Very simple DSP filter - not working as intended

EDIT: the N and M were used before (I used dynamic arrays *X_Global/Local before)
EDIT#2: I changed the shifting loop part "m = 0; to
1
2
3
4
5
 
    for (n = 0; n < N; n++){ //for n=0 means the global array's 1st element, n<N is the last element (which is equivalent as n<=N-1)
        for (int j = 0; j <= M-1; j++) { //Shifts all X_Local element to 1 lower subscript loop
            X_Local[j-1] = X_Local[j];
        } //End of Shifting loop 

It worked as intended but I don't know why...could someone kindly explain this to me?

My goal was to create a very simple filter (which should be low-pass), using if and for statements.
Sadly, I couldn't get my output right.
On the line "printf("%f \n", Y_Output);", I got results as below:
0.000000
0.000000
0.000000
0.000000
0.000000
0.000000
-0.002694
0.000000
0.000000
0.000000
0.000000
0.000000
0.000000
0.000000
0.000000
0.000000
0.000000
0.000000
0.000000
0.000000
0.000000
0.000000
0.000000
0.000000
0.000000
0.000000
0.000000
0.000000
0.000000
0.000000
0.000000

Which is totally wrong (The figures should shift along the Y_Output).
Could anyone please give me a hand and tell me what's wrong with my code?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#include <stdio.h>
#include <iostream>
//DECLARE VARIABLES BEFORE PROGRAM START. FUCK.

int main() { //main program
#pragma mark variables
    int N = 50; //number of samples in the X_Global array
    int n = N-1; //n is the subscript of the X_Global array, which is N-1
    float X_Global[50] = {};

    int M = 19; //number of samples required in the X_Local array
    float X_Local[19] = {};
    int m = M-1; //m is the subscript of the X_Local array, which is M-1
    
    double b_weight[M]; //the size of b array is the same as size of X_Local array.
    int i; //i is the subscript of the b_weight array. This is inverse to the subscript of X_local
    //THE SUBSCRIPT OF b_weight WILL BE CHANGED TO "i" LATER//
    //                  //
    //                  //
    //////////////////////
    double Y_Output; //instead of using an array, Y_Output is used
    double Y_Total; //Y_Total is used to add up all the outputs of X_Local * b_weight (from 0 - 19 elements)

#pragma mark weight of b

    b_weight[0] = b_weight[18] = -0.002693840;
    b_weight[1] = b_weight[17] = -0.002519748;
    b_weight[2] = b_weight[16] = 0.005014695;
    b_weight[3] = b_weight[15] = 0.015641050;
    b_weight[4] = b_weight[14] = 0.000000000;
    b_weight[5] = b_weight[13] = -0.046914239;
    b_weight[6] = b_weight[12] = -0.048021820;
    b_weight[7] = b_weight[11] = 0.083481298;
    b_weight[8] = b_weight[10] = 0.294332820;
    b_weight[9] = 0.400000000;
    
#pragma mark initialization
    
/*    for (n=0; n<N; n++) { //init X_Global
        X_Global[n] = 0;
//        printf("%f \n",X_Global[n]);
    }
    */
/*    for (int m=0; m<M; m++){ //init X_Local
        X_Local[m] = 0;
//        printf("%f \n",X_Local[m]);
    }*/

#pragma mark main program
    
    X_Global[25] = 1; //the middle element of the Global array is 1 (for testing)
    
    for (n = 0; n < N; n++){ //for n=0 means the global array's 1st element, n<N is the last element (which is equivalent as n<=N-1)
        for (m = 0; m <= M-1; m++) { //Shifts all X_Local element to 1 lower subscript loop
            X_Local[m-1] = X_Local[m];
        } //End of Shifting loop
        
        X_Local[m] = X_Global[n]; //Copy the newest global array element "n" to the newest local array element [m]

        if (n >= m){ //n >= M-1 ("m") makes sure the X_Local is already filled up with M samples, so calculations can be made by the program
                     //correctly
            Y_Output = 0;
            for (i = 0; i <= m; i++){
                Y_Output += b_weight[i]*X_Local[m-i];
            }
            printf("%f \n", Y_Output);
        }

    } //End of the whole global loop
}
Last edited on
Topic archived. No new replies allowed.