forked from ludlows/PESQ
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_pesq.py
executable file
·131 lines (95 loc) · 4.2 KB
/
test_pesq.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
from pathlib import Path
import numpy as np
import pytest
import scipy.io.wavfile
from pesq import pesq, pesq_batch, NoUtterancesError, PesqError
def test():
data_dir = Path(__file__).parent.parent / 'audio'
ref_path = data_dir / 'speech.wav'
deg_path = data_dir / 'speech_bab_0dB.wav'
sample_rate, ref = scipy.io.wavfile.read(ref_path)
sample_rate, deg = scipy.io.wavfile.read(deg_path)
score = pesq(ref=ref, deg=deg, fs=sample_rate, mode='wb')
print(f'WB score: {score}, was: 1.0832337141036987')
assert score == 1.5128041505813599, score
score = pesq(ref=ref, deg=deg, fs=sample_rate, mode='nb')
assert score == 1.6072081327438354, score
print(f'NB score: {score}, was:1.6072081327438354')
return score
def test_no_utterances_nb_mode():
sample_rate = 8000
silent_ref = np.zeros(sample_rate)
deg = np.random.randn(sample_rate)
with pytest.raises(NoUtterancesError) as e:
pesq(ref=silent_ref, deg=deg, fs=sample_rate, mode='nb')
score = pesq(ref=silent_ref, deg=deg, fs=sample_rate, mode='nb',
on_error=PesqError.RETURN_VALUES)
assert score == PesqError.NO_UTTERANCES_DETECTED, score
print(f'No Utterance NB score: {score}, was: {PesqError.NO_UTTERANCES_DETECTED}')
return score
def test_no_utterances_wb_mode():
sample_rate = 16000
silent_ref = np.zeros(sample_rate)
deg = np.random.randn(sample_rate)
with pytest.raises(NoUtterancesError) as e:
pesq(ref=silent_ref, deg=deg, fs=sample_rate, mode='wb')
score = pesq(ref=silent_ref, deg=deg, fs=sample_rate, mode='wb',
on_error=PesqError.RETURN_VALUES)
assert score == PesqError.NO_UTTERANCES_DETECTED, score
print(f'No Utterance WB score: {score}, was: {PesqError.NO_UTTERANCES_DETECTED}')
return score
def test_pesq_batch():
data_dir = Path(__file__).parent.parent / 'audio'
ref_path = data_dir / 'speech.wav'
deg_path = data_dir / 'speech_bab_0dB.wav'
sample_rate, ref = scipy.io.wavfile.read(ref_path)
sample_rate, deg = scipy.io.wavfile.read(deg_path)
n_file = 10
ideally = np.array([1.5128041505813599 for i in range(n_file)])
# 1D - 1D
score = pesq_batch(ref=ref, deg=deg, fs=sample_rate, mode='wb')
#print(f'score: {score}, was: {ideally}')
assert score == [1.5128041505813599], score
# 1D - 2D
deg_2d = np.repeat(deg[np.newaxis, :], n_file, axis=0)
scores = pesq_batch(ref=ref, deg=deg_2d, fs=sample_rate, mode='wb')
assert np.allclose(np.array(scores), ideally), scores
# 2D - 2D
ref_2d = np.repeat(ref[np.newaxis, :], n_file, axis=0)
scores = pesq_batch(ref=ref_2d, deg=deg_2d, fs=sample_rate, mode='wb')
assert np.allclose(np.array(scores), ideally), scores
# narrowband
score = pesq_batch(ref=ref, deg=deg, fs=sample_rate, mode='nb')
assert score == [1.6072081327438354], score
# 1D - 2D multiprocessing
deg_2d = np.repeat(deg[np.newaxis, :], n_file, axis=0)
scores = pesq_batch(ref=ref, deg=deg_2d, fs=sample_rate, mode='wb', n_processor=4)
assert np.allclose(np.array(scores), ideally), scores
# 2D - 2D multiprocessing
ref_2d = np.repeat(ref[np.newaxis, :], n_file, axis=0)
scores = pesq_batch(ref=ref_2d, deg=deg_2d, fs=sample_rate, mode='wb', n_processor=4)
assert np.allclose(np.array(scores), ideally), scores
# def test_time_efficiency():
# data_dir = Path(__file__).parent.parent / 'audio'
# ref_path = data_dir / 'speech.wav'
# deg_path = data_dir / 'speech_bab_0dB.wav'
#
# sample_rate, ref = scipy.io.wavfile.read(ref_path)
# sample_rate, deg = scipy.io.wavfile.read(deg_path)
# import time
# nums = [100, 1000, 10000]
# durations = []
# n_processors = 8
# degs = [np.repeat(deg[np.newaxis, :], n, axis=0) for n in nums]
# for d, n in zip(degs, nums):
# start = time.time()
# pesq_batch(ref=ref, deg=d, fs=sample_rate, mode='wb', n_processor=n_processors)
# end = time.time()
# durations.append(end - start)
# print(durations)
# # [5.192636251449585, 30.032038688659668, 294.47159910202026]
if __name__ == "__main__":
test()
test_no_utterances_nb_mode()
test_no_utterances_wb_mode()
test_pesq_batch()