| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
#include <stdio.h> |
| 9 |
#include <stdlib.h> |
| 10 |
#include <math.h> |
| 11 |
#include "biquad.h" |
| 12 |
|
| 13 |
#define SAMPLE_RATE 48000.0 |
| 14 |
|
| 15 |
static int test_count = 0; |
| 16 |
static int pass_count = 0; |
| 17 |
static int fail_count = 0; |
| 18 |
|
| 19 |
static void test_filter( |
| 20 |
BiquadFilterType type, |
| 21 |
double freq, |
| 22 |
double q, |
| 23 |
double gain_db |
| 24 |
) { |
| 25 |
struct biquad_params orig = { |
| 26 |
.type = type, |
| 27 |
.freq = freq, |
| 28 |
.q = q, |
| 29 |
.gain_db = gain_db |
| 30 |
}; |
| 31 |
|
| 32 |
|
| 33 |
struct biquad_coeffs coeffs; |
| 34 |
biquad_calculate(&orig, SAMPLE_RATE, &coeffs); |
| 35 |
|
| 36 |
|
| 37 |
struct biquad_params result; |
| 38 |
biquad_analyze(&coeffs, SAMPLE_RATE, &result); |
| 39 |
|
| 40 |
|
| 41 |
int type_ok = (result.type == orig.type); |
| 42 |
|
| 43 |
int freq_ok = (type == BIQUAD_TYPE_GAIN) || |
| 44 |
fabs(result.freq - orig.freq) < orig.freq * 0.000000001; |
| 45 |
|
| 46 |
int q_ok = !biquad_type_uses_q(type) || |
| 47 |
fabs(result.q - orig.q) < orig.q * 0.000000001; |
| 48 |
int gain_ok = !biquad_type_uses_gain(type) || |
| 49 |
fabs(result.gain_db - orig.gain_db) < 1e-8; |
| 50 |
|
| 51 |
int pass = type_ok && freq_ok && q_ok && gain_ok; |
| 52 |
|
| 53 |
test_count++; |
| 54 |
if (pass) { |
| 55 |
pass_count++; |
| 56 |
} else { |
| 57 |
fail_count++; |
| 58 |
printf("FAIL: %s freq=%.1f Q=%.2f gain=%.1f\n", |
| 59 |
biquad_type_name(type), freq, q, gain_db); |
| 60 |
printf(" -> got: %s freq=%.1f Q=%.2f gain=%.1f\n", |
| 61 |
biquad_type_name(result.type), result.freq, result.q, result.gain_db); |
| 62 |
printf(" coeffs: b0=%.6f b1=%.6f b2=%.6f a1=%.6f a2=%.6f\n", |
| 63 |
coeffs.b0, coeffs.b1, coeffs.b2, coeffs.a1, coeffs.a2); |
| 64 |
if (!type_ok) printf(" type mismatch!\n"); |
| 65 |
if (!freq_ok) printf(" freq error: %.1f%%\n", |
| 66 |
100.0 * fabs(result.freq - orig.freq) / orig.freq); |
| 67 |
if (!q_ok) printf(" Q error: %.1f%%\n", |
| 68 |
100.0 * fabs(result.q - orig.q) / orig.q); |
| 69 |
if (!gain_ok) printf(" gain error: %.2f dB\n", |
| 70 |
fabs(result.gain_db - orig.gain_db)); |
| 71 |
} |
| 72 |
} |
| 73 |
|
| 74 |
int main(void) { |
| 75 |
double freqs[] = { 20, 50, 100, 200, 500, 1000, 2000, 5000, 10000, 15000, 20000 }; |
| 76 |
double qs[] = { 0.1, 0.5, 0.707, 1.0, 2.0, 5.0, 10.0 }; |
| 77 |
double gains[] = { |
| 78 |
-GAIN_DB_LIMIT, -18, -12, -6, -3, 0, 3, 6, 12, 18, GAIN_DB_LIMIT |
| 79 |
}; |
| 80 |
|
| 81 |
int n_freqs = sizeof(freqs) / sizeof(freqs[0]); |
| 82 |
int n_qs = sizeof(qs) / sizeof(qs[0]); |
| 83 |
int n_gains = sizeof(gains) / sizeof(gains[0]); |
| 84 |
|
| 85 |
printf("Testing biquad analysis...\n\n"); |
| 86 |
|
| 87 |
for (int type = 0; type < BIQUAD_TYPE_COUNT; type++) { |
| 88 |
printf("Testing %s...\n", biquad_type_name(type)); |
| 89 |
|
| 90 |
|
| 91 |
if (type == BIQUAD_TYPE_GAIN) { |
| 92 |
for (int gi = 0; gi < n_gains; gi++) { |
| 93 |
test_filter(type, 1000, 0.707, gains[gi]); |
| 94 |
} |
| 95 |
continue; |
| 96 |
} |
| 97 |
|
| 98 |
|
| 99 |
if (!biquad_type_uses_q(type)) { |
| 100 |
for (int fi = 0; fi < n_freqs; fi++) { |
| 101 |
if (biquad_type_uses_gain(type)) { |
| 102 |
for (int gi = 0; gi < n_gains; gi++) { |
| 103 |
|
| 104 |
if (gains[gi] == 0) |
| 105 |
continue; |
| 106 |
test_filter(type, freqs[fi], 0.707, gains[gi]); |
| 107 |
} |
| 108 |
} else { |
| 109 |
test_filter(type, freqs[fi], 0.707, 0); |
| 110 |
} |
| 111 |
} |
| 112 |
continue; |
| 113 |
} |
| 114 |
|
| 115 |
|
| 116 |
for (int fi = 0; fi < n_freqs; fi++) { |
| 117 |
for (int qi = 0; qi < n_qs; qi++) { |
| 118 |
if (biquad_type_uses_gain(type)) { |
| 119 |
for (int gi = 0; gi < n_gains; gi++) { |
| 120 |
|
| 121 |
if (gains[gi] == 0 && |
| 122 |
(type == BIQUAD_TYPE_LOW_SHELF || type == BIQUAD_TYPE_HIGH_SHELF)) |
| 123 |
continue; |
| 124 |
test_filter(type, freqs[fi], qs[qi], gains[gi]); |
| 125 |
} |
| 126 |
} else { |
| 127 |
test_filter(type, freqs[fi], qs[qi], 0); |
| 128 |
} |
| 129 |
} |
| 130 |
} |
| 131 |
} |
| 132 |
|
| 133 |
printf("\n========================================\n"); |
| 134 |
printf("Results: %d tests, %d passed, %d failed\n", |
| 135 |
test_count, pass_count, fail_count); |
| 136 |
|
| 137 |
return fail_count > 0 ? 1 : 0; |
| 138 |
} |