• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Copyright 2021 Huawei Technologies Co., Ltd
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7# http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14# ==============================================================================
15"""
16Testing EqualizerBiquad op in DE
17"""
18import numpy as np
19import pytest
20
21import mindspore.dataset as ds
22import mindspore.dataset.audio.transforms as audio
23from mindspore import log as logger
24
25
26def count_unequal_element(data_expected, data_me, rtol, atol):
27    assert data_expected.shape == data_me.shape
28    total_count = len(data_expected.flatten())
29    error = np.abs(data_expected - data_me)
30    greater = np.greater(error, atol + np.abs(data_expected) * rtol)
31    loss_count = np.count_nonzero(greater)
32    assert (loss_count / total_count) < rtol, "\ndata_expected_std:{0}\ndata_me_error:{1}\nloss:{2}".format(
33        data_expected[greater], data_me[greater], error[greater])
34
35
36def test_equalizer_biquad_eager():
37    """ mindspore eager mode normal testcase:highpass_biquad op"""
38    # Original waveform
39    waveform = np.array([[0.8236, 0.2049, 0.3335], [0.5933, 0.9911, 0.2482],
40                         [0.3007, 0.9054, 0.7598], [0.5394, 0.2842, 0.5634], [0.6363, 0.2226, 0.2288]])
41    # Expect waveform
42    expect_waveform = np.array([[1.0000, 0.2532, 0.1273], [0.7333, 1.0000, 0.1015],
43                                [0.3717, 1.0000, 0.8351], [0.6667, 0.3513, 0.5098], [0.7864, 0.2751, 0.0627]])
44    equalizer_biquad_op = audio.EqualizerBiquad(4000, 1000.0, 5.5, 1)
45    # Filtered waveform by highpass_biquad
46    output = equalizer_biquad_op(waveform)
47    count_unequal_element(expect_waveform, output, 0.0001, 0.0001)
48
49
50def test_equalizer_biquad_pipeline():
51    """ mindspore pipeline mode normal testcase:highpass_biquad op"""
52    # Original waveform
53    waveform = np.array([[0.4063, 0.7729, 0.2325], [0.2687, 0.1426, 0.8987],
54                         [0.6914, 0.6681, 0.1783], [0.2704, 0.2680, 0.7975], [0.5880, 0.1776, 0.6323]])
55    # Expect waveform
56    expect_waveform = np.array([[0.5022, 0.9553, 0.1468], [0.3321, 0.1762, 1.0000],
57                                [0.8545, 0.8257, -0.0188], [0.3342, 0.3312, 0.8921], [0.7267, 0.2195, 0.5781]])
58    dataset = ds.NumpySlicesDataset(waveform, ["col1"], shuffle=False)
59    equalizer_biquad_op = audio.EqualizerBiquad(4000, 1000.0, 5.5, 1)
60    # Filtered waveform by equalizer_biquad
61    dataset = dataset.map(input_columns=["col1"], operations=equalizer_biquad_op, num_parallel_workers=4)
62    i = 0
63    for item in dataset.create_dict_iterator(output_numpy=True):
64        count_unequal_element(expect_waveform[i, :],
65                              item["col1"], 0.0001, 0.0001)
66        i += 1
67
68
69def test_equalizer_biquad_invalid_input():
70    """
71    Test invalid input of HighpassBiquad
72    """
73    def test_invalid_input(test_name, sample_rate, center_freq, gain, Q, error, error_msg):
74        logger.info("Test EqualizerBiquad with bad input: {0}".format(test_name))
75        with pytest.raises(error) as error_info:
76            audio.EqualizerBiquad(sample_rate, center_freq, gain, Q)
77        assert error_msg in str(error_info.value)
78
79    test_invalid_input("invalid sample_rate parameter type as a float", 44100.5, 1000, 5.5, 0.707, TypeError,
80                       "Argument sample_rate with value 44100.5 is not of type [<class 'int'>],"
81                       " but got <class 'float'>.")
82    test_invalid_input("invalid sample_rate parameter type as a String", "44100", 1000, 5.5, 0.707, TypeError,
83                       "Argument sample_rate with value 44100 is not of type [<class 'int'>],"
84                       " but got <class 'str'>.")
85    test_invalid_input("invalid central_freq parameter type as a String", 44100, "1000", 5.5, 0.707, TypeError,
86                       "Argument central_freq with value 1000 is not of type [<class 'float'>, <class 'int'>],"
87                       " but got <class 'str'>.")
88    test_invalid_input("invalid gain parameter type as a String", 44100, 1000, "5.5", 0.707, TypeError,
89                       "Argument gain with value 5.5 is not of type [<class 'float'>, <class 'int'>],"
90                       " but got <class 'str'>.")
91    test_invalid_input("invalid Q parameter type as a String", 44100, 1000, 5.5, "0.707", TypeError,
92                       "Argument Q with value 0.707 is not of type [<class 'float'>, <class 'int'>],"
93                       " but got <class 'str'>.")
94
95    test_invalid_input("invalid sample_rate parameter value", 441324343243242342345300, 1000, 5.5, 0.707, ValueError,
96                       "Input sample_rate is not within the required interval of [-2147483648, 0) and (0, 2147483647].")
97    test_invalid_input("invalid central_freq parameter value", 44100, 3243432434, 5.5, 0.707, ValueError,
98                       "Input central_freq is not within the required interval of [-16777216, 16777216].")
99    test_invalid_input("invalid sample_rate parameter value", 0, 1000, 5.5, 0.707, ValueError,
100                       "Input sample_rate is not within the required interval of [-2147483648, 0) and (0, 2147483647].")
101    test_invalid_input("invalid Q parameter value", 44100, 1000, 5.5, 0, ValueError,
102                       "Input Q is not within the required interval of (0, 1].")
103
104    test_invalid_input("invalid sample_rate parameter value", None, 1000, 5.5, 0.707, TypeError,
105                       "Argument sample_rate with value None is not of type [<class 'int'>], "
106                       "but got <class 'NoneType'>.")
107    test_invalid_input("invalid central_freq parameter value", 44100, None, 5.5, 0.707, TypeError,
108                       "Argument central_freq with value None is not of type [<class 'float'>, <class 'int'>],"
109                       " but got <class 'NoneType'>.")
110    test_invalid_input("invalid gain parameter value", 44100, 200, None, 0.707, TypeError,
111                       "Argument gain with value None is not of type [<class 'float'>, <class 'int'>], "
112                       "but got <class 'NoneType'>.")
113
114
115if __name__ == "__main__":
116    test_equalizer_biquad_eager()
117    test_equalizer_biquad_pipeline()
118    test_equalizer_biquad_invalid_input()
119