-
Notifications
You must be signed in to change notification settings - Fork 1
/
filter_code.cpp
1436 lines (861 loc) · 31.2 KB
/
filter_code.cpp
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
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
Copyright (C) 2019 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.cpp
//v1.01 01-sep-2018
//v1.02 07-nov-2020 //changed 'twopi' to a constant, commented out some iir code
//v1.03 09-oct-2022 //added function: 'iir_process(..)' which is similar to: 'filter_iir_2nd_order(..)' and 'filter_iir_2nd_order_2ch(..)'
//added function prototypes to: 'filter_code.h' ---> 'bool create_iir_filter_from_coeffs( st_iir &iir, vector<double> &vcoeff );'
//added function prototypes to: 'filter_code.h' ---> 'void iir_delete_filter( st_iir &iir );' and 'void iir_init( st_iir &iir );'
//added extra filter types to 'en_filter_window_type_tag' and 'filter_fir_windowed()': 'fwt_lanczos1', 'fwt_lanczos1_5', 'fwt_lanczos2', 'fwt_lanczos3'
//added 'filter_fir_sinc(..)'
//v1.04 22-jul-2023 //added namespace 'filter_code::'
//v1.05 18-sep-2023 //added 'create_filter_iir_using_q()'
//v1.06 26-feb-2024 //remove include(s) of 'globals.h' 'mgraph.h'
//changed 'delete' to 'delete[]'
//added 'st_fir.verb' 'st_fir.suser0' 'st_fir.suser1' 'st_fir.user_id0' 'st_fir.user_id1'
#include "filter_code.h"
namespace filter_code
{
float iir2nd_coeffs[5]; //second order iir
#define twopi (2.0*M_PI)
//reads an iowa hills coeffients text file and get filter coeffs for one specified section,
//only parses the 2'nd Order Sections that have headings: 'Sect xx',
//specify required section starting at index 0,
//returns 1 on success, else 0
bool read_iowa_hills_coeffs( string fname, int section, double &a1, double &a2, double &b0, double &b1, double &b2 )
{
string s1, ssection;
mystr m1;
if( !m1.readfile( fname, 20000 ) ) //saftey margin
{
printf("read_iowa_hills_coeffs() - failed to read file: '%s'\n", fname.c_str() );
return 0;
}
printf("read_iowa_hills_coeffs() ---------------------------------\n" );
strpf( ssection, "Sect %d", section );
bool in_section = 0;
int section_line = 0;
int check_count = 0;
char c0, c1;
double dd;
int ii = 0;
for( ii = 0; ii < 20000; ii++ ) //saftey margin
{
if( !m1.gets( s1 ) ) break;
if( s1.compare( ssection ) == 0 ) in_section = 1;
if( in_section )
{
printf("got %03d: line: %03d, '%s'\n", ii, section_line, s1.c_str() );
if( section_line == 2 ) { sscanf( s1.c_str(), "%c%c %lf", &c0, &c1, &dd ); a1 = dd; printf("check %c%c %.17f\n", c0, c1, a1 ); check_count++; }
if( section_line == 3 ) { sscanf( s1.c_str(), "%c%c %lf", &c0, &c1, &dd ); a2 = dd; printf("check %c%c %.17f\n", c0, c1, a2 ); check_count++; }
if( section_line == 4 ) { sscanf( s1.c_str(), "%c%c %lf", &c0, &c1, &dd ); b0 = dd; printf("check %c%c %.17f\n", c0, c1, b0 ); check_count++; }
if( section_line == 5 ) { sscanf( s1.c_str(), "%c%c %lf", &c0, &c1, &dd ); b1 = dd; printf("check %c%c %.17f\n", c0, c1, b1 ); check_count++; }
if( section_line == 6 ) { sscanf( s1.c_str(), "%c%c %lf", &c0, &c1, &dd ); b2 = dd; printf("check %c%c %.17f\n", c0, c1, b2 ); check_count++; }
section_line++;
if( section_line >= 7 ) break;
}
}
//printf("read_iowa_hills_coeffs() - done - section_line %d, check_count: %d\n", section_line, check_count );
if( section_line != 7 )
{
printf("read_iowa_hills_coeffs() - 'section_line' expected to reach 7, only reached: %d, check coeffs read correctly\n", section_line );
return 0;
}
if( check_count != 5 )
{
printf("read_iowa_hills_coeffs() - 'check_count' expected to reach 5, only reached: %d, check coeffs read correctly\n", check_count );
return 0;
}
printf("read_iowa_hills_coeffs() ---------------------------------\n" );
return 1;
}
//note a0 is not used (it would be one), the other coeffs should be divided by the a0 you calc'd
// -- X --> add ------->------+-----> b0 ----> add --- Y ---->
// ^ | ^
// | v |
// | | |
// ^ xn0 ^
// | | |
// | | |
// | v |
// add <...-a1 <... xn0 ....> b1 ...> add
// ^ | ^
// | v |
// | | |
// | xn1 |
// ^ | ^
// | | |
// | v |
// |<....-a2 <... add ....> b2 ....> |
//NOTE this is SIMILAR to 'iir_process()'
//2nd order direct form 2 iir filter
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 )
{
for ( int i = 0; i < viq.size(); i++ ) //for every sample
{
double sum_rev = viq[ i ].real - a1 * d0r - a2 * d1r;
double sum_fwd = sum_rev * b0 + b1 * d0r + b2 * d1r;
viq[ i ].real = sum_fwd;
d1r = d0r;
d0r = sum_rev;
}
for ( int i = 0; i < viq.size(); i++ ) //for every sample
{
double sum_rev = viq[ i ].imag - a1 * d0i - a2 * d1i;
double sum_fwd = sum_rev * b0 + b1 * d0i + b2 * d1i;
viq[ i ].imag = sum_fwd;
d1i = d0i;
d0i = sum_rev;
}
}
void filter_iir_2nd_order( float &fsignal, st_iir_2nd_order_tag &of )
{
//a1 = coeff[0]
//a1 = coeff[1]
//b0 = coeff[2]
//b1 = coeff[3]
//b2 = coeff[4]
float sum_rev = fsignal - of.coeff[0] * of.delay0[0] - of.coeff[1] * of.delay0[1];
float sum_fwd = sum_rev * of.coeff[2] + of.coeff[3] * of.delay0[0] + of.coeff[4] * of.delay0[1];
fsignal = sum_fwd;
of.delay0[1] = of.delay0[0];
of.delay0[0] = sum_rev;
}
void filter_iir_2nd_order_2ch( float &fsig0, float &fsig1, st_iir_2nd_order_tag &of )
{
//a1 = coeff[0]
//a1 = coeff[1]
//b0 = coeff[2]
//b1 = coeff[3]
//b2 = coeff[4]
float sum_rev0 = fsig0 - of.coeff[0] * of.delay0[0] - of.coeff[1] * of.delay0[1];
float sum_rev1 = fsig1 - of.coeff[0] * of.delay1[0] - of.coeff[1] * of.delay1[1]; //ch1
float sum_fwd0 = sum_rev0 * of.coeff[2] + of.coeff[3] * of.delay0[0] + of.coeff[4] * of.delay0[1];
float sum_fwd1 = sum_rev1 * of.coeff[2] + of.coeff[3] * of.delay1[0] + of.coeff[4] * of.delay1[1]; //ch1
fsig0 = sum_fwd0;
fsig1 = sum_fwd1; //ch1
of.delay0[1] = of.delay0[0];
of.delay0[0] = sum_rev0;
of.delay1[1] = of.delay1[0];
of.delay1[0] = sum_rev1; //ch1
}
//----------------------------------------------------------------------
void iir_init( st_iir &iir )
{
if( iir.created == 0 ) return;
iir.dly0 = 0;
iir.dly1 = 0;
}
//delete filter
void iir_delete_filter( st_iir &iir )
{
if ( iir.created == 0 ) return;
iir.created = 0;
}
//note a0 is not used (it would be one), the other coeffs should be divided by the a0 you calc'd
// -- X --> add ------->------+-----> b0 ----> add --- Y ---->
// ^ | ^
// | v |
// | | |
// ^ xn0 ^
// | | |
// | | |
// | v |
// add <...-a1 <... xn0 ....> b1 ...> add
// ^ | ^
// | v |
// | | |
// | xn1 |
// ^ | ^
// | | |
// | v |
// |<....-a2 <... add ....> b2 ....> |
//NOTE this is SIMILAR to 'filter_iir_2nd_order()'
//2nd order direct form 2 iir filter
double iir_process( st_iir &iir, double in )
{
if( iir.created == 0 ) return 0;
if ( iir.bypass )
{
return in;
}
double sum_rev = in - iir.a1 * iir.dly0 - iir.a2 * iir.dly1;
double sum_fwd = sum_rev * iir.b0 + iir.b1 * iir.dly0 + iir.b2 * iir.dly1;
iir.dly1 = iir.dly0;
iir.dly0 = sum_rev;
return sum_fwd;
}
//build a filter from a vector loaded with coeffs
bool create_iir_filter_from_coeffs( st_iir &iir, vector<double> &vcoeff )
{
if( iir.created ) iir_delete_filter( iir );
//if( vcoeff.size() == 0 ) return 0;
if( vcoeff.size() < 5 ) return 0;
iir.a1 = vcoeff[0]; //load coeffs
iir.a2 = vcoeff[1];
iir.b0 = vcoeff[2];
iir.b1 = vcoeff[3];
iir.b2 = vcoeff[4];
iir.coeff_cnt = vcoeff.size();
iir.bypass = 0;
iir_init( iir ); //clear iir delays
iir.created = 1;
return 1;
}
//----------------------------------------------------------------------
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 )
{
vector<double> vfilt_coeff;
float db_gain = 0;
if( !calc_filter_iir_2nd_order( filt_type, filt_freq_in, filt_q_in, db_gain, srate_in, vfilt_coeff ) )
{
printf( "create_filter_iir_using_q() - failed to calc filter coeffs, freq %f, q %f\n", filt_freq_in, filt_q_in );
return;
}
//printf( "create_filter0() - iir freq %f, q %f %f %f %f %f %f\n", filt_freq0, filt_q0, iir0.coeff[0], iir0.coeff[1], iir0.coeff[2], iir0.coeff[3], iir0.coeff[4] );
iir.bypass = 0;
iir.coeff[0] = vfilt_coeff[0]; //a1
iir.coeff[1] = vfilt_coeff[1]; //a2
iir.coeff[2] = vfilt_coeff[2]; //b0
iir.coeff[3] = vfilt_coeff[3]; //b1
iir.coeff[4] = vfilt_coeff[4]; //b2
iir.delay0[0] = 0;
iir.delay0[1] = 0;
iir.delay1[0] = 0;
iir.delay1[1] = 0;
iir.bypass = 1;
}
/*
void filter_iir_2nd_order_slow( double &dsignal, double a1, double a2, double b0, double b1, double b2, double &d0, double &d1 )
{
double sum_rev = dsignal - a1 * d0 - a2 * d1;
double sum_fwd = sum_rev * b0 + b1 * d0 + b2 * d1;
dsignal = sum_fwd;
d1 = d0;
d0 = sum_rev;
}
*/
//calc spec filter coeffs suitable for a Direct Form 1 - IIR, 2nd Order
//see ...
// http://www.musicdsp.org/files/Audio-EQ-Cookbook.txt
//Direct Form 1 - IIR, 2nd Order
// -X-----+---> b0 ----> add --------------+----Y---->
// | ^ |
// xn0 | yn0
// | | |
// |...> b1 ....> add <.... -a1 <...|
// | ^ |
// xn1 | yn1
// | | |
// |...> b2 ....> add <.... -a2 <...|
//can be used with 'st_iir' or 'st_iir_2nd_order_tag' and associated functions: iir_process()' or 'filter_iir_2nd_order()'
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 )
{
printf( "filter_iir_2nd_order() - type: %u, fc1: %f, q: %f, db_gain: %f\n", filt_type, fc1, in_Q, db_gain );
if( fc1 >= srate / 2.0 )
{
printf( "filter_iir_2nd_order() - fc1: %f violates Nyquist, limiting to: %f\n", fc1, srate / 2.0 - 1 );
fc1 = srate / 2.0 - 1;
}
double Fs = srate;
double f0 = fc1;
double w0 = twopi * f0 / Fs;
double Q = in_Q;
//double BW = in_bw / fc1;
double dBgain = db_gain;
double A = pow ( 10.0 , ( dBgain / 40.0 ) ); // A = sqrt( 10.0 ^( dBgain / 20.0 ) ) (for peaking and shelving EQ filters only)
double gain = pow ( 10.0 , ( db_gain / 20.0 ) );
double alpha = sin( w0 ) / ( 2.0 * Q ); //(case: Q)
//double alpha = sin( w0 ) * sinh( log( 2.0 ) / 2.0 * BW * w0 / sin( w0 ) ); //(case: BW)
//double alpha = sin( w0 ) / 2.0 * sqrt( ( A + 1.0 / A ) * ( 1.0 / S - 1 ) + 2.0 ); //(case: S)
double a0, a1, a2;
double b0, b1, b2;
bool ok = 0;
printf( "filter_iir_2nd_order() - w0: %f, alpha: %f, A: %f\n", w0, alpha, A );
if( filt_type == fpt_lowpass )
{
b0 = ( 1.0 - cos( w0 ) ) / 2.0;
b1 = 1.0 - cos( w0 );
b2 = ( 1.0 - cos( w0 ) ) / 2.0;
a0 = 1.0 + alpha;
a1 = -2.0 * cos( w0 );
a2 = 1.0 - alpha;
ok = 1;
}
if( filt_type == fpt_highpass )
{
b0 = ( 1.0 + cos( w0 ) ) / 2.0;
b1 = - ( 1.0 + cos( w0 ) );
b2 = ( 1.0 + cos( w0 ) ) / 2.0;
a0 = 1.0 + alpha;
a1 = -2.0 * cos( w0 );
a2 = 1.0 - alpha;
ok = 1;
}
if( filt_type == fpt_bandpass ) //constant skirt gain, peak gain = Q
{
b0 = sin(w0)/2.0; //Q*alpha;
b1 = 0.0;
b2 = -sin(w0)/2.0; //-Q*alpha;
a0 = 1.0 + alpha;
a1 = -2.0*cos(w0);
a2 = 1.0 - alpha;
ok = 1;
}
if( filt_type == fpt_bandpass2 ) //constant 0 dB peak gain
{
b0 = alpha;
b1 = 0.0;
b2 = -alpha;
a0 = 1.0 + alpha;
a1 = -2.0*cos(w0);
a2 = 1.0 - alpha;
ok = 1;
}
if( filt_type == fpt_notch )
{
b0 = 1.0;
b1 = -2.0*cos(w0);
b2 = 1.0;
a0 = 1.0 + alpha;
a1 = -2.0*cos(w0);
a2 = 1.0 - alpha;
ok = 1;
}
if( filt_type == fpt_apf )
{
b0 = 1.0 - alpha;
b1 = -2.0*cos(w0);
b2 = 1.0 + alpha;
a0 = 1.0 + alpha;
a1 = -2.0*cos(w0);
a2 = 1 - alpha;
ok = 1;
}
if( filt_type == fpt_peakeq )
{
b0 = 1.0 + alpha*A;
b1 = -2.0*cos(w0);
b2 = 1.0 - alpha*A;
a0 = 1.0 + alpha/A;
a1 = -2.0*cos(w0);
a2 = 1.0 - alpha/A;
ok = 1;
}
if( filt_type == fpt_lowshelf )
{
b0 = A*( (A+1.0) - (A-1.0)*cos(w0) + 2.0*sqrt(A)*alpha );
b1 = 2.0*A*( (A-1.0) - (A+1.0)*cos(w0) );
b2 = A*( (A+1.0) - (A-1.0)*cos(w0) - 2.0*sqrt(A)*alpha );
a0 = (A+1.0) + (A-1.0)*cos(w0) + 2.0*sqrt(A)*alpha;
a1 = -2.0*( (A-1.0) + (A+1.0)*cos(w0) );
a2 = (A+1.0) + (A-1.0)*cos(w0) - 2.0*sqrt(A)*alpha;
ok = 1;
}
if( filt_type == fpt_highshelf )
{
b0 = A*( (A+1.0) + (A-1.0)*cos(w0) + 2.0*sqrt(A)*alpha );
b1 = -2.0*A*( (A-1.0) + (A+1.0)*cos(w0) );
b2 = A*( (A+1.0) + (A-1.0)*cos(w0) - 2.0*sqrt(A)*alpha );
a0 = (A+1.0) - (A-1.0)*cos(w0) + 2.0*sqrt(A)*alpha;
a1 = 2.0*( (A-1.0) - (A+1.0)*cos(w0) );
a2 = (A+1.0) - (A-1.0)*cos(w0) - 2.0*sqrt(A)*alpha;
ok = 1;
}
if( !ok )
{
printf( "filter_iir_2nd_order() - type: %u, is not supported\n", filt_type );
return 0;
}
vcoeff.clear();
vcoeff.push_back( a1 / a0 );
vcoeff.push_back( a2 / a0 );
vcoeff.push_back( b0 / a0 );
vcoeff.push_back( b1 / a0 );
vcoeff.push_back( b2 / a0 );
printf( "filter_iir_2nd_order():\n" );
printf( "a1: %f, a2: %f\n", a1 / a0, a2 / a0 );
printf( "b0: %f, b1: %f, b2: %f\n", b0 / a0, b1 / a0, b2 / a0 );
//conv_vcoeff_to_scoeff( vcoeff, scoeff );
return 1;
}
//v1.03
double filter_fir_sinc(double x)
{
if (x != 0)
{
x *= M_PI;
return ( sin(x)/x);
}
return 1;
}
//calc spec filter coeffs using spec window type(e.g. blackman) and spec filter type(e.g: hpf, notch)
//refer http://www.mikroe.com/chapters/view/72/chapter-2-fir-filters/
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 )
{
int ilow;
float x;
if( ( taps & 0x1 ) == 1 )
{
taps++;
printf( "filter_fir_windowed() - to keep window impulse response symetrical, taps count will be slighlty increased\n" );
}
if( taps >= cn_filter_tap_limit )
{
printf( "filter_fir_windowed() - tap count is too large: %u, limit is: %u\n", taps, cn_filter_tap_limit - 1 );
return 0;
}
printf( "filter_fir_windowed() - srate: %f, fc1: %f, fc2: %f\n", srate, fc1, fc2 );
//printf( "filter_fir_windowed() - wnd_type: %d, filt_type: %d\n", wnd_type, filt_type );
double fs = srate;
// fs = 20000;
// fc1 = 2500;
double *w = new double[ taps + 10 ]; //add some extra space, even though only one extra space is required (as N = taps + 1)
double *hd = new double[ taps + 10 ];
double *hcoeff = new double[ taps + 10 ];
//filt_type = fpt_highpass;
vcoeff.clear();
//double twopi = 2.0 * M_PI;
double wc1, wc2;
wc1 = twopi * fc1 / fs;
wc2 = twopi * fc2 / fs;
int Nf = taps;
double N = Nf + 1;
printf( "filter_fir_windowed() - coeff count will be: %u\n", (unsigned int)N );
double M = (double)Nf / 2.0;
//cslpf( 0, "filter_fir_windowed() - wnd_type: %d, filt_type: %d, taps: %d, wc1: %g, wc2: %g\n", wnd_type, filt_type, taps, wc1, wc2 );
//!! fix this
if( wnd_type == fwt_kaiser )
{
wnd_type = fwt_undefined;
printf( "filter_fir_windowed() - wnd=kaiser not yet supported\n" );
}
for( double n = 0; n < N; n++ )
{
int i = (int)n;
switch( wnd_type )
{
case fwt_rect:
w[ i ] = 1.0; //rect window impulse resp
break;
case fwt_bartlett:
ilow = ( N - 1.0 ) / 2.0 ; //work out which formula to use (what side of the triangle peak we are in)
if( i <= ilow ) w[ i ] = 2.0 * n / ( N - 1.0 ) ; //bartlett/triangle window impulse resp
else w[ i ] = 2.0 - 2.0 * n / ( N - 1.0 ) ;
break;
case fwt_hann:
w[ i ] = 0.5 * ( 1.0 - cos( ( twopi * n / ( N - 1.0 ) ) ) ); //hann window impulse resp
break;
case fwt_bartlett_hanning:
w[ i ] = 0.62 - 0.48 * fabs( n / ( N - 1.0 ) - 0.5 ) + 0.38 * cos( ( twopi * ( n / ( N - 1.0 ) - 0.5 ) ) ); //bartlett-hanning window impulse resp
break;
case fwt_hamming:
w[ i ] = 0.54 - 0.46 * cos( twopi * n / ( N - 1.0 ) ); //hamming window impulse resp, note the text in http://www.mikroe.com.. listed above is slightly incorrect, used wikipedia version
break;
case fwt_blackman:
w[ i ] = 0.42 - 0.5 * cos( twopi * n / ( N - 1.0 ) ) + 0.08 * cos( 2.0 * twopi * n / ( N - 1.0 ) ); //blackman window impulse resp
break;
case fwt_blackman_harris:
w[ i ] = 0.35875 - 0.48829 * cos( twopi * n / ( N - 1.0 ) ) + 0.14128 * cos( 2.0 * twopi * n / ( N - 1.0 ) ) - 0.01168 * cos( 3.0 * twopi * n / ( N - 1.0 ) ); //blackman-harris window impulse resp
break;
case fwt_lanczos1: //v1.03
x = 2.0f * (float)n/(N-1);
x -= 1;
if (x < 0) x = - x;
if (x < 1) w[ i ] = ( filter_fir_sinc( x ) * filter_fir_sinc( x / 1.0 ) );
else w[ i ] = 0;
break;
case fwt_lanczos1_5: //v1.03
x = 3.0f * (float)n/(N-1);
x -= 1.5f;
if (x < 0) x = - x;
if (x < 1.5) w[ i ] = ( filter_fir_sinc( x ) * filter_fir_sinc( x / 1.5 ) );
else w[ i ] = 0;
break;
case fwt_lanczos2: //v1.03
x = 4.0f * (float)n/(N-1);
x -= 2;
if (x < 0) x = - x;
if (x < 2) w[ i ] = ( filter_fir_sinc( x ) * filter_fir_sinc( x / 2.0 ) );
else w[ i ] = 0;
break;
case fwt_lanczos3: //v1.03
x = 6.0f * (float)n/(N-1);
x -= 3;
if (x < 0) x = - x;
if (x < 3) w[ i ] = ( filter_fir_sinc( x ) * filter_fir_sinc( x / 3.0 ) );
else w[ i ] = 0;
break;
default:
printf( "filter_fir_windowed() - unknown filter window type(wnd): %u\n", wnd_type );
return 0;
break;
}
switch( filt_type )
{
case fpt_lowpass:
//ideal lowpass impulse response
if( (int)n != (int)M ) hd[ i ] = ( ( sin( wc1 * ( n - M ) ) ) / ( M_PI * ( n - M ) ) );
else hd[ i ] = wc1 / M_PI;
break;
case fpt_highpass:
//ideal highpass impulse response
if( (int)n != (int)M ) hd[ i ] = -( ( sin( wc1 * ( n - M ) ) ) / ( M_PI * ( n - M ) ) );
else hd[ i ] = 1.0 - wc1 / M_PI;
break;
case fpt_bandpass:
//ideal bandpass impulse response
if( (int)n != (int)M ) hd[ i ] = ( sin( wc2 * ( n - M ) ) / ( M_PI * ( n - M ) ) ) - ( sin( wc1 * ( n - M ) ) / ( M_PI * ( n - M ) ) );
else hd[ i ] = ( wc2 - wc1 ) / M_PI;
break;
case fpt_notch:
//ideal notch impulse response
if( (int)n != (int)M ) hd[ i ] = ( sin( wc1 * ( n - M ) ) / ( M_PI * ( n - M ) ) ) - ( sin( wc2 * ( n - M ) ) / ( M_PI * ( n - M ) ) );
else hd[ i ] = 1.0 - ( wc1 - wc2 ) / M_PI;
break;
default:
printf( "filter_fir_windowed() - unknown filter type: %u\n", filt_type );
return 0;
break;
}
hcoeff[ i ] = w[ i ] * hd[ i ]; //window * impulse response
// printf( "filter_fir_windowed() - w[%d]: %g, hd[]: %g, h[]: %g\n", (int)n, w[ i ], hd[ i ], hcoeff[ i ] );
//printf( "filter_fir_windowed() - w[%d]: %g, hd[]: %g, h[]: %g\n", (int)n, w[ i ], hd[ i ], hcoeff[ i ] );
vcoeff.push_back( hcoeff[ i ] );
}
delete w;
delete hd;
delete hcoeff;
return 1;
}
//just clears a created filter's bufs, make sure you set various 'st_fir' elements manually, this function does not alter 'st_fir'
//MAKE SURE you set 'fir.bypass = 0', see 'create_filter_from_coeffs()' for example
void fir_init( st_fir &fir )
{
if( fir.created == 0 ) return;
fir.prev_idx = 0;
for( int i = 0; i < fir.coeff_cnt; i++ ) fir.prev[ i ] = 0;
}
//not for user use
void fir_init_internal_use( st_fir &fir )
{
if( fir.created == 0 ) return;
fir.prev_idx = 0;
for( int i = 0; i < fir.coeff_cnt; i++ ) fir.prev[ i ] = 0;
}
//EXAMPLE of HOW to set up 'st_fir' before 'filter_code::create_filter_from_coeffs()'
//filter_code::st_fir fir00;
//fir00.verb = 1;
//fir00.suser0 = "fir00";
//fir00.suser1 = "in function xxxx";
//fir00.user_id0 = 0;
//fir00.user_id1 = 0;
//fir00.bypass = 0;
//fir00.created = 0;
//build an fir filter from a string loaded with coeffs, allocates memory so it must be deleted
//see ABOVE for example usage
bool create_filter_from_coeffs( st_fir &fir, vector<double> &vcoeff )
{
bool vb = fir.verb; //v1.06
if( fir.created ) delete_filter( fir );
if( vcoeff.size() == 0 )
{
if(vb)printf( "create_filter_from_coeffs() - filter not created, size requested was zero, '%s' '%s'\n", fir.suser0.c_str(), fir.suser1.c_str() );
return 0;
}
if(vb) printf( "create_filter_from_coeffs() - alloc 2 filter bufs, size each %d, '%s' '%s'\n", (int)vcoeff.size(), fir.suser0.c_str(), fir.suser1.c_str() );
fir.coeff_ptr = new double [ vcoeff.size() ]; //alloc space
fir.prev = new double [ vcoeff.size() ];
if(vb) printf( "create_filter_from_coeffs() - about to load filter coeffs, '%s' '%s'\n", fir.suser0.c_str(), fir.suser1.c_str() );
for( int i = 0; i < vcoeff.size(); i++ ) //load coeffs
{
fir.coeff_ptr[ i ] = vcoeff[ i ];
}
fir.coeff_cnt = vcoeff.size();
fir.created = 1;
//fir.bypass = 0;
fir_init_internal_use( fir ); //clear fir buf
if(vb) printf( "create_filter_from_coeffs() - filter created, '%s' '%s'\n", fir.suser0.c_str(), fir.suser1.c_str() );
if(vb) if( fir.bypass ) printf( "create_filter_from_coeffs() - filter is in BYPASS, '%s' '%s'\n", fir.suser0.c_str(), fir.suser1.c_str() );
return 1;
}
//delete filter and free any memory allocated
void delete_filter( st_fir &fir )
{
bool vb = fir.verb; //v1.06
if ( fir.created == 0 )
{
if(vb)printf( "delete_filter() - can't delete filter, it's not created, '%s' '%s'\n", fir.suser0.c_str(), fir.suser1.c_str() );
return;
}
if(vb)printf( "delete_filter() - about to delete filter, '%s' '%s'\n", fir.suser0.c_str(), fir.suser1.c_str() );
fir.created = 0;
if(vb)printf( "delete_filter() - deleted filter, '%s' '%s'\n", fir.suser0.c_str(), fir.suser1.c_str() );
delete[] fir.coeff_ptr; //v1.06
delete[] fir.prev;
fir.coeff_ptr = 0;
fir.prev = 0;
}
void fir_in( st_fir &fir, double in )
{
if( fir.created == 0 ) return;
fir.prev[ fir.prev_idx++ ] = in;