-
Notifications
You must be signed in to change notification settings - Fork 206
/
Copy pathtest_audio_signal.py
1444 lines (1312 loc) · 61.3 KB
/
test_audio_signal.py
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
# encoding: utf-8
# pylint: skip-file
"""
This file contains tests for the madmom.audio.signal module.
"""
from __future__ import absolute_import, division, print_function
import os
import sys
import tempfile
import unittest
from os.path import join as pj
from madmom.audio.signal import *
from . import AUDIO_PATH
from .test_audio_comb_filters import sig_1d, sig_2d
sample_file = pj(AUDIO_PATH, 'sample.wav')
sample_file_22k = pj(AUDIO_PATH, 'sample_22050.wav')
stereo_sample_file = pj(AUDIO_PATH, 'stereo_sample.wav')
tmp_file = tempfile.NamedTemporaryFile(delete=False).name
# test signal functions
class TestSmoothFunction(unittest.TestCase):
def test_types(self):
# mono signals
result = smooth(sig_1d, None)
self.assertTrue(type(result) == type(sig_1d))
self.assertTrue(len(result) == len(sig_1d))
self.assertTrue(result.shape == sig_1d.shape)
result = smooth(sig_1d, 3)
self.assertTrue(type(result) == type(sig_1d))
self.assertTrue(len(result) == len(sig_1d))
self.assertTrue(result.shape == sig_1d.shape)
# multi-channel signals
result = smooth(sig_2d, None)
self.assertTrue(type(result) == type(sig_2d))
self.assertTrue(len(result) == len(sig_2d))
self.assertTrue(result.shape == sig_2d.shape)
result = smooth(sig_2d, 3)
self.assertTrue(type(result) == type(sig_2d))
self.assertTrue(len(result) == len(sig_2d))
self.assertTrue(result.shape == sig_2d.shape)
def test_errors(self):
with self.assertRaises(ValueError):
smooth(np.zeros(9).reshape(3, 3), -1)
with self.assertRaises(ValueError):
smooth(np.zeros(9).reshape(3, 3), 'bla')
with self.assertRaises(ValueError):
smooth(np.zeros(18).reshape(3, 3, 2), 4)
def test_values(self):
# mono signals
result = smooth(sig_1d, None)
self.assertTrue(np.allclose(result, sig_1d))
result = smooth(sig_1d, 0)
self.assertTrue(np.allclose(result, sig_1d))
result = smooth(sig_1d, 3)
result_3 = [0, 0.08, 1, 0.08, 0.08, 1, 0.08, 0.08, 1]
self.assertTrue(np.allclose(result, result_3))
result = smooth(sig_1d, 5)
result_5 = [0.08, 0.54, 1, 0.62, 0.62, 1, 0.62, 0.62, 1]
self.assertTrue(np.allclose(result, result_5))
result = smooth(sig_1d, 7)
result_7 = [0.31, 0.77, 1.08, 1.08, 1.08, 1.16, 1.08, 1.08, 1.08]
self.assertTrue(np.allclose(result, result_7))
result = smooth(sig_1d, np.ones(3))
result_3_ones = [0, 1, 1, 1, 1, 1, 1, 1, 1]
self.assertTrue(np.allclose(result, result_3_ones))
result = smooth(sig_1d, np.ones(4))
result_4_ones = [0, 1, 1, 1, 2, 1, 1, 2, 1]
self.assertTrue(np.allclose(result, result_4_ones))
# multi-channel signals
result = smooth(sig_2d, None)
self.assertTrue(np.allclose(result, sig_2d))
result = smooth(sig_2d, 3)
result_3 = [[0, 0.08, 1, 0.08, 0.08, 1, 0.08, 0.08, 1],
[1, 0.16, 1, 0.16, 1, 0.16, 1, 0.16, 1]]
self.assertTrue(np.allclose(result, np.asarray(result_3).T))
result = smooth(sig_2d, 5)
result_5 = [[0.08, 0.54, 1, 0.62, 0.62, 1, 0.62, 0.62, 1],
[1.08, 1.08, 1.16, 1.08, 1.16, 1.08, 1.16, 1.08, 1.08]]
self.assertTrue(np.allclose(result, np.asarray(result_5).T))
result = smooth(sig_2d, 7)
result_7 = [[0.31, 0.77, 1.08, 1.08, 1.08, 1.16, 1.08, 1.08, 1.08],
[1.31, 1.62, 1.62, 1.7, 1.62, 1.7, 1.62, 1.62, 1.31]]
self.assertTrue(np.allclose(result, np.asarray(result_7).T))
class TestAdjustGainFunction(unittest.TestCase):
def test_types(self):
# mono signals
result = adjust_gain(sig_1d, 0)
self.assertTrue(type(result) == type(sig_1d))
self.assertTrue(len(result) == len(sig_1d))
self.assertTrue(result.shape == sig_1d.shape)
self.assertTrue(result.dtype == sig_1d.dtype)
# same with int16 dtype
result = adjust_gain(sig_1d.astype(np.int16), 0)
self.assertTrue(len(result) == len(sig_1d))
self.assertTrue(result.shape == sig_1d.shape)
self.assertTrue(result.dtype == np.int16)
# from file
signal = Signal(sample_file)
result = adjust_gain(signal, 0)
self.assertIsInstance(result, Signal)
self.assertIsInstance(result, np.ndarray)
self.assertTrue(result.dtype == signal.dtype)
# multi-channel signals
result = adjust_gain(sig_2d, 0)
self.assertTrue(type(result) == type(sig_2d))
self.assertTrue(len(result) == len(sig_2d))
self.assertTrue(result.shape == sig_2d.shape)
self.assertTrue(result.dtype == sig_2d.dtype)
# same with int dtype
result = adjust_gain(sig_2d.astype(int), 0)
self.assertTrue(len(result) == len(sig_2d))
self.assertTrue(result.shape == sig_2d.shape)
self.assertTrue(result.dtype == int)
def test_values(self):
# mono signals
result = adjust_gain(sig_1d, 0)
self.assertTrue(np.allclose(result, sig_1d))
result = adjust_gain(sig_1d, -10)
self.assertTrue(np.allclose(result, 0.31622777 * sig_1d))
result = adjust_gain(sig_1d, 10)
self.assertTrue(np.allclose(result, 3.1622777 * sig_1d))
# same with int dtype
result = adjust_gain(sig_1d.astype(int), 0)
self.assertTrue(np.allclose(result, sig_1d.astype(int)))
result = adjust_gain(sig_1d.astype(int), -5)
self.assertTrue(np.allclose(result, 0 * sig_1d))
# multi-channel signals
result = adjust_gain(sig_2d, 0)
self.assertTrue(np.allclose(result, sig_2d))
result = adjust_gain(sig_2d, -3)
self.assertTrue(np.allclose(result, 0.70794578 * sig_2d))
# same with int16 dtype
result = adjust_gain(sig_2d.astype(np.int16), 0)
self.assertTrue(np.allclose(result, sig_2d))
result = adjust_gain(sig_2d.astype(np.int16), -1)
self.assertTrue(np.allclose(result, 0 * sig_2d))
def test_errors(self):
with self.assertRaises(ValueError):
adjust_gain(sig_2d.astype(np.int16), +60)
class TestAttenuateFunction(unittest.TestCase):
def test_types(self):
# mono signals
result = attenuate(sig_1d, 0)
self.assertTrue(type(result) == type(sig_1d))
self.assertTrue(len(result) == len(sig_1d))
self.assertTrue(result.shape == sig_1d.shape)
self.assertTrue(result.dtype == sig_1d.dtype)
# same as int16 dtype
result = attenuate(sig_1d.astype(np.int16), 0)
self.assertTrue(len(result) == len(sig_1d))
self.assertTrue(result.shape == sig_1d.shape)
self.assertTrue(result.dtype == np.int16)
# from file
signal = Signal(sample_file)
result = attenuate(signal, 0)
self.assertIsInstance(result, Signal)
self.assertIsInstance(result, np.ndarray)
self.assertTrue(result.dtype == np.int16)
# multi-channel signals
result = attenuate(sig_2d, 0)
self.assertTrue(type(result) == type(sig_2d))
self.assertTrue(len(result) == len(sig_2d))
self.assertTrue(result.shape == sig_2d.shape)
self.assertTrue(result.dtype == sig_2d.dtype)
# same as int dtype
result = attenuate(sig_2d.astype(int), 0)
self.assertTrue(len(result) == len(sig_2d))
self.assertTrue(result.shape == sig_2d.shape)
self.assertTrue(result.dtype == int)
def test_values(self):
# mono signals
result = attenuate(sig_1d, 0)
self.assertTrue(np.allclose(result, sig_1d))
result = attenuate(sig_1d, 10)
self.assertTrue(np.allclose(result, 0.31622777 * sig_1d))
result = attenuate(sig_1d, -10)
self.assertTrue(np.allclose(result, 3.1622777 * sig_1d))
# same with int dtype
result = attenuate(sig_1d.astype(int), 0)
self.assertTrue(np.allclose(result, sig_1d.astype(int)))
result = attenuate(sig_1d.astype(int), 5)
self.assertTrue(np.allclose(result, 0 * sig_1d))
# multi-channel signals
result = attenuate(sig_2d, 0)
self.assertTrue(np.allclose(result, sig_2d))
result = attenuate(sig_2d, 3)
self.assertTrue(np.allclose(result, 0.70794578 * sig_2d))
# same with int16 dtype
result = attenuate(sig_2d.astype(np.int16), 0)
self.assertTrue(np.allclose(result, sig_2d))
result = attenuate(sig_2d.astype(np.int16), 1)
self.assertTrue(np.allclose(result, 0 * sig_2d))
def test_errors(self):
with self.assertRaises(ValueError):
attenuate(sig_2d.astype(np.int16), -10)
class TestNormalizeFunction(unittest.TestCase):
def test_types(self):
# mono signals
result = normalize(sig_1d)
self.assertTrue(len(result) == len(sig_1d))
self.assertTrue(result.shape == sig_1d.shape)
self.assertTrue(result.dtype == sig_1d.dtype)
# same as int16 dtype
result = normalize(sig_1d.astype(np.int16))
self.assertTrue(len(result) == len(sig_1d))
self.assertTrue(result.shape == sig_1d.shape)
self.assertTrue(result.dtype == np.int16)
# from file
signal = Signal(sample_file)
result = normalize(signal)
self.assertIsInstance(result, Signal)
self.assertIsInstance(result, np.ndarray)
self.assertTrue(result.dtype == np.int16)
# multi-channel signals
result = normalize(sig_2d)
self.assertTrue(len(result) == len(sig_2d))
self.assertTrue(result.shape == sig_2d.shape)
self.assertTrue(result.dtype == sig_2d.dtype)
# same as int32 dtype
result = normalize(sig_2d.astype(np.int32))
self.assertTrue(len(result) == len(sig_2d))
self.assertTrue(result.shape == sig_2d.shape)
self.assertTrue(result.dtype == np.int32)
def test_values(self):
# mono signals
result = normalize(sig_1d)
self.assertTrue(np.allclose(result, sig_1d))
result = normalize(sig_1d * 0.5)
self.assertTrue(np.allclose(result, sig_1d))
self.assertTrue(np.max(result) == 1)
# same as int16 dtype
result = normalize(10 * sig_1d.astype(np.int16))
self.assertTrue(np.allclose(result, sig_1d * 32767))
self.assertTrue(np.max(result) == 32767)
# multi-channel signals
result = normalize(sig_2d)
self.assertTrue(np.allclose(result, sig_2d))
self.assertTrue(np.max(result) == 1)
# negative values
result = normalize(sig_2d * 4 - 2)
self.assertTrue(np.allclose(result, sig_2d * 2 - 1))
self.assertTrue(result.max() == 1)
self.assertTrue(result.min() == -1)
# same as int32 dtype
result = normalize(3 * sig_2d.astype(np.int32))
self.assertTrue(np.allclose(result, sig_2d * 2147483647))
self.assertTrue(np.max(result) == 2147483647)
def test_errors(self):
with self.assertRaises(ValueError):
normalize(sig_2d.astype(np.int64))
class TestMixFunction(unittest.TestCase):
mono_2d = np.asarray([0.5, 0, 1, 0, 0.5, 0.5, 0.5, 0, 1], dtype=float)
def test_types(self):
# mono signals
result = remix(sig_1d, 1)
self.assertTrue(len(result) == len(sig_1d))
self.assertTrue(result.shape == sig_1d.shape)
self.assertTrue(result.dtype == sig_1d.dtype)
result = remix(sig_1d, 2)
self.assertTrue(len(result) == len(sig_1d))
self.assertTrue(result.shape == (len(sig_1d), 2))
self.assertTrue(result.dtype == sig_1d.dtype)
result = remix(sig_1d, 3)
self.assertTrue(len(result) == len(sig_1d))
self.assertTrue(result.shape == (len(sig_1d), 3))
self.assertTrue(result.dtype == sig_1d.dtype)
# same as int dtype
result = remix(sig_1d.astype(int), 1)
self.assertTrue(len(result) == len(sig_1d))
self.assertTrue(result.shape == sig_1d.shape)
self.assertTrue(result.dtype == int)
result = remix(sig_1d.astype(int), 2)
self.assertTrue(len(result) == len(sig_1d))
self.assertTrue(result.shape == (len(sig_1d), 2))
self.assertTrue(result.dtype == int)
# from file
signal = Signal(sample_file)
result = remix(signal, 1)
self.assertTrue(isinstance(result, Signal))
self.assertTrue(isinstance(result, np.ndarray))
self.assertTrue(result.dtype == np.int16)
# multi-channel signals
result = remix(sig_2d, 1)
self.assertTrue(len(result) == len(sig_2d))
self.assertTrue(result.shape == sig_1d.shape)
self.assertTrue(result.dtype == sig_2d.dtype)
result = remix(sig_2d, 2)
self.assertTrue(len(result) == len(sig_2d))
self.assertTrue(result.shape == sig_2d.shape)
self.assertTrue(result.dtype == sig_2d.dtype)
# from file
signal = Signal(stereo_sample_file)
result = remix(signal, 1)
self.assertTrue(isinstance(result, Signal))
self.assertTrue(isinstance(result, np.ndarray))
self.assertTrue(result.dtype == np.int16)
result = remix(signal, 2)
self.assertTrue(isinstance(result, Signal))
self.assertTrue(isinstance(result, np.ndarray))
self.assertTrue(result.dtype == np.int16)
with self.assertRaises(NotImplementedError):
remix(sig_2d, 3)
# same as int dtype
result = remix(sig_2d.astype(int), 1)
self.assertTrue(len(result) == len(sig_2d))
self.assertTrue(result.shape == sig_1d.shape)
self.assertTrue(result.dtype == int)
result = remix(sig_2d.astype(int), 2)
self.assertTrue(len(result) == len(sig_2d))
self.assertTrue(result.shape == sig_2d.shape)
self.assertTrue(result.dtype == int)
def test_channel_selection(self):
result = remix(sig_2d, 1, channel=0)
self.assertEqual(result.shape, sig_1d.shape)
self.assertTrue(np.array_equal(result, sig_1d))
result = remix(sig_2d, 1, channel=1)
self.assertTrue(np.array_equal(result, sig_2d[:, 1]), 0)
def test_values(self):
# mono signals
result = remix(sig_1d, 1)
self.assertTrue(np.allclose(result, sig_1d))
# same as int dtype
result = remix(sig_2d.astype(int), 1)
self.assertTrue(np.allclose(result, self.mono_2d.astype(int)))
# multi-channel signals
result = remix(sig_2d, 1)
self.assertTrue(np.allclose(result, self.mono_2d))
# same as int dtype
result = remix(2 * sig_2d.astype(int), 1)
self.assertTrue(np.allclose(result, 2 * self.mono_2d))
class TestResampleFunction(unittest.TestCase):
def setUp(self):
self.signal = Signal(sample_file)
self.signal_22k = Signal(sample_file_22k)
self.signal_float = Signal(sample_file, dtype=np.float32)
self.stereo_signal = Signal(stereo_sample_file)
self.float_target = np.array([-0.07537885, -0.077897, -0.08440731,
-0.07527363, -0.06685895, -0.05827513])
def test_types(self):
# mono signal
result = resample(self.signal, 22050)
self.assertTrue(isinstance(result, Signal))
self.assertTrue(isinstance(result, np.ndarray))
self.assertTrue(result.dtype == self.signal.dtype)
# stereo signal
result = resample(self.stereo_signal, 22050)
self.assertTrue(isinstance(result, Signal))
self.assertTrue(isinstance(result, np.ndarray))
self.assertTrue(result.dtype == self.stereo_signal.dtype)
def test_values_mono(self):
result = resample(self.signal, 22050)
self.assertEqual(result.sample_rate, 22050)
self.assertEqual(result.num_samples, 61741)
self.assertEqual(result.dtype, self.signal.dtype)
self.assertEqual(result.num_channels, self.signal.num_channels)
self.assertTrue(np.allclose(result.length, self.signal.length))
self.assertTrue(np.allclose(result, self.signal_22k))
def test_values_mono_float(self):
result = resample(self.signal_float, 22050)
self.assertEqual(result.sample_rate, 22050)
self.assertEqual(result.num_samples, 61741)
self.assertEqual(result.dtype, self.signal_float.dtype)
self.assertEqual(result.num_channels, self.signal_float.num_channels)
self.assertTrue(np.allclose(result.length, self.signal_float.length))
self.assertTrue(np.allclose(result[:6], self.float_target))
def test_values_dtype(self):
result = resample(self.signal, 22050, dtype=np.float32)
self.assertEqual(result.sample_rate, 22050)
self.assertEqual(result.num_samples, 61741)
self.assertEqual(result.dtype, np.float32)
self.assertEqual(result.num_channels, self.signal_float.num_channels)
self.assertTrue(np.allclose(result.length, self.signal_float.length))
self.assertTrue(np.allclose(result[:6], self.float_target))
def test_values_stereo(self):
result = resample(self.stereo_signal, 22050)
self.assertEqual(result.sample_rate, 22050)
self.assertEqual(result.num_samples, 91460)
self.assertEqual(result.dtype, self.stereo_signal.dtype)
self.assertEqual(result.num_channels, self.stereo_signal.num_channels)
self.assertTrue(np.allclose(result.length, self.stereo_signal.length))
self.assertTrue(np.allclose(result[:6],
[[34, 38], [32, 33], [37, 31],
[35, 35], [32, 34], [33, 34]]))
def test_values_upmixing(self):
result = resample(self.signal, 22050, num_channels=2)
self.assertEqual(result.sample_rate, 22050)
self.assertEqual(result.num_samples, 61741)
self.assertEqual(result.dtype, self.signal.dtype)
self.assertEqual(result.num_channels, 2)
self.assertTrue(np.allclose(result.length, self.signal.length))
stereo = np.vstack((self.signal_22k, self.signal_22k)).T / np.sqrt(2)
self.assertTrue(np.allclose(result, stereo, atol=np.sqrt(2)))
def test_values_downmixing(self):
result = resample(self.stereo_signal, 22050, num_channels=1)
self.assertEqual(result.sample_rate, 22050)
self.assertEqual(result.num_samples, 91460)
self.assertEqual(result.dtype, self.stereo_signal.dtype)
self.assertEqual(result.num_channels, 1)
self.assertTrue(np.allclose(result.length, self.stereo_signal.length))
self.assertTrue(np.allclose(result[:6], [36, 33, 34, 35, 33, 34]))
def test_errors(self):
with self.assertRaises(ValueError):
resample(sig_1d, 2)
class TestRescaleFunction(unittest.TestCase):
def test_types(self):
# mono signals
result = rescale(sig_1d, float)
self.assertTrue(len(result) == len(sig_1d))
self.assertTrue(result.shape == sig_1d.shape)
self.assertTrue(result.dtype == float)
# from file
signal = Signal(sample_file)
result = rescale(signal)
self.assertTrue(isinstance(result, Signal))
self.assertTrue(isinstance(result, np.ndarray))
self.assertTrue(result.dtype == np.float32)
# multi-channel signals
result = rescale(sig_2d, np.float16)
self.assertTrue(len(result) == len(sig_2d))
self.assertTrue(result.shape == sig_2d.shape)
self.assertTrue(result.dtype == np.float16)
# from file
signal = Signal(stereo_sample_file)
result = rescale(signal, float)
self.assertTrue(isinstance(result, Signal))
self.assertTrue(isinstance(result, np.ndarray))
self.assertTrue(result.dtype == float)
def test_errors(self):
with self.assertRaises(ValueError):
rescale(sig_2d, complex)
with self.assertRaises(ValueError):
rescale(sig_2d, int)
with self.assertRaises(ValueError):
rescale(np.ones(10, dtype=complex))
def test_values(self):
# mono signals
result = rescale(sig_1d, float)
self.assertTrue(np.allclose(result, sig_1d))
# from file
signal = Signal(sample_file)
result = rescale(signal)
self.assertTrue(np.allclose(result[:6],
[-0.07611316, -0.07660146, -0.07580798,
-0.08172857, -0.08645894, -0.08212531]))
# multi-channel signals
result = rescale(sig_2d, np.float16)
self.assertTrue(np.allclose(result, sig_2d))
# from file
signal = Signal(stereo_sample_file)
result = rescale(signal, float)
self.assertTrue(np.allclose(result[:6], [[0.00100711, 0.0011597],
[0.00106815, 0.00109867],
[0.00088504, 0.00103763],
[0.00109867, 0.00094607],
[0.00112918, 0.00091556],
[0.00109867, 0.00103763]]))
class TestTrimFunction(unittest.TestCase):
def test_types(self):
# mono signals
result = trim(sig_1d)
self.assertTrue(type(result) == type(sig_1d))
self.assertTrue(result.ndim == sig_1d.ndim)
self.assertTrue(result.dtype == sig_1d.dtype)
signal = Signal(sample_file)
result = trim(signal)
self.assertIsInstance(result, Signal)
self.assertIsInstance(result, np.ndarray)
self.assertTrue(result.ndim == signal.ndim)
self.assertTrue(result.dtype == np.int16)
# multi-channel signals
result = trim(sig_2d)
self.assertTrue(type(result) == type(sig_2d))
self.assertTrue(len(result) == len(sig_2d))
self.assertTrue(result.ndim == sig_2d.ndim)
self.assertTrue(result.dtype == sig_2d.dtype)
signal = Signal(stereo_sample_file)
result = trim(signal)
self.assertIsInstance(result, Signal)
self.assertIsInstance(result, np.ndarray)
self.assertTrue(result.ndim == signal.ndim)
self.assertTrue(result.dtype == np.int16)
def test_values(self):
# mono signals
result = trim(sig_1d)
trimmed_1d = [1, 0, 0, 1, 0, 0, 1]
self.assertTrue(np.allclose(result, trimmed_1d))
self.assertTrue(len(result) == len(sig_1d) - 2)
# multi-channel signals
# signal has leading zeros only in one channel
result = trim(sig_2d)
self.assertTrue(result.shape == sig_2d.shape)
signal = Signal(stereo_sample_file)
result = trim(signal)
self.assertTrue(result.shape == signal.shape)
# signal with leading zeros only in both channels
signal = np.tile(np.arange(5), 2).reshape(2, 5).T
result = trim(signal)
self.assertTrue(result.shape == (4, 2))
self.assertTrue(np.allclose(result[:, 0], np.arange(1, 5)))
self.assertTrue(np.allclose(result[:, 1], np.arange(1, 5)))
# zeros at the end
result = trim(np.arange(5, 0, -1))
self.assertTrue(np.allclose(result, [5, 4, 3, 2, 1]))
class TestEnergyFunction(unittest.TestCase):
def test_types(self):
# mono signals
result = energy(sig_1d)
self.assertIsInstance(result, float)
# multi-channel signals
result = energy(sig_2d)
self.assertIsInstance(result, float)
def test_errors(self):
with self.assertRaises(TypeError):
energy(None)
def test_values(self):
# mono signals
result = energy(sig_1d)
self.assertTrue(np.allclose(result, 3))
result = energy(np.zeros(100))
self.assertTrue(np.allclose(result, 0))
# multi-channel signals
result = energy(sig_2d)
self.assertTrue(np.allclose(result, 8))
result = energy(np.zeros(100).reshape(-1, 2))
self.assertTrue(np.allclose(result, 0))
def test_frames(self):
# mono signals
frames = FramedSignal(sig_1d, frame_size=4, hop_size=2)
result = energy(frames)
self.assertTrue(np.allclose(result, [0, 1, 2, 1, 1]))
result = energy(np.zeros(100))
self.assertTrue(np.allclose(result, 0))
# multi-channel signals
frames = FramedSignal(sig_2d, frame_size=4, hop_size=2)
result = energy(frames)
self.assertTrue(np.allclose(result, [1, 3, 4, 3, 3]))
result = energy(np.zeros(100).reshape(-1, 2))
self.assertTrue(np.allclose(result, 0))
class TestRootMeanSquareFunction(unittest.TestCase):
def test_types(self):
# mono signals
result = root_mean_square(sig_1d)
self.assertIsInstance(result, float)
# multi-channel signals
result = root_mean_square(sig_2d)
self.assertIsInstance(result, float)
def test_values(self):
# mono signals
result = root_mean_square(sig_1d)
self.assertTrue(np.allclose(result, 0.57735026919))
result = root_mean_square(np.zeros(100))
self.assertTrue(np.allclose(result, 0))
# multi-channel signals
result = root_mean_square(sig_2d)
self.assertTrue(np.allclose(result, 2. / 3))
result = root_mean_square(np.zeros(100).reshape(-1, 2))
self.assertTrue(np.allclose(result, 0))
def test_frames(self):
# mono signals
frames = FramedSignal(sig_1d, frame_size=4, hop_size=2)
result = root_mean_square(frames)
self.assertTrue(np.allclose(result, [0, 0.5, 0.70710678, 0.5, 0.5]))
result = root_mean_square(np.zeros(100))
self.assertTrue(np.allclose(result, 0))
# multi-channel signals
frames = FramedSignal(sig_2d, frame_size=4, hop_size=2)
result = root_mean_square(frames)
self.assertTrue(np.allclose(result, [0.35355339, 0.61237244,
0.70710678, 0.61237244,
0.61237244]))
result = root_mean_square(np.zeros(100).reshape(-1, 2))
self.assertTrue(np.allclose(result, 0))
class TestSoundPressureLevelFunction(unittest.TestCase):
def test_types(self):
# mono signals
result = sound_pressure_level(sig_1d)
self.assertIsInstance(result, float)
# multi-channel signals
result = sound_pressure_level(sig_2d)
self.assertIsInstance(result, float)
def test_values(self):
# mono signals
result = sound_pressure_level(sig_1d)
self.assertTrue(np.allclose(result, -4.7712125472))
# silence
result = sound_pressure_level(np.zeros(100))
self.assertTrue(np.allclose(result, -np.finfo(float).max))
# maximum float amplitude, alternating between -1 and 1
sinus = np.cos(np.linspace(0, 2 * np.pi * 100, 2 * 100 + 1))
result = sound_pressure_level(sinus)
self.assertTrue(np.allclose(result, 0.))
# maximum int16 amplitude, alternating between -1 and 1
sinus_int16 = (sinus * np.iinfo(np.int16).max).astype(np.int16)
result = sound_pressure_level(sinus_int16)
self.assertTrue(np.allclose(result, 0., atol=1e-3))
# multi-channel signals
result = sound_pressure_level(sig_2d)
self.assertTrue(np.allclose(result, -3.52182518111))
# silence
result = sound_pressure_level(np.zeros(100).reshape(-1, 2))
self.assertTrue(np.allclose(result, -np.finfo(float).max))
# maximum float amplitude, alternating between -1 and 1
sig = remix(sinus, 2)
result = sound_pressure_level(sig)
self.assertTrue(np.allclose(result, 0.))
# maximum int16 amplitude, alternating between -1 and 1
sig = remix(sinus_int16, 2)
result = sound_pressure_level(sig)
self.assertTrue(np.allclose(result, 0., atol=1e-3))
def test_frames(self):
# mono signals
frames = FramedSignal(sig_1d, frame_size=4, hop_size=2)
result = sound_pressure_level(frames)
self.assertTrue(np.allclose(result, [-np.finfo(float).max, -6.0206,
-3.0103, -6.0206, -6.0206]))
result = sound_pressure_level(np.zeros(100))
self.assertTrue(np.allclose(result, -np.finfo(float).max))
# multi-channel signals
frames = FramedSignal(sig_2d, frame_size=4, hop_size=2)
result = sound_pressure_level(frames)
self.assertTrue(np.allclose(result, [-9.03089987, -4.25968732,
-3.01029996, -4.25968732,
-4.25968732]))
result = sound_pressure_level(np.zeros(100).reshape(-1, 2))
self.assertTrue(np.allclose(result, -np.finfo(float).max))
# signal classes
class TestSignalClass(unittest.TestCase):
def test_types_array(self):
result = Signal(sig_1d)
self.assertIsInstance(result, Signal)
self.assertIsInstance(result, np.ndarray)
self.assertTrue(result.dtype == float)
self.assertIsInstance(result.start, type(None))
self.assertIsInstance(result.stop, type(None))
self.assertIsInstance(result.num_samples, int)
self.assertIsInstance(result.sample_rate, type(None))
self.assertIsInstance(result.num_channels, int)
self.assertIsInstance(result.length, type(None))
self.assertIsInstance(result.ndim, int)
def test_types_array_with_sample_rate(self):
result = Signal(sig_1d, 1)
self.assertIsInstance(result, Signal)
self.assertIsInstance(result, np.ndarray)
self.assertTrue(result.dtype == float)
self.assertIsInstance(result.start, type(None))
self.assertIsInstance(result.stop, type(None))
self.assertIsInstance(result.num_samples, int)
self.assertIsInstance(result.sample_rate, int)
self.assertIsInstance(result.num_channels, int)
self.assertIsInstance(result.length, float)
self.assertIsInstance(result.ndim, int)
def test_types_file(self):
result = Signal(sample_file)
self.assertIsInstance(result, Signal)
self.assertIsInstance(result, np.ndarray)
self.assertTrue(result.dtype == np.int16)
def test_values_1d(self):
result = Signal(sig_1d, 1)
self.assertTrue(np.allclose(result, sig_1d))
self.assertTrue(len(result) == 9)
self.assertTrue(result.num_samples == 9)
self.assertTrue(result.sample_rate == 1)
self.assertTrue(result.num_channels == 1)
self.assertTrue(result.length == 9)
def test_values_1d_no_sample_rate(self):
result = Signal(sig_1d)
self.assertTrue(np.allclose(result, sig_1d))
self.assertTrue(len(result) == 9)
self.assertTrue(result.num_samples == 9)
self.assertTrue(result.sample_rate is None)
self.assertTrue(result.num_channels == 1)
self.assertTrue(result.length is None)
def test_values_2d(self):
result = Signal(sig_2d, 12.3)
self.assertTrue(np.allclose(result, sig_2d))
self.assertTrue(len(result) == 9)
self.assertTrue(result.num_samples == 9)
# not officially supported, but Signal can handle float sample rates
self.assertTrue(result.sample_rate == 12.3)
self.assertTrue(result.num_channels == 2)
self.assertTrue(result.length == 9 / 12.3)
self.assertTrue(result.ndim == 2)
def test_num_channels(self):
result = Signal(sig_2d, sample_rate=1, num_channels=1)
self.assertTrue(result.shape == (9, ))
self.assertTrue(np.allclose(result,
[0.5, 0, 1, 0, 0.5, 0.5, 0.5, 0, 1]))
def test_values_file(self):
result = Signal(sample_file)
self.assertTrue(np.allclose(result[:5],
[-2494, -2510, -2484, -2678, -2833]))
self.assertTrue(len(result) == 123481)
self.assertTrue(result.num_samples == 123481)
self.assertTrue(result.sample_rate == 44100)
self.assertTrue(result.num_channels == 1)
self.assertTrue(result.ndim == 1)
self.assertTrue(np.allclose(result.length, 2.8))
def test_write_method(self):
orig = Signal(sample_file)
orig.write(tmp_file)
result = Signal(tmp_file)
self.assertTrue(np.allclose(orig, result))
def test_methods(self):
# mono signals
signal = Signal(sig_1d)
self.assertTrue(np.allclose(signal.energy(), 3))
self.assertTrue(np.allclose(signal.rms(), 0.57735026919))
self.assertTrue(np.allclose(signal.spl(), -4.7712125472))
# multi-channel signals
signal = Signal(sig_2d)
self.assertTrue(np.allclose(signal.energy(), 8))
self.assertTrue(np.allclose(signal.root_mean_square(), 2. / 3))
self.assertTrue(np.allclose(signal.sound_pressure_level(),
-3.52182518111))
class TestSignalProcessorClass(unittest.TestCase):
def setUp(self):
self.processor = SignalProcessor()
def test_types(self):
self.assertIsInstance(self.processor, SignalProcessor)
self.assertIsInstance(self.processor, Processor)
# attributes
self.assertTrue(self.processor.sample_rate is None)
self.assertTrue(self.processor.num_channels is None)
self.assertTrue(self.processor.start is None)
self.assertTrue(self.processor.stop is None)
self.assertIsInstance(self.processor.norm, bool)
self.assertIsInstance(self.processor.gain, float)
def test_values(self):
# attributes
self.assertTrue(self.processor.sample_rate is None)
self.assertTrue(self.processor.num_channels is None)
self.assertTrue(self.processor.start is None)
self.assertTrue(self.processor.stop is None)
self.assertTrue(self.processor.norm is False)
self.assertTrue(self.processor.gain == 0)
def test_process(self):
result = self.processor.process(sample_file)
self.assertIsInstance(result, Signal)
self.assertIsInstance(result, np.ndarray)
self.assertTrue(result.dtype == np.int16)
self.assertTrue(np.allclose(result[:5],
[-2494, -2510, -2484, -2678, -2833]))
self.assertTrue(len(result) == 123481)
self.assertTrue(result.min() == -20603)
self.assertTrue(result.max() == 17977)
self.assertTrue(result.mean() == -172.88385257650975)
# attributes
self.assertTrue(result.sample_rate == 44100)
# properties
self.assertTrue(result.num_samples == 123481)
self.assertTrue(result.num_channels == 1)
self.assertTrue(np.allclose(result.length, 2.8))
def test_process_stereo(self):
self.processor.num_channels = 1
self.assertTrue(self.processor.num_channels == 1)
result = self.processor.process(stereo_sample_file)
self.assertIsInstance(result, Signal)
self.assertIsInstance(result, np.ndarray)
self.assertTrue(result.dtype == np.int16)
def test_process_norm(self):
self.processor.norm = True
self.assertTrue(self.processor.norm is True)
result = self.processor.process(sample_file)
self.assertIsInstance(result, Signal)
self.assertIsInstance(result, np.ndarray)
self.assertTrue(result.dtype == np.int16)
self.assertTrue(np.allclose(result[:5],
[-3966, -3991, -3950, -4259, -4505]))
self.assertTrue(len(result) == 123481)
self.assertTrue(result.min() == -32767)
self.assertTrue(result.max() == 28590)
self.assertTrue(result.mean() == -274.92599671204476)
# attributes
self.assertTrue(result.sample_rate == 44100)
# properties
self.assertTrue(result.num_samples == 123481)
self.assertTrue(result.num_channels == 1)
self.assertTrue(np.allclose(result.length, 2.8))
def test_process_gain(self):
self.processor.gain = -10
self.assertTrue(self.processor.gain == -10.)
result = self.processor.process(sample_file)
self.assertIsInstance(result, Signal)
self.assertIsInstance(result, np.ndarray)
self.assertTrue(result.dtype == np.int16)
self.assertTrue(np.allclose(result[:5],
[-788, -793, -785, -846, -895]))
self.assertTrue(len(result) == 123481)
# attributes
self.assertTrue(result.sample_rate == 44100)
# properties
self.assertTrue(result.num_samples == 123481)
self.assertTrue(result.num_channels == 1)
self.assertTrue(np.allclose(result.length, 2.8))
def test_pickle(self):
self.processor.dump(tmp_file)
processor = SignalProcessor.load(tmp_file)
self.assertEqual(type(self.processor), type(processor))
self.assertEqual(self.processor.sample_rate,
processor.sample_rate)
self.assertEqual(self.processor.num_channels,
processor.num_channels)
self.assertEqual(self.processor.start, processor.start)
self.assertEqual(self.processor.stop, processor.stop)
self.assertEqual(self.processor.norm, processor.norm)
self.assertEqual(self.processor.gain, processor.gain)
@unittest.skipIf(sys.version_info < (3, 2), 'assertWarns needs Python 3.2')
def test_multiprocessing(self):
from concurrent.futures import ProcessPoolExecutor
sig = Signal(sample_file)
pool = ProcessPoolExecutor(max_workers=2)
pool.submit(self.processor, sig).result()
# framing functions
class TestSignalFrameFunction(unittest.TestCase):
def test_types(self):
result = signal_frame(np.arange(10), 0, 4, 2)
self.assertIsInstance(result, np.ndarray)
self.assertTrue(result.dtype == int)
result = signal_frame(np.arange(10, dtype=float), 0, 4, 2)
self.assertIsInstance(result, np.ndarray)
self.assertTrue(result.dtype == float)
signal = Signal(sample_file)
result = signal_frame(signal, 0, 4, 2)
self.assertIsInstance(result, Signal)
self.assertIsInstance(result, np.ndarray)
self.assertTrue(result.dtype == np.int16)
result = signal_frame(signal, 2000, 400, 200)
self.assertIsInstance(result, Signal)
self.assertIsInstance(result, np.ndarray)
self.assertTrue(result.dtype == np.int16)
result = signal_frame(signal, -10, 400, 200)
self.assertIsInstance(result, Signal)
self.assertIsInstance(result, np.ndarray)
self.assertTrue(result.dtype == np.int16)
def test_short_input_length(self):
result = signal_frame(np.arange(4), 0, 10, 5)
self.assertTrue(np.allclose(result, [0, 0, 0, 0, 0, 0, 1, 2, 3, 0]))
result = signal_frame(np.arange(4), 1, 10, 5)
self.assertTrue(np.allclose(result, [0, 1, 2, 3, 0, 0, 0, 0, 0, 0]))
result = signal_frame(np.arange(4), 2, 10, 5)
self.assertTrue(np.allclose(result, [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]))
result = signal_frame(np.arange(4), -2, 10, 5)
self.assertTrue(np.allclose(result, [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]))
def test_values(self):
result = signal_frame(np.arange(10), -1, 4, 2)
self.assertTrue(np.allclose(result, [0, 0, 0, 0]))
result = signal_frame(np.arange(10), 0, 4, 2)
self.assertTrue(np.allclose(result, [0, 0, 0, 1]))
result = signal_frame(np.arange(10), 1, 4, 2)
self.assertTrue(np.allclose(result, [0, 1, 2, 3]))
result = signal_frame(np.arange(10), 2, 4, 2)
self.assertTrue(np.allclose(result, [2, 3, 4, 5]))
result = signal_frame(np.arange(10), 3, 4, 2)
self.assertTrue(np.allclose(result, [4, 5, 6, 7]))
result = signal_frame(np.arange(10), 4, 4, 2)
self.assertTrue(np.allclose(result, [6, 7, 8, 9]))
result = signal_frame(np.arange(10), 5, 4, 2)
self.assertTrue(np.allclose(result, [8, 9, 0, 0]))
def test_stereo_values(self):
signal = np.tile(np.arange(10)[:, np.newaxis], 2)
result = signal_frame(signal, 0, 4, 2)
self.assertTrue(np.allclose(result, [[0, 0], [0, 0], [0, 0], [1, 1]]))
result = signal_frame(signal, 1, 4, 2)
self.assertTrue(np.allclose(result, [[0, 0], [1, 1], [2, 2], [3, 3]]))
result = signal_frame(signal, 2, 4, 2)
self.assertTrue(np.allclose(result, [[2, 2], [3, 3], [4, 4], [5, 5]]))
result = signal_frame(signal, 3, 4, 2)
self.assertTrue(np.allclose(result, [[4, 4], [5, 5], [6, 6], [7, 7]]))
result = signal_frame(signal, 4, 4, 2)
self.assertTrue(np.allclose(result, [[6, 6], [7, 7], [8, 8], [9, 9]]))
result = signal_frame(signal, 5, 4, 2)
self.assertTrue(np.allclose(result, [[8, 8], [9, 9], [0, 0], [0, 0]]))
result = signal_frame(signal, 6, 4, 2)
self.assertTrue(np.allclose(result, [[0, 0], [0, 0], [0, 0], [0, 0]]))
def test_float_hop_size(self):
result = signal_frame(np.arange(10), 0, 3.5, 2)
self.assertTrue(np.allclose(result, [0, 0, 1]))
result = signal_frame(np.arange(10), 1, 3.5, 2)
self.assertTrue(np.allclose(result, [1, 2, 3]))
result = signal_frame(np.arange(10), 2, 3.5, 2)
self.assertTrue(np.allclose(result, [3, 4, 5]))
def test_origin(self):
result = signal_frame(np.arange(10), 0, 4, 2)
self.assertTrue(np.allclose(result, [0, 0, 0, 1]))
# positive values shift to the left
result = signal_frame(np.arange(10), 0, 4, 2, 1)
self.assertTrue(np.allclose(result, [0, 0, 0, 0]))
# negative values shift to the right
result = signal_frame(np.arange(10), 0, 4, 2, -1)
self.assertTrue(np.allclose(result, [0, 0, 1, 2]))
result = signal_frame(np.arange(10), 0, 4, 2, -2)
self.assertTrue(np.allclose(result, [0, 1, 2, 3]))
result = signal_frame(np.arange(10), 0, 4, 2, -4)
self.assertTrue(np.allclose(result, [2, 3, 4, 5]))
# test with float origin with half the size of the frame size
result = signal_frame(np.arange(10), 0, 5, 2, -2.5)
self.assertTrue(np.allclose(result, [0, 1, 2, 3, 4]))
def test_pad(self):
# 1D signal
x = np.arange(10, 20)
# pad with a fixed value