From ac4a270bc87d9dbb6c5bf03cff80ab70d1cfd2b6 Mon Sep 17 00:00:00 2001 From: takenori-y Date: Tue, 29 Oct 2024 14:37:28 +0900 Subject: [PATCH 1/2] use partial_sort instead of sort --- src/filter/median_filter.cc | 5 +++-- src/main/median.cc | 6 ++++-- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/src/filter/median_filter.cc b/src/filter/median_filter.cc index b08a3767..a5c9395b 100644 --- a/src/filter/median_filter.cc +++ b/src/filter/median_filter.cc @@ -16,7 +16,7 @@ #include "SPTK/filter/median_filter.h" -#include // std::sort +#include // std::partial_sort #include // std::size_t #include // std::vector @@ -90,8 +90,9 @@ bool MedianFilter::Get(std::vector* output) { continue; } - std::sort(flat_.begin(), flat_.begin() + num_valid_numbers); const int half_size(num_valid_numbers / 2); + std::partial_sort(flat_.begin(), flat_.begin() + half_size + 1, + flat_.begin() + num_valid_numbers); if (IsEven(num_valid_numbers)) { (*output)[m] = 0.5 * (flat_[half_size - 1] + flat_[half_size]); } else { diff --git a/src/main/median.cc b/src/main/median.cc index 37c38fe3..deb6c03e 100644 --- a/src/main/median.cc +++ b/src/main/median.cc @@ -14,7 +14,7 @@ // limitations under the License. // // ------------------------------------------------------------------------ // -#include // std::sort +#include // std::partial_sort #include // std::ifstream #include // std::setw #include // std::cerr, std::cin, std::cout, std::endl, etc. @@ -61,7 +61,9 @@ bool OutputMedian(const std::vector >& input_vectors) { for (int i(0); i < num_vector; ++i) { vector_for_sort[i] = input_vectors[i][data_index]; } - std::sort(vector_for_sort.begin(), vector_for_sort.end()); + std::partial_sort(vector_for_sort.begin(), + vector_for_sort.begin() + half_num_vector + 1, + vector_for_sort.end()); const double median(0 == num_vector % 2 ? (vector_for_sort[half_num_vector - 1] + vector_for_sort[half_num_vector]) * From 7f696ce51c9c3b6a6b52cd66946e8838ec00bd73 Mon Sep 17 00:00:00 2001 From: takenori-y Date: Tue, 29 Oct 2024 18:41:13 +0900 Subject: [PATCH 2/2] support sawtooth generation --- src/main/pitch_mark.cc | 35 ++++++++++++++++++++++++++++------- 1 file changed, 28 insertions(+), 7 deletions(-) diff --git a/src/main/pitch_mark.cc b/src/main/pitch_mark.cc index a5c9ef32..a96b2001 100644 --- a/src/main/pitch_mark.cc +++ b/src/main/pitch_mark.cc @@ -35,6 +35,7 @@ enum OutputFormats { kPositionInSamples, kSine, kCosine, + kSawtooth, kNumOutputFormats }; @@ -64,6 +65,7 @@ void PrintUsage(std::ostream* stream) { *stream << " 2 (position in samples)" << std::endl; *stream << " 3 (sine waveform)" << std::endl; *stream << " 4 (cosine waveform)" << std::endl; + *stream << " 5 (sawtooth waveform)" << std::endl; *stream << " -h : print this message" << std::endl; *stream << " infile:" << std::endl; *stream << " waveform (double)[stdin]" << std::endl; // NOLINT @@ -99,6 +101,7 @@ void PrintUsage(std::ostream* stream) { * @arg @c 2 position in samples * @arg @c 3 sine waveform * @arg @c 4 cosine waveform + * @arg @c 5 sawtooth waveform * - @b infile @e str * - double-type waveform * - @b stdout @@ -262,7 +265,8 @@ int main(int argc, char* argv[]) { if (waveform.empty()) return 0; const bool sinusoidal_output(kSine == output_format || - kCosine == output_format); + kCosine == output_format || + kSawtooth == output_format); std::vector f0; std::vector pitch_mark; sptk::PitchExtractionInterface::Polarity polarity; @@ -332,8 +336,8 @@ int main(int argc, char* argv[]) { break; } case kSine: - case kCosine: { - const double bias(kSine == output_format ? 0.0 : 0.5 * sptk::kPi); + case kCosine: + case kSawtooth: { for (int n(0), i(0); n <= num_pitch_marks; ++n) { const int next_pitch_mark( n < num_pitch_marks ? static_cast(std::round(pitch_mark[n])) @@ -352,10 +356,27 @@ int main(int argc, char* argv[]) { std::accumulate(f0.begin() + i, f0.begin() + j, 0.0)); const double multiplier(sptk::kTwoPi / sum_f0); - double phase(bias); + double phase(0.0); for (int k(i); k < j; ++k) { - const double output(binary_polarity * std::sin(phase)); - if (!sptk::WriteStream(output, &std::cout)) { + double value; + switch (output_format) { + case kSine: { + value = std::sin(phase); + break; + } + case kCosine: { + value = std::cos(phase); + break; + } + case kSawtooth: { + value = std::fmod(phase, sptk::kTwoPi) / sptk::kPi - 1.0; + break; + } + default: { + return 1; + } + } + if (!sptk::WriteStream(binary_polarity * value, &std::cout)) { std::ostringstream error_message; error_message << "Failed to write sinusoidal sequence"; sptk::PrintErrorMessage("pitch_mark", error_message); @@ -380,7 +401,7 @@ int main(int argc, char* argv[]) { break; } default: { - break; + return 1; } }