!46185 fix bug of operator LFCC

Merge pull request !46185 from 杨旭华/LFCC
This commit is contained in:
i-robot 2022-11-30 04:00:23 +00:00 committed by Gitee
commit c05a7d37bd
No known key found for this signature in database
GPG Key ID: 173E9B9CA92EEF8F
3 changed files with 68 additions and 49 deletions

View File

@ -56,7 +56,9 @@ Status LFCCOperation::ValidateParams() {
CHECK_FAIL_RETURN_UNEXPECTED(dct_type_ == TWO,
"LFCC: dct_type must be equal to 2, but got: " + std::to_string(dct_type_));
RETURN_IF_NOT_OK(ValidateFloatScalarNonNegative("LFCC", "f_max", f_max_));
CHECK_FAIL_RETURN_UNEXPECTED(n_lfcc_ < n_fft_, "LFCC: Cannot select more LFCC coefficients than # fft bins.");
CHECK_FAIL_RETURN_UNEXPECTED(n_lfcc_ <= n_fft_,
"LFCC: n_fft should be greater than or equal to n_lfcc, but got n_lfcc: " +
std::to_string(n_lfcc_) + " and n_fft: " + std::to_string(n_fft_));
// Spectrogram params
RETURN_IF_NOT_OK(ValidateIntScalarPositive("LFCC", "n_fft", n_fft_));
@ -64,13 +66,16 @@ Status LFCCOperation::ValidateParams() {
RETURN_IF_NOT_OK(ValidateIntScalarNonNegative("LFCC", "hop_length", hop_length_));
RETURN_IF_NOT_OK(ValidateIntScalarNonNegative("LFCC", "pad", pad_));
RETURN_IF_NOT_OK(ValidateFloatScalarNonNegative("LFCC", "power", power_));
CHECK_FAIL_RETURN_UNEXPECTED(pad_mode_ != BorderType::kEdge, "LFCC: pad_mode can only be Reflect or Constant.");
CHECK_FAIL_RETURN_UNEXPECTED(pad_mode_ != BorderType::kEdge, "LFCC: invalid BorderType, kEdge is not supported.");
if (f_max_ != 0) {
RETURN_IF_NOT_OK(ValidateFloatScalarNonNegative("LFCC", "f_max", f_max_));
CHECK_FAIL_RETURN_UNEXPECTED(f_min_ <= f_max_, "LFCC: f_max must be greater than or equal to f_min.");
CHECK_FAIL_RETURN_UNEXPECTED(
f_min_ <= f_max_, "LFCC: f_max must be greater than or equal to f_min, but got f_max: " + std::to_string(f_max_) +
" and f_min: " + std::to_string(f_min_));
} else {
CHECK_FAIL_RETURN_UNEXPECTED(f_min_ < (sample_rate_ / TWO),
"LFCC: f_min must be less than half of sample_rate when f_max is 0.");
"LFCC: f_min must be less than half of sample_rate when f_max is 0, but got f_min: " +
std::to_string(f_min_) + " and sample_rate: " + std::to_string(sample_rate_));
}
CHECK_FAIL_RETURN_UNEXPECTED(win_length_ <= n_fft_,
"LFCC: win_length must be less than or equal to n_fft, but got win_length: " +

View File

@ -960,7 +960,7 @@ def check_lfcc(method):
type_check(sample_rate, (int,), "sample_rate")
check_non_negative_int32(sample_rate, "sample_rate")
type_check(n_filter, (int,), "n_filter")
check_value(n_filter, [1, 2147483645], "n_filter")
check_pos_int32(n_filter, "n_filter")
type_check(n_lfcc, (int,), "n_lfcc")
check_pos_int32(n_lfcc, "n_lfcc")
type_check(log_lf, (bool,), "log_lf")
@ -971,20 +971,38 @@ def check_lfcc(method):
type_check(f_max, (int, float), "f_max")
check_non_negative_float32(f_max, "f_max")
if f_min > f_max:
raise ValueError("LFCC: f_max should be greater than or equal to f_min.")
raise ValueError(
"f_max should be greater than or equal to f_min, but got f_min: {0} and f_max: {1}.".format(
f_min, f_max))
else:
if f_min >= sample_rate // 2:
raise ValueError("LFCC: sample_rate // 2 should be greater than f_min when f_max is set to None.")
raise ValueError(
"Input sample_rate // 2 should be greater than f_min when f_max is set to None, but got f_min: {0} "
"and sample_rate: {1}.".format(f_min, sample_rate))
if dct_type != 2:
raise ValueError("dct_type must be 2, but got : {0}.".format(dct_type))
raise ValueError("Input dct_type must be 2, but got : {0}.".format(dct_type))
if speckwargs is not None:
type_check(speckwargs, (dict,), "speckwargs")
window = speckwargs["window"]
pad_mode = speckwargs["pad_mode"]
n_fft = speckwargs["n_fft"]
win_length = speckwargs["win_length"]
pad = speckwargs["pad"]
power = speckwargs["power"]
type_check(window, (WindowType,), "window")
type_check(pad_mode, (BorderType,), "pad_mode")
type_check(pad, (int,), "pad")
check_non_negative_int32(pad, "pad")
type_check(power, (float,), "power")
check_non_negative_float32(power, "power")
if n_fft < n_lfcc:
raise ValueError(
"n_fft should be greater than or equal to n_lfcc, but got n_fft: {0} and n_lfcc: {1}.".format(
n_fft, n_lfcc))
if win_length > n_fft:
raise ValueError(
"win_length must be less than or equal to n_fft, but got win_length: {0} and n_fft: {1}.".format(
win_length, n_fft))
return method(self, *args, **kwargs)
return new_method

View File

@ -100,7 +100,7 @@ def test_lfcc_invalid_input():
_ = audio.LFCC(n_filter=-1)
except ValueError as error:
logger.info("Got an exception in LFCC: {}".format(str(error)))
assert "Input n_filter is not within the required interval of [1, 2147483645]." in str(error)
assert "Input n_filter is not within the required interval of [1, 2147483647]." in str(error)
try:
_ = audio.LFCC(n_filter=1.1)
except TypeError as error:
@ -135,7 +135,7 @@ def test_lfcc_invalid_input():
_ = audio.LFCC(f_min=10000)
except ValueError as error:
logger.info("Got an exception in LFCC: {}".format(str(error)))
assert "sample_rate // 2 should be greater than f_min when f_max is set to None." in str(error)
assert "sample_rate // 2 should be greater than f_min when f_max is set to None" in str(error)
try:
_ = audio.LFCC(f_min=False)
except TypeError as error:
@ -145,7 +145,7 @@ def test_lfcc_invalid_input():
_ = audio.LFCC(f_min=2, f_max=1)
except ValueError as error:
logger.info("Got an exception in LFCC: {}".format(str(error)))
assert "f_max should be greater than or equal to f_min." in str(error)
assert "f_max should be greater than or equal to f_min" in str(error)
try:
_ = audio.LFCC(f_max=False)
except TypeError as error:
@ -171,51 +171,47 @@ def test_lfcc_invalid_input():
logger.info("Got an exception in LFCC: {}".format(str(error)))
assert "Argument pad_mode with value BorderType.REFLECT is not of type [<enum 'BorderType'>]" in str(error)
try:
wav = np.array([[[1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 4, 4, 3, 3, 2, 2, 1, 1, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5]]])
_ = audio.LFCC(speckwargs={"n_fft": 400, "win_length": -1, "hop_length": 8, "pad": 0,
"window": WindowType.HANN, "power": 2.0, "normalized": False, "center": True,
"pad_mode": BorderType.REFLECT, "onesided": True})(wav)
except RuntimeError as error:
logger.info("Got an exception in LFCC: {}".format(str(error)))
assert "'win_length' must be greater than or equal to 0, got: -1" in str(error)
try:
wav = np.array([[[1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 4, 4, 3, 3, 2, 2, 1, 1, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5]]])
_ = audio.LFCC(speckwargs={"n_fft": 400, "win_length": 16, "hop_length": -1, "pad": 0,
"window": WindowType.HANN, "power": 2.0, "normalized": False, "center": True,
"pad_mode": BorderType.REFLECT, "onesided": True})(wav)
except RuntimeError as error:
logger.info("Got an exception in LFCC: {}".format(str(error)))
assert "'hop_length' must be greater than or equal to 0, got: -1" in str(error)
try:
wav = np.array([[[1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 4, 4, 3, 3, 2, 2, 1, 1, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5]]])
_ = audio.LFCC(speckwargs={"n_fft": 400, "win_length": 16, "hop_length": 8, "pad": -1,
"window": WindowType.HANN, "power": 2.0, "normalized": False, "center": True,
"pad_mode": BorderType.REFLECT, "onesided": True})(wav)
except RuntimeError as error:
"pad_mode": BorderType.REFLECT, "onesided": True})
except ValueError as error:
logger.info("Got an exception in LFCC: {}".format(str(error)))
assert "'pad' must be greater than or equal to 0, got: -1" in str(error)
assert "Input pad is not within the required interval of [0, 2147483647]" in str(error)
try:
wav = np.array([[[1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 4, 4, 3, 3, 2, 2, 1, 1, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5]]])
_ = audio.LFCC(speckwargs={"n_fft": 400, "win_length": 16, "hop_length": 8, "pad": 0,
"window": WindowType.HANN, "power": -1, "normalized": False, "center": True,
"pad_mode": BorderType.REFLECT, "onesided": True})(wav)
except RuntimeError as error:
logger.info("Got an exception in LFCC: {}".format(str(error)))
assert "'power' must be greater than or equal to 0.000000, got: -1.000000." in str(error)
try:
wav = np.array([[[1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 4, 4, 3, 3, 2, 2, 1, 1, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5]]])
_ = audio.LFCC(speckwargs={"n_fft": 400, "win_length": 401, "hop_length": 8, "pad": 0,
_ = audio.LFCC(speckwargs={"n_fft": 400, "win_length": 16, "hop_length": 8, "pad": 1.1,
"window": WindowType.HANN, "power": 2.0, "normalized": False, "center": True,
"pad_mode": BorderType.REFLECT, "onesided": True})(wav)
except RuntimeError as error:
"pad_mode": BorderType.REFLECT, "onesided": True})
except TypeError as error:
logger.info("Got an exception in LFCC: {}".format(str(error)))
assert "Argument pad with value 1.1 is not of type [<class 'int'>]" in str(error)
try:
_ = audio.LFCC(speckwargs={"n_fft": 400, "win_length": 16, "hop_length": 8, "pad": 0,
"window": WindowType.HANN, "power": -1.0, "normalized": False, "center": True,
"pad_mode": BorderType.REFLECT, "onesided": True})
except ValueError as error:
logger.info("Got an exception in LFCC: {}".format(str(error)))
assert "Input power is not within the required interval of [0, 16777216]" in str(error)
try:
_ = audio.LFCC(speckwargs={"n_fft": 400, "win_length": 16, "hop_length": 8, "pad": 0,
"window": WindowType.HANN, "power": 2, "normalized": False, "center": True,
"pad_mode": BorderType.REFLECT, "onesided": True})
except TypeError as error:
logger.info("Got an exception in LFCC: {}".format(str(error)))
assert "Argument power with value 2 is not of type [<class 'float'>]" in str(error)
try:
_ = audio.LFCC(speckwargs={"n_fft": 40, "win_length": 41, "hop_length": 8, "pad": 0,
"window": WindowType.HANN, "power": 2.0, "normalized": False, "center": True,
"pad_mode": BorderType.REFLECT, "onesided": True})
except ValueError as error:
logger.info("Got an exception in LFCC: {}".format(str(error)))
assert "win_length must be less than or equal to n_fft" in str(error)
try:
wav = np.array([[[1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 4, 4, 3, 3, 2, 2, 1, 1, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5]]])
_ = audio.LFCC(n_lfcc=401)(wav)
except RuntimeError as error:
_ = audio.LFCC(speckwargs={"n_fft": 16, "win_length": 16, "hop_length": 8, "pad": 0,
"window": WindowType.HANN, "power": 2.0, "normalized": False, "center": True,
"pad_mode": BorderType.REFLECT, "onesided": True})
except ValueError as error:
logger.info("Got an exception in LFCC: {}".format(str(error)))
assert "Cannot select more LFCC coefficients than # fft bins." in str(error)
assert "n_fft should be greater than or equal to n_lfcc" in str(error)
if __name__ == '__main__':