Repository navigation
Expand file tree
/
Copy pathTests.cpp
More file actions
616 lines (450 loc) · 18.5 KB
/
Copy pathTests.cpp
File metadata and controls
616 lines (450 loc) · 18.5 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
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
/** $VER: Tests.cpp (2026.09.09) P. Stuer - Various unit tests **/
#include "pch.h"
#ifdef _DEBUG
#include "Analyzers/Analysis.h"
#include "Analyzers/FrequencyScaler.h"
#include "Analyzers/WindowFunctions.h"
#include "Log.h"
static void TestWeighting()
{
constexpr double WeightTolerance = 1.e-9;
constexpr double DecibelTolerance = 1.e-6;
analysis_t Analysis;
struct weighting_test_t
{
double Frequency;
double ExpectedWeight;
double ExpectedDecibels;
};
constexpr weighting_test_t AWeightingTests[] =
{
{ 20., 0.003021740854364, -50.394855673 },
{ 100., 0.110342379727598, -19.145153079 },
{ 1'000., 0.999993407716264, -0.000057260 },
{ 4'000., 1.117300404192550, 0.963399118 },
{ 8'000., 0.876286444332179, -1.147078131 },
{ 10'000., 0.750605235629460, -2.491768212 },
{ 12'500., 0.612759671202555, -4.254196511 },
{ 16'000., 0.462044856449356, -6.706317200 },
{ 20'000., 0.340918345911634, -9.346992547 },
{ 22'050., 0.294666762978363, -10.613376955 }
};
for (auto & Test : AWeightingTests)
{
const double Weight = Analysis.GetAcousticWeight(Test.Frequency, WeightingType::AWeighting, 1.);
const double Decibels = 20. * std::log10(Weight);
assert(std::abs(Weight - Test.ExpectedWeight) < WeightTolerance);
assert(std::abs(Decibels - Test.ExpectedDecibels) < DecibelTolerance);
}
constexpr weighting_test_t BWeightingTests[] =
{
{ 20., 0.061945880288931, -24.159751422 },
{ 100., 0.521951593253106, -5.647395448 },
{ 1'000., 1.000039043321642, 0.000339119 },
{ 4'000., 0.919948172289609, -0.724732782 },
{ 8'000., 0.712770611671976, -2.941004301 },
{ 10'000., 0.609637005298333, -4.298573579 },
{ 12'500., 0.497206407676075, -6.069265662 },
{ 16'000., 0.374665736290569, -8.527120439 },
{ 20'000., 0.276342931213370, -11.171032802 },
{ 22'050., 0.238824071836831, -12.438438028 }
};
for (auto & Test : BWeightingTests)
{
const double Weight = Analysis.GetAcousticWeight(Test.Frequency, WeightingType::BWeighting, 1.);
const double Decibels = 20. * std::log10(Weight);
assert(std::abs(Weight - Test.ExpectedWeight) < WeightTolerance);
assert(std::abs(Decibels - Test.ExpectedDecibels) < DecibelTolerance);
}
constexpr weighting_test_t CWeightingTests[] =
{
{ 20., 0.488587005734043, -6.221161751 },
{ 100., 0.965876039258696, -0.301572149 },
{ 1'000., 0.999780785464601, -0.001904282 },
{ 4'000., 0.909084064953623, -0.827919096 },
{ 8'000., 0.703938966400541, -3.049299877 },
{ 10'000., 0.602040720003176, -4.407482671 },
{ 12'500., 0.490988847556463, -6.178567449 },
{ 16'000., 0.369968958083791, -8.636694270 },
{ 20'000., 0.272873899712495, -11.280760049 },
{ 22'050., 0.235824715060297, -12.548213633 }
};
for (auto & Test : CWeightingTests)
{
const double Weight = Analysis.GetAcousticWeight(Test.Frequency, WeightingType::CWeighting, 1.);
const double Decibels = 20. * std::log10(Weight);
assert(std::abs(Weight - Test.ExpectedWeight) < WeightTolerance);
assert(std::abs(Decibels - Test.ExpectedDecibels) < DecibelTolerance);
}
constexpr weighting_test_t DWeightingTests[] =
{
{ 20., 0.093040046288330, -20.626601644 },
{ 100., 0.436351784271307, -7.203264877 },
{ 1'000., 1.000000000000001, 0.000000000 },
{ 4'000., 3.590888586341657, 11.104038612 },
{ 8'000., 1.875687842405231, 5.463211269 },
{ 10'000., 1.485438479750624, 3.437093399 },
{ 12'500., 1.179055775054986, 1.430686996 },
{ 16'000., 0.915612192737624, -0.765768654 },
{ 20'000., 0.729865992657882, -2.735137427 },
{ 22'050., 0.661242984785963, -3.592778448 }
};
for (auto & Test : DWeightingTests)
{
const double Weight = Analysis.GetAcousticWeight(Test.Frequency, WeightingType::DWeighting, 1.);
const double Decibels = 20. * std::log10(Weight);
assert(std::abs(Weight - Test.ExpectedWeight) < WeightTolerance);
assert(std::abs(Decibels - Test.ExpectedDecibels) < DecibelTolerance);
}
constexpr weighting_test_t MWeightingTests[] =
{
{ 20., 0.0202609969281888, -33.866783786 },
{ 100., 0.1012873424294580, -19.888896474 },
{ 1'000., 0.9955572537679230, -0.038675179 },
{ 4'000., 3.3488312656300400, 10.497865314 },
{ 8'000., 3.6854119108895500, 11.329720702 },
{ 10'000., 2.5399209406451300, 8.096403974 },
{ 12'500., 0.9938189010351010, -0.053854957 },
{ 16'000., 0.2588781981336750, -11.738090457 },
{ 20'000., 0.0774867544332273, -22.215450587 },
{ 22'050., 0.0462349277052746, -26.700596342 }
};
for (auto & Test : MWeightingTests)
{
const double Weight = Analysis.GetAcousticWeight(Test.Frequency, WeightingType::MWeighting, 1.);
const double Decibels = 20. * std::log10(Weight);
assert(std::abs(Weight - Test.ExpectedWeight) < WeightTolerance);
assert(std::abs(Decibels - Test.ExpectedDecibels) < DecibelTolerance);
}
}
static void TestWindowFunctions()
{
// constexpr double Epsilon = 1.0e-12;
constexpr double Epsilon = std::numeric_limits<double>::epsilon();
{
const BoxCar w{0., true};
assert(w( 0.) == 1.);
assert(w(-1.) == 1.);
assert(w( 1.) == 1.);
assert(w(-1.01) == 0.);
assert(w( 1.01) == 0.);
}
{
const Hann w{0., true};
assert(std::abs(w( 0.) - 1.) < Epsilon);
assert(std::abs(w(-1.)) < Epsilon);
assert(std::abs(w( 1.)) < Epsilon);
assert(w(-1.01) == 0.);
assert(w( 1.01) == 0.);
}
// Non-truncated Hann
{
const Hann w{0., false};
const double Expected = (1. + std::cos(1.5 * std::numbers::pi)) / 2.;
assert(std::abs(w(1.5) - Expected) < Epsilon);
}
{
const Hamming w{0., true};
// Test maximum at the center.
assert(std::abs(w(0.) - 1.) < Epsilon);
// Test endpoints.
assert(std::abs(w(-1.) - 0.08) < Epsilon);
assert(std::abs(w( 1.) - 0.08) < Epsilon);
// Test symmetry around zero.
assert(std::abs(w(-0.25) - w(0.25)) < Epsilon);
assert(std::abs(w(-0.75) - w(0.75)) < Epsilon);
assert(w(-1.01) == 0.);
assert(w( 1.01) == 0.);
}
// Non-truncated Hamming
{
const Hamming w{0., false};
const double Expected = 0.54 + (0.46 * std::cos(1.5 * std::numbers::pi));
assert(std::abs(w(1.5) - Expected) < Epsilon);
}
// Blackmann
{
const Blackman w{0., true};
// Test maximum at the center.
assert(std::abs(w(0.) - 1.) < Epsilon);
// Test endpoints.
assert(std::abs(w(-1.)) < Epsilon);
assert(std::abs(w( 1.)) < Epsilon);
// Test symmetry around zero.
assert(std::abs(w(-0.25) - w(0.25)) < Epsilon);
assert(std::abs(w(-0.75) - w(0.75)) < Epsilon);
assert(w(-1.01) == 0.);
assert(w( 1.01) == 0.);
}
// Non-truncated Blackmann
{
const Blackman w{0., false};
const double Phase = 1.5 * std::numbers::pi;
const double Expected = 0.42 + (0.5 * std::cos(Phase)) + (0.08 * std::cos(2. * Phase));
assert(std::abs(w(1.5) - Expected) < Epsilon);
}
// Nuttall
{
const Nuttall w{0., true};
// Test maximum at the center.
assert(std::abs(w(0.) - 1.) < Epsilon);
// Test endpoints.
assert(std::abs(w(-1.)) < Epsilon);
assert(std::abs(w( 1.)) < Epsilon);
// Test symmetry around zero.
assert(std::abs(w(-0.25) - w(0.25)) < Epsilon);
assert(std::abs(w(-0.75) - w(0.75)) < Epsilon);
assert(w(-1.01) == 0.);
assert(w( 1.01) == 0.);
}
// Non-truncated Nuttall
{
const Nuttall w{0., false};
const double Phase = 1.5 * std::numbers::pi;
const double Expected = 0.355768 + (0.487396 * std::cos(Phase)) + (0.144232 * std::cos(2. * Phase)) + (0.012604 * std::cos(3. * Phase));
assert(std::abs(w(1.5) - Expected) < Epsilon);
}
// Flat Top
{
constexpr double A0 = 0.215578950;
constexpr double A1 = 0.416631580;
constexpr double A2 = 0.277263158;
constexpr double A3 = 0.083578947;
constexpr double A4 = 0.006947368;
constexpr double Normalization = A0 + A1 + A2 + A3 + A4;
constexpr double Endpoint = (A0 - A1 + A2 - A3 + A4) / Normalization;
const FlatTop w{0., true};
// Test maximum at the center.
assert(std::abs(w(0.) - 1.) < Epsilon);
// Test endpoints.
assert(std::abs(w(-1.) - Endpoint) < Epsilon);
assert(std::abs(w( 1.) - Endpoint) < Epsilon);
// Test symmetry around zero.
assert(std::abs(w(-0.25) - w(0.25)) < Epsilon);
assert(std::abs(w(-0.75) - w(0.75)) < Epsilon);
assert(w(-1.01) == 0.);
assert(w( 1.01) == 0.);
}
// Non-truncated Flat Top
{
const FlatTop w{0., false};
constexpr double A0 = 0.215578950;
constexpr double A1 = 0.416631580;
constexpr double A2 = 0.277263158;
constexpr double A3 = 0.083578947;
constexpr double A4 = 0.006947368;
constexpr double Normalization = A0 + A1 + A2 + A3 + A4;
const double Phase = 1.5 * std::numbers::pi;
const double Expected = (A0 + (A1 * std::cos(Phase)) + (A2 * std::cos(2. * Phase)) + (A3 * std::cos(3. * Phase)) + (A4 * std::cos(4. * Phase))) / Normalization;
assert(std::abs(w(1.5) - Expected) < Epsilon);
}
// Bartlett
{
const Bartlett w{0., true};
// Test maximum at the center.
assert(std::abs(w(0.) - 1.) < Epsilon);
// Test endpoints.
assert(std::abs(w(-1.)) < Epsilon);
assert(std::abs(w( 1.)) < Epsilon);
// Test known intermediate values.
assert(std::abs(w(-0.25) - 0.75) < Epsilon);
assert(std::abs(w( 0.25) - 0.75) < Epsilon);
assert(std::abs(w(-0.75) - 0.25) < Epsilon);
assert(std::abs(w( 0.75) - 0.25) < Epsilon);
// Test symmetry around zero.
assert(std::abs(w(-0.25) - w(0.25)) < Epsilon);
assert(std::abs(w(-0.75) - w(0.75)) < Epsilon);
// Test truncation outside the supported interval.
assert(w(-1.01) == 0.);
assert(w( 1.01) == 0.);
}
// Non-truncated Bartlett
{
const Bartlett w{0., false};
const double Expected = 1. - std::abs(1.5);
assert(std::abs(w(1.5) - Expected) < Epsilon);
}
// Welch
{
const Welch w(0., true, 1.);
// Test maximum at the center.
assert(std::fabs(w(0.) - 1.) < Epsilon);
// Test endpoints.
assert(std::fabs(w(-1.)) < Epsilon);
assert(std::fabs(w( 1.)) < Epsilon);
// Test known intermediate values.
assert(std::fabs(w(-0.5) - 0.75) < Epsilon);
assert(std::fabs(w( 0.5) - 0.75) < Epsilon);
assert(std::fabs(w(-0.25) - 0.9375) < Epsilon);
assert(std::fabs(w( 0.25) - 0.9375) < Epsilon);
// Test symmetry around zero.
assert(std::fabs(w(-0.125) - w(0.125)) < Epsilon);
assert(std::fabs(w(-0.625) - w(0.625)) < Epsilon);
assert(std::fabs(w(-0.875) - w(0.875)) < Epsilon);
assert(w(-1.01) == 0.);
assert(w( 1.01) == 0.);
assert(w(-2.) == 0.);
assert(w( 2.) == 0.);
}
{
const Welch w(0., false, 1.);
assert(std::fabs(w(-1.5) + 1.25) < Epsilon);
assert(std::fabs(w( 1.5) + 1.25) < Epsilon);
assert(std::fabs(w(-2.) + 3.) < Epsilon);
assert(std::fabs(w( 2.) + 3.) < Epsilon);
}
// Tukey
{
const Tukey w(0., true, 0.5);
// Test maximum at the center.
assert(std::fabs(w(0.) - 1.) < Epsilon);
// Test central region.
assert(std::fabs(w(-0.5) - 1.) < Epsilon);
assert(std::fabs(w(-0.25) - 1.) < Epsilon);
assert(std::fabs(w( 0.25) - 1.) < Epsilon);
assert(std::fabs(w( 0.5) - 1.) < Epsilon);
// Test endpoints.
assert(std::fabs(w(-1.)) < Epsilon);
assert(std::fabs(w( 1.)) < Epsilon);
// Halfway through each taper, the value is 0.5.
assert(std::fabs(w(-0.75) - 0.5) < Epsilon);
assert(std::fabs(w( 0.75) - 0.5) < Epsilon);
// Verify another known value in the taper.
const double Expected = (1. + std::cos(std::numbers::pi / 2.)) / 2.;
assert(std::fabs(w(0.75) - Expected) < Epsilon);
// The non-skewed Tukey window is symmetric.
constexpr double Samples[]
{
0.,
0.125,
0.25,
0.5,
0.625,
0.75,
0.875,
1.
};
for (const double x : Samples)
assert(std::fabs(w(-x) - w(x)) < Epsilon);
assert(w(-1.01) == 0.);
assert(w( 1.01) == 0.);
assert(w(-2.) == 0.);
assert(w( 2.) == 0.);
}
// Ogg Vorbis
{
const OggVorbis w{0., true};
assert(std::abs(w( 0.) - 1.) < Epsilon);
assert(std::abs(w(-1.)) < Epsilon);
assert(std::abs(w( 1.)) < Epsilon);
assert(std::abs(w(-0.25) - w(0.25)) < Epsilon);
assert(std::abs(w(-0.75) - w(0.75)) < Epsilon);
assert(w(-1.01) == 0.);
assert(w( 1.01) == 0.);
}
// Cascaded sine
{
const CascadedSine w{0., true};
// Test maximum at the center.
assert(std::abs(w(0.) - 1.) < Epsilon);
// Test endpoints.
assert(std::abs(w(-1.)) < Epsilon);
assert(std::abs(w( 1.)) < Epsilon);
// Test symmetry around zero.
assert(std::abs(w(-0.25) - w(0.25)) < Epsilon);
assert(std::abs(w(-0.75) - w(0.75)) < Epsilon);
// Test truncation outside the supported interval.
assert(w(-1.01) == 0.);
assert(w( 1.01) == 0.);
}
// Non-truncated cascaded sine
{
const CascadedSine w{0., false};
const double HalfPi = std::numbers::pi / 2.;
const double Sine = std::sin(1.5 * HalfPi);
const double Expected = 1. - std::sin(HalfPi * Sine * Sine);
assert(std::abs(w(1.5) - Expected) < Epsilon);
}
// 4-term Blackman-Harris
{
const BlackmanHarris4 w{0., true};
// Test maximum at the center.
assert(std::abs(w(0.) - 1.) < Epsilon);
// Test endpoints.
constexpr double Endpoint = 0.00006;
assert(std::abs(w(-1.) - Endpoint) < Epsilon);
assert(std::abs(w( 1.) - Endpoint) < Epsilon);
// Test symmetry around zero.
assert(std::abs(w(-0.25) - w(0.25)) < Epsilon);
assert(std::abs(w(-0.75) - w(0.75)) < Epsilon);
// Test truncation outside the supported interval.
assert(w(-1.01) == 0.);
assert(w( 1.01) == 0.);
}
// Non-truncated 4-term Blackman-Harris
{
const BlackmanHarris4 w{0., false};
constexpr double a0 = 0.35875;
constexpr double a1 = 0.48829;
constexpr double a2 = 0.14128;
constexpr double a3 = 0.01168;
const double t = 1.5;
const double Expected = a0 + (a1 * std::cos(std::numbers::pi * t)) + (a2 * std::cos(2. * std::numbers::pi * t)) + (a3 * std::cos(3. * std::numbers::pi * t));
assert(std::abs(w(1.5) - Expected) < Epsilon);
}
}
static void TestFrequencyScaler()
{
constexpr double Epsilon = 1.0e-5;
for (double f = MinFrequency; f <= MaxFrequency; f += 10.)
{
double Scale = ScaleFrequency(f, ScalingFunction::Linear, 0.);
double Frequency = DescaleFrequency(Scale, ScalingFunction::Linear, 0.);
assert(std::abs(Frequency - f) <= Epsilon);
Scale = ScaleFrequency(f, ScalingFunction::Logarithmic, 0.);
Frequency = DescaleFrequency(Scale, ScalingFunction::Logarithmic, 0.);
assert(std::abs(Frequency - f) <= Epsilon);
Scale = ScaleFrequency(f, ScalingFunction::ShiftedLogarithmic, 0.);
Frequency = DescaleFrequency(Scale, ScalingFunction::ShiftedLogarithmic, 0.);
assert(std::abs(Frequency - f) <= Epsilon);
Scale = ScaleFrequency(f, ScalingFunction::Mel, 0.);
Frequency = DescaleFrequency(Scale, ScalingFunction::Mel, 0.);
assert(std::abs(Frequency - f) <= Epsilon);
Scale = ScaleFrequency(f, ScalingFunction::Bark, 0.);
Frequency = DescaleFrequency(Scale, ScalingFunction::Bark, 0.);
assert(std::abs(Frequency - f) <= Epsilon);
Scale = ScaleFrequency(f, ScalingFunction::AdjustableBark, 0.);
Frequency = DescaleFrequency(Scale, ScalingFunction::AdjustableBark, 0.);
assert(std::abs(Frequency - f) <= Epsilon);
Scale = ScaleFrequency(f, ScalingFunction::ERB, 0.);
Frequency = DescaleFrequency(Scale, ScalingFunction::ERB, 0.);
assert(std::abs(Frequency - f) <= Epsilon);
Scale = ScaleFrequency(f, ScalingFunction::Cams, 0.);
Frequency = DescaleFrequency(Scale, ScalingFunction::Cams, 0.);
assert(std::abs(Frequency - f) <= Epsilon);
Scale = ScaleFrequency(f, ScalingFunction::HyperbolicSine, 0.);
Frequency = DescaleFrequency(Scale, ScalingFunction::HyperbolicSine, 0.);
assert(std::abs(Frequency - f) <= Epsilon);
Scale = ScaleFrequency(f, ScalingFunction::NthRoot, 0.);
Frequency = DescaleFrequency(Scale, ScalingFunction::NthRoot, 0.);
assert(std::abs(Frequency - f) <= Epsilon);
Scale = ScaleFrequency(f, ScalingFunction::NegativeExponential, 0.);
Frequency = DescaleFrequency(Scale, ScalingFunction::NegativeExponential, 0.);
assert(std::abs(Frequency - f) <= Epsilon);
Scale = ScaleFrequency(f, ScalingFunction::Period, 0.);
Frequency = DescaleFrequency(Scale, ScalingFunction::Period, 0.);
assert(std::abs(Frequency - f) <= Epsilon);
}
}
void RunTests()
{
Log.Write("Running tests...");
Log.Write("Testing frequency scalers...");
TestFrequencyScaler();
Log.Write("Testing window functions...");
TestWindowFunctions();
Log.Write("Testing weighting functions...");
TestWeighting();
Log.Write("Finished running tests.");
}
#endif