From: Takenori Y. <tak...@us...> - 2016-10-11 06:01:31
|
Update of /cvsroot/sp-tk/SPTK4/src In directory sfp-cvs-1.v30.ch3.sourceforge.com:/tmp/cvs-serv28100 Modified Files: Makefile all_zero_digital_filter.cc all_zero_digital_filter.h input_source_preprocessing_for_filter_gain.cc input_source_preprocessing_for_filter_gain.h Added Files: zerodf.cc Log Message: add zerodf command Index: all_zero_digital_filter.cc =================================================================== RCS file: /cvsroot/sp-tk/SPTK4/src/all_zero_digital_filter.cc,v retrieving revision 1.1 retrieving revision 1.2 diff -C2 -d -r1.1 -r1.2 *** all_zero_digital_filter.cc 23 Aug 2016 08:15:59 -0000 1.1 --- all_zero_digital_filter.cc 11 Oct 2016 06:01:28 -0000 1.2 *************** *** 55,59 **** AllZeroDigitalFilter::StoredSignals* stored_signals) const { // check inputs ! if (filter_coefficients.empty() || NULL == filter_output || NULL == stored_signals) { --- 55,60 ---- AllZeroDigitalFilter::StoredSignals* stored_signals) const { // check inputs ! if (filter_coefficients.size() != ! static_cast<std::size_t>(num_filter_order_ + 1) || NULL == filter_output || NULL == stored_signals) { *************** *** 62,69 **** // prepare memory - const int num_filter_order(filter_coefficients.size() - 1); if (stored_signals->signals_.size() != ! static_cast<std::size_t>(num_filter_order)) { ! stored_signals->signals_.resize(num_filter_order); std::fill(stored_signals->signals_.begin(), stored_signals->signals_.end(), 0.0); --- 63,69 ---- // prepare memory if (stored_signals->signals_.size() != ! static_cast<std::size_t>(num_filter_order_)) { ! stored_signals->signals_.resize(num_filter_order_); std::fill(stored_signals->signals_.begin(), stored_signals->signals_.end(), 0.0); *************** *** 72,76 **** // set value const double gained_input(filter_input * filter_coefficients[0]); ! if (0 == num_filter_order) { *filter_output = gained_input; return true; --- 72,76 ---- // set value const double gained_input(filter_input * filter_coefficients[0]); ! if (0 == num_filter_order_) { *filter_output = gained_input; return true; *************** *** 83,95 **** // apply filter double sum(gained_input); ! if (is_transposed_) { sum += signals[0]; ! for (int i(1); i < num_filter_order; ++i) { signals[i - 1] = signals[i] + coefficients[i] * filter_input; } ! signals[num_filter_order - 1] = coefficients[num_filter_order] * filter_input; } else { ! for (int i(num_filter_order - 1); 0 < i; --i) { sum += coefficients[i + 1] * signals[i]; signals[i] = signals[i - 1]; --- 83,95 ---- // apply filter double sum(gained_input); ! if (transposition_) { sum += signals[0]; ! for (int i(1); i < num_filter_order_; ++i) { signals[i - 1] = signals[i] + coefficients[i] * filter_input; } ! signals[num_filter_order_ - 1] = coefficients[num_filter_order_] * filter_input; } else { ! for (int i(num_filter_order_ - 1); 0 < i; --i) { sum += coefficients[i + 1] * signals[i]; signals[i] = signals[i - 1]; Index: all_zero_digital_filter.h =================================================================== RCS file: /cvsroot/sp-tk/SPTK4/src/all_zero_digital_filter.h,v retrieving revision 1.1 retrieving revision 1.2 diff -C2 -d -r1.1 -r1.2 *** all_zero_digital_filter.h 23 Aug 2016 08:15:58 -0000 1.1 --- all_zero_digital_filter.h 11 Oct 2016 06:01:28 -0000 1.2 *************** *** 80,85 **** // ! explicit AllZeroDigitalFilter(bool is_transposed) : ! is_transposed_(is_transposed) {} // --- 80,85 ---- // ! AllZeroDigitalFilter(int num_filter_order, bool transposition) : ! num_filter_order_(num_filter_order), transposition_(transposition) {} // *************** *** 87,92 **** // bool GetTranspositionFlag() const { ! return is_transposed_; } --- 87,97 ---- // + int GetNumFilterOrder() const { + return num_filter_order_; + } + + // bool GetTranspositionFlag() const { ! return transposition_; } *************** *** 98,102 **** private: // ! const bool is_transposed_; // --- 103,110 ---- private: // ! const int num_filter_order_; ! ! // ! const bool transposition_; // Index: input_source_preprocessing_for_filter_gain.cc =================================================================== RCS file: /cvsroot/sp-tk/SPTK4/src/input_source_preprocessing_for_filter_gain.cc,v retrieving revision 1.1 retrieving revision 1.2 diff -C2 -d -r1.1 -r1.2 *** input_source_preprocessing_for_filter_gain.cc 8 Oct 2016 15:14:59 -0000 1.1 --- input_source_preprocessing_for_filter_gain.cc 11 Oct 2016 06:01:29 -0000 1.2 *************** *** 45,49 **** #include "input_source_preprocessing_for_filter_gain.h" ! #include <cmath> // std::exp namespace sptk { --- 45,51 ---- #include "input_source_preprocessing_for_filter_gain.h" ! #include <algorithm> // std::transform ! #include <cmath> // std::exp ! #include <functional> // std::bind1st, std::multiplies namespace sptk { *************** *** 61,74 **** switch (gain_type_) { ! case InputSourcePreprocessingForFilterGain::FilterGainType::kUnity: buffer->at(0) = 1.0; break; ! case InputSourcePreprocessingForFilterGain::FilterGainType::kLinear: break; ! case InputSourcePreprocessingForFilterGain::FilterGainType::kLog: buffer->at(0) = std::exp(buffer->at(0)); break; ! default: return false; } --- 63,87 ---- switch (gain_type_) { ! case InputSourcePreprocessingForFilterGain::FilterGainType::kUnity: { buffer->at(0) = 1.0; break; ! } ! case InputSourcePreprocessingForFilterGain::FilterGainType::kUnityForAllZeroFilter: { // NOLINT ! if (0.0 == buffer->at(0)) return false; ! const double inverse_of_b0(1.0 / buffer->at(0)); ! std::transform(buffer->begin(), buffer->end(), buffer->begin(), ! std::bind1st(std::multiplies<double>(), inverse_of_b0)); break; ! } ! case InputSourcePreprocessingForFilterGain::FilterGainType::kLinear: { ! break; ! } ! case InputSourcePreprocessingForFilterGain::FilterGainType::kLog: { buffer->at(0) = std::exp(buffer->at(0)); break; ! } ! default: { return false; + } } Index: Makefile =================================================================== RCS file: /cvsroot/sp-tk/SPTK4/src/Makefile,v retrieving revision 1.12 retrieving revision 1.13 diff -C2 -d -r1.12 -r1.13 *** Makefile 11 Oct 2016 05:31:10 -0000 1.12 --- Makefile 11 Oct 2016 06:01:28 -0000 1.13 *************** *** 46,49 **** --- 46,50 ---- SOURCES = all_pole_digital_filter.cc \ + all_zero_digital_filter.cc \ autocorrelation.cc \ cepstrum_to_minimum_phase_impulse_response.cc \ Index: input_source_preprocessing_for_filter_gain.h =================================================================== RCS file: /cvsroot/sp-tk/SPTK4/src/input_source_preprocessing_for_filter_gain.h,v retrieving revision 1.1 retrieving revision 1.2 diff -C2 -d -r1.1 -r1.2 *** input_source_preprocessing_for_filter_gain.h 8 Oct 2016 15:14:59 -0000 1.1 --- input_source_preprocessing_for_filter_gain.h 11 Oct 2016 06:01:29 -0000 1.2 *************** *** 59,65 **** // enum class FilterGainType { ! kUnity = 0, ! kLinear, kLog, }; --- 59,66 ---- // enum class FilterGainType { ! kLinear = 0, kLog, + kUnity, + kUnityForAllZeroFilter, }; --- NEW FILE: zerodf.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 "all_zero_digital_filter.h" #include "input_source_from_stream.h" #include "input_source_interpolation.h" #include "input_source_preprocessing_for_filter_gain.h" #include "sptk_utils.h" namespace { const int kDefaultNumFilterOrder(25); const int kDefaultFramePeriod(100); const int kDefaultInterpolationPeriod(1); const bool kDefaultTranspositionFlag(false); const bool kDefaultGainFlag(true); void PrintUsage(std::ostream* stream) { *stream << std::endl; *stream << " zerodf - all-zero digital filter for speech synthesis" << std::endl; // NOLINT *stream << std::endl; *stream << " usage:" << std::endl; *stream << " zerodf [ options ] bfile [ infile ] > stdout" << std::endl; *stream << " options:" << std::endl; *stream << " -m m : order of filter coefficients [" << kDefaultNumFilterOrder << "]" << std::endl; // NOLINT *stream << " -p p : frame period [" << kDefaultFramePeriod << "]" << std::endl; // NOLINT *stream << " -i i : interpolation period [" << kDefaultInterpolationPeriod << "]" << std::endl; // NOLINT *stream << " -t : transpose filter [" << sptk::ConvertBooleanToString(kDefaultTranspositionFlag) << "]" << std::endl; // NOLINT *stream << " -k : filtering without gain [" << sptk::ConvertBooleanToString(!kDefaultGainFlag) << "]" << std::endl; // NOLINT *stream << " -h : print this message" << std::endl; *stream << " infile:" << std::endl; *stream << " filter input (double) [stdin]" << std::endl; *stream << " stdout:" << std::endl; *stream << " filter output (double)" << std::endl; *stream << " bfile:" << std::endl; *stream << " filter (MA) coefficients (double)" << std::endl; *stream << std::endl; *stream << " SPTK: version " << sptk::kVersion << std::endl; *stream << std::endl; } } // namespace int main(int argc, char* argv[]) { int num_filter_order(kDefaultNumFilterOrder); int frame_period(kDefaultFramePeriod); int interpolation_period(kDefaultInterpolationPeriod); bool transposition_flag(kDefaultTranspositionFlag); bool gain_flag(kDefaultGainFlag); for (;;) { const char option_char(getopt(argc, argv, "m:p:i:tkh")); if (-1 == option_char) break; switch (option_char) { case 'm': { if (!sptk::ConvertStringToInteger(optarg, &num_filter_order) || num_filter_order < 0) { std::ostringstream error_message; error_message << "The argument for the -m option must be a non-negative integer"; sptk::PrintErrorMessage("zerodf", error_message); return 1; } break; } case 'p': { if (!sptk::ConvertStringToInteger(optarg, &frame_period) || frame_period <= 0) { std::ostringstream error_message; error_message << "The argument for the -p option must be a positive integer"; sptk::PrintErrorMessage("zerodf", error_message); return 1; } break; } case 'i': { if (!sptk::ConvertStringToInteger(optarg, &interpolation_period) || interpolation_period < 0) { std::ostringstream error_message; error_message << "The argument for the -i option must be a non-negative integer"; sptk::PrintErrorMessage("zerodf", error_message); return 1; } break; } case 't': { transposition_flag = true; break; } case 'k': { gain_flag = false; break; } case 'h': { PrintUsage(&std::cout); return 0; } default: { PrintUsage(&std::cerr); return 1; } } } if (frame_period / 2 < interpolation_period) { std::ostringstream error_message; error_message << "Interpolation period should not be greater than half frame period"; sptk::PrintErrorMessage("zerodf", error_message); return 1; } // Get input file names. const char* filter_coefficients_file; const char* filter_input_file; const int num_rest_args(argc - optind); if (2 == num_rest_args) { filter_coefficients_file = argv[argc - 2]; filter_input_file = argv[argc - 1]; } else if (1 == num_rest_args) { filter_coefficients_file = argv[argc - 1]; filter_input_file = NULL; } else { std::ostringstream error_message; error_message << "Just two input files, bfile and infile, are required"; sptk::PrintErrorMessage("zerodf", error_message); return 1; } // Open stream for reading filter coeffcients. std::ifstream ifs1; ifs1.open(filter_coefficients_file, std::ios::in | std::ios::binary); if (ifs1.fail()) { std::ostringstream error_message; error_message << "Cannot open file " << filter_coefficients_file; sptk::PrintErrorMessage("zerodf", error_message); return 1; } std::istream& stream_for_filter_coefficients(ifs1); // Open stream for reading input signals. std::ifstream ifs2; ifs2.open(filter_input_file, std::ios::in | std::ios::binary); if (ifs2.fail() && NULL != filter_input_file) { std::ostringstream error_message; error_message << "Cannot open file " << filter_input_file; sptk::PrintErrorMessage("zerodf", error_message); return 1; } std::istream& stream_for_filter_input(ifs2.fail() ? std::cin : ifs2); // Prepare variables for filtering. const int filter_length(num_filter_order + 1); std::vector<double> filter_coefficients(filter_length); sptk::InputSourceFromStream input_source(false, filter_length, &stream_for_filter_coefficients); const sptk::InputSourcePreprocessingForFilterGain::FilterGainType gain_type( gain_flag ? sptk::InputSourcePreprocessingForFilterGain::FilterGainType::kLinear : sptk::InputSourcePreprocessingForFilterGain::FilterGainType::kUnityForAllZeroFilter); // NOLINT sptk::InputSourcePreprocessingForFilterGain preprocessing(gain_type, &input_source); sptk::InputSourceInterpolation interpolation(frame_period, interpolation_period, true, &preprocessing); double filter_input, filter_output; sptk::AllZeroDigitalFilter filter(num_filter_order, transposition_flag); sptk::AllZeroDigitalFilter::StoredSignals stored_signals; if (!interpolation.IsValid()) { std::ostringstream error_message; error_message << "Failed to set the conditions for filtering"; sptk::PrintErrorMessage("zerodf", error_message); return 1; } while (sptk::ReadStream(&filter_input, &stream_for_filter_input)) { if (!interpolation.Get(&filter_coefficients)) { std::ostringstream error_message; error_message << "Cannot get filter coefficients"; sptk::PrintErrorMessage("zerodf", error_message); return 1; } if (!filter.Run(filter_coefficients, filter_input, &filter_output, &stored_signals)) { std::ostringstream error_message; error_message << "Failed to apply all-zero digital filter"; sptk::PrintErrorMessage("zerodf", error_message); return 1; } if (!sptk::WriteStream(filter_output, &std::cout)) { std::ostringstream error_message; error_message << "Failed to write a filter output"; sptk::PrintErrorMessage("zerodf", error_message); return 1; } } return 0; } |