diff --git a/include/SPTK/utils/sptk_utils.h b/include/SPTK/utils/sptk_utils.h index 4da98b59..31659818 100644 --- a/include/SPTK/utils/sptk_utils.h +++ b/include/SPTK/utils/sptk_utils.h @@ -95,6 +95,7 @@ bool IsInRange(int num, int min, int max); bool IsInRange(double num, double min, double max); bool IsPowerOfTwo(int num); bool IsValidAlpha(double alpha); +bool IsValidGamma(double gamma); int ExtractSign(double x); double FloorLog(double x); double FloorLog2(double x); diff --git a/src/converter/linear_predictive_coefficients_to_parcor_coefficients.cc b/src/converter/linear_predictive_coefficients_to_parcor_coefficients.cc index 70bda087..4dd1ddb6 100644 --- a/src/converter/linear_predictive_coefficients_to_parcor_coefficients.cc +++ b/src/converter/linear_predictive_coefficients_to_parcor_coefficients.cc @@ -55,7 +55,7 @@ LinearPredictiveCoefficientsToParcorCoefficients:: LinearPredictiveCoefficientsToParcorCoefficients(int num_order, double gamma) : num_order_(num_order), gamma_(gamma), is_valid_(true) { - if (num_order_ < 0) { + if (num_order_ < 0 || !sptk::IsValidGamma(gamma_)) { is_valid_ = false; } } @@ -109,7 +109,6 @@ bool LinearPredictiveCoefficientsToParcorCoefficients::Run( linear_predictive_coefficients.end(), buffer->a_.begin() + 1, std::bind1st(std::multiplies(), gamma_)); } - double* a(&buffer->a_[0]); for (int i(num_order_); 1 <= i; --i) { for (int j(1); j <= i; ++j) { diff --git a/src/converter/mel_generalized_cepstrum_to_mel_generalized_cepstrum.cc b/src/converter/mel_generalized_cepstrum_to_mel_generalized_cepstrum.cc index 2523aa3a..bfe2aced 100644 --- a/src/converter/mel_generalized_cepstrum_to_mel_generalized_cepstrum.cc +++ b/src/converter/mel_generalized_cepstrum_to_mel_generalized_cepstrum.cc @@ -270,7 +270,9 @@ MelGeneralizedCepstrumToMelGeneralizedCepstrum:: alpha_transform_(0.0), is_valid_(true) { if (num_input_order_ < 0 || !sptk::IsValidAlpha(input_alpha_) || - num_output_order_ < 0 || !sptk::IsValidAlpha(output_alpha_) || + !sptk::IsValidGamma(input_gamma_) || num_output_order_ < 0 || + !sptk::IsValidAlpha(output_alpha_) || + !sptk::IsValidGamma(output_gamma_) || (is_multiplied_input_ && 0.0 == input_gamma_)) { is_valid_ = false; return; diff --git a/src/converter/mel_generalized_line_spectral_pairs_to_spectrum.cc b/src/converter/mel_generalized_line_spectral_pairs_to_spectrum.cc index c2c515c4..8a2fecad 100644 --- a/src/converter/mel_generalized_line_spectral_pairs_to_spectrum.cc +++ b/src/converter/mel_generalized_line_spectral_pairs_to_spectrum.cc @@ -58,8 +58,8 @@ MelGeneralizedLineSpectralPairsToSpectrum:: gamma_(gamma), num_output_order_(num_output_order), is_valid_(true) { - if (num_input_order_ < 0 || num_output_order_ < 0 || gamma_ < -1.0 || - 0.0 <= gamma_) { + if (num_input_order_ < 0 || !sptk::IsValidAlpha(alpha_) || + !sptk::IsValidGamma(gamma_) || num_output_order_ < 0) { is_valid_ = false; } } diff --git a/src/filter/inverse_mglsa_digital_filter.cc b/src/filter/inverse_mglsa_digital_filter.cc index 8c4e8bbb..b5826a12 100644 --- a/src/filter/inverse_mglsa_digital_filter.cc +++ b/src/filter/inverse_mglsa_digital_filter.cc @@ -63,8 +63,9 @@ InverseMglsaDigitalFilter::InverseMglsaDigitalFilter(int num_filter_order, mlsa_digital_filter_(num_filter_order_, num_pade_order, alpha_, transposition_), is_valid_(true) { - if (num_filter_order_ < 0 || num_stage_ < 0) { + if (num_filter_order_ < 0 || num_stage_ < 0 || !sptk::IsValidAlpha(alpha_)) { is_valid_ = false; + return; } if (0 == num_stage && !mlsa_digital_filter_.IsValid()) { is_valid_ = false; diff --git a/src/main/gnorm.cc b/src/main/gnorm.cc index faf3edb1..134b27a5 100644 --- a/src/main/gnorm.cc +++ b/src/main/gnorm.cc @@ -65,9 +65,9 @@ void PrintUsage(std::ostream* stream) { *stream << " usage:" << std::endl; *stream << " gnorm [ options ] [ infile ] > stdout" << std::endl; *stream << " options:" << std::endl; - *stream << " -m m : order of generalized cepstrum ( int)[" << std::setw(5) << std::right << kDefaultNumOrder << "][ 0 <= m <= ]" << std::endl; // NOLINT - *stream << " -g g : gamma (double)[" << std::setw(5) << std::right << kDefaultGamma << "][ <= g <= ]" << std::endl; // NOLINT - *stream << " -c c : gamma = -1 / c ( int)[" << std::setw(5) << std::right << "N/A" << "][ 1 <= c <= ]" << std::endl; // NOLINT + *stream << " -m m : order of generalized cepstrum ( int)[" << std::setw(5) << std::right << kDefaultNumOrder << "][ 0 <= m <= ]" << std::endl; // NOLINT + *stream << " -g g : gamma (double)[" << std::setw(5) << std::right << kDefaultGamma << "][ -1.0 <= g <= 1.0 ]" << std::endl; // NOLINT + *stream << " -c c : gamma = -1 / c ( int)[" << std::setw(5) << std::right << "N/A" << "][ 1 <= c <= ]" << std::endl; // NOLINT *stream << " -h : print this message" << std::endl; *stream << " infile:" << std::endl; *stream << " generalized cepstrum (double)[stdin]" << std::endl; // NOLINT @@ -102,9 +102,11 @@ int main(int argc, char* argv[]) { break; } case 'g': { - if (!sptk::ConvertStringToDouble(optarg, &gamma)) { + if (!sptk::ConvertStringToDouble(optarg, &gamma) || + !sptk::IsValidGamma(gamma)) { std::ostringstream error_message; - error_message << "The argument for the -g option must be numeric"; + error_message + << "The argument for the -g option must be in [-1.0, 1.0]"; sptk::PrintErrorMessage("gnorm", error_message); return 1; } diff --git a/src/main/ignorm.cc b/src/main/ignorm.cc index 5f68c23b..7e5e91a6 100644 --- a/src/main/ignorm.cc +++ b/src/main/ignorm.cc @@ -65,9 +65,9 @@ void PrintUsage(std::ostream* stream) { *stream << " usage:" << std::endl; *stream << " ignorm [ options ] [ infile ] > stdout" << std::endl; *stream << " options:" << std::endl; - *stream << " -m m : order of generalized cepstrum ( int)[" << std::setw(5) << std::right << kDefaultNumOrder << "][ 0 <= m <= ]" << std::endl; // NOLINT - *stream << " -g g : gamma (double)[" << std::setw(5) << std::right << kDefaultGamma << "][ <= g <= ]" << std::endl; // NOLINT - *stream << " -c c : gamma = -1 / c ( int)[" << std::setw(5) << std::right << "N/A" << "][ 1 <= c <= ]" << std::endl; // NOLINT + *stream << " -m m : order of generalized cepstrum ( int)[" << std::setw(5) << std::right << kDefaultNumOrder << "][ 0 <= m <= ]" << std::endl; // NOLINT + *stream << " -g g : gamma (double)[" << std::setw(5) << std::right << kDefaultGamma << "][ -1.0 <= g <= 1.0 ]" << std::endl; // NOLINT + *stream << " -c c : gamma = -1 / c ( int)[" << std::setw(5) << std::right << "N/A" << "][ 1 <= c <= ]" << std::endl; // NOLINT *stream << " -h : print this message" << std::endl; *stream << " infile:" << std::endl; *stream << " normalized generalized cepstrum (double)[stdin]" << std::endl; // NOLINT @@ -102,9 +102,11 @@ int main(int argc, char* argv[]) { break; } case 'g': { - if (!sptk::ConvertStringToDouble(optarg, &gamma)) { + if (!sptk::ConvertStringToDouble(optarg, &gamma) || + !sptk::IsValidGamma(gamma)) { std::ostringstream error_message; - error_message << "The argument for the -g option must be numeric"; + error_message + << "The argument for the -g option must be in [-1.0, 1.0]"; sptk::PrintErrorMessage("ignorm", error_message); return 1; } diff --git a/src/main/lpc2par.cc b/src/main/lpc2par.cc index 066bbd14..a46705c8 100644 --- a/src/main/lpc2par.cc +++ b/src/main/lpc2par.cc @@ -68,10 +68,10 @@ void PrintUsage(std::ostream* stream) { *stream << " usage:" << std::endl; *stream << " lpc2par [ options ] [ infile ] > stdout" << std::endl; *stream << " options:" << std::endl; - *stream << " -m m : order of linear predictive coefficients ( int)[" << std::setw(5) << std::right << kDefaultNumOrder << "][ 0 <= m <= ]" << std::endl; // NOLINT - *stream << " -g g : gamma of generalized cepstrum (double)[" << std::setw(5) << std::right << kDefaultGamma << "][ <= g <= ]" << std::endl; // NOLINT - *stream << " -c c : gamma of generalized cepstrum = -1 / c ( int)[" << std::setw(5) << std::right << "N/A" << "][ 1 <= c <= ]" << std::endl; // NOLINT - *stream << " -e e : warning type of unstable index ( int)[" << std::setw(5) << std::right << kDefaultWarningType << "][ 0 <= e <= 2 ]" << std::endl; // NOLINT + *stream << " -m m : order of linear predictive coefficients ( int)[" << std::setw(5) << std::right << kDefaultNumOrder << "][ 0 <= m <= ]" << std::endl; // NOLINT + *stream << " -g g : gamma of generalized cepstrum (double)[" << std::setw(5) << std::right << kDefaultGamma << "][ -1.0 <= g <= 1.0 ]" << std::endl; // NOLINT + *stream << " -c c : gamma of generalized cepstrum = -1 / c ( int)[" << std::setw(5) << std::right << "N/A" << "][ 1 <= c <= ]" << std::endl; // NOLINT + *stream << " -e e : warning type of unstable index ( int)[" << std::setw(5) << std::right << kDefaultWarningType << "][ 0 <= e <= 2 ]" << std::endl; // NOLINT *stream << " 0 (no warning)" << std::endl; *stream << " 1 (output the index to stderr)" << std::endl; *stream << " 2 (output the index to stderr" << std::endl; @@ -111,9 +111,11 @@ int main(int argc, char* argv[]) { break; } case 'g': { - if (!sptk::ConvertStringToDouble(optarg, &gamma)) { + if (!sptk::ConvertStringToDouble(optarg, &gamma) || + !sptk::IsValidGamma(gamma)) { std::ostringstream error_message; - error_message << "The argument for the -g option must be numeric"; + error_message + << "The argument for the -g option must be in [-1.0, 1.0]"; sptk::PrintErrorMessage("lpc2par", error_message); return 1; } diff --git a/src/main/mgc2mgc.cc b/src/main/mgc2mgc.cc index 34f7049f..f6eed176 100644 --- a/src/main/mgc2mgc.cc +++ b/src/main/mgc2mgc.cc @@ -75,13 +75,13 @@ void PrintUsage(std::ostream* stream) { *stream << " options:" << std::endl; *stream << " -m m : order of mel-generalized cepstrum (input) ( int)[" << std::setw(5) << std::right << kDefaultInputNumOrder << "][ 0 <= m <= ]" << std::endl; // NOLINT *stream << " -a a : alpha of mel-generalized cepstrum (input) (double)[" << std::setw(5) << std::right << kDefaultInputAlpha << "][ -1.0 < a < 1.0 ]" << std::endl; // NOLINT - *stream << " -g g : gamma of mel-generalized cepstrum (input) (double)[" << std::setw(5) << std::right << kDefaultInputGamma << "][ <= g <= ]" << std::endl; // NOLINT + *stream << " -g g : gamma of mel-generalized cepstrum (input) (double)[" << std::setw(5) << std::right << kDefaultInputGamma << "][ -1.0 <= g <= 1.0 ]" << std::endl; // NOLINT *stream << " -c c : gamma of mel-generalized cepstrum = -1 / c (input) ( int)[" << std::setw(5) << std::right << "N/A" << "][ 1 <= c <= ]" << std::endl; // NOLINT *stream << " -n : regard input as normalized mel-generalized cepstrum ( bool)[" << std::setw(5) << std::right << sptk::ConvertBooleanToString(kDefaultInputNormalizationFlag) << "]" << std::endl; // NOLINT *stream << " -u : regard input as multiplied by gamma ( bool)[" << std::setw(5) << std::right << sptk::ConvertBooleanToString(kDefaultInputMultiplicationFlag) << "]" << std::endl; // NOLINT *stream << " -M M : order of mel-generalized cepstrum (output) ( int)[" << std::setw(5) << std::right << kDefaultOutputNumOrder << "][ 0 <= M <= ]" << std::endl; // NOLINT *stream << " -A A : alpha of mel-generalized cepstrum (output) (double)[" << std::setw(5) << std::right << kDefaultOutputAlpha << "][ -1.0 < A < 1.0 ]" << std::endl; // NOLINT - *stream << " -G G : gamma of mel-generalized cepstrum (output) (double)[" << std::setw(5) << std::right << kDefaultOutputGamma << "][ <= G <= ]" << std::endl; // NOLINT + *stream << " -G G : gamma of mel-generalized cepstrum (output) (double)[" << std::setw(5) << std::right << kDefaultOutputGamma << "][ -1.0 <= G <= 1.0 ]" << std::endl; // NOLINT *stream << " -C C : gamma of mel-generalized cepstrum = -1 / C (output) ( int)[" << std::setw(5) << std::right << "N/A" << "][ 1 <= C <= ]" << std::endl; // NOLINT *stream << " -N : regard output as normalized mel-generalized cepstrum ( bool)[" << std::setw(5) << std::right << sptk::ConvertBooleanToString(kDefaultOutputNormalizationFlag) << "]" << std::endl; // NOLINT *stream << " -U : regard output as multiplied by gamma ( bool)[" << std::setw(5) << std::right << sptk::ConvertBooleanToString(kDefaultOutputMultiplicationFlag) << "]" << std::endl; // NOLINT @@ -142,9 +142,11 @@ int main(int argc, char* argv[]) { break; } case 'g': { - if (!sptk::ConvertStringToDouble(optarg, &input_gamma)) { + if (!sptk::ConvertStringToDouble(optarg, &input_gamma) || + !sptk::IsValidGamma(input_gamma)) { std::ostringstream error_message; - error_message << "The argument for the -g option must be numeric"; + error_message + << "The argument for the -g option must be in [-1.0, 1.0]"; sptk::PrintErrorMessage("mgc2mgc", error_message); return 1; } @@ -193,9 +195,11 @@ int main(int argc, char* argv[]) { break; } case 'G': { - if (!sptk::ConvertStringToDouble(optarg, &output_gamma)) { + if (!sptk::ConvertStringToDouble(optarg, &output_gamma) || + !sptk::IsValidGamma(output_gamma)) { std::ostringstream error_message; - error_message << "The argument for the -G option must be numeric"; + error_message + << "The argument for the -G option must be in [-1.0, 1.0]"; sptk::PrintErrorMessage("mgc2mgc", error_message); return 1; } diff --git a/src/main/mglsp2sp.cc b/src/main/mglsp2sp.cc index a1629505..9a91807d 100644 --- a/src/main/mglsp2sp.cc +++ b/src/main/mglsp2sp.cc @@ -99,7 +99,7 @@ void PrintUsage(std::ostream* stream) { *stream << " options:" << std::endl; *stream << " -m m : order of mel-generalized line spectral pairs ( int)[" << std::setw(5) << std::right << kDefaultNumOrder << "][ 0 <= m <= ]" << std::endl; // NOLINT *stream << " -a a : alpha of mel-generalized line spectral pairs (double)[" << std::setw(5) << std::right << kDefaultAlpha << "][ -1.0 < a < 1.0 ]" << std::endl; // NOLINT - *stream << " -g g : gamma of mel-generalized line spectral pairs (double)[" << std::setw(5) << std::right << kDefaultGamma << "][ -1.0 <= g <= 0.0 ]" << std::endl; // NOLINT + *stream << " -g g : gamma of mel-generalized line spectral pairs (double)[" << std::setw(5) << std::right << kDefaultGamma << "][ -1.0 <= g <= 1.0 ]" << std::endl; // NOLINT *stream << " -c c : gamma of mel-generalized line spectral pairs = -1 / c ( int)[" << std::setw(5) << std::right << "N/A" << "][ 1 <= c <= ]" << std::endl; // NOLINT *stream << " -l l : spectrum legnth ( int)[" << std::setw(5) << std::right << kDefaultSpectrumLength << "][ 0 < l <= ]" << std::endl; // NOLINT *stream << " -s s : sampling frequency (double)[" << std::setw(5) << std::right << kDefaultSamplingFrequency << "][ 0.0 < s <= ]" << std::endl; // NOLINT @@ -171,13 +171,11 @@ int main(int argc, char* argv[]) { break; } case 'g': { - const double min(-1.0); - const double max(0.0); - if (!sptk::ConvertStringToDouble(optarg, &gamma) || gamma < min || - max <= gamma) { + if (!sptk::ConvertStringToDouble(optarg, &gamma) || + !sptk::IsValidGamma(gamma)) { std::ostringstream error_message; - error_message << "The argument for the -g option must be numeric " - << "in the range of " << min << " to " << max; + error_message + << "The argument for the -g option must be in [-1.0, 1.0]"; sptk::PrintErrorMessage("mglsp2sp", error_message); return 1; } diff --git a/src/normalizer/generalized_cepstrum_gain_normalization.cc b/src/normalizer/generalized_cepstrum_gain_normalization.cc index 9edb0a5d..5f79f45d 100644 --- a/src/normalizer/generalized_cepstrum_gain_normalization.cc +++ b/src/normalizer/generalized_cepstrum_gain_normalization.cc @@ -44,8 +44,8 @@ #include "SPTK/normalizer/generalized_cepstrum_gain_normalization.h" -#include // std::copy -#include // std::pow, std::exp +#include // std::copy, std::transform +#include // std::exp, std::pow #include // std::size_t namespace sptk { @@ -53,7 +53,7 @@ namespace sptk { GeneralizedCepstrumGainNormalization::GeneralizedCepstrumGainNormalization( int num_order, double gamma) : num_order_(num_order), gamma_(gamma), is_valid_(true) { - if (num_order_ < 0) { + if (num_order_ < 0 || !sptk::IsValidGamma(gamma_)) { is_valid_ = false; } } @@ -62,7 +62,7 @@ bool GeneralizedCepstrumGainNormalization::Run( const std::vector& generalized_cepstrum, std::vector* normalized_generalized_cepstrum) const { if (!is_valid_ || - static_cast(num_order_ + 1) != generalized_cepstrum.size() || + generalized_cepstrum.size() != static_cast(num_order_ + 1) || NULL == normalized_generalized_cepstrum) { return false; } @@ -72,18 +72,16 @@ bool GeneralizedCepstrumGainNormalization::Run( normalized_generalized_cepstrum->resize(num_order_ + 1); } - if (0.0 != gamma_) { - const double* c1(&generalized_cepstrum[0]); - double* c2(&(*normalized_generalized_cepstrum)[0]); - const double k(1.0 + gamma_ * c1[0]); - for (int m(num_order_); 1 <= m; --m) { - c2[m] = c1[m] / k; - } - c2[0] = std::pow(k, 1.0 / gamma_); - } else { + if (0.0 == gamma_) { + (*normalized_generalized_cepstrum)[0] = std::exp(generalized_cepstrum[0]); std::copy(generalized_cepstrum.begin() + 1, generalized_cepstrum.end(), normalized_generalized_cepstrum->begin() + 1); - (*normalized_generalized_cepstrum)[0] = std::exp(generalized_cepstrum[0]); + } else { + const double k(1.0 + gamma_ * generalized_cepstrum[0]); + (*normalized_generalized_cepstrum)[0] = std::pow(k, 1.0 / gamma_); + std::transform(generalized_cepstrum.begin() + 1, generalized_cepstrum.end(), + normalized_generalized_cepstrum->begin() + 1, + [k](double x) { return x / k; }); } return true; diff --git a/src/normalizer/generalized_cepstrum_inverse_gain_normalization.cc b/src/normalizer/generalized_cepstrum_inverse_gain_normalization.cc index b0613a21..3b0618ad 100644 --- a/src/normalizer/generalized_cepstrum_inverse_gain_normalization.cc +++ b/src/normalizer/generalized_cepstrum_inverse_gain_normalization.cc @@ -44,8 +44,8 @@ #include "SPTK/normalizer/generalized_cepstrum_inverse_gain_normalization.h" -#include // std::copy -#include // std::pow, std::log +#include // std::copy, std::transform +#include // std::log, std::pow #include // std::size_t namespace sptk { @@ -53,7 +53,7 @@ namespace sptk { GeneralizedCepstrumInverseGainNormalization:: GeneralizedCepstrumInverseGainNormalization(int num_order, double gamma) : num_order_(num_order), gamma_(gamma), is_valid_(true) { - if (num_order_ < 0) { + if (num_order_ < 0 || !sptk::IsValidGamma(gamma_)) { is_valid_ = false; } } @@ -62,8 +62,8 @@ bool GeneralizedCepstrumInverseGainNormalization::Run( const std::vector& normalized_generalized_cepstrum, std::vector* generalized_cepstrum) const { if (!is_valid_ || - static_cast(num_order_ + 1) != - normalized_generalized_cepstrum.size() || + normalized_generalized_cepstrum.size() != + static_cast(num_order_ + 1) || NULL == generalized_cepstrum) { return false; } @@ -73,19 +73,18 @@ bool GeneralizedCepstrumInverseGainNormalization::Run( generalized_cepstrum->resize(num_order_ + 1); } - if (0.0 != gamma_) { - const double* c1(&normalized_generalized_cepstrum[0]); - double* c2(&(*generalized_cepstrum)[0]); - const double k(std::pow(c1[0], gamma_)); - for (int m(num_order_); 1 <= m; --m) { - c2[m] = k * c1[m]; - } - c2[0] = (k - 1.0) / gamma_; - } else { + if (0.0 == gamma_) { + (*generalized_cepstrum)[0] = std::log(normalized_generalized_cepstrum[0]); std::copy(normalized_generalized_cepstrum.begin() + 1, normalized_generalized_cepstrum.end(), generalized_cepstrum->begin() + 1); - (*generalized_cepstrum)[0] = std::log(normalized_generalized_cepstrum[0]); + } else { + const double k(std::pow(normalized_generalized_cepstrum[0], gamma_)); + (*generalized_cepstrum)[0] = (k - 1.0) / gamma_; + std::transform(normalized_generalized_cepstrum.begin() + 1, + normalized_generalized_cepstrum.end(), + generalized_cepstrum->begin() + 1, + [k](double x) { return x * k; }); } return true; diff --git a/src/utils/sptk_utils.cc b/src/utils/sptk_utils.cc index a84bc520..b48cbb99 100644 --- a/src/utils/sptk_utils.cc +++ b/src/utils/sptk_utils.cc @@ -373,6 +373,10 @@ bool IsValidAlpha(double alpha) { return (std::fabs(alpha) < 1.0); } +bool IsValidGamma(double gamma) { + return (std::fabs(gamma) <= 1.0); +} + int ExtractSign(double x) { if (0.0 < x) return 1; if (x < 0.0) return -1;