Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable to whiten residual signal #27

Merged
merged 4 commits into from
Jan 31, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
enable to whiten residual signal
  • Loading branch information
takenori-y committed Jan 4, 2023
commit e5f8fab1767342d091fda5347970a3e60b583b73
13 changes: 11 additions & 2 deletions include/SPTK/analysis/adaptive_generalized_cepstral_analysis.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ namespace sptk {
* \bar{\nabla} \epsilon^{(n)} = \tau \bar{\nabla} \epsilon^{(n-1)}
* -2 (1-\tau) e_\gamma(n) \boldsymbol{e}^{(n)}_{\gamma}
* @f]
* where @f$\tau@f$ is the moment and
* where @f$\tau@f$ is the momentum and
* @f$\boldsymbol{e}^{(n)}_{\gamma} =
* [e_\gamma(n-1),\ldots,e_\gamma(n-M)]^{\mathsf{T}}@f$
* is the set of outputs of the filter
Expand Down Expand Up @@ -98,11 +98,12 @@ class AdaptiveGeneralizedCepstralAnalysis {
* @param[in] momentum Momentum, @f$\tau@f$.
* @param[in] forgetting_factor Forgetting factor, @f$\lambda@f$.
* @param[in] step_size_factor Step-size factor, @f$a@f$.
* @param[in] gain_flag If true, perform filtering with gain.
*/
AdaptiveGeneralizedCepstralAnalysis(int num_order, int num_stage,
double min_epsilon, double momentum,
double forgetting_factor,
double step_size_factor);
double step_size_factor, bool gain_flag);

virtual ~AdaptiveGeneralizedCepstralAnalysis() {
}
Expand Down Expand Up @@ -156,6 +157,13 @@ class AdaptiveGeneralizedCepstralAnalysis {
return step_size_factor_;
}

/**
* @return Gain flag.
*/
bool GetGainFlag() const {
return gain_flag_;
}

