summaryrefslogtreecommitdiff
path: root/FIR/firmath.c
blob: 3fdf20bbdffb9fb89c036ab2b5ca55bdd6e6f124 (plain) (blame)
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
//
//  firmath.c
//  FIR_rect_win
//
//  Created by Jacky Jack on 04/07/2021.
//

#include "firmath.h"

/*
void window_func_rect(double *w, int N1) {
    int j;
    
    for (j=0;j<N1;j++) {
        w[j] = 1.0;
    }
}
*/

void fir_init(fir_t *fir, uint32_t taps) {
    int i;
    for (i=0;i<taps;i++) {
        fir->fir_coef[i] = (double)0.0;
    }
    fir->num_taps = taps;
}


void firw_init(firw_t *fir_win, uint32_t taps) {
    int i;
    for (i=0;i<taps;i++) {
        fir_win->win_coef[i] = (double)0.0;
    }
    fir_win->num_taps = taps;
}


double math_sinx(double x) {
    if (x>-1.0E-5 && x < 1.0E-5) return (1.0);
    return sin(x)/x;
}

void fir_sinc_lp(fir_t *fir, double omega_c) {
    int i;
    double arg=0.0;
    //should set 2fc?!
    printf("omega_c=%f\n",omega_c);
    for (i=0;i<fir->num_taps;i++) {
        arg = (double)i-(double)(fir->num_taps-1)/2.0;
        fir->fir_coef[i] = omega_c * math_sinx(omega_c*arg*M_PI);
    }
}

void fir_sinc_hp(fir_t *fir, double omega_c) {
    int i;
    double arg=0.0;
    int even = fir->num_taps%2 ? 0: 1;
    
    //should set 2fc?!
    printf("omega_c=%f\n",omega_c);
    if (even) {
        for (i=0;i<fir->num_taps;i++) {
            arg = (double)i-(double)(fir->num_taps-1)/2.0;
            if (arg == 0.0) {
                fir->fir_coef[i] = 0.0;
            } else {
                //from IOWA
                fir->fir_coef[i] = cos(omega_c*arg*M_PI)/M_PI/arg + cos(arg*M_PI);
            }
        }
    } else {
        for (i=0;i<fir->num_taps;i++) {
            arg = (double)i-(double)(fir->num_taps-1)/2.0;
            fir->fir_coef[i] = math_sinx(arg*M_PI)-omega_c * math_sinx(omega_c*arg*M_PI);
        }
    }
}

void fir_sinc_bp(fir_t *fir, double omega_c1, double omega_c2) {
    int i;
    double arg=0.0;
    //should set 2fc?!
    printf("omega_c1=%f\n",omega_c1);
    printf("omega_c2=%f\n",omega_c2);
    for (i=0;i<fir->num_taps;i++) {
        arg = (double)i-(double)(fir->num_taps-1)/2.0;
        if (arg==0.0) {
            fir->fir_coef[i] = 0.0;
        } else {
            //fir->fir_coef[i] = math_sinx(arg*M_PI) - omega_c2 * math_sinx(omega_c2*arg*M_PI) - omega_c1 * math_sinx(omega_c1*arg*M_PI);
            fir->fir_coef[i] = (cos(omega_c1*arg*M_PI) - cos(omega_c2*arg*M_PI))/M_PI/arg;
        }
    }
}

void fir_sinc_bs(fir_t *fir, double omega_c1, double omega_c2) {
    int i;
    double arg=0.0;
    //should set 2fc?!
    printf("omega_c1=%f\n",omega_c1);
    printf("omega_c2=%f\n",omega_c2);
    for (i=0;i<fir->num_taps;i++) {
        arg = (double)i-(double)(fir->num_taps-1)/2.0;
        if (arg==0.0) {
            fir->fir_coef[i] = 0.0;
        } else {
            //fir->fir_coef[i] = math_sinx(arg*M_PI) - omega_c2 * math_sinx(omega_c2*arg*M_PI) - omega_c1 * math_sinx(omega_c1*arg*M_PI);
            fir->fir_coef[i] = math_sinx(arg*M_PI)  - omega_c2*math_sinx(omega_c2*arg*M_PI) - omega_c1*math_sinx(omega_c1*arg*M_PI);
        }
    }
}

