forked from thedrgreenthumb/orfanidis_eq
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathorfanidis_eq.h
1449 lines (1182 loc) · 32.9 KB
/
orfanidis_eq.h
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) 2018 Fedor Uporov
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#pragma once
#include <cmath>
#include <vector>
#include <complex>
#include <limits>
#include <numeric>
#include <algorithm>
namespace OrfanidisEq {
/*
* Just version.
*/
static const char* eq_version = "0.02";
/*
* The float type usage here could be cause the lack of precession.
*/
typedef double eq_double_t;
/*
* Eq configuration constants.
* The defaultEqBandPassFiltersOrder should be more then 2.
*/
static const eq_double_t defaultSampleFreqHz = 48000;
static const size_t defaultEqBandPassFiltersOrder = 4;
/* Default frequency values to get frequency grid. */
static const eq_double_t lowestGridCenterFreqHz = 31.25;
static const eq_double_t bandsGridCenterFreqHz = 1000;
static const eq_double_t lowestAudioFreqHz = 20;
static const eq_double_t highestAudioFreqHz = 20000;
/* Default gain values for every type of filter channel. */
static const eq_double_t eqGainRangeDb = 40;
static const eq_double_t eqGainStepDb = 1;
static const eq_double_t eqDefaultGainDb = 0;
/*
* Allow to convert values between different representations.
* Also, fast conversions between linear and logarithmic scales are included.
*/
class Conversions {
std::vector<eq_double_t> linGains;
int linGainsIndex(eq_double_t x)
{
int inTx = (int)x;
int range = linGains.size() / 2;
if ((x >= -range) && (x < range - 1))
return range + inTx;
return range;
}
Conversions();
Conversions(const Conversions&);
Conversions(const Conversions&&);
Conversions& operator= (const Conversions&);
Conversions& operator= (Conversions&&);
public:
Conversions(int range)
{
int step = -range;
while (step <= range)
linGains.push_back(db2Lin(step++));
}
eq_double_t fastDb2Lin(eq_double_t x)
{
int intPart = x;
eq_double_t fractPart = x - intPart;
return linGains.at(linGainsIndex(intPart)) * (1-fractPart) +
linGains.at(linGainsIndex(intPart + 1)) * fractPart;
}
eq_double_t fastLin2Db(eq_double_t x)
{
if ((x >= linGains[0]) && (x < linGains[linGains.size() - 1]))
for (size_t i = 0; i < linGains.size() - 2; i++)
if ((x >= linGains[i]) && (x < linGains[i + 1]))
return i - linGains.size() / 2.0 + (x - (int)(x));
return 0;
}
static eq_double_t db2Lin(eq_double_t x)
{
return pow(10, x/20.0);
}
static eq_double_t lin2Db(eq_double_t x)
{
return 20.0*log10(x);
}
static eq_double_t rad2Hz(eq_double_t x, eq_double_t fs)
{
return 2.0*M_PI/x*fs;
}
static eq_double_t hz2Rad(eq_double_t x, eq_double_t fs)
{
return 2.0*M_PI*x/fs;
}
};
/*
* Filter frequency band representation.
*/
class Band {
public:
eq_double_t minFreq;
eq_double_t centerFreq;
eq_double_t maxFreq;
Band() : minFreq(0), centerFreq(0), maxFreq(0) {}
Band(eq_double_t fl, eq_double_t fc, eq_double_t fh)
: minFreq(fl), centerFreq(fc), maxFreq(fh) {}
};
/* Basic eq errors handling. */
typedef enum {
no_error,
invalid_input_data_error,
processing_error
} eq_error_t;
/*
* Frequency grid representation.
* Allow to calculate all required bandpass filters frequencies
* for equalizer construction.
*/
class FrequencyGrid {
std::vector<Band> freqs;
public:
FrequencyGrid() {}
eq_error_t setBand(eq_double_t fl, eq_double_t fc, eq_double_t fh)
{
freqs.clear();
return addBand(fl, fc, fh);
}
eq_error_t addBand(eq_double_t fl, eq_double_t fc, eq_double_t fh)
{
if (fl < fc && fc < fh) {
freqs.push_back(Band(fl, fc, fh));
return no_error;
}
return invalid_input_data_error;
}
eq_error_t addBand(eq_double_t fc, eq_double_t df)
{
if (fc >= df /2 ) {
freqs.push_back(Band(fc - df / 2, fc, fc + df / 2));
return no_error;
}
return invalid_input_data_error;
}
eq_error_t set5Bands(eq_double_t fc = bandsGridCenterFreqHz)
{
freqs.clear();
if (lowestAudioFreqHz < fc && fc < highestAudioFreqHz) {
/* Find lowest center frequency in the band. */
eq_double_t lowestFc = fc;
while (lowestFc > lowestGridCenterFreqHz)
lowestFc /= 4.0;
if (lowestFc < lowestGridCenterFreqHz)
lowestFc *= 4.0;
/* Calculate frequencies. */
eq_double_t f0 = lowestFc;
for (size_t i = 0; i < 5 ; i++) {
freqs.push_back(Band(f0 / 2, f0, f0 * 2));
f0 *= 4;
}
return no_error;
}
return invalid_input_data_error;
}
eq_error_t set10Bands(eq_double_t fc = bandsGridCenterFreqHz)
{
freqs.clear();
if (lowestAudioFreqHz < fc && fc < highestAudioFreqHz) {
/* Find lowest center frequency in the band. */
eq_double_t lowestFc = fc;
while (lowestFc > lowestGridCenterFreqHz)
lowestFc /= 2;
if (lowestFc < lowestGridCenterFreqHz)
lowestFc *= 2;
/* Calculate frequencies. */
eq_double_t f0 = lowestFc;
for (size_t i = 0; i < 10; i++) {
freqs.push_back(
Band(f0 / pow(2, 0.5), f0, f0 * pow(2, 0.5)));
f0 *= 2;
}
return no_error;
}
return invalid_input_data_error;
}
eq_error_t set20Bands(eq_double_t fc = bandsGridCenterFreqHz)
{
freqs.clear();
if (lowestAudioFreqHz < fc && fc < highestAudioFreqHz) {
/* Find lowest center frequency in the band. */
eq_double_t lowestFc = fc;
while (lowestFc >= lowestAudioFreqHz)
lowestFc /= pow(2, 0.5);
if (lowestFc < lowestAudioFreqHz)
lowestFc *= pow(2, 0.5);
/* Calculate frequencies. */
eq_double_t f0 = lowestFc;
for (size_t i = 0; i < 20; i++) {
freqs.push_back(Band(f0 / pow(2, 0.25),
f0, f0 * pow(2, 0.25)));
f0 *= pow(2, 0.5);
}
return no_error;
}
return invalid_input_data_error;
}
eq_error_t set30Bands(eq_double_t fc = bandsGridCenterFreqHz)
{
freqs.clear();
if (lowestAudioFreqHz < fc && fc < highestAudioFreqHz) {
/* Find lowest center frequency in the band. */
eq_double_t lowestFc = fc;
while (lowestFc > lowestAudioFreqHz)
lowestFc /= pow(2.0, 1.0/3.0);
if (lowestFc < lowestAudioFreqHz)
lowestFc *= pow(2.0, 1.0/3.0);
/* Calculate frequencies. */
eq_double_t f0 = lowestFc;
for (size_t i = 0; i < 30; i++) {
freqs.push_back(Band(f0 / pow(2.0, 1.0/6.0),
f0, f0 * pow(2.0, 1.0/6.0)));
f0 *= pow(2, 1.0/3.0);
}
return no_error;
}
return invalid_input_data_error;
}
size_t getNumberOfBands()
{
return freqs.size();
}
std::vector<Band> getFreqs()
{
return freqs;
}
size_t getFreq(size_t index)
{
if (index < freqs.size())
return freqs[index].centerFreq;
else
return 0;
}
size_t getRoundedFreq(size_t index)
{
if (index < freqs.size()) {
size_t freq = freqs[index].centerFreq;
if (freq < 100) {
return freq;
} else if (freq >= 100 && freq < 1000) {
size_t rest = freq % 10;
if (rest < 5)
return freq - rest;
else
return freq - rest + 10;
} else if (freq >= 1000 && freq < 10000) {
size_t rest = freq % 100;
if (rest < 50)
return freq - rest;
else
return freq - rest + 100;
} else if (freq >= 10000) {
size_t rest = freq%1000;
if (rest < 500)
return freq - rest;
else
return freq - rest + 1000;
}
}
return 0;
}
void printFrequencyGrid()
{
size_t i = 0;
for (auto const& band: freqs)
std::cout << i++ << ") " << band.minFreq << " " <<
band.centerFreq << " " << band.maxFreq << std::endl;
}
};
/*
* Second order biquad section representation.
*/
struct SOSection
{
eq_double_t b0, b1, b2;
eq_double_t a0, a1, a2;
};
/*
* Fourth order biquad section representation.
*/
class FOSection {
protected:
eq_double_t b0, b1, b2, b3, b4;
eq_double_t a0, a1, a2, a3, a4;
eq_double_t numBuf[4];
eq_double_t denumBuf[4];
eq_double_t df1FOProcess(eq_double_t in)
{
eq_double_t out = 0;
out+= b0*in;
out+= (b1*numBuf[0] - denumBuf[0]*a1);
out+= (b2*numBuf[1] - denumBuf[1]*a2);
out+= (b3*numBuf[2] - denumBuf[2]*a3);
out+= (b4*numBuf[3] - denumBuf[3]*a4);
numBuf[3] = numBuf[2];
numBuf[2] = numBuf[1];
numBuf[1] = numBuf[0];
*numBuf = in;
denumBuf[3] = denumBuf[2];
denumBuf[2] = denumBuf[1];
denumBuf[1] = denumBuf[0];
*denumBuf = out;
return out;
}
public:
FOSection() : b0(1), b1(0), b2(0), b3(0), b4(0),
a0(1), a1(0), a2(0), a3(0), a4(0), numBuf{0}, denumBuf{0}
{}
FOSection(std::vector<eq_double_t>& b, std::vector<eq_double_t> a) :
numBuf{0}, denumBuf{0}
{
b0 = b[0]; b1 = b[1]; b2 = b[2]; b3 = b[3]; b4 = b[4];
a0 = a[0]; a1 = a[1]; a2 = a[2]; a3 = a[3]; a4 = a[4];
}
eq_double_t process(eq_double_t in)
{
return df1FOProcess(in);
}
};
/*
* Bandpass filter representation.
*/
class BPFilter {
public:
BPFilter() {}
virtual ~BPFilter() {}
virtual eq_double_t process(eq_double_t in) = 0;
};
class ButterworthBPFilter : public BPFilter {
std::vector<FOSection> sections;
ButterworthBPFilter() {}
public:
ButterworthBPFilter(ButterworthBPFilter& f)
{
this->sections = f.sections;
}
ButterworthBPFilter(size_t N,
eq_double_t w0, eq_double_t wb,
eq_double_t G, eq_double_t Gb)
{
/* Case if G == 0 : allpass. */
if (G == 0) {
sections.push_back(FOSection());
return;
}
/* Get number of analog sections. */
size_t r = N % 2;
size_t L = (N - r) / 2;
/* Convert gains to linear scale. */
eq_double_t G0 = Conversions::db2Lin(0.0);
G = Conversions::db2Lin(G);
Gb = Conversions::db2Lin(Gb);
eq_double_t e = sqrt((G*G - Gb*Gb) / (Gb*Gb - G0*G0));
eq_double_t g = pow(G, 1.0 / N);
eq_double_t g0 = pow(G0, 1.0 / N);
eq_double_t beta = pow(e, -1.0 / N) * tan(wb / 2.0);
eq_double_t c0 = cos(w0);
/* Calculate every section. */
for (size_t i = 1; i <= L; i++) {
eq_double_t ui = (2.0 * i - 1) / N;
eq_double_t si = sin(M_PI * ui / 2.0);
eq_double_t Di = beta*beta + 2*si*beta + 1;
std::vector<eq_double_t> B = {
(g*g*beta*beta + 2*g*g0*si*beta + g0*g0)/Di,
-4*c0*(g0*g0 + g*g0*si*beta)/Di,
2*(g0*g0*(1 + 2*c0*c0) - g*g*beta*beta)/Di,
-4*c0*(g0*g0 - g*g0*si*beta)/Di,
(g*g*beta*beta - 2*g*g0*si*beta + g0*g0)/Di
};
std::vector<eq_double_t> A = {
1,
-4*c0*(1 + si*beta)/Di,
2*(1 + 2*c0*c0 - beta*beta)/Di,
-4*c0*(1 - si*beta)/Di,
(beta*beta - 2*si*beta + 1)/Di
};
sections.push_back(FOSection(B, A));
}
}
~ButterworthBPFilter() {}
static eq_double_t computeBWGainDb(eq_double_t gain)
{
eq_double_t bwGain = 0;
if (gain < -3)
bwGain = gain + 3;
else if (gain >= -3 && gain < 3)
bwGain = gain / sqrt(2);
else if (gain >= 3)
bwGain = gain - 3;
return bwGain;
}
virtual eq_double_t process(eq_double_t in)
{
eq_double_t p0 = in, p1 = 0;
/* Process FO sections in serial connection. */
for (size_t i = 0; i < sections.size(); i++) {
p1 = sections[i].process(p0);
p0 = p1;
}
return p1;
}
};
class ChebyshevType1BPFilter : public BPFilter {
std::vector<FOSection> sections;
ChebyshevType1BPFilter() {}
public:
ChebyshevType1BPFilter(size_t N,
eq_double_t w0, eq_double_t wb,
eq_double_t G, eq_double_t Gb)
{
/* Case if G == 0 : allpass. */
if(G == 0) {
sections.push_back(FOSection());
return;
}
/* Get number of analog sections. */
size_t r = N % 2;
size_t L = (N - r) / 2;
/* Convert gains to linear scale. */
eq_double_t G0 = Conversions::db2Lin(0.0);
G = Conversions::db2Lin(G);
Gb = Conversions::db2Lin(Gb);
eq_double_t e = sqrt((G*G - Gb*Gb) / (Gb*Gb - G0*G0));
eq_double_t g0 = pow(G0, 1.0 / N);
eq_double_t alfa = pow(1.0 / e + pow(1 + pow(e, -2.0), 0.5), 1.0 / N);
eq_double_t beta = pow(G / e + Gb*pow(1 + pow(e, -2.0), 0.5),1.0 / N);
eq_double_t a = 0.5 * (alfa - 1.0 / alfa);
eq_double_t b = 0.5*(beta - g0*g0*(1 / beta));
eq_double_t tetta_b = tan(wb / 2);
eq_double_t c0 = cos(w0);
/* Calculate every section. */
for (size_t i = 1; i <= L; i++) {
eq_double_t ui = (2.0*i - 1.0) / N;
eq_double_t ci = cos(M_PI * ui / 2.0);
eq_double_t si = sin(M_PI * ui / 2.0);
eq_double_t Di = (a*a + ci*ci) * tetta_b * tetta_b +
2.0 * a * si * tetta_b + 1;
std::vector<eq_double_t> B = {
((b*b + g0*g0*ci*ci)*tetta_b*tetta_b + 2*g0*b*si*tetta_b + g0*g0)/Di,
-4*c0*(g0*g0 + g0*b*si*tetta_b)/Di,
2*(g0*g0*(1 + 2*c0*c0) - (b*b + g0*g0*ci*ci)*tetta_b*tetta_b)/Di,
-4*c0*(g0*g0 - g0*b*si*tetta_b)/Di,
((b*b + g0*g0*ci*ci)*tetta_b*tetta_b - 2*g0*b*si*tetta_b + g0*g0)/Di
};
std::vector<eq_double_t> A = {
1,
-4*c0*(1 + a*si*tetta_b)/Di,
2*(1 + 2*c0*c0 - (a*a + ci*ci)*tetta_b*tetta_b)/Di,
-4*c0*(1 - a*si*tetta_b)/Di,
((a*a + ci*ci)*tetta_b*tetta_b - 2*a*si*tetta_b + 1)/Di
};
sections.push_back(FOSection(B, A));
}
}
~ChebyshevType1BPFilter() {}
static eq_double_t computeBWGainDb(eq_double_t gain)
{
eq_double_t bwGain = 0;
if (gain < 0)
bwGain = gain + 0.1;
else
bwGain = gain - 0.1;
return bwGain;
}
eq_double_t process(eq_double_t in)
{
eq_double_t p0 = in, p1 = 0;
/* Process FO sections in serial connection. */
for (size_t i = 0; i < sections.size(); i++) {
p1 = sections[i].process(p0);
p0 = p1;
}
return p1;
}
};
class ChebyshevType2BPFilter : public BPFilter {
std::vector<FOSection> sections;
ChebyshevType2BPFilter() {}
public:
ChebyshevType2BPFilter(size_t N,
eq_double_t w0, eq_double_t wb,
eq_double_t G, eq_double_t Gb)
{
/* Case if G == 0 : allpass. */
if (G == 0) {
sections.push_back(FOSection());
return;
}
/* Get number of analog sections. */
size_t r = N % 2;
size_t L = (N - r) / 2;
/* Convert gains to linear scale. */
eq_double_t G0 = Conversions::db2Lin(0.0);
G = Conversions::db2Lin(G);
Gb = Conversions::db2Lin(Gb);
eq_double_t e = sqrt((G*G - Gb*Gb) / (Gb*Gb - G0*G0));
eq_double_t g = pow(G, 1.0 / N);
eq_double_t eu = pow(e + sqrt(1 + e*e), 1.0 / N);
eq_double_t ew = pow(G0*e + Gb*sqrt(1 + e*e), 1.0 / N);
eq_double_t a = (eu - 1.0 / eu) / 2.0;
eq_double_t b = (ew - g*g / ew) / 2.0;
eq_double_t tetta_b = tan(wb / 2);
eq_double_t c0 = cos(w0);
/* Calculate every section. */
for (size_t i = 1; i <= L; i++) {
eq_double_t ui = (2.0 * i - 1.0) / N;
eq_double_t ci = cos(M_PI * ui / 2.0);
eq_double_t si = sin(M_PI * ui / 2.0);
eq_double_t Di = tetta_b*tetta_b + 2*a*si*tetta_b +
a*a + ci*ci;
std::vector<eq_double_t> B = {
(g*g*tetta_b*tetta_b + 2*g*b*si*tetta_b + b*b + g*g*ci*ci)/Di,
-4*c0*(b*b + g*g*ci*ci + g*b*si*tetta_b)/Di,
2*((b*b + g*g*ci*ci)*(1 + 2*c0*c0) - g*g*tetta_b*tetta_b)/Di,
-4*c0*(b*b + g*g*ci*ci - g*b*si*tetta_b)/Di,
(g*g*tetta_b*tetta_b - 2*g*b*si*tetta_b + b*b + g*g*ci*ci)/Di
};
std::vector<eq_double_t> A = {
1,
-4*c0*(a*a + ci*ci + a*si*tetta_b)/Di,
2*((a*a + ci*ci)*(1 + 2*c0*c0) - tetta_b*tetta_b)/Di,
-4*c0*(a*a + ci*ci - a*si*tetta_b)/Di,
(tetta_b*tetta_b - 2*a*si*tetta_b + a*a + ci*ci)/Di
};
sections.push_back(FOSection(B, A));
}
}
~ChebyshevType2BPFilter(){}
static eq_double_t computeBWGainDb(eq_double_t gain)
{
eq_double_t bwGain = 0;
if (gain < 0)
bwGain = -0.1;
else
bwGain = 0.1;
return bwGain;
}
eq_double_t process(eq_double_t in)
{
eq_double_t p0 = in, p1 = 0;
/* Process FO sections in serial connection. */
for (size_t i = 0; i < sections.size(); i++) {
p1 = sections[i].process(p0);
p0 = p1;
}
return p1;
}
};
class EllipticTypeBPFilter : public BPFilter {
private:
/* complex -1. */
std::complex<eq_double_t> j;
std::vector<FOSection> sections;
EllipticTypeBPFilter() {}
/*
* Landen transformations of an elliptic modulus.
*/
std::vector<eq_double_t> landen(eq_double_t k, eq_double_t tol)
{
std::vector<eq_double_t> v;
if (k == 0 || k == 1.0)
v.push_back(k);
if (tol < 1) {
while (k > tol) {
k = pow(k/(1.0 + sqrt(1.0 - k*k)), 2);
v.push_back(k);
}
} else {
eq_double_t M = tol;
for (size_t i = 1; i <= M; i++) {
k = pow(k/(1.0 + sqrt(1.0 - k*k)), 2);
v.push_back(k);
}
}
return v;
}
/*
* Complete elliptic integral.
*/
void ellipk(eq_double_t k, eq_double_t tol, eq_double_t& K, eq_double_t& Kprime)
{
eq_double_t kmin = 1e-6;
eq_double_t kmax = sqrt(1 - kmin*kmin);
if (k == 1.0) {
K = std::numeric_limits<eq_double_t>::infinity();
} else if (k > kmax) {
eq_double_t kp = sqrt(1.0 - k*k);
eq_double_t L = -log(kp / 4.0);
K = L + (L - 1) * kp*kp / 4.0;
} else {
std::vector<eq_double_t> v = landen(k, tol);
std::transform(v.begin(), v.end(), v.begin(),
bind2nd(std::plus<eq_double_t>(), 1.0));
K = std::accumulate(begin(v), end(v),
1, std::multiplies<eq_double_t>()) * M_PI/2.0;
}
if (k == 0.0) {
Kprime = std::numeric_limits<eq_double_t>::infinity();
} else if (k < kmin) {
eq_double_t L = -log(k / 4.0);
Kprime = L + (L - 1.0) * k*k / 4.0;
} else {
eq_double_t kp = sqrt(1.0 - k*k);
std::vector<eq_double_t> vp = landen(kp, tol);
std::transform(vp.begin(), vp.end(), vp.begin(),
bind2nd(std::plus<eq_double_t>(), 1.0));
Kprime = std::accumulate(std::begin(vp), std::end(vp),
1.0, std::multiplies<eq_double_t>()) * M_PI/2.0;
}
}
/*
* Solves the degree equation in analog elliptic filter design.
*/
eq_double_t ellipdeg2(eq_double_t n, eq_double_t k, eq_double_t tol)
{
const size_t M = 7;
eq_double_t K, Kprime;
ellipk(k, tol, K, Kprime);
eq_double_t q = exp(-M_PI * Kprime / K);
eq_double_t q1 = pow(q, n);
eq_double_t s1 = 0, s2 = 0;
for (size_t i = 1; i <= M; i++)
{
s1 += pow(q1, i*(i+1));
s2 += pow(q1, i*i);
}
return 4 * sqrt(q1) * pow((1.0 + s1) / (1.0 + 2 * s2), 2);
}
eq_double_t srem(eq_double_t x, eq_double_t y)
{
eq_double_t z = remainder(x, y);
return z - y * std::copysign(1.0, z) * ((eq_double_t)(abs(z) > y / 2.0));
}
/*
* Inverse of cd elliptic function.
*/
std::complex<eq_double_t> acde(std::complex<eq_double_t> w, eq_double_t k,
eq_double_t tol)
{
std::vector<eq_double_t> v = landen(k, tol);
for (size_t i = 0; i < v.size(); i++) {
eq_double_t v1;
if (i == 0)
v1 = k;
else
v1 = v[i - 1];
w = w / (1.0 + sqrt(1.0 - w*w * v1*v1)) * 2.0/(1 + v[i]);
}
std::complex<eq_double_t> u = 2.0 / M_PI * acos(w);
eq_double_t K, Kprime;
ellipk(k ,tol, K, Kprime);
return srem(real(u), 4) + j*srem(imag(u), 2*(Kprime/K));
}
/*
* Inverse of sn elliptic function.
*/
std::complex<eq_double_t> asne(std::complex<eq_double_t> w, eq_double_t k,
eq_double_t tol)
{
return 1.0 - acde(w, k, tol);
}
/*
* cd elliptic function with normalized complex argument.
*/
std::complex<eq_double_t> cde(std::complex<eq_double_t> u, eq_double_t k,
eq_double_t tol)
{
std::vector<eq_double_t> v = landen(k, tol);
std::complex<eq_double_t> w = cos(u * M_PI / 2.0);
for (int i = v.size() - 1; i >= 0; i--)
w = (1 + v[i]) * w / (1.0 + v[i] * pow(w, 2));
return w;
}
/*
* sn elliptic function with normalized complex argument.
*/
std::vector<eq_double_t> sne(const std::vector<eq_double_t> &u,
eq_double_t k, eq_double_t tol)
{
std::vector<eq_double_t> v = landen(k, tol);
std::vector<eq_double_t> w;
for (size_t i = 0; i < u.size(); i++)
w.push_back(sin(u[i] * M_PI / 2.0));
for (int i = v.size() - 1; i >= 0; i--)
for (size_t j = 0; j < w.size(); j++)
w[j] = ((1 + v[i])*w[j])/(1 + v[i]*w[j]*w[j]);
return w;
}
/*
* Solves the degree equation in analog elliptic filter design.
*/
eq_double_t ellipdeg(size_t N, eq_double_t k1, eq_double_t tol)
{
eq_double_t L = floor(N / 2);
std::vector<eq_double_t> ui;
for (size_t i = 1; i <= L; i++)
ui.push_back((2.0*i - 1.0) / N);
eq_double_t kmin = 1e-6;
if (k1 < kmin) {
return ellipdeg2(1.0 / N, k1, tol);
} else {
eq_double_t kc = sqrt(1 - k1*k1);
std::vector<eq_double_t> w = sne(ui, kc, tol);
eq_double_t prod = std::accumulate(begin(w), end(w),
1.0, std::multiplies<eq_double_t>());
eq_double_t kp = pow(kc, N) * pow(prod, 4);
return sqrt(1 - kp*kp);
}
}
/*
* Bilinear transformation of analog second-order sections.
*/
void blt(const std::vector<SOSection>& aSections, eq_double_t w0,
std::vector<FOSection>& sections)
{
eq_double_t c0 = cos(w0);
size_t K = aSections.size();
std::vector<std::vector<eq_double_t>> B, A, Bhat, Ahat;
for (size_t i = 0; i < K; i++) {
B.push_back(std::vector<eq_double_t>(5));
A.push_back(std::vector<eq_double_t>(5));
Bhat.push_back(std::vector<eq_double_t>(3));
Ahat.push_back(std::vector<eq_double_t>(3));
}
std::vector<eq_double_t> B0(3), B1(3), B2(3), A0(3), A1(3), A2(3);
B0[0] = aSections[0].b0; B0[1] = aSections[1].b0; B0[2] = aSections[2].b0;
B1[0] = aSections[0].b1; B1[1] = aSections[1].b1; B1[2] = aSections[2].b1;
B2[0] = aSections[0].b2; B2[1] = aSections[1].b2; B2[2] = aSections[2].b2;
A0[0] = aSections[0].a0; A0[1] = aSections[1].a0; A0[2] = aSections[2].a0;
A1[0] = aSections[0].a1; A1[1] = aSections[1].a1; A1[2] = aSections[2].a1;
A2[0] = aSections[0].a2; A2[1] = aSections[1].a2; A2[2] = aSections[2].a2;
/* Find 0th-order sections (i.e., gain sections). */
std::vector<size_t> zths;
for (size_t i = 0; i < B0.size(); i++)
if ((B1[i] == 0 && A1[i] == 0) && (B2[i] == 0 && A2[i] == 0))
zths.push_back(i);
for (size_t i = 0; i < zths.size(); i++) {
size_t j = zths[i];
Bhat[j][0] = B0[j] / A0[j];
Ahat[j][0] = 1;
B[j][0] = Bhat[j][0];
A[j][0] = 1;
}
/* Find 1st-order analog sections. */
std::vector<size_t> fths;
for (size_t i = 0; i < B0.size(); i++)
if ((B1[i] != 0 || A1[i] != 0) && (B2[i] == 0 && A2[i] == 0))
fths.push_back(i);
for (size_t i = 0; i < fths.size(); i++) {
size_t j = fths[i];
eq_double_t D = A0[j] + A1[j];
Bhat[j][0] = (B0[j] + B1[j]) / D;
Bhat[j][1] = (B0[j] - B1[j]) / D;
Ahat[j][0] = 1;
Ahat[j][1] = (A0[j] - A1[j]) / D;
B[j][0] = Bhat[j][0];
B[j][1] = c0 * (Bhat[j][1] - Bhat[j][0]);
B[j][2] = -Bhat[j][1];
A[j][0] = 1;
A[j][1] = c0 * (Ahat[j][1] - 1);
A[j][2] = -Ahat[j][1];
}
/* Find 2nd-order sections. */
std::vector<size_t> sths;
for (size_t i = 0; i < B0.size(); i++)
if (B2[i] != 0 || A2[i] != 0)
sths.push_back(i);
for (size_t i = 0; i < sths.size(); i++) {
size_t j = sths[i];
eq_double_t D = A0[j] + A1[j] + A2[j];
Bhat[j][0] = (B0[j] + B1[j] + B2[j]) / D;
Bhat[j][1] = 2 * (B0[j] - B2[j]) / D;
Bhat[j][2] = (B0[j] - B1[j] + B2[j]) / D;
Ahat[j][0] = 1;
Ahat[j][1] = 2 * (A0[j] - A2[j]) / D;
Ahat[j][2] = (A0[j] - A1[j] + A2[j]) /D;
B[j][0] = Bhat[j][0];
B[j][1] = c0 * (Bhat[j][1] - 2 * Bhat[j][0]);
B[j][2] = (Bhat[j][0] - Bhat[j][1] + Bhat[j][2]) *c0*c0 - Bhat[j][1];
B[j][3] = c0 * (Bhat[j][1] - 2 * Bhat[j][2]);
B[j][4] = Bhat[j][2];
A[j][0] = 1;
A[j][1] = c0 * (Ahat[j][1] - 2);
A[j][2] = (1 - Ahat[j][1] + Ahat[j][2])*c0*c0 - Ahat[j][1];
A[j][3] = c0 * (Ahat[j][1] - 2*Ahat[j][2]);
A[j][4] = Ahat[j][2];
}
/* LP or HP shelving filter. */
if (c0 == 1 || c0 == -1) {
for (size_t i = 0; i < Bhat.size(); i++) {
B[i] = Bhat[i];
A[i] = Ahat[i];
}
for (size_t i = 0; i < B.size(); i++) {
B[i][1] *= c0;
A[i][1] *= c0;