/**
* @return True if this object is valid.
*/
Expand All @@ -181,6 +189,7 @@ class AdaptiveGeneralizedCepstralAnalysis {
const double momentum_;
const double forgetting_factor_;
const double step_size_factor_;
const bool gain_flag_;

const GeneralizedCepstrumInverseGainNormalization
generalized_cepstrum_inverse_gain_normalization_;
Expand Down
15 changes: 12 additions & 3 deletions include/SPTK/analysis/adaptive_mel_cepstral_analysis.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ namespace sptk {
* \bar{\nabla} \epsilon^{(n)} = \tau \bar{\nabla} \epsilon^{(n-1)}
* -2 (1-\tau) e(n) \boldsymbol{e}^{(n)}_{\Phi}
* @f]
* where @f$\tau@f$ is the moment and
* where @f$\tau@f$ is the momentum and
* @f$\boldsymbol{e}^{(n)}_{\Phi}=[e_1(n),e_2(n),\ldots,e_M(n)]^{\mathsf{T}}@f$
* is the set of outputs of the filter @f$\Phi_m(z)@f$:
* @image html amcep_2.png
Expand Down Expand Up @@ -97,11 +97,12 @@ class AdaptiveMelCepstralAnalysis {
* @param[in] momentum Momentum, @f$\tau@f$.
* @param[in] forgetting_factor Forgetting factor, @f$\lambda@f$.
* @param[in] step_size_factor Step-size factor, @f$a@f$.
* @param[in] gain_flag If true, perform filtering with gain.
*/
AdaptiveMelCepstralAnalysis(int num_order, int num_pade_order, double alpha,
double min_epsilon, double momentum,
double forgetting_factor,
double step_size_factor);
double forgetting_factor, double step_size_factor,
bool gain_flag);

virtual ~AdaptiveMelCepstralAnalysis() {
}
Expand Down Expand Up @@ -155,6 +156,13 @@ class AdaptiveMelCepstralAnalysis {
return step_size_factor_;
}

/**
* @return Gain flag.
*/
bool GetGainFlag() const {
return gain_flag_;
}

/**
* @return True if this object is valid.
*/
Expand All @@ -178,6 +186,7 @@ class AdaptiveMelCepstralAnalysis {
const double momentum_;
const double forgetting_factor_;
const double step_size_factor_;
const bool gain_flag_;

const MlsaDigitalFilter mlsa_digital_filter_;
const MlsaDigitalFilterCoefficientsToMelCepstrum
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,14 @@ class AdaptiveMelGeneralizedCepstralAnalysis {
* @param[in] momentum Momentum, @f$\tau@f$.
* @param[in] forgetting_factor Forgetting factor, @f$\lambda@f$.
* @param[in] step_size_factor Step-size factor, @f$a@f$.
* @param[in] gain_flag If true, perform filtering with gain.
*/
AdaptiveMelGeneralizedCepstralAnalysis(int num_order, int num_pade_order,
int num_stage, double alpha,
double min_epsilon, double momentum,
double forgetting_factor,
double step_size_factor);
double step_size_factor,
bool gain_flag);

virtual ~AdaptiveMelGeneralizedCepstralAnalysis() {
}
Expand Down Expand Up @@ -126,6 +128,13 @@ class AdaptiveMelGeneralizedCepstralAnalysis {
return mel_cepstral_analysis_.GetStepSizeFactor();
}

/**
* @return Gain flag.
*/
bool GetGainFlag() const {
return mel_cepstral_analysis_.GetGainFlag();
}

/**
* @return True if this object is valid.
*/
Expand Down
19 changes: 14 additions & 5 deletions src/analysis/adaptive_generalized_cepstral_analysis.cc
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,13 @@ namespace sptk {

AdaptiveGeneralizedCepstralAnalysis::AdaptiveGeneralizedCepstralAnalysis(
int num_order, int num_stage, double min_epsilon, double momentum,
double forgetting_factor, double step_size_factor)
double forgetting_factor, double step_size_factor, bool gain_flag)
: num_stage_(num_stage),
min_epsilon_(min_epsilon),
momentum_(momentum),
forgetting_factor_(forgetting_factor),
step_size_factor_(step_size_factor),
gain_flag_(gain_flag),
generalized_cepstrum_inverse_gain_normalization_(num_order,
-1.0 / num_stage_),
is_valid_(true) {
Expand Down Expand Up @@ -73,7 +74,8 @@ bool AdaptiveGeneralizedCepstralAnalysis::Run(
// Store e_\gamma(n - M).
const double last_e(buffer->d_.back());

// Apply cascade all-zero digital filters.
// Apply cascaded all-zero digital filters.
double curr_prediction_error;
{
const double gamma(GetGamma());
double x(input_signal);
Expand All @@ -89,7 +91,7 @@ bool AdaptiveGeneralizedCepstralAnalysis::Run(
d[0] = x;
x += y * gamma;
}
*prediction_error = x;
curr_prediction_error = x;
}

// Update epsilon.
Expand All @@ -102,7 +104,7 @@ bool AdaptiveGeneralizedCepstralAnalysis::Run(

// Update normalized generalized cepstrum.
if (0 < num_order) {
const double sigma(2.0 * (1.0 - momentum_) * *prediction_error);
const double sigma(2.0 * (1.0 - momentum_) * curr_prediction_error);
const double mu(step_size_factor_ / (num_order * curr_epsilon));
const double* e(&buffer->d_[num_order * (num_stage_ - 1) + 1]);
double* gradient(&buffer->gradient_[0]);
Expand All @@ -117,10 +119,17 @@ bool AdaptiveGeneralizedCepstralAnalysis::Run(
// Update gain.
const double curr_adjusted_error(
(forgetting_factor_ * buffer->prev_adjusted_error_) +
(1.0 - forgetting_factor_) * (*prediction_error * *prediction_error));
(1.0 - forgetting_factor_) *
(curr_prediction_error * curr_prediction_error));
buffer->normalized_generalized_cepstrum_[0] = std::sqrt(curr_adjusted_error);

// Store outputs.
if (gain_flag_) {
*prediction_error =
curr_prediction_error / buffer->normalized_generalized_cepstrum_[0];
} else {
*prediction_error = curr_prediction_error;
}
buffer->prev_adjusted_error_ = curr_adjusted_error;
buffer->prev_epsilon_ = curr_epsilon;

Expand Down
14 changes: 11 additions & 3 deletions src/analysis/adaptive_mel_cepstral_analysis.cc
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,20 @@
#include "SPTK/analysis/adaptive_mel_cepstral_analysis.h"

#include <algorithm> // std::fill, std::transform
#include <cmath> // std::log
#include <cmath> // std::log, std::sqrt
#include <cstddef> // std::size_t

namespace sptk {

AdaptiveMelCepstralAnalysis::AdaptiveMelCepstralAnalysis(
int num_order, int num_pade_order, double alpha, double min_epsilon,
double momentum, double forgetting_factor, double step_size_factor)
double momentum, double forgetting_factor, double step_size_factor,
bool gain_flag)
: min_epsilon_(min_epsilon),
momentum_(momentum),
forgetting_factor_(forgetting_factor),
step_size_factor_(step_size_factor),
gain_flag_(gain_flag),
mlsa_digital_filter_(num_order, num_pade_order, alpha, false),
mlsa_digital_filter_coefficients_to_mel_cepstrum_(num_order, alpha),
is_valid_(true) {
Expand Down Expand Up @@ -122,10 +124,16 @@ bool AdaptiveMelCepstralAnalysis::Run(
b[i] -= mu * gradient[i];
}
}

// Update gain.
buffer->mlsa_digital_filter_coefficients_[0] = 0.5 * std::log(curr_epsilon);

// Store outputs.
*prediction_error = curr_prediction_error;
if (gain_flag_) {
*prediction_error = curr_prediction_error / std::sqrt(curr_epsilon);
} else {
*prediction_error = curr_prediction_error;
}
buffer->prev_prediction_error_ = curr_prediction_error;
buffer->prev_epsilon_ = curr_epsilon;

Expand Down
7 changes: 4 additions & 3 deletions src/analysis/adaptive_mel_generalized_cepstral_analysis.cc
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,13 @@ namespace sptk {
AdaptiveMelGeneralizedCepstralAnalysis::AdaptiveMelGeneralizedCepstralAnalysis(
int num_order, int num_pade_order, int num_stage, double alpha,
double min_epsilon, double momentum, double forgetting_factor,
double step_size_factor)
double step_size_factor, bool gain_flag)
: generalized_cepstral_analysis_(num_order, num_stage, min_epsilon,
momentum, forgetting_factor,
step_size_factor),
step_size_factor, gain_flag),
mel_cepstral_analysis_(num_order, num_pade_order, alpha, min_epsilon,
momentum, forgetting_factor, step_size_factor),
momentum, forgetting_factor, step_size_factor,
gain_flag),
is_valid_(true) {
if ((0 != num_stage && 0.0 != alpha) ||
(0 != num_stage && !generalized_cepstral_analysis_.IsValid()) ||
Expand Down
15 changes: 12 additions & 3 deletions src/main/amgcep.cc
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ const double kDefaultForgettingFactor(0.98);
const double kDefaultStepSizeFactor(0.1);
const int kDefaultOutputPeriod(1);
const int kDefaultNumPadeOrder(4);
const bool kDefaultGainFlag(false);

void PrintUsage(std::ostream* stream) {
// clang-format off
Expand All @@ -50,9 +51,10 @@ void PrintUsage(std::ostream* stream) {
*stream << " -e e : minimum value for epsilon (double)[" << std::setw(5) << std::right << kDefaultMinEpsilon << "][ 0.0 < e <= ]" << std::endl; // NOLINT
*stream << " -t t : momentum constant (double)[" << std::setw(5) << std::right << kDefaultMomentum << "][ 0.0 <= t < 1.0 ]" << std::endl; // NOLINT
*stream << " -l l : forgetting factor (double)[" << std::setw(5) << std::right << kDefaultForgettingFactor << "][ 0.0 <= l < 1.0 ]" << std::endl; // NOLINT
*stream << " -k k : step-size factor (double)[" << std::setw(5) << std::right << kDefaultStepSizeFactor << "][ 0.0 < s < 1.0 ]" << std::endl; // NOLINT
*stream << " -k k : step-size factor (double)[" << std::setw(5) << std::right << kDefaultStepSizeFactor << "][ 0.0 < k < 1.0 ]" << std::endl; // NOLINT
*stream << " -p p : output period ( int)[" << std::setw(5) << std::right << kDefaultOutputPeriod << "][ 1 <= p <= ]" << std::endl; // NOLINT
*stream << " -P P : order of Pade approximation ( int)[" << std::setw(5) << std::right << kDefaultNumPadeOrder << "][ 4 <= P <= 7 ]" << std::endl; // NOLINT
*stream << " -K : filtering with gain ( bool)[" << std::setw(5) << std::right << sptk::ConvertBooleanToString(kDefaultGainFlag) << "]" << std::endl; // NOLINT
*stream << " -E E : output filename of double (string)[" << std::setw(5) << std::right << "N/A" << "]" << std::endl; // NOLINT
*stream << " type prediction error" << std::endl;
*stream << " -h : print this message" << std::endl;
Expand Down Expand Up @@ -91,6 +93,8 @@ void PrintUsage(std::ostream* stream) {
* - output period @f$(1 \le p)@f$
* - @b -P @e int
* - order of Pade approximation @f$(4 \le P \le 7)@f$
* - @b -K
* - filtering with gain
* - @b -E @e str
* - double-type prediction errors
* - @b infile @e str
Expand Down Expand Up @@ -131,11 +135,12 @@ int main(int argc, char* argv[]) {
double step_size_factor(kDefaultStepSizeFactor);
int output_period(kDefaultOutputPeriod);
int num_pade_order(kDefaultNumPadeOrder);
bool gain_flag(kDefaultGainFlag);
const char* prediction_error_file(NULL);

for (;;) {
const int option_char(
getopt_long(argc, argv, "m:a:c:e:t:l:k:p:P:E:h", NULL, NULL));
getopt_long(argc, argv, "m:a:c:e:t:l:k:p:P:KE:h", NULL, NULL));
if (-1 == option_char) break;

switch (option_char) {
Expand Down Expand Up @@ -240,6 +245,10 @@ int main(int argc, char* argv[]) {
}
break;
}
case 'K': {
gain_flag = true;
break;
}
case 'E': {
prediction_error_file = optarg;
break;
Expand Down Expand Up @@ -296,7 +305,7 @@ int main(int argc, char* argv[]) {

sptk::AdaptiveMelGeneralizedCepstralAnalysis analysis(
num_order, num_pade_order, num_stage, alpha, min_epsilon, momentum,
forgetting_factor, step_size_factor);
forgetting_factor, step_size_factor, gain_flag);
sptk::AdaptiveMelGeneralizedCepstralAnalysis::Buffer buffer;
if (!analysis.IsValid()) {
std::ostringstream error_message;
Expand Down