Skip to content

Commit

Permalink
Merge pull request #67 from sp-nitech/update
Browse files Browse the repository at this point in the history
Support sawtooth generation
  • Loading branch information
takenori-y authored Oct 29, 2024
2 parents 7fca038 + 7f696ce commit 0313fa8
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 11 deletions.
5 changes: 3 additions & 2 deletions src/filter/median_filter.cc
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

#include "SPTK/filter/median_filter.h"

#include <algorithm> // std::sort
#include <algorithm> // std::partial_sort
#include <cstddef> // std::size_t
#include <vector> // std::vector

Expand Down Expand Up @@ -90,8 +90,9 @@ bool MedianFilter::Get(std::vector<double>* 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 {
Expand Down
6 changes: 4 additions & 2 deletions src/main/median.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
// limitations under the License. //
// ------------------------------------------------------------------------ //

#include <algorithm> // std::sort
#include <algorithm> // std::partial_sort
#include <fstream> // std::ifstream
#include <iomanip> // std::setw
#include <iostream> // std::cerr, std::cin, std::cout, std::endl, etc.
Expand Down Expand Up @@ -61,7 +61,9 @@ bool OutputMedian(const std::vector<std::vector<double> >& 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]) *
Expand Down
35 changes: 28 additions & 7 deletions src/main/pitch_mark.cc
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ enum OutputFormats {
kPositionInSamples,
kSine,
kCosine,
kSawtooth,
kNumOutputFormats
};

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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<double> f0;
std::vector<double> pitch_mark;
sptk::PitchExtractionInterface::Polarity polarity;
Expand Down Expand Up @@ -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<int>(std::round(pitch_mark[n]))
Expand All @@ -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);
Expand All @@ -380,7 +401,7 @@ int main(int argc, char* argv[]) {
break;
}
default: {
break;
return 1;
}
}

Expand Down

0 comments on commit 0313fa8

Please sign in to comment.