From: Keiichiro O. <ur...@us...> - 2016-10-07 00:47:54
|
Update of /cvsroot/sp-tk/SPTK4/src In directory sfp-cvs-1.v30.ch3.sourceforge.com:/tmp/cvs-serv10558 Modified Files: Makefile Added Files: normal_distributed_random_value_generation.cc normal_distributed_random_value_generation.h nrand.cc Log Message: add nrand command --- NEW FILE: nrand.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 "normal_distributed_random_value_generation.h" #include "sptk_utils.h" #include <cmath> // std::sqrt namespace { const int kDefaultOutputLength(256); const int kDefaultSeed(1); const double kDefaultMean(0.0); const double kDefaultStandardDeviation(1.0); void PrintUsage(std::ostream* stream) { *stream << std::endl; *stream << " nrand - generate normal distributed random value" << std::endl; *stream << std::endl; *stream << " usage:" << std::endl; *stream << " nrand [ options ] > stdout" << std::endl; *stream << " options:" << std::endl; *stream << " -l l : output length [" << kDefaultOutputLength << "]" << std::endl; // NOLINT *stream << " -s s : seed [" << kDefaultSeed << "]" << std::endl; // NOLINT *stream << " -m m : mean [" << kDefaultMean << "]" << std::endl; // NOLINT *stream << " -v v : variance [" << kDefaultStandardDeviation * kDefaultStandardDeviation << "]" << std::endl; // NOLINT *stream << " -d d : standard deviation [" << kDefaultStandardDeviation << "]" << std::endl; // NOLINT *stream << " -h : print this message" << std::endl; *stream << " stdout:" << std::endl; *stream << " random values (double)" << std::endl; *stream << " notice:" << std::endl; *stream << " if l<0, generate infinite sequence" << std::endl; *stream << std::endl; *stream << " SPTK: version " << sptk::kVersion << std::endl; *stream << std::endl; } } // namespace int main(int argc, char* argv[]) { int output_length(kDefaultOutputLength); int seed(kDefaultSeed); double mean(kDefaultMean); double standard_deviation(kDefaultStandardDeviation); for (;;) { const char option_char(getopt(argc, argv, "l:s:m:v:d:h")); if (-1 == option_char) break; switch (option_char) { case 'l': { if (!sptk::ConvertStringToInteger(optarg, &output_length)) { std::ostringstream error_message; error_message << "The argument for the -l option must be integer"; sptk::PrintErrorMessage("nrand", error_message); return 1; } break; } case 's': { if (!sptk::ConvertStringToInteger(optarg, &seed)) { std::ostringstream error_message; error_message << "The argument for the -s option must be integer"; sptk::PrintErrorMessage("nrand", error_message); return 1; } break; } case 'm': { if (!sptk::ConvertStringToDouble(optarg, &mean)) { std::ostringstream error_message; error_message << "The argument for the -m option must be double"; sptk::PrintErrorMessage("nrand", error_message); return 1; } break; } case 'v': { double variance; if (!sptk::ConvertStringToDouble(optarg, &variance) || variance < 0.0) { std::ostringstream error_message; error_message << "The argument for the -v option must be double"; sptk::PrintErrorMessage("nrand", error_message); return 1; } standard_deviation = std::sqrt(variance); break; } case 'd': { if (!sptk::ConvertStringToDouble(optarg, &standard_deviation) || standard_deviation < 0.0) { std::ostringstream error_message; error_message << "The argument for the -v option must be double"; sptk::PrintErrorMessage("nrand", error_message); return 1; } break; } case 'h': { PrintUsage(&std::cout); return 0; } default: { PrintUsage(&std::cerr); return 1; } } } sptk::NormalDistributedRandomValueGeneration generator(seed); sptk::NormalDistributedRandomValueGeneration::Buffer buffer; for (int i(0); output_length < 0 || i < output_length; ++i) { double output; if (!generator.Run(&output, &buffer)) { std::ostringstream error_message; error_message << "Failed to generate M sequence"; sptk::PrintErrorMessage("nrand", error_message); return 1; } output = mean + output * standard_deviation; if (!sptk::WriteStream(output, &std::cout)) { std::ostringstream error_message; error_message << "Failed to write an output sequence"; sptk::PrintErrorMessage("nrand", error_message); return 1; } } return 0; } Index: Makefile =================================================================== RCS file: /cvsroot/sp-tk/SPTK4/src/Makefile,v retrieving revision 1.9 retrieving revision 1.10 diff -C2 -d -r1.9 -r1.10 *** Makefile 6 Oct 2016 10:24:54 -0000 1.9 --- Makefile 7 Oct 2016 00:47:51 -0000 1.10 *************** *** 53,56 **** --- 53,57 ---- levinson_durbin_recursion.cc \ m_sequence_generation.cc \ + normal_distributed_random_value_generation.cc \ sptk_utils.cc --- NEW FILE: normal_distributed_random_value_generation.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_NORMAL_DISTRIBUTED_RANDOM_VALUE_GENERATION_H_ #define SPTK_SRC_NORMAL_DISTRIBUTED_RANDOM_VALUE_GENERATION_H_ #include "sptk_utils.h" #include <cstdint> // std::uint64_t namespace sptk { class NormalDistributedRandomValueGeneration { public: class Buffer { public: // Buffer(); // ~Buffer() {} // void Clear(); private: // bool is_first_; std::uint64_t next_; bool switch_; double r1_, r2_, s_; // friend class NormalDistributedRandomValueGeneration; // DISALLOW_COPY_AND_ASSIGN(Buffer); }; public: // explicit NormalDistributedRandomValueGeneration(int seed) : seed_(seed) {} // virtual ~NormalDistributedRandomValueGeneration() {} // bool Run(double *output, NormalDistributedRandomValueGeneration::Buffer *buffer) const; private: // const int seed_; // DISALLOW_COPY_AND_ASSIGN(NormalDistributedRandomValueGeneration); }; } // namespace sptk #endif // SPTK_SRC_NORMAL_DISTRIBUTED_RANDOM_VALUE_GENERATION_H_ --- NEW FILE: normal_distributed_random_value_generation.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 "normal_distributed_random_value_generation.h" #include <cmath> // std::sqrt, std::log namespace { // pseudorandom generation double PseudoRandomGeneration(std::uint64_t *next) { double r; *next = (*next) * 1103515245L + 12345; r = ((*next) / 65536L) % 32768L; return r / 32767.0; } }; // namespace namespace sptk { NormalDistributedRandomValueGeneration::Buffer::Buffer() : is_first_(true), switch_(true) { } void NormalDistributedRandomValueGeneration::Buffer::Clear() { is_first_ = true; switch_ = true; } bool NormalDistributedRandomValueGeneration::Run(double *output, NormalDistributedRandomValueGeneration::Buffer *buffer) const { // check input if (NULL == output || NULL == buffer) { return false; } // set seed if (buffer->is_first_) { buffer->next_ = static_cast<std::uint64_t>(seed_); buffer->is_first_ = false; } if (buffer->switch_) { buffer->switch_ = false; buffer->s_ = 0.0; while (1.0 < buffer->s_ || 0.0 == buffer->s_) { buffer->r1_ = 2.0 * PseudoRandomGeneration(&(buffer->next_)) - 1.0; buffer->r2_ = 2.0 * PseudoRandomGeneration(&(buffer->next_)) - 1.0; buffer->s_ = buffer->r1_ * buffer->r1_ + buffer->r2_ * buffer->r2_; } buffer->s_ = std::sqrt(-2.0 * std::log(buffer->s_) / buffer->s_); *output = buffer->r1_ * buffer->s_; } else { buffer->switch_ = true; *output = buffer->r2_ * buffer->s_; } return true; } } // namespace sptk |