From: Natsumi K. <koi...@us...> - 2017-07-16 07:28:20
|
Update of /cvsroot/sp-tk/SPTK4/src In directory sfp-cvs-1.v30.ch3.sourceforge.com:/tmp/cvs-serv333 Modified Files: Makefile Added Files: filter_coefficient_normalization.h filter_coefficient_normalization.cc norm0.cc Log Message: add norm0 command Index: Makefile =================================================================== RCS file: /cvsroot/sp-tk/SPTK4/src/Makefile,v retrieving revision 1.36 retrieving revision 1.37 diff -C2 -d -r1.36 -r1.37 *** Makefile 16 Jun 2017 08:04:49 -0000 1.36 --- Makefile 16 Jul 2017 07:28:18 -0000 1.37 *************** *** 55,58 **** --- 55,59 ---- fast_fourier_transform.cc \ fast_fourier_transform_for_real_sequence.cc \ + filter_coefficient_normalization.cc \ frequency_transform.cc \ generalized_cepstrum_gain_normalization.cc \ --- NEW FILE: filter_coefficient_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-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 "filter_coefficient_normalization.h" #include <algorithm> // std::transform #include <functional> // std::bind1st, std::multiplies namespace sptk { FilterCoefficientNormalization::FilterCoefficientNormalization(int num_order) : num_order_(num_order), is_valid_(true) { if (num_order_ < 0) { is_valid_ = false; } } bool FilterCoefficientNormalization::Run( const std::vector<double>& filter_coefficients, std::vector<double>* normalized_filter_coefficients) const { // check inputs if (!is_valid_ || filter_coefficients.size() != static_cast<std::size_t>(num_order_ + 1) || 0.0 == filter_coefficients[0] || NULL == normalized_filter_coefficients) { return false; } // prepare memory if (normalized_filter_coefficients->size() < static_cast<std::size_t>(num_order_ + 1)) { normalized_filter_coefficients->resize(num_order_ + 1); } // normarize (*normalized_filter_coefficients)[0] = 1.0 / filter_coefficients[0]; std::transform(filter_coefficients.begin() + 1, filter_coefficients.end(), normalized_filter_coefficients->begin() + 1, std::bind1st(std::multiplies<double>(), (*normalized_filter_coefficients)[0])); return true; } } // namespace sptk --- NEW FILE: norm0.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 <getopt.h> #include <fstream> #include <iomanip> #include <iostream> #include <sstream> #include <vector> #include "filter_coefficient_normalization.h" #include "sptk_utils.h" namespace { const int kDefaultNumOrder(25); void PrintUsage(std::ostream* stream) { // clang-format off *stream << std::endl; *stream << " norm0 - normalize coefficients" << std::endl; // NOLINT *stream << std::endl; *stream << " usage:" << std::endl; *stream << " norm0 [ options ] [ infile ] > stdout" << std::endl; *stream << " options:" << std::endl; *stream << " -m m : order ( int)[" << std::setw(5) << std::right << kDefaultNumOrder << "][ 0 <= m <= ]" << std::endl; // NOLINT *stream << " -h : print this message" << std::endl; *stream << " infile:" << std::endl; *stream << " coefficients (double)[stdin]" << std::endl; // NOLINT *stream << " stdout:" << std::endl; *stream << " normalized coefficients (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); for (;;) { const int option_char(getopt_long(argc, argv, "m:h", NULL, NULL)); 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("norm0", error_message); return 1; } 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("norm0", error_message); return 1; } std::istream& input_stream(ifs.fail() ? std::cin : ifs); // prepare for normalization sptk::FilterCoefficientNormalization filter_coefficient_normalization( num_order); if (!filter_coefficient_normalization.IsValid()) { std::ostringstream error_message; error_message << "Failed to set the condition"; sptk::PrintErrorMessage("norm0", error_message); return 1; } const int length(num_order + 1); std::vector<double> filter_coefficients(length); std::vector<double> normalized_filter_coefficients(length); while (sptk::ReadStream(false, 0, 0, length, &filter_coefficients, &input_stream)) { if (!filter_coefficient_normalization.Run( filter_coefficients, &normalized_filter_coefficients)) { std::ostringstream error_message; error_message << "Failed to normalize filter coefficients"; sptk::PrintErrorMessage("norm0", error_message); return 1; } if (!sptk::WriteStream(0, length, normalized_filter_coefficients, &std::cout)) { std::ostringstream error_message; error_message << "Failed to write normalized filter coefficients"; sptk::PrintErrorMessage("norm0", error_message); return 1; } } return 0; } --- NEW FILE: filter_coefficient_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-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_FILTER_COEFFICIENT_NORMALIZATION_H_ #define SPTK_SRC_FILTER_COEFFICIENT_NORMALIZATION_H_ #include <vector> // std::vector #include "sptk_utils.h" namespace sptk { class FilterCoefficientNormalization { public: // explicit FilterCoefficientNormalization(int num_order); // virtual ~FilterCoefficientNormalization() { } // int GetNumOrder() const { return num_order_; } // bool IsValid() const { return is_valid_; } // bool Run(const std::vector<double>& filter_coefficients, std::vector<double>* normalized_filter_coefficients) const; private: // const int num_order_; // bool is_valid_; // DISALLOW_COPY_AND_ASSIGN(FilterCoefficientNormalization); }; } // namespace sptk #endif // SPTK_SRC_FILTER_COEFFICIENT_NORMALIZATION_H_ |