|
| 1 | +// ------------------------------------------------------------------------ // |
| 2 | +// Copyright 2021 SPTK Working Group // |
| 3 | +// // |
| 4 | +// Licensed under the Apache License, Version 2.0 (the "License"); // |
| 5 | +// you may not use this file except in compliance with the License. // |
| 6 | +// You may obtain a copy of the License at // |
| 7 | +// // |
| 8 | +// http://www.apache.org/licenses/LICENSE-2.0 // |
| 9 | +// // |
| 10 | +// Unless required by applicable law or agreed to in writing, software // |
| 11 | +// distributed under the License is distributed on an "AS IS" BASIS, // |
| 12 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // |
| 13 | +// See the License for the specific language governing permissions and // |
| 14 | +// limitations under the License. // |
| 15 | +// ------------------------------------------------------------------------ // |
| 16 | + |
| 17 | +#include "SPTK/analysis/goertzel_analysis.h" |
| 18 | + |
| 19 | +#include <cmath> // std::round |
| 20 | +#include <cstddef> // std::size_t |
| 21 | +#include <vector> // std::vector |
| 22 | + |
| 23 | +namespace sptk { |
| 24 | + |
| 25 | +GoertzelAnalysis::GoertzelAnalysis(double sampling_rate, |
| 26 | + const std::vector<double>& frequencies, |
| 27 | + int fft_length) |
| 28 | + : is_valid_(true) { |
| 29 | + if (sampling_rate <= 0.0 || frequencies.empty() || fft_length <= 0) { |
| 30 | + is_valid_ = false; |
| 31 | + return; |
| 32 | + } |
| 33 | + |
| 34 | + const int table_size(frequencies.size()); |
| 35 | + sine_table_.resize(table_size); |
| 36 | + cosine_table_.resize(table_size); |
| 37 | + for (int k(0); k < table_size; ++k) { |
| 38 | + if (frequencies[k] < 0.0 || 0.5 * sampling_rate <= frequencies[k]) { |
| 39 | + is_valid_ = false; |
| 40 | + break; |
| 41 | + } |
| 42 | + const double n(std::round(fft_length * frequencies[k] / sampling_rate)); |
| 43 | + const double omega(sptk::kTwoPi * n / fft_length); |
| 44 | + sine_table_[k] = std::sin(omega); |
| 45 | + cosine_table_[k] = std::cos(omega); |
| 46 | + } |
| 47 | +} |
| 48 | + |
| 49 | +bool GoertzelAnalysis::Run(const std::vector<double>& signals, |
| 50 | + std::vector<double>* real_part, |
| 51 | + std::vector<double>* imag_part) const { |
| 52 | + // Check inputs. |
| 53 | + if (!is_valid_ || NULL == real_part || NULL == imag_part) { |
| 54 | + return false; |
| 55 | + } |
| 56 | + |
| 57 | + // Prepare memories. |
| 58 | + const int table_size(sine_table_.size()); |
| 59 | + if (real_part->size() != static_cast<std::size_t>(table_size)) { |
| 60 | + real_part->resize(table_size); |
| 61 | + } |
| 62 | + if (imag_part->size() != static_cast<std::size_t>(table_size)) { |
| 63 | + imag_part->resize(table_size); |
| 64 | + } |
| 65 | + |
| 66 | + const int signal_size(signals.size()); |
| 67 | + const double* x(&(signals[0])); |
| 68 | + double* real(&((*real_part)[0])); |
| 69 | + double* imag(&((*imag_part)[0])); |
| 70 | + |
| 71 | + for (int k(0); k < table_size; ++k) { |
| 72 | + // Perform second-order IIR filtering. |
| 73 | + const double c(2.0 * cosine_table_[k]); |
| 74 | + double s1(0.0), s2(0.0); |
| 75 | + { |
| 76 | + double s0; |
| 77 | + for (int t(0); t < signal_size; ++t) { |
| 78 | + s0 = x[t] + c * s1 - s2; |
| 79 | + s2 = s1; |
| 80 | + s1 = s0; |
| 81 | + } |
| 82 | + } |
| 83 | + // Perform first-order FIR filtering. |
| 84 | + real[k] = s1 * cosine_table_[k] - s2; |
| 85 | + imag[k] = s1 * sine_table_[k]; |
| 86 | + } |
| 87 | + |
| 88 | + return true; |
| 89 | +} |
| 90 | + |
| 91 | +} // namespace sptk |
0 commit comments