From: Takenori Y. <tak...@us...> - 2017-04-03 02:03:08
|
Update of /cvsroot/sp-tk/SPTK4/src In directory sfp-cvs-1.v30.ch3.sourceforge.com:/tmp/cvs-serv29728 Modified Files: Makefile line_spectral_pairs_to_linear_predictive_coefficients.cc linear_predictive_coefficients_to_line_spectral_pairs.cc linear_predictive_coefficients_to_line_spectral_pairs.h lsp2lpc.cc sptk_utils.cc sptk_utils.h Added Files: line_spectral_pairs_to_spectrum.cc line_spectral_pairs_to_spectrum.h Log Message: add LineSpectralPairsToSpectrum class Index: lsp2lpc.cc =================================================================== RCS file: /cvsroot/sp-tk/SPTK4/src/lsp2lpc.cc,v retrieving revision 1.1 retrieving revision 1.2 diff -C2 -d -r1.1 -r1.2 *** lsp2lpc.cc 15 Mar 2017 13:42:44 -0000 1.1 --- lsp2lpc.cc 3 Apr 2017 02:03:06 -0000 1.2 *************** *** 237,255 **** switch (input_format) { case kNormalizedFrequencyInRadians: { ! std::transform( ! line_spectral_pairs.begin() + 1, line_spectral_pairs.end(), ! line_spectral_pairs.begin() + 1, ! std::bind1st(std::multiplies<double>(), 1.0 / sptk::kTwoPi)); break; } case kNormalizedFrequencyInCycles: { ! // nothing to do break; } case kFrequecnyInkHz: { ! std::transform( ! line_spectral_pairs.begin() + 1, line_spectral_pairs.end(), ! line_spectral_pairs.begin() + 1, ! std::bind1st(std::multiplies<double>(), 1.0 / sampling_frequency)); break; } --- 237,256 ---- switch (input_format) { case kNormalizedFrequencyInRadians: { ! // nothing to do break; } case kNormalizedFrequencyInCycles: { ! std::transform(line_spectral_pairs.begin() + 1, ! line_spectral_pairs.end(), ! line_spectral_pairs.begin() + 1, ! std::bind1st(std::multiplies<double>(), sptk::kTwoPi)); break; } case kFrequecnyInkHz: { ! std::transform(line_spectral_pairs.begin() + 1, ! line_spectral_pairs.end(), ! line_spectral_pairs.begin() + 1, ! std::bind1st(std::multiplies<double>(), ! sptk::kTwoPi / sampling_frequency)); break; } *************** *** 259,263 **** line_spectral_pairs.begin() + 1, std::bind1st(std::multiplies<double>(), ! 0.001 / sampling_frequency)); break; } --- 260,264 ---- line_spectral_pairs.begin() + 1, std::bind1st(std::multiplies<double>(), ! sptk::kTwoPi * 0.001 / sampling_frequency)); break; } Index: sptk_utils.cc =================================================================== RCS file: /cvsroot/sp-tk/SPTK4/src/sptk_utils.cc,v retrieving revision 1.14 retrieving revision 1.15 diff -C2 -d -r1.14 -r1.15 *** sptk_utils.cc 15 Mar 2017 13:38:04 -0000 1.14 --- sptk_utils.cc 3 Apr 2017 02:03:06 -0000 1.15 *************** *** 47,57 **** #include <algorithm> // std::fill_n #include <cerrno> // errno, ERANGE ! #include <cmath> // std::ceil #include <cstdint> // int8_t, etc. ! #include <cstdlib> // std::size_t, std::strtol. std::strtod #include "int24_t.h" #include "uint24_t.h" namespace sptk { --- 47,64 ---- #include <algorithm> // std::fill_n #include <cerrno> // errno, ERANGE ! #include <cmath> // std::ceil, std::exp, std::log #include <cstdint> // int8_t, etc. ! #include <cstdlib> // std::size_t, std::strtod, std::strtol #include "int24_t.h" #include "uint24_t.h" + namespace { + + // 34 is a reasonable number near log(1e-15) + static const double kThresholdOfInformationLossInLogSpace(-34.0); + + } // namespace + namespace sptk { *************** *** 192,195 **** --- 199,217 ---- } + double FloorLog(double x) { + return (x <= 0.0) ? sptk::kLogZero : std::log(x); + } + + // compute log(x + y) given log(x) and log(y). + double AddInLogSpace(double log_x, double log_y) { + if (log_x == log_y) return log_x + sptk::kLogTwo; + + const double smaller((log_x < log_y) ? log_x : log_y); + const double greater((log_x < log_y) ? log_y : log_x); + const double diff(smaller - greater); + if (diff < kThresholdOfInformationLossInLogSpace) return greater; + return greater + std::log(std::exp(diff) + 1.0); + } + void PrintErrorMessage(const std::string& program_name, const std::ostringstream& message) { Index: linear_predictive_coefficients_to_line_spectral_pairs.cc =================================================================== RCS file: /cvsroot/sp-tk/SPTK4/src/linear_predictive_coefficients_to_line_spectral_pairs.cc,v retrieving revision 1.2 retrieving revision 1.3 diff -C2 -d -r1.2 -r1.3 *** linear_predictive_coefficients_to_line_spectral_pairs.cc 7 Feb 2017 10:00:38 -0000 1.2 --- linear_predictive_coefficients_to_line_spectral_pairs.cc 3 Apr 2017 02:03:06 -0000 1.3 *************** *** 48,51 **** --- 48,74 ---- #include <cstddef> // std::size_t + namespace { + + bool CalculateChebyshevPolynomial(const std::vector<double>& coefficients, + double x, double* y) { + if (coefficients.empty() || NULL == y) { + return false; + } + + const double* c(&coefficients[0]); + double b2(0.0); + double b1(0.0); + for (int i(coefficients.size() - 1); 0 < i; --i) { + const double b0(2.0 * x * b1 - b2 + c[i]); + b2 = b1; + b1 = b0; + } + *y = x * b1 - b2 + c[0]; + + return true; + } + + } // namespace + namespace sptk { *************** *** 179,201 **** } - bool LinearPredictiveCoefficientsToLineSpectralPairs:: - CalculateChebyshevPolynomial(const std::vector<double>& coefficients, - double x, double* y) const { - if (coefficients.empty() || NULL == y) { - return false; - } - - const double* c(&coefficients[0]); - double b2(0.0); - double b1(0.0); - for (int i(coefficients.size() - 1); 0 < i; --i) { - const double b0(2.0 * x * b1 - b2 + c[i]); - b2 = b1; - b1 = b0; - } - *y = x * b1 - b2 + c[0]; - - return true; - } - } // namespace sptk --- 202,204 ---- Index: Makefile =================================================================== RCS file: /cvsroot/sp-tk/SPTK4/src/Makefile,v retrieving revision 1.27 retrieving revision 1.28 diff -C2 -d -r1.27 -r1.28 *** Makefile 7 Feb 2017 10:00:38 -0000 1.27 --- Makefile 3 Apr 2017 02:03:06 -0000 1.28 *************** *** 67,70 **** --- 67,71 ---- levinson_durbin_recursion.cc \ line_spectral_pairs_to_linear_predictive_coefficients.cc \ + line_spectral_pairs_to_spectrum.cc \ linear_predictive_coefficients_to_cepstrum.cc \ linear_predictive_coefficients_to_line_spectral_pairs.cc \ Index: linear_predictive_coefficients_to_line_spectral_pairs.h =================================================================== RCS file: /cvsroot/sp-tk/SPTK4/src/linear_predictive_coefficients_to_line_spectral_pairs.h,v retrieving revision 1.1 retrieving revision 1.2 diff -C2 -d -r1.1 -r1.2 *** linear_predictive_coefficients_to_line_spectral_pairs.h 26 Jan 2017 10:33:53 -0000 1.1 --- linear_predictive_coefficients_to_line_spectral_pairs.h 3 Apr 2017 02:03:06 -0000 1.2 *************** *** 110,117 **** private: // - bool CalculateChebyshevPolynomial(const std::vector<double>& coefficients, - double x, double* y) const; - - // const int num_order_; --- 110,113 ---- Index: sptk_utils.h =================================================================== RCS file: /cvsroot/sp-tk/SPTK4/src/sptk_utils.h,v retrieving revision 1.10 retrieving revision 1.11 diff -C2 -d -r1.10 -r1.11 *** sptk_utils.h 15 Mar 2017 13:38:04 -0000 1.10 --- sptk_utils.h 3 Apr 2017 02:03:06 -0000 1.11 *************** *** 63,66 **** --- 63,68 ---- static const double kTwoPi(6.283185307179586); static const double kNp(8.685889638065035); // 1 Np = 20 / ln(10) dB + static const double kLogTwo(0.693147180559945); + static const double kLogZero(-1.0e+10); template <typename T> *************** *** 81,84 **** --- 83,88 ---- bool IsInRange(int num, int min, int max); bool IsPowerOfTwo(int num); + double FloorLog(double x); + double AddInLogSpace(double log_x, double log_y); void PrintErrorMessage(const std::string& program_name, const std::ostringstream& message); --- NEW FILE: line_spectral_pairs_to_spectrum.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 "line_spectral_pairs_to_spectrum.h" #include <cmath> // std::cos, std::fabs, std::sin #include <cstddef> // std::size_t namespace sptk { LineSpectralPairsToSpectrum::LineSpectralPairsToSpectrum(int num_input_order, int num_output_order) : num_input_order_(num_input_order), num_output_order_(num_output_order), is_valid_(true) { if (num_input_order < 0 || num_output_order < 0) { is_valid_ = false; } } bool LineSpectralPairsToSpectrum::Run( const std::vector<double>& line_spectral_pairs, std::vector<double>* spectrum) const { if (!is_valid_ || line_spectral_pairs.size() != static_cast<std::size_t>(num_input_order_ + 1) || NULL == spectrum) { return false; } // prepare memory const int output_length(num_output_order_ + 1); if (spectrum->size() < static_cast<std::size_t>(output_length)) { spectrum->resize(output_length); } // get values const double* input(&(line_spectral_pairs[0])); double* output(&((*spectrum)[0])); // set value const bool is_odd(num_input_order_ % 2 == 1); const double c(is_odd ? (num_input_order_ - 1) * sptk::kLogTwo : num_input_order_ * sptk::kLogTwo); const double delta(0 == num_output_order_ ? 0.0 : sptk::kPi / num_output_order_); double omega(0.0); for (int j(0); j < output_length; ++j, omega += delta) { const double cos_omega(std::cos(omega)); double p(0.0); for (int i(2); i <= num_input_order_; i += 2) { p += 2.0 * sptk::FloorLog(std::fabs(cos_omega - std::cos(input[i]))); } double q(0.0); for (int i(1); i <= num_input_order_; i += 2) { q += 2.0 * sptk::FloorLog(std::fabs(cos_omega - std::cos(input[i]))); } if (is_odd) { p += 2.0 * sptk::FloorLog(std::sin(omega)); } else { p += 2.0 * sptk::FloorLog(std::sin(omega * 0.5)); q += 2.0 * sptk::FloorLog(std::cos(omega * 0.5)); } output[j] = sptk::FloorLog(input[0]) - 0.5 * (c + sptk::AddInLogSpace(p, q)); } return true; } } // namespace sptk --- NEW FILE: line_spectral_pairs_to_spectrum.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-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. // // ----------------------------------------------------------------- // #ifndef SPTK_SRC_LINE_SPECTRAL_PAIRS_TO_SPECTRUM_H_ #define SPTK_SRC_LINE_SPECTRAL_PAIRS_TO_SPECTRUM_H_ #include <vector> // std::vector #include "sptk_utils.h" namespace sptk { class LineSpectralPairsToSpectrum { public: // LineSpectralPairsToSpectrum(int num_input_order, int num_output_order); // virtual ~LineSpectralPairsToSpectrum() { } // int GetNumInputOrder() const { return num_input_order_; } // int GetNumOutputOrder() const { return num_output_order_; } // bool IsValid() const { return is_valid_; } // Assume that the first element of line_spectral_pairs is linear gain and // the other elements are in normalized frequency (0...pi). bool Run(const std::vector<double>& line_spectral_pairs, std::vector<double>* spectrum) const; private: // const int num_input_order_; // const int num_output_order_; // bool is_valid_; // DISALLOW_COPY_AND_ASSIGN(LineSpectralPairsToSpectrum); }; } // namespace sptk #endif // SPTK_SRC_LINE_SPECTRAL_PAIRS_TO_SPECTRUM_H_ Index: line_spectral_pairs_to_linear_predictive_coefficients.cc =================================================================== RCS file: /cvsroot/sp-tk/SPTK4/src/line_spectral_pairs_to_linear_predictive_coefficients.cc,v retrieving revision 1.1 retrieving revision 1.2 diff -C2 -d -r1.1 -r1.2 *** line_spectral_pairs_to_linear_predictive_coefficients.cc 7 Feb 2017 10:00:38 -0000 1.1 --- line_spectral_pairs_to_linear_predictive_coefficients.cc 3 Apr 2017 02:03:06 -0000 1.2 *************** *** 135,142 **** // calculate line spectral pairs filter parameters for (int i(0), j(2); i < num_asymmetric_polynomial_order_; ++i, j += 2) { ! p[i] = -2.0 * std::cos(sptk::kTwoPi * input[j]); } for (int i(0), j(1); i < num_symmetric_polynomial_order_; ++i, j += 2) { ! q[i] = -2.0 * std::cos(sptk::kTwoPi * input[j]); } --- 135,142 ---- // calculate line spectral pairs filter parameters for (int i(0), j(2); i < num_asymmetric_polynomial_order_; ++i, j += 2) { ! p[i] = -2.0 * std::cos(input[j]); } for (int i(0), j(1); i < num_symmetric_polynomial_order_; ++i, j += 2) { ! q[i] = -2.0 * std::cos(input[j]); } |