-
Notifications
You must be signed in to change notification settings - Fork 53
/
Copy pathbiquad.rs
1098 lines (1041 loc) · 44.4 KB
/
biquad.rs
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
// Based on https://github.com/korken89/biquad-rs
// coeffs: https://arachnoid.com/BiQuadDesigner/index.html
//mod filters;
#[cfg(all(target_arch = "x86_64", not(feature = "32bit")))]
use std::arch::x86_64::{
_mm_add_pd, _mm_load1_pd, _mm_load_sd, _mm_loadh_pd, _mm_mul_pd, _mm_mul_sd, _mm_shuffle_pd,
_mm_storeh_pd, _mm_storel_pd, _mm_sub_pd, _mm_unpacklo_pd,
};
use crate::config;
use crate::filters::Filter;
// Sample format
//type SmpFmt = i16;
use crate::NewValue;
use crate::PrcFmt;
use crate::Res;
/// Struct to hold the biquad coefficients
#[derive(Clone, Copy, Debug)]
pub struct BiquadCoefficients {
pub a1: PrcFmt,
pub a2: PrcFmt,
pub b0: PrcFmt,
pub b1: PrcFmt,
pub b2: PrcFmt,
}
impl BiquadCoefficients {
pub fn new(a1: PrcFmt, a2: PrcFmt, b0: PrcFmt, b1: PrcFmt, b2: PrcFmt) -> Self {
BiquadCoefficients { a1, a2, b0, b1, b2 }
}
pub fn normalize(
a0: PrcFmt,
a1: PrcFmt,
a2: PrcFmt,
b0: PrcFmt,
b1: PrcFmt,
b2: PrcFmt,
) -> Self {
let a1n = a1 / a0;
let a2n = a2 / a0;
let b0n = b0 / a0;
let b1n = b1 / a0;
let b2n = b2 / a0;
debug!("a1={} a2={} b0={} b1={} b2={}", a1n, a2n, b0n, b1n, b2n);
BiquadCoefficients {
a1: a1n,
a2: a2n,
b0: b0n,
b1: b1n,
b2: b2n,
}
}
pub fn is_stable(&self) -> bool {
self.a2.abs() < 1.0 && (self.a1.abs() < (self.a2 + 1.0))
}
/// Create biquad filters from config.
/// Filter types
/// - Free: just coefficients
/// - Highpass: second order highpass specified by frequency and Q-value.
/// - Lowpass: second order lowpass specified by frequency and Q-value.
/// - Peaking: parametric peaking filter specified by gain, frequency and Q-value.
/// - Highshelf: shelving filter affecting high frequencies with arbitrary slope in between.
/// The frequency specified is the middle of the slope
/// - Lowshelf: shelving filter affecting low frequencies with arbitrary slope in between.
/// The frequency specified is the middle of the slope
pub fn from_config(fs: usize, parameters: config::BiquadParameters) -> Self {
match parameters {
config::BiquadParameters::Free { a1, a2, b0, b1, b2 } => {
BiquadCoefficients::new(a1, a2, b0, b1, b2)
}
config::BiquadParameters::Highpass { freq, q } => {
let omega = 2.0 * (std::f64::consts::PI as PrcFmt) * freq / (fs as PrcFmt);
let sn = omega.sin();
let cs = omega.cos();
let alpha = sn / (2.0 * q);
let b0 = (1.0 + cs) / 2.0;
let b1 = -(1.0 + cs);
let b2 = (1.0 + cs) / 2.0;
let a0 = 1.0 + alpha;
let a1 = -2.0 * cs;
let a2 = 1.0 - alpha;
BiquadCoefficients::normalize(a0, a1, a2, b0, b1, b2)
}
config::BiquadParameters::Lowpass { freq, q } => {
let omega = 2.0 * (std::f64::consts::PI as PrcFmt) * freq / (fs as PrcFmt);
let sn = omega.sin();
let cs = omega.cos();
let alpha = sn / (2.0 * q);
let b0 = (1.0 - cs) / 2.0;
let b1 = 1.0 - cs;
let b2 = (1.0 - cs) / 2.0;
let a0 = 1.0 + alpha;
let a1 = -2.0 * cs;
let a2 = 1.0 - alpha;
BiquadCoefficients::normalize(a0, a1, a2, b0, b1, b2)
}
config::BiquadParameters::Peaking(config::PeakingWidth::Q { freq, gain, q }) => {
let omega = 2.0 * (std::f64::consts::PI as PrcFmt) * freq / (fs as PrcFmt);
let sn = omega.sin();
let cs = omega.cos();
let ampl = PrcFmt::coerce(10.0).powf(gain / 40.0);
let alpha = sn / (2.0 * q);
let b0 = 1.0 + (alpha * ampl);
let b1 = -2.0 * cs;
let b2 = 1.0 - (alpha * ampl);
let a0 = 1.0 + (alpha / ampl);
let a1 = -2.0 * cs;
let a2 = 1.0 - (alpha / ampl);
BiquadCoefficients::normalize(a0, a1, a2, b0, b1, b2)
}
config::BiquadParameters::Peaking(config::PeakingWidth::Bandwidth {
freq,
gain,
bandwidth,
}) => {
let omega = 2.0 * (std::f64::consts::PI as PrcFmt) * freq / (fs as PrcFmt);
let sn = omega.sin();
let cs = omega.cos();
let ampl = PrcFmt::coerce(10.0).powf(gain / 40.0);
let alpha =
sn * ((std::f64::consts::LN_2 as PrcFmt) / 2.0 * bandwidth * omega / sn).sinh();
let b0 = 1.0 + (alpha * ampl);
let b1 = -2.0 * cs;
let b2 = 1.0 - (alpha * ampl);
let a0 = 1.0 + (alpha / ampl);
let a1 = -2.0 * cs;
let a2 = 1.0 - (alpha / ampl);
BiquadCoefficients::normalize(a0, a1, a2, b0, b1, b2)
}
config::BiquadParameters::Highshelf(config::ShelfSteepness::Q { freq, q, gain }) => {
let omega = 2.0 * (std::f64::consts::PI as PrcFmt) * freq / (fs as PrcFmt);
let sn = omega.sin();
let cs = omega.cos();
let ampl = PrcFmt::coerce(10.0).powf(gain / 40.0);
let beta = sn * ampl.sqrt() / q;
let b0 = ampl * ((ampl + 1.0) + (ampl - 1.0) * cs + beta);
let b1 = -2.0 * ampl * ((ampl - 1.0) + (ampl + 1.0) * cs);
let b2 = ampl * ((ampl + 1.0) + (ampl - 1.0) * cs - beta);
let a0 = (ampl + 1.0) - (ampl - 1.0) * cs + beta;
let a1 = 2.0 * ((ampl - 1.0) - (ampl + 1.0) * cs);
let a2 = (ampl + 1.0) - (ampl - 1.0) * cs - beta;
BiquadCoefficients::normalize(a0, a1, a2, b0, b1, b2)
}
config::BiquadParameters::Highshelf(config::ShelfSteepness::Slope {
freq,
slope,
gain,
}) => {
let omega = 2.0 * (std::f64::consts::PI as PrcFmt) * freq / (fs as PrcFmt);
let sn = omega.sin();
let cs = omega.cos();
let ampl = PrcFmt::coerce(10.0).powf(gain / 40.0);
let alpha =
sn / 2.0 * ((ampl + 1.0 / ampl) * (1.0 / (slope / 12.0) - 1.0) + 2.0).sqrt();
let beta = 2.0 * ampl.sqrt() * alpha;
let b0 = ampl * ((ampl + 1.0) + (ampl - 1.0) * cs + beta);
let b1 = -2.0 * ampl * ((ampl - 1.0) + (ampl + 1.0) * cs);
let b2 = ampl * ((ampl + 1.0) + (ampl - 1.0) * cs - beta);
let a0 = (ampl + 1.0) - (ampl - 1.0) * cs + beta;
let a1 = 2.0 * ((ampl - 1.0) - (ampl + 1.0) * cs);
let a2 = (ampl + 1.0) - (ampl - 1.0) * cs - beta;
BiquadCoefficients::normalize(a0, a1, a2, b0, b1, b2)
}
config::BiquadParameters::HighshelfFO { freq, gain } => {
let omega = 2.0 * (std::f64::consts::PI as PrcFmt) * freq / (fs as PrcFmt);
let tn = (omega / 2.0).tan();
let ampl = PrcFmt::coerce(10.0).powf(gain / 40.0);
let b0 = ampl * tn + ampl.powi(2);
let b1 = ampl * tn - ampl.powi(2);
let b2 = 0.0;
let a0 = ampl * tn + 1.0;
let a1 = ampl * tn - 1.0;
let a2 = 0.0;
BiquadCoefficients::normalize(a0, a1, a2, b0, b1, b2)
}
config::BiquadParameters::Lowshelf(config::ShelfSteepness::Q { freq, q, gain }) => {
let omega = 2.0 * (std::f64::consts::PI as PrcFmt) * freq / (fs as PrcFmt);
let sn = omega.sin();
let cs = omega.cos();
let ampl = PrcFmt::coerce(10.0).powf(gain / 40.0);
let beta = sn * ampl.sqrt() / q;
let b0 = ampl * ((ampl + 1.0) - (ampl - 1.0) * cs + beta);
let b1 = 2.0 * ampl * ((ampl - 1.0) - (ampl + 1.0) * cs);
let b2 = ampl * ((ampl + 1.0) - (ampl - 1.0) * cs - beta);
let a0 = (ampl + 1.0) + (ampl - 1.0) * cs + beta;
let a1 = -2.0 * ((ampl - 1.0) + (ampl + 1.0) * cs);
let a2 = (ampl + 1.0) + (ampl - 1.0) * cs - beta;
BiquadCoefficients::normalize(a0, a1, a2, b0, b1, b2)
}
config::BiquadParameters::Lowshelf(config::ShelfSteepness::Slope {
freq,
slope,
gain,
}) => {
let omega = 2.0 * (std::f64::consts::PI as PrcFmt) * freq / (fs as PrcFmt);
let sn = omega.sin();
let cs = omega.cos();
let ampl = PrcFmt::coerce(10.0).powf(gain / 40.0);
let alpha =
sn / 2.0 * ((ampl + 1.0 / ampl) * (1.0 / (slope / 12.0) - 1.0) + 2.0).sqrt();
let beta = 2.0 * ampl.sqrt() * alpha;
let b0 = ampl * ((ampl + 1.0) - (ampl - 1.0) * cs + beta);
let b1 = 2.0 * ampl * ((ampl - 1.0) - (ampl + 1.0) * cs);
let b2 = ampl * ((ampl + 1.0) - (ampl - 1.0) * cs - beta);
let a0 = (ampl + 1.0) + (ampl - 1.0) * cs + beta;
let a1 = -2.0 * ((ampl - 1.0) + (ampl + 1.0) * cs);
let a2 = (ampl + 1.0) + (ampl - 1.0) * cs - beta;
BiquadCoefficients::normalize(a0, a1, a2, b0, b1, b2)
}
config::BiquadParameters::LowshelfFO { freq, gain } => {
let omega = 2.0 * (std::f64::consts::PI as PrcFmt) * freq / (fs as PrcFmt);
let tn = (omega / 2.0).tan();
let ampl = PrcFmt::coerce(10.0).powf(gain / 40.0);
let b0 = ampl.powi(2) * tn + ampl;
let b1 = ampl.powi(2) * tn - ampl;
let b2 = 0.0;
let a0 = tn + ampl;
let a1 = tn - ampl;
let a2 = 0.0;
BiquadCoefficients::normalize(a0, a1, a2, b0, b1, b2)
}
config::BiquadParameters::LowpassFO { freq } => {
let omega = 2.0 * (std::f64::consts::PI as PrcFmt) * freq / (fs as PrcFmt);
let k = (omega / 2.0).tan();
let alpha = 1.0 + k;
let a0 = 1.0;
let a1 = -(1.0 - k) / alpha;
let a2 = 0.0;
let b0 = k / alpha;
let b1 = k / alpha;
let b2 = 0.0;
BiquadCoefficients::normalize(a0, a1, a2, b0, b1, b2)
}
config::BiquadParameters::HighpassFO { freq } => {
let omega = 2.0 * (std::f64::consts::PI as PrcFmt) * freq / (fs as PrcFmt);
let k = (omega / 2.0).tan();
let alpha = 1.0 + k;
let a0 = 1.0;
let a1 = -(1.0 - k) / alpha;
let a2 = 0.0;
let b0 = 1.0 / alpha;
let b1 = -1.0 / alpha;
let b2 = 0.0;
BiquadCoefficients::normalize(a0, a1, a2, b0, b1, b2)
}
config::BiquadParameters::Notch(config::NotchWidth::Q { freq, q }) => {
let omega = 2.0 * (std::f64::consts::PI as PrcFmt) * freq / (fs as PrcFmt);
let sn = omega.sin();
let cs = omega.cos();
let alpha = sn / (2.0 * q);
let b0 = 1.0;
let b1 = -2.0 * cs;
let b2 = 1.0;
let a0 = 1.0 + alpha;
let a1 = -2.0 * cs;
let a2 = 1.0 - alpha;
BiquadCoefficients::normalize(a0, a1, a2, b0, b1, b2)
}
config::BiquadParameters::Notch(config::NotchWidth::Bandwidth { freq, bandwidth }) => {
let omega = 2.0 * (std::f64::consts::PI as PrcFmt) * freq / (fs as PrcFmt);
let sn = omega.sin();
let cs = omega.cos();
let alpha =
sn * ((std::f64::consts::LN_2 as PrcFmt) / 2.0 * bandwidth * omega / sn).sinh();
let b0 = 1.0;
let b1 = -2.0 * cs;
let b2 = 1.0;
let a0 = 1.0 + alpha;
let a1 = -2.0 * cs;
let a2 = 1.0 - alpha;
BiquadCoefficients::normalize(a0, a1, a2, b0, b1, b2)
}
config::BiquadParameters::GeneralNotch(params) => {
let tn_z =
((std::f64::consts::PI as PrcFmt) * params.freq_z / (fs as PrcFmt)).tan();
let tn_p =
((std::f64::consts::PI as PrcFmt) * params.freq_p / (fs as PrcFmt)).tan();
let alpha = tn_p / params.q_p;
let tn2_p = tn_p.powi(2);
let tn2_z = tn_z.powi(2);
let gain = if params.normalize_at_dc() {
tn2_p / tn2_z
} else {
1.0
};
let b0 = gain * (1.0 + tn2_z);
let b1 = -2.0 * gain * (1.0 - tn2_z);
let b2 = gain * (1.0 + tn2_z);
let a0 = 1.0 + alpha + tn2_p;
let a1 = -2.0 + 2.0 * tn2_p;
let a2 = 1.0 - alpha + tn2_p;
BiquadCoefficients::normalize(a0, a1, a2, b0, b1, b2)
}
config::BiquadParameters::Bandpass(config::NotchWidth::Q { freq, q }) => {
let omega = 2.0 * (std::f64::consts::PI as PrcFmt) * freq / (fs as PrcFmt);
let sn = omega.sin();
let cs = omega.cos();
let alpha = sn / (2.0 * q);
let b0 = alpha;
let b1 = 0.0;
let b2 = -alpha;
let a0 = 1.0 + alpha;
let a1 = -2.0 * cs;
let a2 = 1.0 - alpha;
BiquadCoefficients::normalize(a0, a1, a2, b0, b1, b2)
}
config::BiquadParameters::Bandpass(config::NotchWidth::Bandwidth {
freq,
bandwidth,
}) => {
let omega = 2.0 * (std::f64::consts::PI as PrcFmt) * freq / (fs as PrcFmt);
let sn = omega.sin();
let cs = omega.cos();
let alpha =
sn * ((std::f64::consts::LN_2 as PrcFmt) / 2.0 * bandwidth * omega / sn).sinh();
let b0 = alpha;
let b1 = 0.0;
let b2 = -alpha;
let a0 = 1.0 + alpha;
let a1 = -2.0 * cs;
let a2 = 1.0 - alpha;
BiquadCoefficients::normalize(a0, a1, a2, b0, b1, b2)
}
config::BiquadParameters::Allpass(config::NotchWidth::Q { freq, q }) => {
let omega = 2.0 * (std::f64::consts::PI as PrcFmt) * freq / (fs as PrcFmt);
let sn = omega.sin();
let cs = omega.cos();
let alpha = sn / (2.0 * q);
let b0 = 1.0 - alpha;
let b1 = -2.0 * cs;
let b2 = 1.0 + alpha;
let a0 = 1.0 + alpha;
let a1 = -2.0 * cs;
let a2 = 1.0 - alpha;
BiquadCoefficients::normalize(a0, a1, a2, b0, b1, b2)
}
config::BiquadParameters::Allpass(config::NotchWidth::Bandwidth {
freq,
bandwidth,
}) => {
let omega = 2.0 * (std::f64::consts::PI as PrcFmt) * freq / (fs as PrcFmt);
let sn = omega.sin();
let cs = omega.cos();
let alpha =
sn * ((std::f64::consts::LN_2 as PrcFmt) / 2.0 * bandwidth * omega / sn).sinh();
let b0 = 1.0 - alpha;
let b1 = -2.0 * cs;
let b2 = 1.0 + alpha;
let a0 = 1.0 + alpha;
let a1 = -2.0 * cs;
let a2 = 1.0 - alpha;
BiquadCoefficients::normalize(a0, a1, a2, b0, b1, b2)
}
config::BiquadParameters::AllpassFO { freq } => {
let omega = 2.0 * (std::f64::consts::PI as PrcFmt) * freq / (fs as PrcFmt);
let tn = (omega / 2.0).tan();
let alpha = (tn + 1.0) / (tn - 1.0);
let b0 = 1.0;
let b1 = alpha;
let b2 = 0.0;
let a0 = alpha;
let a1 = 1.0;
let a2 = 0.0;
BiquadCoefficients::normalize(a0, a1, a2, b0, b1, b2)
}
config::BiquadParameters::LinkwitzTransform {
freq_act,
q_act,
freq_target,
q_target,
} => {
let d0i = (2.0 * (std::f64::consts::PI as PrcFmt) * freq_act).powi(2);
let d1i = (2.0 * (std::f64::consts::PI as PrcFmt) * freq_act) / q_act;
let c0i = (2.0 * (std::f64::consts::PI as PrcFmt) * freq_target).powi(2);
let c1i = (2.0 * (std::f64::consts::PI as PrcFmt) * freq_target) / q_target;
let fc = (freq_target + freq_act) / 2.0;
let gn = 2.0 * (std::f64::consts::PI as PrcFmt) * fc
/ ((std::f64::consts::PI as PrcFmt) * fc / (fs as PrcFmt)).tan();
let gn2 = gn.powi(2);
let cci = c0i + gn * c1i + gn2;
let b0 = (d0i + gn * d1i + gn2) / cci;
let b1 = 2.0 * (d0i - gn2) / cci;
let b2 = (d0i - gn * d1i + gn2) / cci;
let a0 = 1.0;
let a1 = 2.0 * (c0i - gn2) / cci;
let a2 = (c0i - gn * c1i + gn2) / cci;
BiquadCoefficients::normalize(a0, a1, a2, b0, b1, b2)
}
}
}
}
#[derive(Clone, Debug)]
pub struct Biquad {
samplerate: usize,
pub s1: PrcFmt,
pub s2: PrcFmt,
coeffs: BiquadCoefficients,
pub name: String,
}
impl Biquad {
/// Creates a Direct Form 2 Transposed biquad filter from a set of coefficients
pub fn new(name: &str, samplerate: usize, coefficients: BiquadCoefficients) -> Self {
Biquad {
samplerate,
s1: 0.0,
s2: 0.0,
coeffs: coefficients,
name: name.to_string(),
}
}
/// Process a single sample, SSE2 version
#[cfg(all(target_arch = "x86_64", not(feature = "32bit")))]
fn process_single(&mut self, input: PrcFmt) -> PrcFmt {
unsafe {
// load input
let input_input = _mm_load1_pd(&input);
// load variables
let b0 = _mm_load_sd(&self.coeffs.b0);
let b0_b1 = _mm_loadh_pd(b0, &self.coeffs.b1);
let b2_nul = _mm_load_sd(&self.coeffs.b2);
let a1 = _mm_load_sd(&self.coeffs.a1);
let a1_a2 = _mm_loadh_pd(a1, &self.coeffs.a2);
let s1 = _mm_load_sd(&self.s1);
let s1_s2 = _mm_loadh_pd(s1, &self.s2);
// calculations
let out_s1a = _mm_add_pd(s1_s2, _mm_mul_pd(input_input, b0_b1));
let out_out = _mm_unpacklo_pd(out_s1a, out_s1a);
let s2a_nul = _mm_mul_sd(b2_nul, input_input);
let s1a_s2a = _mm_shuffle_pd(out_s1a, s2a_nul, 0x01);
let s1new_s2new = _mm_sub_pd(s1a_s2a, _mm_mul_pd(a1_a2, out_out));
// store s1 and s2
_mm_storel_pd(&mut self.s1, s1new_s2new);
_mm_storeh_pd(&mut self.s2, s1new_s2new);
// return result
let mut out = 0.0;
_mm_storel_pd(&mut out, out_out);
out
}
}
/// Process a single sample, generic version
#[cfg(not(all(target_arch = "x86_64", not(feature = "32bit"))))]
fn process_single(&mut self, input: PrcFmt) -> PrcFmt {
let out = self.s1 + self.coeffs.b0 * input;
self.s1 = self.s2 + self.coeffs.b1 * input - self.coeffs.a1 * out;
self.s2 = self.coeffs.b2 * input - self.coeffs.a2 * out;
out
}
/// Flush stored subnormal numbers to zero.
fn flush_subnormals(&mut self) {
if self.s1.is_subnormal() {
trace!("Biquad filter '{}', flushing subnormal s1", self.name);
self.s1 = 0.0;
}
if self.s2.is_subnormal() {
trace!("Biquad filter '{}', flushing subnormal s2", self.name);
self.s2 = 0.0;
}
}
}
impl Filter for Biquad {
fn name(&self) -> &str {
&self.name
}
fn process_waveform(&mut self, waveform: &mut [PrcFmt]) -> Res<()> {
for item in waveform.iter_mut() {
*item = self.process_single(*item);
}
self.flush_subnormals();
Ok(())
}
fn update_parameters(&mut self, conf: config::Filter) {
if let config::Filter::Biquad {
parameters: conf, ..
} = conf
{
let coeffs = BiquadCoefficients::from_config(self.samplerate, conf);
self.coeffs = coeffs;
} else {
// This should never happen unless there is a bug somewhere else
panic!("Invalid config change!");
}
}
}
pub fn validate_config(samplerate: usize, parameters: &config::BiquadParameters) -> Res<()> {
let maxfreq = samplerate as PrcFmt / 2.0;
// Check frequency
match parameters {
config::BiquadParameters::Highpass { freq, .. }
| config::BiquadParameters::Lowpass { freq, .. }
| config::BiquadParameters::HighpassFO { freq, .. }
| config::BiquadParameters::LowpassFO { freq, .. }
| config::BiquadParameters::Peaking(config::PeakingWidth::Q { freq, .. })
| config::BiquadParameters::Peaking(config::PeakingWidth::Bandwidth { freq, .. })
| config::BiquadParameters::Highshelf(config::ShelfSteepness::Q { freq, .. })
| config::BiquadParameters::Lowshelf(config::ShelfSteepness::Q { freq, .. })
| config::BiquadParameters::Highshelf(config::ShelfSteepness::Slope { freq, .. })
| config::BiquadParameters::Lowshelf(config::ShelfSteepness::Slope { freq, .. })
| config::BiquadParameters::HighshelfFO { freq, .. }
| config::BiquadParameters::LowshelfFO { freq, .. }
| config::BiquadParameters::Notch(config::NotchWidth::Q { freq, .. })
| config::BiquadParameters::Bandpass(config::NotchWidth::Q { freq, .. })
| config::BiquadParameters::Allpass(config::NotchWidth::Q { freq, .. })
| config::BiquadParameters::Notch(config::NotchWidth::Bandwidth { freq, .. })
| config::BiquadParameters::Bandpass(config::NotchWidth::Bandwidth { freq, .. })
| config::BiquadParameters::Allpass(config::NotchWidth::Bandwidth { freq, .. })
| config::BiquadParameters::AllpassFO { freq, .. } => {
if *freq <= 0.0 {
return Err(config::ConfigError::new("Frequency must be > 0").into());
} else if *freq >= maxfreq {
return Err(config::ConfigError::new("Frequency must be < samplerate/2").into());
}
}
_ => {}
}
// Check Q
match parameters {
config::BiquadParameters::Highpass { q, .. }
| config::BiquadParameters::Lowpass { q, .. }
| config::BiquadParameters::Peaking(config::PeakingWidth::Q { q, .. })
| config::BiquadParameters::Notch(config::NotchWidth::Q { q, .. })
| config::BiquadParameters::Bandpass(config::NotchWidth::Q { q, .. })
| config::BiquadParameters::Allpass(config::NotchWidth::Q { q, .. })
| config::BiquadParameters::Highshelf(config::ShelfSteepness::Q { q, .. })
| config::BiquadParameters::Lowshelf(config::ShelfSteepness::Q { q, .. })
| config::BiquadParameters::GeneralNotch(config::GeneralNotchParams { q_p: q, .. }) => {
if *q <= 0.0 {
return Err(config::ConfigError::new("Q must be > 0").into());
}
}
_ => {}
}
// Check Bandwidth
match parameters {
config::BiquadParameters::Peaking(config::PeakingWidth::Bandwidth {
bandwidth, ..
})
| config::BiquadParameters::Notch(config::NotchWidth::Bandwidth { bandwidth, .. })
| config::BiquadParameters::Bandpass(config::NotchWidth::Bandwidth { bandwidth, .. })
| config::BiquadParameters::Allpass(config::NotchWidth::Bandwidth { bandwidth, .. }) => {
if *bandwidth <= 0.0 {
return Err(config::ConfigError::new("Bandwidth must be > 0").into());
}
}
_ => {}
}
// Check slope
match parameters {
config::BiquadParameters::Highshelf(config::ShelfSteepness::Slope { slope, .. })
| config::BiquadParameters::Lowshelf(config::ShelfSteepness::Slope { slope, .. }) => {
if *slope <= 0.0 {
return Err(config::ConfigError::new("Slope must be > 0").into());
} else if *slope > 12.0 {
return Err(config::ConfigError::new("Slope must be <= 12.0").into());
}
}
_ => {}
}
// Check LT
if let config::BiquadParameters::LinkwitzTransform {
freq_act,
q_act,
freq_target,
q_target,
} = parameters
{
if *freq_act <= 0.0 || *freq_target <= 0.0 {
return Err(config::ConfigError::new("Frequency must be > 0").into());
} else if *freq_act >= maxfreq || *freq_target >= maxfreq {
return Err(config::ConfigError::new("Frequency must be < samplerate/2").into());
}
if *q_act <= 0.0 || *q_target <= 0.0 {
return Err(config::ConfigError::new("Q must be > 0").into());
}
}
// Check GeneralNotch frequencies
if let config::BiquadParameters::GeneralNotch(params) = parameters {
if params.freq_p <= 0.0 || params.freq_z <= 0.0 {
return Err(config::ConfigError::new("Pole and zero frequencies must be > 0").into());
} else if params.freq_p >= maxfreq || params.freq_z >= maxfreq {
return Err(config::ConfigError::new(
"Pole and zero frequencies must be < samplerate/2",
)
.into());
}
}
let coeffs = BiquadCoefficients::from_config(samplerate, parameters.clone());
if !coeffs.is_stable() {
return Err(config::ConfigError::new("Unstable filter specified").into());
}
Ok(())
}
#[cfg(test)]
mod tests {
use crate::biquad::{validate_config, Biquad, BiquadCoefficients};
use crate::config::{
BiquadParameters, GeneralNotchParams, NotchWidth, PeakingWidth, ShelfSteepness,
};
use crate::filters::Filter;
use crate::PrcFmt;
use num_complex::Complex;
fn is_close(left: PrcFmt, right: PrcFmt, maxdiff: PrcFmt) -> bool {
println!("{left} - {right}");
(left - right).abs() < maxdiff
}
fn is_close_relative(left: PrcFmt, right: PrcFmt, maxdiff: PrcFmt) -> bool {
println!("{left} - {right}");
(left / right - 1.0).abs() < maxdiff
}
fn compare_waveforms(left: Vec<PrcFmt>, right: Vec<PrcFmt>, maxdiff: PrcFmt) -> bool {
for (val_l, val_r) in left.iter().zip(right.iter()) {
if !is_close(*val_l, *val_r, maxdiff) {
return false;
}
}
true
}
fn gain_and_phase(coeffs: BiquadCoefficients, f: PrcFmt, fs: usize) -> (PrcFmt, PrcFmt) {
let pi = std::f64::consts::PI as PrcFmt;
let z = (Complex::i() * 2.0 * pi * f / (fs as PrcFmt)).exp();
let a = (coeffs.b0 + coeffs.b1 * z.powi(-1) + coeffs.b2 * z.powi(-2))
/ (1.0 + coeffs.a1 * z.powi(-1) + coeffs.a2 * z.powi(-2));
let (magn, ang) = a.to_polar();
let gain = 20.0 * magn.log10();
let phase = 180.0 / pi * ang;
(gain, phase)
}
#[test]
fn check_result() {
let conf = BiquadParameters::Lowpass {
freq: 10000.0,
q: 0.5,
};
let coeffs = BiquadCoefficients::from_config(44100, conf);
let mut wave = vec![1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0];
let expected = vec![0.215, 0.461, 0.281, 0.039, 0.004, 0.0, 0.0, 0.0];
let mut filter = Biquad::new("test", 44100, coeffs);
filter.process_waveform(&mut wave).unwrap();
assert!(compare_waveforms(wave, expected, 1e-3));
}
#[test]
fn make_lowpass() {
let conf = BiquadParameters::Lowpass {
freq: 100.0,
q: std::f64::consts::FRAC_1_SQRT_2 as PrcFmt,
};
let coeffs = BiquadCoefficients::from_config(44100, conf);
assert!(coeffs.is_stable());
let (gain_f0, _) = gain_and_phase(coeffs, 100.0, 44100);
let (gain_hf, _) = gain_and_phase(coeffs, 400.0, 44100);
let (gain_lf, _) = gain_and_phase(coeffs, 10.0, 44100);
assert!(is_close(gain_f0, -3.0, 0.1));
assert!(is_close(gain_lf, 0.0, 0.1));
assert!(is_close(gain_hf, -24.0, 0.2));
}
#[test]
fn make_highpass() {
let conf = BiquadParameters::Highpass {
freq: 100.0,
q: std::f64::consts::FRAC_1_SQRT_2 as PrcFmt,
};
let coeffs = BiquadCoefficients::from_config(44100, conf);
assert!(coeffs.is_stable());
let (gain_f0, _) = gain_and_phase(coeffs, 100.0, 44100);
let (gain_hf, _) = gain_and_phase(coeffs, 400.0, 44100);
let (gain_lf, _) = gain_and_phase(coeffs, 25.0, 44100);
assert!(is_close(gain_f0, -3.0, 0.1));
assert!(is_close(gain_lf, -24.0, 0.2));
assert!(is_close(gain_hf, 0.0, 0.1));
}
#[test]
fn make_lowpass_fo() {
let conf = BiquadParameters::LowpassFO { freq: 100.0 };
let coeffs = BiquadCoefficients::from_config(44100, conf);
assert!(coeffs.is_stable());
let (gain_f0, _) = gain_and_phase(coeffs, 100.0, 44100);
let (gain_hf, _) = gain_and_phase(coeffs, 400.0, 44100);
let (gain_lf, _) = gain_and_phase(coeffs, 10.0, 44100);
assert!(is_close(gain_f0, -3.0, 0.1));
assert!(is_close(gain_lf, 0.0, 0.1));
assert!(is_close(gain_hf, -12.3, 0.1));
}
#[test]
fn make_highpass_fo() {
let conf = BiquadParameters::HighpassFO { freq: 100.0 };
let coeffs = BiquadCoefficients::from_config(44100, conf);
assert!(coeffs.is_stable());
let (gain_f0, _) = gain_and_phase(coeffs, 100.0, 44100);
let (gain_hf, _) = gain_and_phase(coeffs, 800.0, 44100);
let (gain_lf, _) = gain_and_phase(coeffs, 25.0, 44100);
assert!(is_close(gain_f0, -3.0, 0.1));
assert!(is_close(gain_lf, -12.3, 0.1));
assert!(is_close(gain_hf, 0.0, 0.1));
}
#[test]
fn make_peaking() {
let conf = BiquadParameters::Peaking(PeakingWidth::Q {
freq: 100.0,
gain: 7.0,
q: 3.0,
});
let coeffs = BiquadCoefficients::from_config(44100, conf);
assert!(coeffs.is_stable());
let (gain_f0, _) = gain_and_phase(coeffs, 100.0, 44100);
let (gain_hf, _) = gain_and_phase(coeffs, 400.0, 44100);
let (gain_lf, _) = gain_and_phase(coeffs, 25.0, 44100);
assert!(is_close(gain_f0, 7.0, 0.1));
assert!(is_close(gain_lf, 0.0, 0.1));
assert!(is_close(gain_hf, 0.0, 0.1));
}
#[test]
fn make_bandpass() {
let conf = BiquadParameters::Bandpass(NotchWidth::Q {
freq: 100.0,
q: 1.0,
});
let coeffs = BiquadCoefficients::from_config(44100, conf);
assert!(coeffs.is_stable());
let (gain_f0, _) = gain_and_phase(coeffs, 100.0, 44100);
let (gain_hf, _) = gain_and_phase(coeffs, 400.0, 44100);
let (gain_lf, _) = gain_and_phase(coeffs, 25.0, 44100);
assert!(is_close(gain_f0, 0.0, 0.1));
assert!(is_close(gain_lf, -12.0, 0.3));
assert!(is_close(gain_hf, -12.0, 0.3));
}
#[test]
fn make_notch() {
let conf = BiquadParameters::Notch(NotchWidth::Q {
freq: 100.0,
q: 3.0,
});
let coeffs = BiquadCoefficients::from_config(44100, conf);
assert!(coeffs.is_stable());
let (gain_f0, _) = gain_and_phase(coeffs, 100.0, 44100);
let (gain_hf, _) = gain_and_phase(coeffs, 400.0, 44100);
let (gain_lf, _) = gain_and_phase(coeffs, 25.0, 44100);
assert!(gain_f0 < -40.0);
assert!(is_close(gain_lf, 0.0, 0.1));
assert!(is_close(gain_hf, 0.0, 0.1));
}
#[test]
fn make_generalnotch_hp() {
let conf = BiquadParameters::GeneralNotch(GeneralNotchParams {
freq_p: 2000.0,
freq_z: 1000.0,
q_p: 1.0,
normalize_at_dc: Some(false),
});
let coeffs = BiquadCoefficients::from_config(44100, conf);
assert!(coeffs.is_stable());
let (gain_fp, _) = gain_and_phase(coeffs, 1000.0, 44100);
let (gain_hf, _) = gain_and_phase(coeffs, 20000.0, 44100);
let (gain_lf, _) = gain_and_phase(coeffs, 1.0, 44100);
println!("{gain_fp} {gain_hf} {gain_lf}");
assert!(gain_fp < -40.0);
assert!(is_close(gain_lf, -12.1, 0.1));
assert!(is_close(gain_hf, 0.0, 0.1));
}
#[test]
fn make_generalnotch_lp() {
let conf = BiquadParameters::GeneralNotch(GeneralNotchParams {
freq_p: 500.0,
freq_z: 1000.0,
q_p: 1.0,
normalize_at_dc: Some(true),
});
let coeffs = BiquadCoefficients::from_config(44100, conf);
assert!(coeffs.is_stable());
let (gain_fp, _) = gain_and_phase(coeffs, 1000.0, 44100);
let (gain_hf, _) = gain_and_phase(coeffs, 20000.0, 44100);
let (gain_lf, _) = gain_and_phase(coeffs, 1.0, 44100);
println!("{gain_fp} {gain_hf} {gain_lf}");
assert!(gain_fp < -40.0);
assert!(is_close(gain_lf, 0.0, 0.1));
assert!(is_close(gain_hf, -12.1, 0.1));
}
#[test]
fn make_allpass() {
let conf = BiquadParameters::Allpass(NotchWidth::Q {
freq: 100.0,
q: 3.0,
});
let coeffs = BiquadCoefficients::from_config(44100, conf);
assert!(coeffs.is_stable());
let (gain_f0, phase_f0) = gain_and_phase(coeffs, 100.0, 44100);
let (gain_hf, phase_hf) = gain_and_phase(coeffs, 10000.0, 44100);
let (gain_lf, phase_lf) = gain_and_phase(coeffs, 1.0, 44100);
assert!(is_close(gain_f0, 0.0, 0.1));
assert!(is_close(gain_lf, 0.0, 0.1));
assert!(is_close(gain_hf, 0.0, 0.1));
assert!(is_close(phase_f0.abs(), 180.0, 0.5));
assert!(is_close(phase_lf, 0.0, 0.5));
assert!(is_close(phase_hf, 0.0, 0.5));
}
#[test]
fn make_allpass_fo() {
let conf = BiquadParameters::AllpassFO { freq: 100.0 };
let coeffs = BiquadCoefficients::from_config(44100, conf);
assert!(coeffs.is_stable());
let (gain_f0, phase_f0) = gain_and_phase(coeffs, 100.0, 44100);
let (gain_hf, phase_hf) = gain_and_phase(coeffs, 10000.0, 44100);
let (gain_lf, phase_lf) = gain_and_phase(coeffs, 1.0, 44100);
assert!(is_close(gain_f0, 0.0, 0.1));
assert!(is_close(gain_lf, 0.0, 0.1));
assert!(is_close(gain_hf, 0.0, 0.1));
assert!(is_close(phase_f0.abs(), 90.0, 0.5));
assert!(is_close(phase_lf, 0.0, 2.0));
assert!(is_close(phase_hf.abs(), 180.0, 2.0));
}
#[test]
fn make_highshelf() {
let conf = BiquadParameters::Highshelf(ShelfSteepness::Slope {
freq: 100.0,
slope: 6.0,
gain: -24.0,
});
let coeffs = BiquadCoefficients::from_config(44100, conf);
assert!(coeffs.is_stable());
let (gain_f0, _) = gain_and_phase(coeffs, 100.0, 44100);
let (gain_f0h, _) = gain_and_phase(coeffs, 200.0, 44100);
let (gain_f0l, _) = gain_and_phase(coeffs, 50.0, 44100);
let (gain_hf, _) = gain_and_phase(coeffs, 10000.0, 44100);
let (gain_lf, _) = gain_and_phase(coeffs, 1.0, 44100);
assert!(is_close(gain_f0, -12.0, 0.1));
assert!(is_close(gain_f0h, -18.0, 1.0));
assert!(is_close(gain_f0l, -6.0, 1.0));
assert!(is_close(gain_lf, 0.0, 0.1));
assert!(is_close(gain_hf, -24.0, 0.1));
}
#[test]
fn make_lowshelf() {
let conf = BiquadParameters::Lowshelf(ShelfSteepness::Slope {
freq: 100.0,
slope: 6.0,
gain: -24.0,
});
let coeffs = BiquadCoefficients::from_config(44100, conf);
assert!(coeffs.is_stable());
let (gain_f0, _) = gain_and_phase(coeffs, 100.0, 44100);
let (gain_f0h, _) = gain_and_phase(coeffs, 200.0, 44100);
let (gain_f0l, _) = gain_and_phase(coeffs, 50.0, 44100);
let (gain_hf, _) = gain_and_phase(coeffs, 10000.0, 44100);
let (gain_lf, _) = gain_and_phase(coeffs, 1.0, 44100);
assert!(is_close(gain_f0, -12.0, 0.1));
assert!(is_close(gain_f0h, -6.0, 1.0));
assert!(is_close(gain_f0l, -18.0, 1.0));
assert!(is_close(gain_lf, -24.0, 0.1));
assert!(is_close(gain_hf, -0.0, 0.1));
}
#[test]
fn lowshelf_slope_vs_q() {
let conf_slope = BiquadParameters::Lowshelf(ShelfSteepness::Slope {
freq: 100.0,
slope: 12.0,
gain: -24.0,
});
let conf_q = BiquadParameters::Lowshelf(ShelfSteepness::Q {
freq: 100.0,
q: std::f64::consts::FRAC_1_SQRT_2 as PrcFmt,
gain: -24.0,
});
let coeffs_slope = BiquadCoefficients::from_config(44100, conf_slope);
let coeffs_q = BiquadCoefficients::from_config(44100, conf_q);
assert!(is_close_relative(coeffs_slope.a1, coeffs_q.a1, 0.001));
assert!(is_close_relative(coeffs_slope.a2, coeffs_q.a2, 0.001));
assert!(is_close_relative(coeffs_slope.b0, coeffs_q.b0, 0.001));
assert!(is_close_relative(coeffs_slope.b1, coeffs_q.b1, 0.001));
assert!(is_close_relative(coeffs_slope.b2, coeffs_q.b2, 0.001));
}
#[test]
fn highshelf_slope_vs_q() {
let conf_slope = BiquadParameters::Highshelf(ShelfSteepness::Slope {
freq: 100.0,
slope: 12.0,
gain: -24.0,
});
let conf_q = BiquadParameters::Highshelf(ShelfSteepness::Q {
freq: 100.0,
q: std::f64::consts::FRAC_1_SQRT_2 as PrcFmt,
gain: -24.0,
});
let coeffs_slope = BiquadCoefficients::from_config(44100, conf_slope);
let coeffs_q = BiquadCoefficients::from_config(44100, conf_q);
assert!(is_close_relative(coeffs_slope.a1, coeffs_q.a1, 0.001));
assert!(is_close_relative(coeffs_slope.a2, coeffs_q.a2, 0.001));
assert!(is_close_relative(coeffs_slope.b0, coeffs_q.b0, 0.001));
assert!(is_close_relative(coeffs_slope.b1, coeffs_q.b1, 0.001));
assert!(is_close_relative(coeffs_slope.b2, coeffs_q.b2, 0.001));
}
#[test]
fn bandpass_bw_vs_q() {
let conf_bw = BiquadParameters::Bandpass(NotchWidth::Bandwidth {
freq: 100.0,
bandwidth: 1.0,
});
let conf_q = BiquadParameters::Bandpass(NotchWidth::Q {
freq: 100.0,
q: std::f64::consts::SQRT_2 as PrcFmt,
});
let coeffs_bw = BiquadCoefficients::from_config(44100, conf_bw);
let coeffs_q = BiquadCoefficients::from_config(44100, conf_q);
assert!(is_close_relative(coeffs_bw.a1, coeffs_q.a1, 0.001));
assert!(is_close_relative(coeffs_bw.a2, coeffs_q.a2, 0.001));
assert!(is_close_relative(coeffs_bw.b0, coeffs_q.b0, 0.001));
assert_eq!(coeffs_bw.b1, 0.0);
assert_eq!(coeffs_q.b1, 0.0);
assert!(is_close_relative(coeffs_bw.b2, coeffs_q.b2, 0.001));
}
#[test]
fn notch_bw_vs_q() {
let conf_bw = BiquadParameters::Notch(NotchWidth::Bandwidth {
freq: 100.0,
bandwidth: 1.0,
});
let conf_q = BiquadParameters::Notch(NotchWidth::Q {
freq: 100.0,
q: std::f64::consts::SQRT_2 as PrcFmt,
});
let coeffs_bw = BiquadCoefficients::from_config(44100, conf_bw);
let coeffs_q = BiquadCoefficients::from_config(44100, conf_q);
assert!(is_close_relative(coeffs_bw.a1, coeffs_q.a1, 0.001));
assert!(is_close_relative(coeffs_bw.a2, coeffs_q.a2, 0.001));
assert!(is_close_relative(coeffs_bw.b0, coeffs_q.b0, 0.001));
assert!(is_close_relative(coeffs_bw.b1, coeffs_q.b1, 0.001));
assert!(is_close_relative(coeffs_bw.b2, coeffs_q.b2, 0.001));
}
#[test]
fn allpass_bw_vs_q() {
let conf_bw = BiquadParameters::Allpass(NotchWidth::Bandwidth {
freq: 100.0,
bandwidth: 1.0,
});
let conf_q = BiquadParameters::Allpass(NotchWidth::Q {
freq: 100.0,
q: std::f64::consts::SQRT_2 as PrcFmt,
});
let coeffs_bw = BiquadCoefficients::from_config(44100, conf_bw);
let coeffs_q = BiquadCoefficients::from_config(44100, conf_q);
assert!(is_close_relative(coeffs_bw.a1, coeffs_q.a1, 0.001));
assert!(is_close_relative(coeffs_bw.a2, coeffs_q.a2, 0.001));
assert!(is_close_relative(coeffs_bw.b0, coeffs_q.b0, 0.001));
assert!(is_close_relative(coeffs_bw.b1, coeffs_q.b1, 0.001));
assert!(is_close_relative(coeffs_bw.b2, coeffs_q.b2, 0.001));
}
#[test]
fn make_highshelf_fo() {
let conf = BiquadParameters::HighshelfFO {
freq: 100.0,
gain: -12.0,