aboutsummaryrefslogblamecommitdiffstats
path: root/cmd/cmd_fir.c
blob: 28adff5de62496295ed6d89314547dd2ceb6d168 (plain) (tree)
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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
































































































































































































































































































































































































































                                                                                                                 
                





                       







                             















































































































































































                                                                                                                                                                                           
                                            











                                        
#include "cmd_fir.h"

#define	pi	3.14159265

typedef struct filter_t
{
	double fs,fc1,fc2,beta;
	int n,n1;
	int wtype;
	int type;
	int nflag;
} filter_t;

#define FILT_LP   1
#define FILT_HP   2
#define FILT_BP   3
#define FILT_BS   4
#define FILT_CHECK(X) (((X)>=FILT_LP)&&((X)<=FILT_BS))

#define FILT_WIN_RECT 1
#define FILT_WIN_HAMM 2
#define FILT_WIN_HANN 3
#define FILT_WIN_BLCK 4
#define FILT_WIN_KAIS 5
#define FILT_WIN_CHECK(X) (((X)>=FILT_WIN_RECT)&&((X)<=FILT_WIN_KAIS))

void	sincx1_(double fc, double *sinc_h, int n1, int parity);
int     filter_spec_(filter_t *filt, int type, double fs, double fc1, double fc2, int wtype, double beta, int n);
void    lowpass_highpass_(filter_t *filt, double *sinc_h);
void	window_func_(filter_t *filt, double *win);
void	filter_output_(filter_t *filt, double *h, double *hb);
double	bessel(double x);

void filter_calc(filter_t *filt, double *hc, int hc_sz);



int filter_spec_(filter_t *filt, int type, double fs, double fc1, double fc2, int wtype, double beta, int n)
{

	/*Check input params*/
	//check sample rate
	if (fs < 0)
	{
		ERROR("ERROR fs\n");
		return -1;
	}
	filt->fs = fs;

	//check filter type
	if (!FILT_CHECK(type))
	{
		ENL();
		return -1;
	}
	filt->type = type;

	if ((filt->type==FILT_LP)||(filt->type==FILT_HP))
	{
		//PRINT("%f %f\n",fc1, fs);
		filt->fc1 = fc1/fs; //fc2 not used
	} else if ((type==FILT_BS)||(type==FILT_BP))
	{
		filt->fc1 = fc1/fs;
		filt->fc2 = fc2/fs;
		//PRINT("%f %f %f\n", fc1, fc2, fs);
	} else
	{
		ENL();
		return -1;
	}

	//window type used
	if (FILT_WIN_CHECK(wtype))
	{
		if (wtype == FILT_WIN_KAIS)
		{
			//beta for Kaiser window
			filt->beta = beta;
		}
	}
	filt->wtype = wtype;

	/* request filter length and determine whether it is odd or even
	* for N even only N/2 coefficients need be calculated, and for N odd
	* only (N+1)/2  because of the symmetry in h(n).
	*/

	filt->n = n;
	filt->n1 = n/2;
	filt->nflag = 1;
	if ((filt->n - 2*filt->n1)==1)
	{
		filt->nflag = 0;
		filt->n1 = filt->n1+1;
	}
	return 0;
}

//sinc_h size as filt->n1 size
void lowpass_highpass_(filter_t *filt, double *h)
{
	int j;

	sincx1_(filt->fc1, h, filt->n1, filt->nflag);
	if (filt->type == FILT_HP)
	{
		h[0]=1-h[0];
		for (j=0;j<filt->n1;++j)
		{
			h[j] = -h[j];
			//printf("%f ",sinc_h[j]);
		}
	}

}


void bandpass_bandstop_(filter_t *filt, double *sinc_h, double *h_bp)
{
	int j;

	double fc;

	fc = filt->fc2;
	sincx1_(fc, sinc_h, filt->n1, filt->nflag);
	for (j=0; j<filt->n1;++j)
	{
		h_bp[j] = sinc_h[j];
	}

	fc = filt->fc1;
	sincx1_(fc, sinc_h, filt->n1, filt->nflag);
	for (j=0;j<filt->n1;++j)
	{
		sinc_h[j] = h_bp[j] - sinc_h[j];
	}

	if (filt->type == FILT_BS)
	{
		sinc_h[0] = 1 - sinc_h[0];
		for (j=1; j<filt->n1; ++j)
		{
			sinc_h[j] = -sinc_h[j];
		}
	}
}

