/*---------------------------------------------------------------------------*\
FILE........: newamp1.c
AUTHOR......: David Rowe
DATE CREATED: Jan 2017
Quantisation functions for the sinusoidal coder, using "newamp1"
algorithm that resamples variable rate L [Am} to a fixed rate K then
VQs.
\*---------------------------------------------------------------------------*/
/*
Copyright David Rowe 2017
All rights reserved.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License version 2.1, as
published by the Free Software Foundation. This program is
distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this program; if not, see .
*/
#include
#include
#include
#include
#include
#include "defines.h"
#include "phase.h"
#include "quantise.h"
#include "mbest.h"
#include "newamp1.h"
#define NEWAMP1_VQ_MBEST_DEPTH 5 /* how many candidates we keep for each stage of mbest search */
/*---------------------------------------------------------------------------*\
FUNCTION....: interp_para()
AUTHOR......: David Rowe
DATE CREATED: Jan 2017
General 2nd order parabolic interpolator. Used splines orginally,
but this is much simpler and we don't need much accuracy. Given two
vectors of points xp and yp, find interpolated values y at points x.
\*---------------------------------------------------------------------------*/
void interp_para(float y[], float xp[], float yp[], int np, float x[], int n)
{
assert(np >= 3);
int k,i;
float xi, x1, y1, x2, y2, x3, y3, a, b;
k = 0;
for (i=0; iL; m++) {
AmdB[m] = 20.0*log10(model->A[m]+1E-16);
if (AmdB[m] > AmdB_peak) {
AmdB_peak = AmdB[m];
}
rate_L_sample_freqs_kHz[m] = m*model->Wo*(c2const->Fs/2000.0)/M_PI;
//printf("m: %d AmdB: %f AmdB_peak: %f sf: %f\n", m, AmdB[m], AmdB_peak, rate_L_sample_freqs_kHz[m]);
}
/* clip between peak and peak -50dB, to reduce dynamic range */
for(m=1; m<=model->L; m++) {
if (AmdB[m] < (AmdB_peak-50.0)) {
AmdB[m] = AmdB_peak-50.0;
}
}
interp_para(rate_K_vec, &rate_L_sample_freqs_kHz[1], &AmdB[1], model->L, rate_K_sample_freqs_kHz, K);
}
/*---------------------------------------------------------------------------*\
FUNCTION....: rate_K_mbest_encode
AUTHOR......: David Rowe
DATE CREATED: Jan 2017
Two stage rate K newamp1 VQ quantiser using mbest search.
\*---------------------------------------------------------------------------*/
float rate_K_mbest_encode(int *indexes, float *x, float *xq, int ndim, int mbest_entries)
{
int i, j, n1, n2;
const float *codebook1 = newamp1vq_cb[0].cb;
const float *codebook2 = newamp1vq_cb[1].cb;
struct MBEST *mbest_stage1, *mbest_stage2;
float target[ndim];
float w[ndim];
int index[MBEST_STAGES];
float mse, tmp;
/* codebook is compiled for a fixed K */
assert(ndim == newamp1vq_cb[0].k);
/* equal weights, could be argued mel freq axis gives freq dep weighting */
for(i=0; ilist[j].index[0];
for(i=0; ilist[0].index[1];
n2 = mbest_stage2->list[0].index[0];
mse = 0.0;
for (i=0;iL; m++) {
rate_L_sample_freqs_kHz[m] = m*model->Wo*(c2const->Fs/2000.0)/M_PI;
}
interp_para(&AmdB[1], rate_K_sample_freqs_kHz_term, rate_K_vec_term, K+2, &rate_L_sample_freqs_kHz[1], model->L);
for(m=1; m<=model->L; m++) {
model->A[m] = pow(10.0, AmdB[m]/20.0);
// printf("m: %d f: %f AdB: %f A: %f\n", m, rate_L_sample_freqs_kHz[m], AmdB[m], model->A[m]);
}
}
/*---------------------------------------------------------------------------*\
FUNCTION....: determine_phase
AUTHOR......: David Rowe
DATE CREATED: Jan 2017
Given a magnitude spectrum determine a phase spectrum, used for
phase synthesis with newamp1.
\*---------------------------------------------------------------------------*/
void determine_phase(C2CONST *c2const, COMP H[], MODEL *model, int Nfft, codec2_fft_cfg fwd_cfg, codec2_fft_cfg inv_cfg)
{
int i,m,b;
int Ns = Nfft/2+1;
float Gdbfk[Ns], sample_freqs_kHz[Ns], phase[Ns];
float AmdB[MAX_AMP+1], rate_L_sample_freqs_kHz[MAX_AMP+1];
for(m=1; m<=model->L; m++) {
assert(model->A[m] != 0.0);
AmdB[m] = 20.0*log10f(model->A[m]);
rate_L_sample_freqs_kHz[m] = (float)m*model->Wo*(c2const->Fs/2000.0)/M_PI;
}
for(i=0; iFs/1000.0)*(float)i/Nfft;
}
interp_para(Gdbfk, &rate_L_sample_freqs_kHz[1], &AmdB[1], model->L, sample_freqs_kHz, Ns);
mag_to_phase(phase, Gdbfk, Nfft, fwd_cfg, inv_cfg);
for(m=1; m<=model->L; m++) {
b = floorf(0.5+m*model->Wo*Nfft/(2.0*M_PI));
H[m].real = cosf(phase[b]); H[m].imag = sinf(phase[b]);
}
}
/*---------------------------------------------------------------------------*\
FUNCTION....: newamp1_model_to_indexes
AUTHOR......: David Rowe
DATE CREATED: Jan 2017
newamp1 encoder for amplitdues {Am}. Given the rate L model
parameters, outputs VQ and energy quantiser indexes.
\*---------------------------------------------------------------------------*/
void newamp1_model_to_indexes(C2CONST *c2const,
int indexes[],
MODEL *model,
float rate_K_vec[],
float rate_K_sample_freqs_kHz[],
int K,
float *mean,
float rate_K_vec_no_mean[],
float rate_K_vec_no_mean_[]
)
{
int k;
/* convert variable rate L to fixed rate K */
resample_const_rate_f(c2const, model, rate_K_vec, rate_K_sample_freqs_kHz, K);
/* remove mean and two stage VQ */
float sum = 0.0;
for(k=0; kvoiced) {
int index = encode_log_Wo(c2const, model->Wo, 6);
if (index == 0) {
index = 1;
}
indexes[3] = index;
}
else {
indexes[3] = 0;
}
}
/*---------------------------------------------------------------------------*\
FUNCTION....: newamp1_interpolate
AUTHOR......: David Rowe
DATE CREATED: Jan 2017
\*---------------------------------------------------------------------------*/
void newamp1_interpolate(float interpolated_surface_[], float left_vec[], float right_vec[], int K)
{
int i, k;
int M = 4;
float c;
/* (linearly) interpolate 25Hz amplitude vectors back to 100Hz */
for(i=0,c=1.0; i