From: fujishita t. <fjs...@us...> - 2017-01-13 03:19:13
|
Update of /cvsroot/sp-tk/SPTK4/src In directory sfp-cvs-1.v30.ch3.sourceforge.com:/tmp/cvs-serv26402 Modified Files: Makefile Added Files: lpc2par.cc linear_predictive_coefficients_to_parcor_coefficients.cc linear_predictive_coefficients_to_parcor_coefficients.h Log Message: add lpc2par command --- NEW FILE: lpc2par.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-2017 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 "linear_predictive_coefficients_to_parcor_coefficients.h" #include "sptk_utils.h" namespace { enum BehaviorForUnstableCoefficients { kIgnore = 0, kWarn, kExit, kNumKindsOfBehavior }; const int kDefaultNumOrder(25); const double kDefaultGamma(1.0); void PrintUsage(std::ostream* stream) { // clang-format off *stream << std::endl; *stream << " lpc2par - transform linear predictive coefficients to PARCOR coefficients" << std::endl; // NOLINT *stream << std::endl; *stream << " usage:" << std::endl; *stream << " lpc2par [ options ] [ infile ] > stdout" << std::endl; *stream << " options:" << std::endl; *stream << " -m m : order of linear predictive coefficients [" << kDefaultNumOrder << "]" << std::endl; // NOLINT *stream << " -g g : gamma of generalized cepstrum [" << kDefaultGamma << "]" << std::endl; // NOLINT *stream << " -c c : gamma of generalized cepstrum = -1 / (int) c" << std::endl; // NOLINT *stream << " -e e : check whether the derived PARCOR [0]" << std::endl; // NOLINT *stream << " coefficients are stable" << std::endl; *stream << " 0 (the check is not performed)" << std::endl; *stream << " 1 (if the coefficients are unstable," << std::endl; // NOLINT *stream << " output the index to stderr)" << std::endl; *stream << " 2 (if the coefficients are unstable," << std::endl; // NOLINT *stream << " output the index to stderr and" << std::endl; *stream << " exit immediately)" << std::endl; *stream << " -h : print this message" << std::endl; *stream << " infile:" << std::endl; *stream << " linear predictive coefficients (double) [stdin]" << std::endl; // NOLINT *stream << " stdout:" << std::endl; *stream << " PARCOR coefficients (double)" << std::endl; *stream << " notice:" << std::endl; *stream << " value of c must be c >= 1" << 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); BehaviorForUnstableCoefficients behavior(kIgnore); for (;;) { const char option_char(getopt(argc, argv, "m:g:c:e: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("lpc2par", 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("lpc2par", 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("lpc2par", error_message); return 1; } gamma = -1.0 / tmp; break; } case 'e': { int tmp; if (!sptk::ConvertStringToInteger(optarg, &tmp)) { std::ostringstream error_message; error_message << "The argument for the -e option must be an integer"; sptk::PrintErrorMessage("lpc2par", error_message); return 1; } const int min(0); const int max(static_cast<int>(kNumKindsOfBehavior) - 1); if (!sptk::IsInRange(tmp, min, max)) { std::ostringstream error_message; error_message << "The argument for the -e option must be in range" << " (" << min << " .. " << max << ")"; sptk::PrintErrorMessage("lpc2par", error_message); return 1; } behavior = static_cast<BehaviorForUnstableCoefficients>(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("lpc2par", error_message); return 1; } std::istream& input_stream(ifs.fail() ? std::cin : ifs); sptk::LinearPredictiveCoefficientsToParcorCoefficients linear_predictive_coefficients_to_parcor_coefficients(num_order, gamma); sptk::LinearPredictiveCoefficientsToParcorCoefficients::Buffer buffer; if (!linear_predictive_coefficients_to_parcor_coefficients.IsValid()) { std::ostringstream error_message; error_message << "Failed to set the condition"; sptk::PrintErrorMessage("lpc2par", error_message); return 1; } const int length(num_order + 1); std::vector<double> linear_predictive_coefficients(length); std::vector<double> parcor_coefficients(length); for (int frame_index(0); sptk::ReadStream( false, length, &linear_predictive_coefficients, &input_stream); ++frame_index) { bool is_stable(false); if (!linear_predictive_coefficients_to_parcor_coefficients.Run( linear_predictive_coefficients, &parcor_coefficients, &is_stable, &buffer)) { std::ostringstream error_message; error_message << "Failed to transform linear predictive coefficients to " << "PARCOR coefficients"; sptk::PrintErrorMessage("lpc2par", error_message); return 1; } if (!is_stable && kIgnore != behavior) { std::ostringstream error_message; error_message << frame_index << "th frame is unstable"; sptk::PrintErrorMessage("lpc2par", error_message); if (kExit == behavior) return 1; } if (!sptk::WriteStream(length, parcor_coefficients, &std::cout)) { std::ostringstream error_message; error_message << "Failed to write PARCOR coefficients"; sptk::PrintErrorMessage("lpc2par", error_message); return 1; } } return 0; } Index: Makefile =================================================================== RCS file: /cvsroot/sp-tk/SPTK4/src/Makefile,v retrieving revision 1.23 retrieving revision 1.24 diff -C2 -d -r1.23 -r1.24 *** Makefile 26 Dec 2016 07:41:20 -0000 1.23 --- Makefile 13 Jan 2017 03:19:11 -0000 1.24 *************** *** 67,70 **** --- 67,71 ---- levinson_durbin_recursion.cc \ linear_predictive_coefficients_to_cepstrum.cc \ + linear_predictive_coefficients_to_parcor_coefficients.cc \ m_sequence_generation.cc \ normal_distributed_random_value_generation.cc \ --- NEW FILE: linear_predictive_coefficients_to_parcor_coefficients.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 "linear_predictive_coefficients_to_parcor_coefficients.h" #include <algorithm> // std::copy, std::fill_n, std::transform #include <cmath> // std::fabs #include <cstddef> // std::size_t #include <functional> // std::bind1st, std::multiplies namespace sptk { LinearPredictiveCoefficientsToParcorCoefficients:: LinearPredictiveCoefficientsToParcorCoefficients(int num_order, double gamma) : num_order_(num_order), gamma_(gamma), is_valid_(true) { if (num_order_ < 0) { is_valid_ = false; } } bool LinearPredictiveCoefficientsToParcorCoefficients::Run( const std::vector<double>& linear_predictive_coefficients, std::vector<double>* parcor_coefficients, bool* is_stable, LinearPredictiveCoefficientsToParcorCoefficients::Buffer* buffer) const { // check inputs if (!is_valid_ || linear_predictive_coefficients.size() != static_cast<std::size_t>(num_order_ + 1) || NULL == parcor_coefficients || NULL == buffer || NULL == is_stable) { return false; } // prepare memory const int output_length(num_order_ + 1); if (parcor_coefficients->size() < static_cast<std::size_t>(output_length)) { parcor_coefficients->resize(output_length); } // set value *is_stable = true; (*parcor_coefficients)[0] = linear_predictive_coefficients[0]; if (0 == num_order_) { return true; } if (0.0 == gamma_) { std::fill_n(parcor_coefficients->begin() + 1, num_order_, 0.0); return true; } // prepare buffer if (buffer->a_.size() < static_cast<std::size_t>(output_length)) { buffer->a_.resize(output_length); } // get value double* output(&((*parcor_coefficients)[0])); // transform linear predictive coefficients to parcor coefficients if (1.0 == gamma_) { std::copy(linear_predictive_coefficients.begin(), linear_predictive_coefficients.end(), buffer->a_.begin()); } else { buffer->a_[0] = linear_predictive_coefficients[0]; std::transform(linear_predictive_coefficients.begin() + 1, 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) { output[j] = a[j]; } const double denominator(1.0 - output[i] * output[i]); if (0.0 == denominator) { return false; } if (1.0 <= std::fabs(output[i])) { *is_stable = false; } for (int j(1); j < i; ++j) { a[j] = (output[j] - output[i] * output[i - j]) / denominator; } } return true; } } // namespace sptk --- NEW FILE: linear_predictive_coefficients_to_parcor_coefficients.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_LINEAR_PREDICTIVE_COEFFICIENTS_TO_PARCOR_COEFFICIENTS_H_ #define SPTK_SRC_LINEAR_PREDICTIVE_COEFFICIENTS_TO_PARCOR_COEFFICIENTS_H_ #include <vector> // std::vector #include "sptk_utils.h" namespace sptk { class LinearPredictiveCoefficientsToParcorCoefficients { public: class Buffer { public: Buffer() { } virtual ~Buffer() { } private: std::vector<double> a_; friend class LinearPredictiveCoefficientsToParcorCoefficients; DISALLOW_COPY_AND_ASSIGN(Buffer); }; // LinearPredictiveCoefficientsToParcorCoefficients(int num_order, double gamma); // virtual ~LinearPredictiveCoefficientsToParcorCoefficients() { } // int GetNumOrder() const { return num_order_; } // double GetGamma() const { return gamma_; } // bool IsValid() const { return is_valid_; } // bool Run( const std::vector<double>& linear_predictive_coefficients, std::vector<double>* parcor_coefficients, bool* is_stable, LinearPredictiveCoefficientsToParcorCoefficients::Buffer* buffer) const; private: // const int num_order_; // const double gamma_; // bool is_valid_; // DISALLOW_COPY_AND_ASSIGN(LinearPredictiveCoefficientsToParcorCoefficients); }; } // namespace sptk #endif // SPTK_SRC_LINEAR_PREDICTIVE_COEFFICIENTS_TO_PARCOR_COEFFICIENTS_H_ |