Skip to content

Commit

Permalink
Merge pull request #58 from sp-nitech/update_pqmf
Browse files Browse the repository at this point in the history
Update pqmf
  • Loading branch information
takenori-y authored Feb 14, 2024
2 parents 732829e + a46f2db commit 7c87798
Show file tree
Hide file tree
Showing 3 changed files with 105 additions and 34 deletions.
49 changes: 49 additions & 0 deletions egs/subband_decomposition/fig/run.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
#!/bin/bash
# ------------------------------------------------------------------------ #
# 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. #
# ------------------------------------------------------------------------ #

set -euo pipefail

sptk4=../../../bin
dump=dump

nband=4 # Number of subbands
order=39 # Order of filter
len=1024 # Number of FFT bins

mkdir -p $dump

# Get impulse response.
$sptk4/impulse -l $len |
$sptk4/pqmf -k $nband -m $order -r > $dump/imp.pqmf

# Convert to frequency domain.
for k in $(seq $nband); do
$sptk4/bcp -l $nband -s $((k - 1)) -e $((k - 1)) $dump/imp.pqmf |
$sptk4/spec -l $len > $dump/"$k".spec
done

# Draw frequency response.
export VIRTUAL_ENV_DISABLE_PROMPT=1
# shellcheck disable=SC1091
. ../../../tools/venv/bin/activate
eval cat $dump/"{1..$nband}".spec |
$sptk4/fdrw -n $((len / 2 + 1)) -g $dump/filter.png \
-xname "Normalized frequency [cyc]" \
-yname "Log amplitude [dB]" \
-xscale 0.5

echo "run.sh: successfully finished"
45 changes: 28 additions & 17 deletions src/main/ipqmf.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,12 @@
// limitations under the License. //
// ------------------------------------------------------------------------ //

#include <fstream> // std::ifstream
#include <iomanip> // std::setw
#include <iostream> // std::cerr, std::cin, std::cout, std::endl, etc.
#include <sstream> // std::ostringstream
#include <vector> // std::vector
#include <algorithm> // std::min
#include <fstream> // std::ifstream
#include <iomanip> // std::setw
#include <iostream> // std::cerr, std::cin, std::cout, std::endl, etc.
#include <sstream> // std::ostringstream
#include <vector> // std::vector

#include "GETOPT/ya_getopt.h"
#include "SPTK/filter/inverse_pseudo_quadrature_mirror_filter_banks.h"
Expand All @@ -32,6 +33,7 @@ const double kDefaultAttenuation(100.0);
const int kDefaultNumIteration(100);
const double kDefaultConvergenceThreshold(1e-6);
const double kDefaultInitialStepSize(1e-2);
const bool kDefaultDelayCompensation(true);

