-
Notifications
You must be signed in to change notification settings - Fork 0
/
window.c
150 lines (115 loc) · 4.08 KB
/
window.c
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
/**
* @file window.c
* @author Sravan Senthilnathan
* @brief FIR filter window generator
* @date 2023-05-03
*
* @copyright Copyright (c) 2023
*
*/
#include "filter.h"
#include <stdlib.h>
#include <math.h>
#define PI 3.141592653
#define FILTER_HANNING 1
#define FILTER_HAMMING 0
/**
* @brief Low pass FIR filter coefficent generator
*
* @param order order of the desired filter
* @param fNyquist frequency of sampling
* @param fCutoff cutoff filtering frequency
* @return float* allocated array of filter coefficients
*/
float* windowLowPass(int order, float fNyquist, float fCutoff){
int t = (order%2 == 0)?order/2:(order-1)/2;
float fNormalized = 2 * PI * fCutoff / fNyquist;
float* coeff = (float*)malloc(order * sizeof(float));
for(int n = 0; n < order; n++){
if(n == t) coeff[n] = fNormalized/PI;
else coeff[n] = sin(fNormalized * (n - t))/(PI * (n - t));
#if FILTER_HANNING
coeff[n] *= 0.5 - 0.5 * cos((2 * PI * n)/(order - 1));
#endif
#if FILTER_HAMMING
coeff[n] *= 0.54 - 0.46 * cos((2 * PI * n)/(order - 1));
#endif
}
return(coeff);
}
/**
* @brief High pass FIR filter coefficent generator
*
* @param order order of the desired filter
* @param fNyquist frequency of sampling
* @param fCutoff cutoff filtering frequency
* @return float* allocated array of filter coefficients
*/
float* windowHighPass(int order, float fNyquist, float fCutoff){
int t = (order%2 == 0)?order/2:(order-1)/2;
float fNormalized = 2 * PI * fCutoff / fNyquist;
float* coeff = (float*)malloc(order * sizeof(float));
for(int n = 0; n < order; n++){
if(n == t) coeff[n] = 1 - fNormalized/PI;
else coeff[n] = - sin(fNormalized * (n - t))/(PI * (n - t));
#if FILTER_HANNING
coeff[n] *= 0.5 - 0.5 * cos((2 * PI * n)/(order - 1));
#endif
#if FILTER_HAMMING
coeff[n] *= 0.54 - 0.46 * cos((2 * PI * n)/(order - 1));
#endif
}
return(coeff);
}
/**
* @brief Band pass FIR filter coefficient generator
*
* @param order order of the desired filter
* @param fNyquist frequency of sampling
* @param fCutLower lower cutoff frequency of the pass window
* @param fCutUpper upper cutoff frequency of the pass window
* @return float*
*/
float* windowBandPass(int order, float fNyquist, float fCutLower, float fCutUpper){
int t = (order%2 == 0)?order/2:(order-1)/2;
float fNormalLower = 2 * PI * fCutLower / fNyquist;
float fNormalUpper = 2 * PI * fCutUpper / fNyquist;
float* coeff = (float*)malloc(order * sizeof(float));
for(int n = 0; n < order; n++){
if(n == t) coeff[n] = (fNormalUpper - fNormalLower)/PI;
else coeff[n] = (sin(fNormalUpper * (n - t)) - sin(fNormalLower * (n - t)))/(PI * (n - t));
#if FILTER_HANNING
coeff[n] *= 0.5 - 0.5 * cos((2 * PI * n)/(order - 1));
#endif
#if FILTER_HAMMING
coeff[n] *= 0.54 - 0.46 * cos((2 * PI * n)/(order - 1));
#endif
}
return(coeff);
}
/**
* @brief Band pass FIR filter coefficient generator
*
* @param order order of the desired filter
* @param fNyquist frequency of sampling
* @param fCutLower lower cutoff frequency of the barrier window
* @param fCutUpper upper cutoff frequency of the barrier window
* @return float*
*/
float* windowBandStop(int order, float fNyquist, float fCutLower, float fCutUpper){
int t = (order%2 == 0)?order/2:(order-1)/2;
float fNormalLower = 2 * PI * fCutLower / fNyquist;
float fNormalUpper = 2 * PI * fCutUpper / fNyquist;
float* coeff = (float*)malloc(order * sizeof(float));
for(int n = 0; n < order; n++){
if(n == t) coeff[n] = 1 - (fNormalUpper - fNormalLower)/PI;
else coeff[n] = - (sin(fNormalUpper * (n - t)) - sin(fNormalLower * (n - t)))/(PI * (n - t));
#if FILTER_HANNING
coeff[n] *= 0.5 - 0.5 * cos((2 * PI * n)/(order - 1));
#endif
#if FILTER_HAMMING
coeff[n] *= 0.54 - 0.46 * cos((2 * PI * n)/(order - 1));
#endif
}
return(coeff);
}