From: Keiichiro O. <ur...@us...> - 2016-10-12 13:39:49
|
Update of /cvsroot/sp-tk/SPTK4/src In directory sfp-cvs-1.v30.ch3.sourceforge.com:/tmp/cvs-serv14903 Modified Files: Makefile Added Files: generalized_cepstrum_inverse_gain_normalization.cc generalized_cepstrum_inverse_gain_normalization.h ignorm.cc Log Message: add ignorm command Index: Makefile =================================================================== RCS file: /cvsroot/sp-tk/SPTK4/src/Makefile,v retrieving revision 1.15 retrieving revision 1.16 diff -C2 -d -r1.15 -r1.16 *** Makefile 12 Oct 2016 13:08:14 -0000 1.15 --- Makefile 12 Oct 2016 13:39:46 -0000 1.16 *************** *** 53,56 **** --- 53,57 ---- frequency_transform.cc \ generalized_cepstrum_gain_normalization.cc \ + generalized_cepstrum_inverse_gain_normalization.cc \ input_source_from_stream.cc \ input_source_interpolation.cc \ --- NEW FILE: generalized_cepstrum_inverse_gain_normalization.cc --- // ----------------------------------------------------------------- // // The Speech Signal Processing Toolkit (SPTK) // // developed by SPTK Working Group // // http://sp-tk.sourceforge.net/ // // ----------------------------------------------------------------- // // // // Copyright (c) 1984-2007 Tokyo Institute of Technology // // Interdisciplinary Graduate School of // // Science and Engineering // // // // 1996-2016 Nagoya Institute of Technology // // Department of Computer Science // // // // All rights reserved. // // // // Redistribution and use in source and binary forms, with or // // without modification, are permitted provided that the following // // conditions are met: // // // // - Redistributions of source code must retain the above copyright // // notice, this list of conditions and the following disclaimer. // // - Redistributions in binary form must reproduce the above // // copyright notice, this list of conditions and the following // // disclaimer in the documentation and/or other materials provided // // with the distribution. // // - Neither the name of the SPTK working group nor the names of its // // contributors may be used to endorse or promote products derived // // from this software without specific prior written permission. // // // // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND // // CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, // // INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF // // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE // // DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS // // BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, // // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED // // TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, // // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON // // ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, // // OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY // // OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE // // POSSIBILITY OF SUCH DAMAGE. // // ----------------------------------------------------------------- // #include "generalized_cepstrum_inverse_gain_normalization.h" #include <cmath> // std::pow, std::log #include <vector> // std::vector #include "sptk_utils.h" namespace sptk { GeneralizedCepstrumInverseGainNormalization:: GeneralizedCepstrumInverseGainNormalization(int num_order, double gamma) : num_order_(num_order), gamma_(gamma), is_valid_(true) { if (num_order_ < 0) { is_valid_ = false; } } bool GeneralizedCepstrumInverseGainNormalization::Run( const std::vector<double>& normalized_generalized_cepstrum, std::vector<double>* generalized_cepstrum) const { if (!is_valid_ || normalized_generalized_cepstrum.empty() || NULL == generalized_cepstrum) { return false; } 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 { std::copy(normalized_generalized_cepstrum.begin() + 1, normalized_generalized_cepstrum.end(), generalized_cepstrum->begin() + 1); (*generalized_cepstrum)[0] = std::log(normalized_generalized_cepstrum[0]); } return true; } } // namespace sptk --- NEW FILE: generalized_cepstrum_inverse_gain_normalization.h --- // ----------------------------------------------------------------- // // The Speech Signal Processing Toolkit (SPTK) // // developed by SPTK Working Group // // http://sp-tk.sourceforge.net/ // // ----------------------------------------------------------------- // // // // Copyright (c) 1984-2007 Tokyo Institute of Technology // // Interdisciplinary Graduate School of // // Science and Engineering // // // // 1996-2016 Nagoya Institute of Technology // // Department of Computer Science // // // // All rights reserved. // // // // Redistribution and use in source and binary forms, with or // // without modification, are permitted provided that the following // // conditions are met: // // // // - Redistributions of source code must retain the above copyright // // notice, this list of conditions and the following disclaimer. // // - Redistributions in binary form must reproduce the above // // copyright notice, this list of conditions and the following // // disclaimer in the documentation and/or other materials provided // // with the distribution. // // - Neither the name of the SPTK working group nor the names of its // // contributors may be used to endorse or promote products derived // // from this software without specific prior written permission. // // // // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND // // CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, // // INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF // // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE // // DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS // // BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, // // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED // // TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, // // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON // // ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, // // OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY // // OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE // // POSSIBILITY OF SUCH DAMAGE. // // ----------------------------------------------------------------- // #ifndef SPTK_SRC_GENERALIZED_CEPSTRUM_INVERSE_GAIN_NORMALIZATION_H_ #define SPTK_SRC_GENERALIZED_CEPSTRUM_INVERSE_GAIN_NORMALIZATION_H_ #include <vector> // std::vector #include "sptk_utils.h" namespace sptk { class GeneralizedCepstrumInverseGainNormalization { public: // GeneralizedCepstrumInverseGainNormalization(int num_order, double gamma); // virtual ~GeneralizedCepstrumInverseGainNormalization() {} // int GetNumOrder() const { return num_order_; } // double GetGamma() const { return gamma_; } // bool IsValid() const { return is_valid_; } // bool Run(const std::vector<double>& normalized_generalized_cepstrum, std::vector<double>* generalized_cepstrum) const; private: // int num_order_; // double gamma_; // bool is_valid_; // DISALLOW_COPY_AND_ASSIGN(GeneralizedCepstrumInverseGainNormalization); }; } // namespace sptk #endif // SPTK_SRC_GENERALIZED_CEPSTRUM_INVERSE_GAIN_NORMALIZATION_H_ --- NEW FILE: ignorm.cc --- // ----------------------------------------------------------------- // // The Speech Signal Processing Toolkit (SPTK) // // developed by SPTK Working Group // // http://sp-tk.sourceforge.net/ // // ----------------------------------------------------------------- // // // // Copyright (c) 1984-2007 Tokyo Institute of Technology // // Interdisciplinary Graduate School of // // Science and Engineering // // // // 1996-2016 Nagoya Institute of Technology // // Department of Computer Science // // // // All rights reserved. // // // // Redistribution and use in source and binary forms, with or // // without modification, are permitted provided that the following // // conditions are met: // // // // - Redistributions of source code must retain the above copyright // // notice, this list of conditions and the following disclaimer. // // - Redistributions in binary form must reproduce the above // // copyright notice, this list of conditions and the following // // disclaimer in the documentation and/or other materials provided // // with the distribution. // // - Neither the name of the SPTK working group nor the names of its // // contributors may be used to endorse or promote products derived // // from this software without specific prior written permission. // // // // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND // // CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, // // INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF // // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE // // DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS // // BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, // // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED // // TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, // // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON // // ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, // // OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY // // OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE // // POSSIBILITY OF SUCH DAMAGE. // // ----------------------------------------------------------------- // #include <unistd.h> #include <fstream> #include <iostream> #include <sstream> #include <vector> #include "generalized_cepstrum_inverse_gain_normalization.h" #include "sptk_utils.h" namespace { const int kDefaultNumOrder(25); const double kDefaultGamma(0.0); void PrintUsage(std::ostream* stream) { // clang-format off *stream << std::endl; *stream << " ignorm - inverse gain normalization of generalized cepstrum" << std::endl; // NOLINT *stream << std::endl; *stream << " usage:" << std::endl; *stream << " ignorm [ options ] [ infile ] > stdout" << std::endl; *stream << " options:" << std::endl; *stream << " -m m : order of generalized cepstrum [" << kDefaultNumOrder << "]" << std::endl; // NOLINT *stream << " -a g : gamma [" << kDefaultGamma << "]" << std::endl; // NOLINT *stream << " -c c : gamma = -1 / (int) c" << std::endl; *stream << " -h : print this message" << std::endl; *stream << " infile:" << std::endl; *stream << " normalized generalized cepstrum (double) [stdin]" << std::endl; // NOLINT *stream << " stdout:" << std::endl; *stream << " generalized cepstrum (double)" << std::endl; *stream << " notice:" << std::endl; *stream << " value of c must be c >= 1 (double)" << std::endl; *stream << std::endl; *stream << " SPTK: version " << sptk::kVersion << std::endl; *stream << std::endl; // clang-format on } } // namespace int main(int argc, char* argv[]) { int num_order(kDefaultNumOrder); double gamma(kDefaultGamma); for (;;) { const char option_char(getopt(argc, argv, "m:g:c:h")); if (-1 == option_char) break; switch (option_char) { case 'm': { if (!sptk::ConvertStringToInteger(optarg, &num_order) || num_order < 0) { std::ostringstream error_message; error_message << "The argument for the -m option must be a " << "non-negative integer"; sptk::PrintErrorMessage("ignorm", error_message); return 1; } break; } case 'g': { if (!sptk::ConvertStringToDouble(optarg, &gamma)) { std::ostringstream error_message; error_message << "The argument for the -g option must be numeric"; sptk::PrintErrorMessage("ignorm", error_message); return 1; } break; } case 'c': { int tmp; if (!sptk::ConvertStringToInteger(optarg, &tmp) || tmp < 1) { std::ostringstream error_message; error_message << "The argument for the -c option must be a " << "positive integer"; sptk::PrintErrorMessage("ignorm", error_message); return 1; } gamma = -1.0 / tmp; break; } case 'h': { PrintUsage(&std::cout); return 0; } default: { PrintUsage(&std::cerr); return 1; } } } // get input file const char* input_file((optind < argc) ? argv[argc - 1] : NULL); // open stream std::ifstream ifs; ifs.open(input_file, std::ios::in | std::ios::binary); if (ifs.fail() && NULL != input_file) { std::ostringstream error_message; error_message << "Cannot open file " << input_file; sptk::PrintErrorMessage("ignorm", error_message); return 1; } std::istream& input_stream(ifs.fail() ? std::cin : ifs); // prepare for inverse gain normalization sptk::GeneralizedCepstrumInverseGainNormalization generalized_cepstrum_inverse_gain_normalization(num_order, gamma); if (!generalized_cepstrum_inverse_gain_normalization.IsValid()) { std::ostringstream error_message; error_message << "Failed to set the condition."; sptk::PrintErrorMessage("ignorm", error_message); return 1; } const int length(num_order + 1); std::vector<double> normalized_generalized_cepstrum(length); std::vector<double> generalized_cepstrum(length); while (sptk::ReadStream(false, length, &normalized_generalized_cepstrum, &input_stream)) { if (!generalized_cepstrum_inverse_gain_normalization.Run( normalized_generalized_cepstrum, &generalized_cepstrum)) { std::ostringstream error_message; error_message << "Failed to run the normalization"; sptk::PrintErrorMessage("ignorm", error_message); return 1; } if (!sptk::WriteStream(length, generalized_cepstrum, &std::cout)) { std::ostringstream error_message; error_message << "Failed to write an output sequence"; sptk::PrintErrorMessage("ignorm", error_message); return 1; } } return 0; } |