Skip to content

Commit

Permalink
Restrict range of gamma
Browse files Browse the repository at this point in the history
  • Loading branch information
takenori-y committed Jun 20, 2019
1 parent 4a83231 commit 149f2db
Show file tree
Hide file tree
Showing 13 changed files with 76 additions and 64 deletions.
1 change: 1 addition & 0 deletions include/SPTK/utils/sptk_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Expand Down Expand Up @@ -109,7 +109,6 @@ bool LinearPredictiveCoefficientsToParcorCoefficients::Run(
linear_predictive_coefficients.end(), buffer->a_.begin() + 1,
std::bind1st(std::multiplies<double>(), gamma_));
}

double* a(&buffer->a_[0]);
for (int i(num_order_); 1 <= i; --i) {
for (int j(1); j <= i; ++j) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Expand Down
3 changes: 2 additions & 1 deletion src/filter/inverse_mglsa_digital_filter.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
12 changes: 7 additions & 5 deletions src/main/gnorm.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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;
}
Expand Down
12 changes: 7 additions & 5 deletions src/main/ignorm.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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;
}
Expand Down
14 changes: 8 additions & 6 deletions src/main/lpc2par.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
Expand Down
16 changes: 10 additions & 6 deletions src/main/mgc2mgc.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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;
}
Expand Down
12 changes: 5 additions & 7 deletions src/main/mglsp2sp.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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;
}
Expand Down
26 changes: 12 additions & 14 deletions src/normalizer/generalized_cepstrum_gain_normalization.cc
Original file line number Diff line number Diff line change
Expand Up @@ -44,16 +44,16 @@

#include "SPTK/normalizer/generalized_cepstrum_gain_normalization.h"

#include <algorithm> // std::copy
#include <cmath> // std::pow, std::exp
#include <algorithm> // std::copy, std::transform
#include <cmath> // std::exp, std::pow
#include <cstddef> // std::size_t

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;
}
}
Expand All @@ -62,7 +62,7 @@ bool GeneralizedCepstrumGainNormalization::Run(
const std::vector<double>& generalized_cepstrum,
std::vector<double>* normalized_generalized_cepstrum) const {
if (!is_valid_ ||
static_cast<std::size_t>(num_order_ + 1) != generalized_cepstrum.size() ||
generalized_cepstrum.size() != static_cast<std::size_t>(num_order_ + 1) ||
NULL == normalized_generalized_cepstrum) {
return false;
}
Expand All @@ -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;
Expand Down
29 changes: 14 additions & 15 deletions src/normalizer/generalized_cepstrum_inverse_gain_normalization.cc
Original file line number Diff line number Diff line change
Expand Up @@ -44,16 +44,16 @@

#include "SPTK/normalizer/generalized_cepstrum_inverse_gain_normalization.h"

#include <algorithm> // std::copy
#include <cmath> // std::pow, std::log
#include <algorithm> // std::copy, std::transform
#include <cmath> // std::log, std::pow
#include <cstddef> // std::size_t

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;
}
}
Expand All @@ -62,8 +62,8 @@ bool GeneralizedCepstrumInverseGainNormalization::Run(
const std::vector<double>& normalized_generalized_cepstrum,
std::vector<double>* generalized_cepstrum) const {
if (!is_valid_ ||
static_cast<std::size_t>(num_order_ + 1) !=
normalized_generalized_cepstrum.size() ||
normalized_generalized_cepstrum.size() !=
static_cast<std::size_t>(num_order_ + 1) ||
NULL == generalized_cepstrum) {
return false;
}
Expand All @@ -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;
Expand Down
4 changes: 4 additions & 0 deletions src/utils/sptk_utils.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 149f2db

Please sign in to comment.