-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathfilter_code.h
More file actions
284 lines (194 loc) · 8.05 KB
/
filter_code.h
File metadata and controls
284 lines (194 loc) · 8.05 KB
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
/*
Copyright (C) 2025 BrerDawg
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
//filter_code.h
//v1.15
#ifndef filter_code_h
#define filter_code_h
#include <stdio.h>
#include <string.h>
#include <string>
#include <wchar.h>
#include <math.h>
#include <vector>
#include <complex>
#include <cmath> //for std::cyl_bessel_i();
//#include "globals.h" //v1.06
#include "GCProfile.h"
//#include "mgraph.h" //v1.06
//#include "audio_formats.h"
using namespace std;
#define cn_filter_tap_limit 16384
namespace filter_code
{
// extern double kaiser_alpha;
extern double kaiser_beta; //v1.15
enum en_filter_pass_type_tag
{
fpt_undefined,
fpt_lowpass,
fpt_highpass,
fpt_bandpass,
fpt_notch,
fpt_bandpass2, //use by: filter_iir_2nd_order(..)
fpt_apf,
fpt_peakeq,
fpt_lowshelf,
fpt_highshelf,
};
//refer http://www.mikroe.com/chapters/view/72/chapter-2-fir-filters/
enum en_filter_window_type_tag
{
fwt_undefined,
fwt_rect,
fwt_kaiser, // ADJUST ALSO as req: 'kaiser_beta'
fwt_bartlett,
fwt_hann,
fwt_bartlett_hanning,
fwt_hamming,
//fwt_bohman, //not yet supported
fwt_blackman,
fwt_blackman_harris,
fwt_lanczos1, //v1.03
fwt_lanczos1_5, //v1.03
fwt_lanczos2, //v1.03
fwt_lanczos3, //v1.03
};
struct st_cplex_tag
{
double real;
double imag;
//conjugate
st_cplex_tag conj() const //v1.13
{
return { real, -imag };
}
//complex multiply
st_cplex_tag operator*(const st_cplex_tag& rhs) const
{
return
{
real * rhs.real - imag * rhs.imag,
real * rhs.imag + imag * rhs.real
};
}
};
struct st_cplex_float_tag
{
float real;
float imag;
//conjugate
st_cplex_float_tag conj() const //v1.13
{
return { real, -imag };
}
//complex multiply
st_cplex_float_tag operator*(const st_cplex_float_tag& rhs) const
{
return
{
real * rhs.real - imag * rhs.imag,
real * rhs.imag + imag * rhs.real
};
}
};
typedef struct //see 'create_filter_from_coeffs()' function for EXAMPLE of HOW to set this structure up
{
bool verb; //verbose for debugging, v1.06
string suser0; //user string for debugging v1.06
string suser1;
int user_id0; //user num storage
int user_id1;
bool created;
int coeff_cnt;
double *coeff_ptr;
double *prev;
unsigned int prev_idx;
bool bypass;
} st_fir;
//v1.03, see also, see also 'st_iir_2nd_order_tag'
typedef struct
{
bool verb; //verbose for debugging, v1.07
string suser0; //user string for debugging v1.07
string suser1;
int user_id0; //user num storage v1.07
int user_id1;
bool bypass;
bool created;
int coeff_cnt;
// double *coeff_ptr; //v1.07
double a1, a2;
double b0, b1, b2;
double dly0;
double dly1;
} st_iir;
//v1.02, see also 'st_iir'
struct st_iir_2nd_order_tag
{
bool verb; //verbose for debugging, v1.06
string suser0; //user string for debugging v1.08
string suser1;
int user_id0; //user num storage
int user_id1;
bool bypass;
float coeff[5]; //a1, a2, b0, b1, b2
float delay0[2]; //for ch0
float delay1[2]; //for ch1
};
//function prototypes
bool calc_filter_iir_2nd_order( en_filter_pass_type_tag filt_type, double fc1, double in_Q, double db_gain, double srate, vector<double> &vcoeff );
bool filter_iir_2nd_order( vector <st_cplex_tag> &viq, double a1, double a2, double b0, double b1, double b2, double &d0r, double &d1r, double &d0i, double &d1i );
bool filter_fir_windowed( en_filter_window_type_tag wnd_type, en_filter_pass_type_tag filt_type, unsigned int taps, double fc1, double fc2, double srate, vector<double> &vcoeff );
bool read_iowa_hills_coeffs( string fname, int section, double &a1, double &a2, double &b0, double &b1, double &b2 );
void fir_init( st_fir &fir );
void fir_in( st_fir &fir, double in );
double fir_out( st_fir &fir );
//double fir_out_inline_4( st_fir &fir );
//double fir_out_inline_8( st_fir &fir );
bool create_filter_from_coeffs( st_fir &fir, vector<double> &vcoeff );
bool create_iir_filter_from_coeffs_b0_first( st_iir &iir, vector<double> &vcoeff );
void delete_filter( st_fir &fir );
bool create_filter_from_string( st_fir &fir, string scoeff );
bool create_filter_from_file( st_fir &fir, string fname );
//void filter_iir_2nd_order_slow( double &dsignal, double a1, double a2, double b0, double b1, double b2, double &d0, double &d1 );
void filter_iir_2nd_order( float &fsignal, st_iir_2nd_order_tag &of ); //v1.02 //NOTE this is SIMILAR to 'iir_process()'
void filter_iir_2nd_order_2ch( float &fsig0, float &fsig1, st_iir_2nd_order_tag &of ); //v1.02
bool create_iir_filter_from_coeffs( st_iir &iir, vector<double> &vcoeff ); //v1.03
void iir_init( st_iir &iir ); //v1.03
void iir_delete_filter( st_iir &iir ); //v1.03
double iir_process( st_iir &iir, double in ); //v1.03 //NOTE this is SIMILAR to 'filter_iir_2nd_order()'
void create_filter_iir_using_q( filter_code::en_filter_pass_type_tag filt_type, float filt_freq_in, float filt_q_in, int srate_in, filter_code::st_iir_2nd_order_tag &iir );
int factorial( int n ); //v1.08
float kaiser_Io( float x ); //v1.08
bool window_calc_and_apply_float( en_filter_window_type_tag wnd_type, vector<float> &vsmpl, vector<float> &vwnd ); //v1.08
bool window_function_float( en_filter_window_type_tag wnd_type, unsigned len, vector<float> &vwnd ); //v1.08
bool load_coeffs_from_file_float( string fname, vector<float> &vcoeff ); //v1.10
bool load_coeffs_from_file_double( string fname, vector<double> &vcoeff ); //v1.10
void polyphase_filter_downsampler_complex( filter_code::st_cplex_float_tag *cin, unsigned int insize, vector<filter_code::st_cplex_float_tag> &vcout, vector<float> &vpolycoeff, unsigned int phase_cnt, unsigned int phase_tap_cnt );
void polyphase_filter_downsampler_for_fft( filter_code::st_cplex_float_tag *cin, unsigned int insize, vector<filter_code::st_cplex_float_tag> &vcout, vector<float> &vpolycoeff, unsigned int phase_cnt, unsigned int phase_tap_cnt );
void polyphase_filter_upsampler_complex( filter_code::st_cplex_float_tag *cin, unsigned int insize, vector<filter_code::st_cplex_float_tag> &vcout, vector<float> &vpolycoeff, unsigned int phase_cnt, unsigned int phase_tap_cnt, bool correct_gain );
bool polyphase_filter_upsampler_complex_vector( vector<filter_code::st_cplex_float_tag> &vcin, vector<filter_code::st_cplex_float_tag> &vcout, vector<float> &vpolycoeff, unsigned int phase_cnt, unsigned int phase_tap_cnt, bool correct_gain );
bool load_coeffs_from_string_float( string scoeff, vector<double> &vcoeff );
bool load_coeffs_from_string_double( string scoeff, vector<double> &vcoeff );
bool save_coeffs_vector_float( string fname, vector<float> &vcoeff );
bool save_coeffs_vector_double( string fname, vector<double> &vcoeff );
void make_string_from_coeffs_float( vector<float>vcef, string &sout );
void make_string_from_coeffs_double( vector<double>vcef, string &sout );
void window_kaiser_float( int ntaps, double kaiser_beta, vector<float> &vkaiser );
void window_kaiser_double( int ntaps, double kaiser_beta, vector<double> &vkaiser );
void make_halfband_coeffs_float( unsigned int odd_tap_cnt, vector<float> &vcef );
void make_halfband_coeffs_double( unsigned int odd_tap_cnt, vector<double> &vcef );
} //namespace 'filter_code'
#endif