void PrintUsage(std::ostream* stream) {
// clang-format off
Expand All @@ -48,6 +50,7 @@ void PrintUsage(std::ostream* stream) {
*stream << " -i i : number of iterations ( int)[" << std::setw(5) << std::right << kDefaultNumIteration << "][ 0 < i <= ]" << std::endl; // NOLINT
*stream << " -d d : convergence threshold (double)[" << std::setw(5) << std::right << kDefaultConvergenceThreshold << "][ 0.0 <= d <= ]" << std::endl; // NOLINT
*stream << " -s s : initial step size (double)[" << std::setw(5) << std::right << kDefaultInitialStepSize << "][ 0 < s <= ]" << std::endl; // NOLINT
*stream << " -r : disable delay compensation ( bool)[" << std::setw(5) << std::right << sptk::ConvertBooleanToString(!kDefaultDelayCompensation) << "]" << std::endl; // NOLINT
*stream << " -h : print this message" << std::endl;
*stream << " infile:" << std::endl;
*stream << " filter-bank input (double)[stdin]" << std::endl; // NOLINT
Expand Down Expand Up @@ -76,6 +79,8 @@ void PrintUsage(std::ostream* stream) {
* - convergence threshold @f$(0 \le \epsilon)@f$
* - @b -s @e double
* - initial step size @f$(0 < \Delta)@f$
* - @b -r
* - disable delay compensation
* - @b infile @e str
* - double-type filter-bank input
* - @b stdout
Expand All @@ -99,9 +104,11 @@ int main(int argc, char* argv[]) {
int num_iteration(kDefaultNumIteration);
double convergence_threshold(kDefaultConvergenceThreshold);
double initial_step_size(kDefaultInitialStepSize);
bool delay_compensation(kDefaultDelayCompensation);

for (;;) {
const int option_char(getopt_long(argc, argv, "k:m:a:i:d:s:h", NULL, NULL));
const int option_char(
getopt_long(argc, argv, "k:m:a:i:d:s:rh", NULL, NULL));
if (-1 == option_char) break;

switch (option_char) {
Expand Down Expand Up @@ -171,6 +178,10 @@ int main(int argc, char* argv[]) {
}
break;
}
case 'r': {
delay_compensation = false;
break;
}
case 'h': {
PrintUsage(&std::cout);
return 0;
Expand Down Expand Up @@ -227,7 +238,7 @@ int main(int argc, char* argv[]) {
const int delay(sptk::IsEven(num_filter_order) ? num_filter_order / 2
: (num_filter_order + 1) / 2);

int n(0);
int num_read(0);
while (
sptk::ReadStream(false, 0, 0, num_subband, &input, &input_stream, NULL)) {
if (!synthesis.Run(input, &output, &buffer)) {
Expand All @@ -236,25 +247,25 @@ int main(int argc, char* argv[]) {
sptk::PrintErrorMessage("ipqmf", error_message);
return 1;
}
if (delay <= n) {
if (!delay_compensation || delay <= num_read++) {
if (!sptk::WriteStream(output, &std::cout)) {
std::ostringstream error_message;
error_message << "Failed to write reconstructed signal";
sptk::PrintErrorMessage("ipqmf", error_message);
return 1;
}
}
++n;
}

for (int i(delay - 1); 0 <= i; --i) {
if (!synthesis.Run(input, &output, &buffer)) {
std::ostringstream error_message;
error_message << "Failed to perform PQMF synthesis";
sptk::PrintErrorMessage("ipqmf", error_message);
return 1;
}
if (i < n) {
if (delay_compensation) {
const int n(std::min(delay, num_read));
for (int i(0); i < n; ++i) {
if (!synthesis.Run(input, &output, &buffer)) {
std::ostringstream error_message;
error_message << "Failed to perform PQMF synthesis";
sptk::PrintErrorMessage("ipqmf", error_message);
return 1;
}
if (!sptk::WriteStream(output, &std::cout)) {
std::ostringstream error_message;
error_message << "Failed to write reconstructed signal";
Expand Down
45 changes: 28 additions & 17 deletions src/main/pqmf.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,12 @@
// limitations under the License. //
// ------------------------------------------------------------------------ //

#include <fstream> // std::ifstream
#include <iomanip> // std::setw
#include <iostream> // std::cerr, std::cin, std::cout, std::endl, etc.
#include <sstream> // std::ostringstream
#include <vector> // std::vector
#include <algorithm> // std::min
#include <fstream> // std::ifstream
#include <iomanip> // std::setw
#include <iostream> // std::cerr, std::cin, std::cout, std::endl, etc.
#include <sstream> // std::ostringstream
#include <vector> // std::vector

#include "GETOPT/ya_getopt.h"
#include "SPTK/filter/pseudo_quadrature_mirror_filter_banks.h"
Expand All @@ -32,6 +33,7 @@ const double kDefaultAttenuation(100.0);
const int kDefaultNumIteration(100);
const double kDefaultConvergenceThreshold(1e-6);
const double kDefaultInitialStepSize(1e-2);
const bool kDefaultDelayCompensation(true);

void PrintUsage(std::ostream* stream) {
// clang-format off
Expand All @@ -48,6 +50,7 @@ void PrintUsage(std::ostream* stream) {
*stream << " -i i : number of iterations ( int)[" << std::setw(5) << std::right << kDefaultNumIteration << "][ 0 < i <= ]" << std::endl; // NOLINT
*stream << " -d d : convergence threshold (double)[" << std::setw(5) << std::right << kDefaultConvergenceThreshold << "][ 0.0 <= d <= ]" << std::endl; // NOLINT
*stream << " -s s : initial step size (double)[" << std::setw(5) << std::right << kDefaultInitialStepSize << "][ 0 < s <= ]" << std::endl; // NOLINT
*stream << " -r : disable delay compensation ( bool)[" << std::setw(5) << std::right << sptk::ConvertBooleanToString(!kDefaultDelayCompensation) << "]" << std::endl; // NOLINT
*stream << " -h : print this message" << std::endl;
*stream << " infile:" << std::endl;
*stream << " filter-bank input (double)[stdin]" << std::endl; // NOLINT
Expand Down Expand Up @@ -76,6 +79,8 @@ void PrintUsage(std::ostream* stream) {
* - convergence threshold @f$(0 \le \epsilon)@f$
* - @b -s @e double
* - initial step size @f$(0 < \Delta)@f$
* - @b -r
* - disable delay compensation
* - @b infile @e str
* - double-type filter-bank input
* - @b stdout
Expand All @@ -99,9 +104,11 @@ int main(int argc, char* argv[]) {
int num_iteration(kDefaultNumIteration);
double convergence_threshold(kDefaultConvergenceThreshold);
double initial_step_size(kDefaultInitialStepSize);
bool delay_compensation(kDefaultDelayCompensation);

for (;;) {
const int option_char(getopt_long(argc, argv, "k:m:a:i:d:s:h", NULL, NULL));
const int option_char(
getopt_long(argc, argv, "k:m:a:i:d:s:rh", NULL, NULL));
if (-1 == option_char) break;

switch (option_char) {
Expand Down Expand Up @@ -171,6 +178,10 @@ int main(int argc, char* argv[]) {
}
break;
}
case 'r': {
delay_compensation = false;
break;
}
case 'h': {
PrintUsage(&std::cout);
return 0;
Expand Down Expand Up @@ -226,33 +237,33 @@ int main(int argc, char* argv[]) {
const int delay(sptk::IsEven(num_filter_order) ? num_filter_order / 2
: (num_filter_order - 1) / 2);

int n(0);
int num_read(0);
while (sptk::ReadStream(&input, &input_stream)) {
if (!analysis.Run(input, &output, &buffer)) {
std::ostringstream error_message;
error_message << "Failed to perform PQMF analysis";
sptk::PrintErrorMessage("pqmf", error_message);
return 1;
}
if (delay <= n) {
if (!delay_compensation || delay <= num_read++) {
if (!sptk::WriteStream(0, num_subband, output, &std::cout, NULL)) {
std::ostringstream error_message;
error_message << "Failed to write subband signals";
sptk::PrintErrorMessage("pqmf", error_message);
return 1;
}
}
++n;
}

for (int i(delay - 1); 0 <= i; --i) {
if (!analysis.Run(input, &output, &buffer)) {
std::ostringstream error_message;
error_message << "Failed to perform PQMF analysis";
sptk::PrintErrorMessage("pqmf", error_message);
return 1;
}
if (i < n) {
if (delay_compensation) {
const int n(std::min(delay, num_read));
for (int i(0); i < n; ++i) {
if (!analysis.Run(input, &output, &buffer)) {
std::ostringstream error_message;
error_message << "Failed to perform PQMF analysis";
sptk::PrintErrorMessage("pqmf", error_message);
return 1;
}
if (!sptk::WriteStream(0, num_subband, output, &std::cout, NULL)) {
std::ostringstream error_message;
error_message << "Failed to write subband signals";
Expand Down

0 comments on commit 7c87798

Please sign in to comment.