void fir_convolute(fir_t *fir,firw_t *firw) {
    int i=0;
    for (i=0;i<fir->num_taps;i++) {
        fir->fir_coef[i] = fir->fir_coef[i]*firw->win_coef[i];
    }
}


void firw_rect(firw_t *fir_win) {
    int i;
    int n1 = fir_win->num_taps/2;
    int even = fir_win->num_taps%2 ? 0: 1;
    
    if (even) n1+=1;
    
    for (i=0;i<n1;i++) {
        fir_win->win_coef[i] = 1.0;
    }
    
    for (i=0;i<n1;i++) {
        fir_win->win_coef[fir_win->num_taps-1-i] =  fir_win->win_coef[i];
    }
    
}


void firw_hamming(firw_t *fir_win) {
    int i;
    double dN=0;
    double arg=0;
    //double p=0;
    //double arg=0;
    
    int n1 = fir_win->num_taps/2;
    int even = fir_win->num_taps%2 ? 0: 1;
    if (!even) n1+=1;
    dN = fir_win->num_taps +1;
    
    for (i=0;i<n1;i++) {
        //arg = (double)i-(double)(fir_win->num_taps-1)/2.0;
        //fir_win->win_coef[i] =  0.54+(1-0.54)*cos(2*M_PI*arg/n1);
        
        //fir_win->win_coef[i] =  0.54+(1-0.54)*cos(2*M_PI*i/n1);
        arg = (double)i-(double)(fir_win->num_taps-1)/2.0;
        fir_win->win_coef[i] = 0.54+0.46*cos(2*M_PI*arg/fir_win->num_taps);
        
    }
    //fold
    for (i=0;i<n1;i++) {
        //arg = (double)i-(double)(fir_win->num_taps-1)/2.0;
        //fir_win->win_coef[i] =  0.54+(1-0.54)*cos(2*M_PI*arg/n1);
        
        fir_win->win_coef[fir_win->num_taps-1-i] =  fir_win->win_coef[i];
    }
    
}


void firw_hanning(firw_t *fir_win) {
    int i;
    double dN=0;
    double arg=0;
    
    int n1 = fir_win->num_taps/2;
    int even = fir_win->num_taps%2 ? 0: 1;
    if (!even) n1+=1;
    dN = fir_win->num_taps +1;
    
    for (i=0;i<n1;i++) {
        arg = (double)i-(double)(fir_win->num_taps-1)/2.0;
        fir_win->win_coef[i] = 0.5+(1-0.5)*cos(2*M_PI*arg/fir_win->num_taps);
        
    }
    //fold
    for (i=0;i<n1;i++) {
        fir_win->win_coef[fir_win->num_taps-1-i] =  fir_win->win_coef[i];
    }
}

void firw_blackman(firw_t *firw) {
    int i;
    double dN=0;
    double arg=0;
    
    int n1 = firw->num_taps/2;
    int even = firw->num_taps%2 ? 0: 1;
    if (!even) n1+=1;
    dN = firw->num_taps +1;
    
    for (i=0;i<n1;i++) {
        arg = (double)i-(double)(firw->num_taps-1)/2.0;
        /*
        firw->win_coef[i] = 0.42
        +0.50*cos(2*M_PI*arg/(firw->num_taps))
        +0.08*cos(4*M_PI*arg/(firw->num_taps));
        */
        firw->win_coef[i] = 0.42
        +0.50*cos(2*M_PI*arg/(firw->num_taps-1))
        +0.08*cos(4*M_PI*arg/(firw->num_taps-1));
        
    }
    //fold
    for (i=0;i<n1;i++) {
        firw->win_coef[firw->num_taps-1-i] =  firw->win_coef[i];
    }
}

void fir_print_matlab(fir_t *fir) {
    int i;
    printf("f=[\n");
    for (i=0;i<fir->num_taps;i++) {
        printf("%f\n",fir->fir_coef[i]);
    }
    printf("];\n");
}

void firw_print_matlab(firw_t *firw) {
    int i;
    printf("w=[\n");
    for (i=0;i<firw->num_taps;i++) {
        printf("%f\n",firw->win_coef[i]);
    }
    printf("];\n");
}


void fir_print_c(fir_t *fir) {
    int i;
    printf("double f=[\n");
    for (i=0;i<fir->num_taps;i++) {
        printf("%f,\n",fir->fir_coef[i]);
    }
    printf("];\n");
}