/*----------------------------------------------------------------------
* function to calculate sincx: 2FC * sin(nwc)/nwc
*/
void	sincx1_(double fc, double *h, int n1, int parity)
{
	int	i,j;
	double	omega, p;

	omega=2*pi*fc;
	h[0]=2*fc;
	i=1;
	if(parity==1)
	{	/* check for N even */
		i=0;
	}

	for(j=i; j<n1; j++)
	{
		p=parity*0.5+j;
		h[j]=2*fc*sin(omega*p)/(omega*p);
	}
}


void	window_func_(filter_t *filt, double *win)
{
	int	j;
	double	bd,p,rm,bn,x1;

	switch (filt->wtype)
	{
	case FILT_WIN_RECT:
		for (j=0; j<filt->n1; ++j)
		{
			win[j]=1.0;
		}
		break;
	case FILT_WIN_HAMM:
		for (j=0; j<filt->n1; ++j)
		{
			p=filt->nflag*0.5+j;
			win[j] = 0.54+(1-0.54)*cos(2*pi*p/filt->n);
		}
		break;
	case FILT_WIN_HANN:
		for (j=0; j<filt->n1; ++j)
		{
			p = filt->nflag*0.5 + j;
			win[j] = 0.5+(1-0.5)*cos(2*pi*p/filt->n);
		}
		break;
	case FILT_WIN_BLCK:
		for (j=0; j<filt->n1; ++j)
		{
			p = filt->nflag*0.5 + j;
			win[j] = 0.42 
				+ 0.5*cos(2*pi*p/(filt->n-1)) 
				+ 0.08*cos(4*pi*p/(filt->n-1));
		}
		break;
	case FILT_WIN_KAIS:
		x1 = filt->beta;
		bd = bessel(x1);
		for (j=0; j<filt->n1; ++j)
		{
			p = filt->nflag*0.5 + j;
			rm = 2*p/(filt->n);
			rm = rm*rm;
			x1 = filt->beta * sqrt(1-rm);
			bn = bessel(x1);
			win[j] = bn/bd;
		}
		break;
	default:
		printf("Unknown window type %d\n", filt->wtype);
	}
}

/*
*
*/
double	bessel(double x)
{
	double	y,t,t1,e,de,sde;
	int	i;

	y=x/2;
	t=1.E-08; e=1; de=1;
	for(i=1; i <25; ++i){
		t1=(double) i;
		de=de*y/t1;
		sde=de*de;
		e=e+sde;
		if((e*t-sde) > 0.0)
			return(e);
	}
	ENL();
	return 0.0;
}


void	filter_output_(filter_t *filt, double *h, double *hb)
{
	int j;

	double fs,fc,fc1,fc2;

	fs  = filt->fs;
	fc  = filt->fc1 * filt->fs;
	fc1 = filt->fc1 * filt->fs;
	fc2 = filt->fc2 * filt->fs;

	switch(filt->type)
	{
	case FILT_LP:	
		printf("lowpass filter \n");
		printf("sampling frequency:\t%f\n", fs);
		printf("cutoff frequency:\t%f\n", fc);
		break;
	case FILT_HP:	
		printf("highpass filter \n");
		printf("sampling frequency:\t%f\n", fs);
		printf("cutoff frequency:\t%f\n", fc);
		break;
	case FILT_BP:	
		printf("bandpass filter \n");
		printf("sampling frequency:\t%f\n", fs);
		printf("cutoff frequencies:\t%f\t%f\n", fc1, fc2);
		break;
	case FILT_BS:	
		printf("bandstop filter \n");
		printf("sampling frequency:\t%f\n", fs);
		printf("cutoff frequencies:\t%f\t%f\n", fc1, fc2);
		break;
	}
	printf("\n");

	switch(filt->wtype)
	{
	case 1:	
		printf("Rectangular window \n"); 
		break;
	case 2:	
		printf("Hamming window \n"); 
		break;
	case 3:	
		printf("Hanning window \n"); 
		break;
	case 4:	
		printf("Blackman window \n");
		break;
	case 5:	
		printf("Kaiser window \n");
		break;
	}
	printf("\n");

	printf("Impulse response coefficients\n");
	printf("\n");
	for (j=0;j<filt->n1;++j)
	{
		printf("h[%2d",j);
		printf("]  =\t%15.5e\t",h[filt->n1-j-1]);
		printf("    =  h[%2d",filt->n-j-1);
		printf("]\n");
		hb[j]=h[filt->n1-j-1]; 
		hb[filt->n-j-1]=hb