Skip to content

Commit

Permalink
Merge pull request #63 from sp-nitech/iltcdf
Browse files Browse the repository at this point in the history
Add iltcdf
  • Loading branch information
takenori-y authored Jul 23, 2024
2 parents daa401f + 716f3cf commit 431de77
Show file tree
Hide file tree
Showing 15 changed files with 577 additions and 24 deletions.
2 changes: 2 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ set(CC_SOURCES
${SOURCE_DIR}/filter/all_pole_digital_filter.cc
${SOURCE_DIR}/filter/all_pole_lattice_digital_filter.cc
${SOURCE_DIR}/filter/all_zero_digital_filter.cc
${SOURCE_DIR}/filter/all_zero_lattice_digital_filter.cc
${SOURCE_DIR}/filter/infinite_impulse_response_digital_filter.cc
${SOURCE_DIR}/filter/inverse_mglsa_digital_filter.cc
${SOURCE_DIR}/filter/inverse_pseudo_quadrature_mirror_filter_banks.cc
Expand Down Expand Up @@ -313,6 +314,7 @@ set(MAIN_SOURCES
${SOURCE_DIR}/main/ifft.cc
${SOURCE_DIR}/main/ifft2.cc
${SOURCE_DIR}/main/ignorm.cc
${SOURCE_DIR}/main/iltcdf.cc
${SOURCE_DIR}/main/imglsadf.cc
${SOURCE_DIR}/main/impulse.cc
${SOURCE_DIR}/main/imsvq.cc
Expand Down
11 changes: 11 additions & 0 deletions doc/main/iltcdf.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
.. _iltcdf:

iltcdf
======

.. doxygenfile:: iltcdf.cc

.. seealso:: :ref:`lpc2par` :ref:`ltcdf`

.. doxygenclass:: sptk::AllZeroLatticeDigitalFilter
:members:
2 changes: 1 addition & 1 deletion doc/main/ltcdf.rst
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ ltcdf

.. doxygenfile:: ltcdf.cc

.. seealso:: :ref:`lpc2par`
.. seealso:: :ref:`lpc2par` :ref:`iltcdf`

.. doxygenclass:: sptk::AllPoleLatticeDigitalFilter
:members:
111 changes: 111 additions & 0 deletions include/SPTK/filter/all_zero_lattice_digital_filter.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
// ------------------------------------------------------------------------ //
// Copyright 2021 SPTK Working Group //
// //
// Licensed under the Apache License, Version 2.0 (the "License"); //
// you may not use this file except in compliance with the License. //
// You may obtain a copy of the License at //
// //
// http://www.apache.org/licenses/LICENSE-2.0 //
// //
// Unless required by applicable law or agreed to in writing, software //
// distributed under the License is distributed on an "AS IS" BASIS, //
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. //
// See the License for the specific language governing permissions and //
// limitations under the License. //
// ------------------------------------------------------------------------ //

#ifndef SPTK_FILTER_ALL_ZERO_LATTICE_DIGITAL_FILTER_H_
#define SPTK_FILTER_ALL_ZERO_LATTICE_DIGITAL_FILTER_H_

#include <vector> // std::vector

#include "SPTK/utils/sptk_utils.h"

namespace sptk {

/**
* Apply all-zero lattice filter for speech synthesis.
*
* Given the @f$M@f$-th order PARCOR coefficients,
* @f[
* \begin{array}{cccc}
* K, & k(1), & \ldots, & k(M),
* \end{array}
* @f]
* an output signal is obtained by applying the all-zero lattice filter to an
* input signal in time domain.
*/
class AllZeroLatticeDigitalFilter {
public:
/**
* Buffer for AllZeroLatticeDigitalFilter class.
*/
class Buffer {
public:
Buffer() {
}

virtual ~Buffer() {
}

private:
std::vector<double> d_;

friend class AllZeroLatticeDigitalFilter;
DISALLOW_COPY_AND_ASSIGN(Buffer);
};

/**
* @param[in] num_filter_order Order of filter coefficients, @f$M@f$.
*/
explicit AllZeroLatticeDigitalFilter(int num_filter_order);

virtual ~AllZeroLatticeDigitalFilter() {
}

/**
* @return Order of coefficients.
*/
int GetNumFilterOrder() const {
return num_filter_order_;
}

/**
* @return True if this object is valid.
*/
bool IsValid() const {
return is_valid_;
}

/**
* @param[in] filter_coefficients @f$M@f$-th order PARCOR coefficients.
* @param[in] filter_input Input signal.
* @param[out] filter_output Output signal.
* @param[in,out] buffer Buffer.
* @return True on success, false on failure.
*/
bool Run(const std::vector<double>& filter_coefficients, double filter_input,
double* filter_output,
AllZeroLatticeDigitalFilter::Buffer* buffer) const;

/**
* @param[in] filter_coefficients @f$M@f$-th order PARCOR coefficients.
* @param[in,out] input_and_output Input/output signal.
* @param[in,out] buffer Buffer.
* @return True on success, false on failure.
*/
bool Run(const std::vector<double>& filter_coefficients,
double* input_and_output,
AllZeroLatticeDigitalFilter::Buffer* buffer) const;

private:
const int num_filter_order_;

bool is_valid_;

DISALLOW_COPY_AND_ASSIGN(AllZeroLatticeDigitalFilter);
};

} // namespace sptk

#endif // SPTK_FILTER_ALL_ZERO_LATTICE_DIGITAL_FILTER_H_
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ class InputSourcePreprocessingForFilterGain : public InputSourceInterface {
enum FilterGainType {
kLinear = 0,
kLog,
kInverse,
kUnity,
kUnityForAllZeroFilter,
};
Expand Down
80 changes: 80 additions & 0 deletions src/filter/all_zero_lattice_digital_filter.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
// ------------------------------------------------------------------------ //
// Copyright 2021 SPTK Working Group //
// //
// Licensed under the Apache License, Version 2.0 (the "License"); //
// you may not use this file except in compliance with the License. //
// You may obtain a copy of the License at //
// //
// http://www.apache.org/licenses/LICENSE-2.0 //
// //
// Unless required by applicable law or agreed to in writing, software //
// distributed under the License is distributed on an "AS IS" BASIS, //
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. //
// See the License for the specific language governing permissions and //
// limitations under the License. //
// ------------------------------------------------------------------------ //

#include "SPTK/filter/all_zero_lattice_digital_filter.h"

#include <algorithm> // std::fill
#include <cstddef> // std::size_t

namespace sptk {

AllZeroLatticeDigitalFilter::AllZeroLatticeDigitalFilter(int num_filter_order)
: num_filter_order_(num_filter_order), is_valid_(true) {
if (num_filter_order_ < 0) {
is_valid_ = false;
return;
}
}

bool AllZeroLatticeDigitalFilter::Run(
const std::vector<double>& filter_coefficients, double filter_input,
double* filter_output, AllZeroLatticeDigitalFilter::Buffer* buffer) const {
// Check inputs
if (!is_valid_ ||
filter_coefficients.size() !=
static_cast<std::size_t>(num_filter_order_ + 1) ||
NULL == filter_output || NULL == buffer) {
return false;
}

// Prepare memories.
if (buffer->d_.size() != static_cast<std::size_t>(num_filter_order_)) {
buffer->d_.resize(num_filter_order_);
std::fill(buffer->d_.begin(), buffer->d_.end(), 0.0);
}

if (0 == num_filter_order_) {
*filter_output = filter_input * filter_coefficients[0];
return true;
}

const double* k(&(filter_coefficients[1]));
double* d(&buffer->d_[0]);
double sum(filter_input);
double next_d(filter_input);

// Apply all-zero lattice filter.
for (int m(0); m < num_filter_order_; ++m) {
const double tmp(d[m] + k[m] * sum);
sum += k[m] * d[m];
d[m] = next_d;
next_d = tmp;
}

// Save result.
*filter_output = sum * filter_coefficients[0];

return true;
}

bool AllZeroLatticeDigitalFilter::Run(
const std::vector<double>& filter_coefficients, double* input_and_output,
AllZeroLatticeDigitalFilter::Buffer* buffer) const {
if (NULL == input_and_output) return false;
return Run(filter_coefficients, *input_and_output, input_and_output, buffer);
}

} // namespace sptk
5 changes: 5 additions & 0 deletions src/input/input_source_preprocessing_for_filter_gain.cc
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,11 @@ bool InputSourcePreprocessingForFilterGain::Get(std::vector<double>* buffer) {
(*buffer)[0] = std::exp((*buffer)[0]);
break;
}
case kInverse: {
if (0.0 == (*buffer)[0]) return false;
(*buffer)[0] = 1.0 / (*buffer)[0];
break;
}
case kUnity: {
(*buffer)[0] = 1.0;
break;
Expand Down
Loading

0 comments on commit 431de77

Please sign in to comment.