From: Keiichiro O. <ur...@us...> - 2016-10-07 06:50:34
|
Update of /cvsroot/sp-tk/SPTK4/src In directory sfp-cvs-1.v30.ch3.sourceforge.com:/tmp/cvs-serv26446 Modified Files: m_sequence_generation.cc m_sequence_generation.h mseq.cc normal_distributed_random_value_generation.cc normal_distributed_random_value_generation.h nrand.cc Added Files: random_generation_interface.h Log Message: add random generation interface class Index: m_sequence_generation.h =================================================================== RCS file: /cvsroot/sp-tk/SPTK4/src/m_sequence_generation.h,v retrieving revision 1.2 retrieving revision 1.3 diff -C2 -d -r1.2 -r1.3 *** m_sequence_generation.h 7 Oct 2016 01:03:25 -0000 1.2 --- m_sequence_generation.h 7 Oct 2016 06:50:32 -0000 1.3 *************** *** 46,80 **** #define SPTK_SRC_M_SEQUENCE_GENERATION_H_ #include "sptk_utils.h" namespace sptk { ! class MSequenceGeneration { ! public: ! class Buffer { ! public: ! // ! Buffer(); ! ! // ! ~Buffer() {} ! ! // ! void Clear(); ! ! private: ! // ! int x_; ! ! // ! friend class MSequenceGeneration; ! ! // ! DISALLOW_COPY_AND_ASSIGN(Buffer); ! }; ! public: // ! MSequenceGeneration() {} // --- 46,58 ---- #define SPTK_SRC_M_SEQUENCE_GENERATION_H_ + #include "random_generation_interface.h" #include "sptk_utils.h" namespace sptk { ! class MSequenceGeneration : public RandomGenerationInterface { public: // ! MSequenceGeneration(); // *************** *** 82,89 **** // ! bool Run(double *output, MSequenceGeneration::Buffer *buffer) const; private: // DISALLOW_COPY_AND_ASSIGN(MSequenceGeneration); }; --- 60,73 ---- // ! void Reset(); ! ! // ! bool Get(double *output); private: // + int x_; + + // DISALLOW_COPY_AND_ASSIGN(MSequenceGeneration); }; Index: nrand.cc =================================================================== RCS file: /cvsroot/sp-tk/SPTK4/src/nrand.cc,v retrieving revision 1.1 retrieving revision 1.2 diff -C2 -d -r1.1 -r1.2 *** nrand.cc 7 Oct 2016 00:47:51 -0000 1.1 --- nrand.cc 7 Oct 2016 06:50:32 -0000 1.2 *************** *** 156,164 **** 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"; --- 156,163 ---- sptk::NormalDistributedRandomValueGeneration generator(seed); for (int i(0); output_length < 0 || i < output_length; ++i) { double output; ! if (!generator.Get(&output)) { std::ostringstream error_message; error_message << "Failed to generate M sequence"; Index: mseq.cc =================================================================== RCS file: /cvsroot/sp-tk/SPTK4/src/mseq.cc,v retrieving revision 1.2 retrieving revision 1.3 diff -C2 -d -r1.2 -r1.3 *** mseq.cc 7 Oct 2016 01:03:25 -0000 1.2 --- mseq.cc 7 Oct 2016 06:50:32 -0000 1.3 *************** *** 100,108 **** sptk::MSequenceGeneration generator; - sptk::MSequenceGeneration::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"; --- 100,107 ---- sptk::MSequenceGeneration generator; for (int i(0); output_length < 0 || i < output_length; ++i) { double output; ! if (!generator.Get(&output)) { std::ostringstream error_message; error_message << "Failed to generate M-Sequence"; Index: m_sequence_generation.cc =================================================================== RCS file: /cvsroot/sp-tk/SPTK4/src/m_sequence_generation.cc,v retrieving revision 1.2 retrieving revision 1.3 diff -C2 -d -r1.2 -r1.3 *** m_sequence_generation.cc 7 Oct 2016 01:03:25 -0000 1.2 --- m_sequence_generation.cc 7 Oct 2016 06:50:32 -0000 1.3 *************** *** 56,70 **** namespace sptk { ! MSequenceGeneration::Buffer::Buffer() : x_(kInitialValue) { } ! void MSequenceGeneration::Buffer::Clear() { x_ = kInitialValue; } ! bool MSequenceGeneration::Run(double *output, ! MSequenceGeneration::Buffer *buffer) const { ! // check input ! if (NULL == output || NULL == buffer) { return false; } --- 56,69 ---- namespace sptk { ! MSequenceGeneration::MSequenceGeneration() : x_(kInitialValue) { } ! void MSequenceGeneration::Reset() { x_ = kInitialValue; } ! bool MSequenceGeneration::Get(double *output) { ! // check output ! if (NULL == output) { return false; } *************** *** 73,84 **** int x0, x28; ! buffer->x_ >>= 1; ! if (buffer->x_ & kB0) x0 = 1; else x0 = -1; ! if (buffer->x_ & kB28) x28 = 1; else --- 72,83 ---- int x0, x28; ! x_ >>= 1; ! if (x_ & kB0) x0 = 1; else x0 = -1; ! if (x_ & kB28) x28 = 1; else *************** *** 86,92 **** if (x0 + x28) ! buffer->x_ &= kB31F; else ! buffer->x_ |= kB31; *output = static_cast<double>(x0); --- 85,91 ---- if (x0 + x28) ! x_ &= kB31F; else ! x_ |= kB31; *output = static_cast<double>(x0); --- NEW FILE: random_generation_interface.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_RANDOM_GENERATION_INTERFACE_H_ #define SPTK_SRC_RANDOM_GENERATION_INTERFACE_H_ #include "sptk_utils.h" namespace sptk { class RandomGenerationInterface { public: // virtual ~RandomGenerationInterface() {} // virtual void Reset() = 0; // virtual bool Get(double *output) = 0; }; } // namespace sptk #endif // SPTK_SRC_RANDOM_GENERATION_INTERFACE_H_ Index: normal_distributed_random_value_generation.h =================================================================== RCS file: /cvsroot/sp-tk/SPTK4/src/normal_distributed_random_value_generation.h,v retrieving revision 1.1 retrieving revision 1.2 diff -C2 -d -r1.1 -r1.2 *** normal_distributed_random_value_generation.h 7 Oct 2016 00:47:51 -0000 1.1 --- normal_distributed_random_value_generation.h 7 Oct 2016 06:50:32 -0000 1.2 *************** *** 46,49 **** --- 46,50 ---- #define SPTK_SRC_NORMAL_DISTRIBUTED_RANDOM_VALUE_GENERATION_H_ + #include "random_generation_interface.h" #include "sptk_utils.h" *************** *** 52,85 **** 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) {} // --- 53,61 ---- namespace sptk { ! class NormalDistributedRandomValueGeneration : ! public RandomGenerationInterface { public: // ! explicit NormalDistributedRandomValueGeneration(int seed); // *************** *** 87,92 **** // ! bool Run(double *output, ! NormalDistributedRandomValueGeneration::Buffer *buffer) const; private: --- 63,70 ---- // ! void Reset(); ! ! // ! bool Get(double *output); private: *************** *** 95,98 **** --- 73,81 ---- // + std::uint64_t next_; + bool switch_; + double r1_, r2_, s_; + + // DISALLOW_COPY_AND_ASSIGN(NormalDistributedRandomValueGeneration); }; Index: normal_distributed_random_value_generation.cc =================================================================== RCS file: /cvsroot/sp-tk/SPTK4/src/normal_distributed_random_value_generation.cc,v retrieving revision 1.1 retrieving revision 1.2 diff -C2 -d -r1.1 -r1.2 *** normal_distributed_random_value_generation.cc 7 Oct 2016 00:47:51 -0000 1.1 --- normal_distributed_random_value_generation.cc 7 Oct 2016 06:50:32 -0000 1.2 *************** *** 63,101 **** 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_; } --- 63,96 ---- namespace sptk { ! NormalDistributedRandomValueGeneration::NormalDistributedRandomValueGeneration( ! int seed) : ! seed_(seed), switch_(true) { ! next_ = static_cast<std::uint64_t>(seed_); } ! void NormalDistributedRandomValueGeneration::Reset() { ! next_ = static_cast<std::uint64_t>(seed_); switch_ = true; } ! bool NormalDistributedRandomValueGeneration::Get(double *output) { ! // check output ! if (NULL == output) { return false; } ! if (switch_) { ! switch_ = false; ! s_ = 0.0; ! while (1.0 < s_ || 0.0 == s_) { ! r1_ = 2.0 * PseudoRandomGeneration(&(next_)) - 1.0; ! r2_ = 2.0 * PseudoRandomGeneration(&(next_)) - 1.0; ! s_ = r1_ * r1_ + r2_ * r2_; } ! s_ = std::sqrt(-2.0 * std::log(s_) / s_); ! *output = r1_ * s_; } else { ! switch_ = true; ! *output = r2_ * s_; } |