/*---------------------------------------------------------------------------*\
FILE........: quantise.c
AUTHOR......: David Rowe
DATE CREATED: 31/5/92
Quantisation functions for the sinusoidal coder.
\*---------------------------------------------------------------------------*/
/*
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
#include "defines.h"
#include "dump.h"
#include "quantise.h"
#include "lpc.h"
#include "lsp.h"
#include "kiss_fft.h"
#define LSP_DELTA1 0.01 /* grid spacing for LSP root searches */
/*---------------------------------------------------------------------------*\
FUNCTION HEADERS
\*---------------------------------------------------------------------------*/
float speech_to_uq_lsps(float lsp[], float ak[], float Sn[], float w[],
int order);
/*---------------------------------------------------------------------------*\
FUNCTIONS
\*---------------------------------------------------------------------------*/
int lsp_bits(int i) {
return lsp_cb[i].log2m;
}
int lspd_bits(int i) {
return lsp_cbd[i].log2m;
}
int lspdt_bits(int i) {
return lsp_cbdt[i].log2m;
}
int lsp_pred_vq_bits(int i) {
return lsp_cbjvm[i].log2m;
}
/*---------------------------------------------------------------------------*\
quantise_init
Loads the entire LSP quantiser comprised of several vector quantisers
(codebooks).
\*---------------------------------------------------------------------------*/
void quantise_init()
{
}
/*---------------------------------------------------------------------------*\
quantise
Quantises vec by choosing the nearest vector in codebook cb, and
returns the vector index. The squared error of the quantised vector
is added to se.
\*---------------------------------------------------------------------------*/
long quantise(const float * cb, float vec[], float w[], int k, int m, float *se)
/* float cb[][K]; current VQ codebook */
/* float vec[]; vector to quantise */
/* float w[]; weighting vector */
/* int k; dimension of vectors */
/* int m; size of codebook */
/* float *se; accumulated squared error */
{
float e; /* current error */
long besti; /* best index so far */
float beste; /* best error so far */
long j;
int i;
float diff;
besti = 0;
beste = 1E32;
for(j=0; j 0);
mbest = (struct MBEST *)malloc(sizeof(struct MBEST));
assert(mbest != NULL);
mbest->entries = entries;
mbest->list = (struct MBEST_LIST *)malloc(entries*sizeof(struct MBEST_LIST));
assert(mbest->list != NULL);
for(i=0; ientries; i++) {
for(j=0; jlist[i].index[j] = 0;
mbest->list[i].error = 1E32;
}
return mbest;
}
static void mbest_destroy(struct MBEST *mbest) {
assert(mbest != NULL);
free(mbest->list);
free(mbest);
}
/*---------------------------------------------------------------------------*\
mbest_insert
Insert the results of a vector to codebook entry comparison. The
list is ordered in order or error, so those entries with the
smallest error will be first on the list.
\*---------------------------------------------------------------------------*/
static void mbest_insert(struct MBEST *mbest, int index[], float error) {
int i, j, found;
struct MBEST_LIST *list = mbest->list;
int entries = mbest->entries;
found = 0;
for(i=0; ii; j--)
list[j] = list[j-1];
for(j=0; jentries; i++) {
for(j=0; jlist[i].index[j]);
printf(" %f\n", mbest->list[i].error);
}
}
/*---------------------------------------------------------------------------*\
mbest_search
Searches vec[] to a codebbook of vectors, and maintains a list of the mbest
closest matches.
\*---------------------------------------------------------------------------*/
static void mbest_search(
const float *cb, /* VQ codebook to search */
float vec[], /* target vector */
float w[], /* weighting vector */
int k, /* dimension of vector */
int m, /* number on entries in codebook */
struct MBEST *mbest, /* list of closest matches */
int index[] /* indexes that lead us here */
)
{
float e;
int i,j;
float diff;
for(j=0; jlist[j].index[0];
for(i=0; ilist[j].index[1];
index[1] = n2 = mbest_stage2->list[j].index[0];
for(i=0; ilist[j].index[2];
index[2] = n2 = mbest_stage3->list[j].index[1];
index[1] = n3 = mbest_stage3->list[j].index[0];
for(i=0; ilist[0].index[3];
n2 = mbest_stage4->list[0].index[2];
n3 = mbest_stage4->list[0].index[1];
n4 = mbest_stage4->list[0].index[0];
for (i=0;i {Am} LPC decode */
return snr;
}
#endif
/*---------------------------------------------------------------------------*\
lpc_post_filter()
Applies a post filter to the LPC synthesis filter power spectrum
Pw, which supresses the inter-formant energy.
The algorithm is from p267 (Section 8.6) of "Digital Speech",
edited by A.M. Kondoz, 1994 published by Wiley and Sons. Chapter 8
of this text is on the MBE vocoder, and this is a freq domain
adaptation of post filtering commonly used in CELP.
I used the Octave simulation lpcpf.m to get an understaing of the
algorithm.
Requires two more FFTs which is significantly more MIPs. However
it should be possible to implement this more efficiently in the
time domain. Just not sure how to handle relative time delays
between the synthesis stage and updating these coeffs. A smaller
FFT size might also be accetable to save CPU.
TODO:
[ ] sync var names between Octave and C version
[ ] doc gain normalisation
[ ] I think the first FFT is not rqd as we do the same
thing in aks_to_M2().
\*---------------------------------------------------------------------------*/
void lpc_post_filter(kiss_fft_cfg fft_fwd_cfg, MODEL *model, COMP Pw[], float ak[],
int order, int dump, float beta, float gamma, int bass_boost)
{
int i;
COMP x[FFT_ENC]; /* input to FFTs */
COMP Aw[FFT_ENC]; /* LPC analysis filter spectrum */
COMP Ww[FFT_ENC]; /* weighting spectrum */
float Rw[FFT_ENC]; /* R = WA */
float e_before, e_after, gain;
float Pfw[FFT_ENC]; /* Post filter mag spectrum */
float max_Rw, min_Rw;
float range, thresh, r, w;
int m, bin;
/* Determine LPC inverse filter spectrum 1/A(exp(jw)) -----------*/
/* we actually want the synthesis filter A(exp(jw)) but the
inverse (analysis) filter is easier to find as it's FIR, we
just use the inverse of 1/A to get the synthesis filter
A(exp(jw)) */
for(i=0; i max_Rw)
max_Rw = Rw[i];
if (Rw[i] < min_Rw)
min_Rw = Rw[i];
}
#ifdef DUMP
if (dump)
dump_Rw(Rw);
#endif
/* create post filter mag spectrum and apply ------------------*/
/* measure energy before post filtering */
e_before = 1E-4;
for(i=0; iL; m++) {
am = floor((m - 0.5)*model->Wo/r + 0.5);
bm = floor((m + 0.5)*model->Wo/r + 0.5);
Em = 0.0;
for(i=am; iA[m],2.0);
noise += pow(model->A[m] - Am,2.0);
/* This code significantly improves perf of LPC model, in
particular when combined with phase0. The LPC spectrum tends
to track just under the peaks of the spectral envelope, and
just above nulls. This algorithm does the reverse to
compensate - raising the amplitudes of spectral peaks, while
attenuating the null. This enhances the formants, and
supresses the energy between formants. */
if (sim_pf) {
if (Am > model->A[m])
Am *= 0.7;
if (Am < model->A[m])
Am *= 1.4;
}
model->A[m] = Am;
}
*snr = 10.0*log10(signal/noise);
}
/*---------------------------------------------------------------------------*\
FUNCTION....: encode_Wo()
AUTHOR......: David Rowe
DATE CREATED: 22/8/2010
Encodes Wo using a WO_LEVELS quantiser.
\*---------------------------------------------------------------------------*/
int encode_Wo(float Wo)
{
int index;
float Wo_min = TWO_PI/P_MAX;
float Wo_max = TWO_PI/P_MIN;
float norm;
norm = (Wo - Wo_min)/(Wo_max - Wo_min);
index = floor(WO_LEVELS * norm + 0.5);
if (index < 0 ) index = 0;
if (index > (WO_LEVELS-1)) index = WO_LEVELS-1;
return index;
}
/*---------------------------------------------------------------------------*\
FUNCTION....: decode_Wo()
AUTHOR......: David Rowe
DATE CREATED: 22/8/2010
Decodes Wo using a WO_LEVELS quantiser.
\*---------------------------------------------------------------------------*/
float decode_Wo(int index)
{
float Wo_min = TWO_PI/P_MAX;
float Wo_max = TWO_PI/P_MIN;
float step;
float Wo;
step = (Wo_max - Wo_min)/WO_LEVELS;
Wo = Wo_min + step*(index);
return Wo;
}
/*---------------------------------------------------------------------------*\
FUNCTION....: encode_Wo_dt()
AUTHOR......: David Rowe
DATE CREATED: 6 Nov 2011
Encodes Wo difference from last frame.
\*---------------------------------------------------------------------------*/
int encode_Wo_dt(float Wo, float prev_Wo)
{
int index, mask, max_index, min_index;
float Wo_min = TWO_PI/P_MAX;
float Wo_max = TWO_PI/P_MIN;
float norm;
norm = (Wo - prev_Wo)/(Wo_max - Wo_min);
index = floor(WO_LEVELS * norm + 0.5);
//printf("ENC index: %d ", index);
/* hard limit */
max_index = (1 << (WO_DT_BITS-1)) - 1;
min_index = - (max_index+1);
if (index > max_index) index = max_index;
if (index < min_index) index = min_index;
//printf("max_index: %d min_index: %d hard index: %d ",
// max_index, min_index, index);
/* mask so that only LSB WO_DT_BITS remain, bit WO_DT_BITS is the sign bit */
mask = ((1 << WO_DT_BITS) - 1);
index &= mask;
//printf("mask: 0x%x index: 0x%x\n", mask, index);
return index;
}
/*---------------------------------------------------------------------------*\
FUNCTION....: decode_Wo_dt()
AUTHOR......: David Rowe
DATE CREATED: 6 Nov 2011
Decodes Wo using WO_DT_BITS difference from last frame.
\*---------------------------------------------------------------------------*/
float decode_Wo_dt(int index, float prev_Wo)
{
float Wo_min = TWO_PI/P_MAX;
float Wo_max = TWO_PI/P_MIN;
float step;
float Wo;
int mask;
/* sign extend index */
//printf("DEC index: %d ");
if (index & (1 << (WO_DT_BITS-1))) {
mask = ~((1 << WO_DT_BITS) - 1);
index |= mask;
}
//printf("DEC mask: 0x%x index: %d \n", mask, index);
step = (Wo_max - Wo_min)/WO_LEVELS;
Wo = prev_Wo + step*(index);
/* bit errors can make us go out of range leading to all sorts of
probs like seg faults */
if (Wo > Wo_max) Wo = Wo_max;
if (Wo < Wo_min) Wo = Wo_min;
return Wo;
}
/*---------------------------------------------------------------------------*\
FUNCTION....: speech_to_uq_lsps()
AUTHOR......: David Rowe
DATE CREATED: 22/8/2010
Analyse a windowed frame of time domain speech to determine LPCs
which are the converted to LSPs for quantisation and transmission
over the channel.
\*---------------------------------------------------------------------------*/
float speech_to_uq_lsps(float lsp[],
float ak[],
float Sn[],
float w[],
int order
)
{
int i, roots;
float Wn[M];
float R[LPC_MAX+1];
float e, E;
e = 0.0;
for(i=0; iWo < (PI*150.0/4000)) {
model->A[1] *= 0.032;
}
}
/*---------------------------------------------------------------------------*\
FUNCTION....: encode_energy()
AUTHOR......: David Rowe
DATE CREATED: 22/8/2010
Encodes LPC energy using an E_LEVELS quantiser.
\*---------------------------------------------------------------------------*/
int encode_energy(float e)
{
int index;
float e_min = E_MIN_DB;
float e_max = E_MAX_DB;
float norm;
e = 10.0*log10(e);
norm = (e - e_min)/(e_max - e_min);
index = floor(E_LEVELS * norm + 0.5);
if (index < 0 ) index = 0;
if (index > (E_LEVELS-1)) index = E_LEVELS-1;
return index;
}
/*---------------------------------------------------------------------------*\
FUNCTION....: decode_energy()
AUTHOR......: David Rowe
DATE CREATED: 22/8/2010
Decodes energy using a E_LEVELS quantiser.
\*---------------------------------------------------------------------------*/
float decode_energy(int index)
{
float e_min = E_MIN_DB;
float e_max = E_MAX_DB;
float step;
float e;
step = (e_max - e_min)/E_LEVELS;
e = e_min + step*(index);
e = pow(10.0,e/10.0);
return e;
}
#ifdef NOT_USED
/*---------------------------------------------------------------------------*\
FUNCTION....: decode_amplitudes()
AUTHOR......: David Rowe
DATE CREATED: 22/8/2010
Given the amplitude quantiser indexes recovers the harmonic
amplitudes.
\*---------------------------------------------------------------------------*/
float decode_amplitudes(kiss_fft_cfg fft_fwd_cfg,
MODEL *model,
float ak[],
int lsp_indexes[],
int energy_index,
float lsps[],
float *e
)
{
float snr;
decode_lsps_scalar(lsps, lsp_indexes, LPC_ORD);
bw_expand_lsps(lsps, LPC_ORD);
lsp_to_lpc(lsps, ak, LPC_ORD);
*e = decode_energy(energy_index);
aks_to_M2(ak, LPC_ORD, model, *e, &snr, 1, 0, 0, 1);
apply_lpc_correction(model);
return snr;
}
#endif
static float ge_coeff[2] = {0.8, 0.9};
void compute_weights2(const float *x, const float *xp, float *w, int ndim)
{
w[0] = 30;
w[1] = 1;
if (x[1]<0)
{
w[0] *= .6;
w[1] *= .3;
}
if (x[1]<-10)
{
w[0] *= .3;
w[1] *= .3;
}
/* Higher weight if pitch is stable */
if (fabs(x[0]-xp[0])<.2)
{
w[0] *= 2;
w[1] *= 1.5;
} else if (fabs(x[0]-xp[0])>.5) /* Lower if not stable */
{
w[0] *= .5;
}
/* Lower weight for low energy */
if (x[1] < xp[1]-10)
{
w[1] *= .5;
}
if (x[1] < xp[1]-20)
{
w[1] *= .5;
}
//w[0] = 30;
//w[1] = 1;
/* Square the weights because it's applied on the squared error */
w[0] *= w[0];
w[1] *= w[1];
}
/*---------------------------------------------------------------------------*\
FUNCTION....: quantise_WoE()
AUTHOR......: Jean-Marc Valin & David Rowe
DATE CREATED: 29 Feb 2012
Experimental joint Wo and LPC energy vector quantiser developed by
Jean-Marc Valin. Exploits correlations between the difference in
the log pitch and log energy from frame to frame. For example
both the pitch and energy tend to only change by small amounts
during voiced speech, however it is important that these changes be
coded carefully. During unvoiced speech they both change a lot but
the ear is less sensitve to errors so coarser quantisation is OK.
The ear is sensitive to log energy and loq pitch so we quantise in
these domains. That way the error measure used to quantise the
values is close to way the ear senses errors.
See http://jmspeex.livejournal.com/10446.html
\*---------------------------------------------------------------------------*/
void quantise_WoE(MODEL *model, float *e, float xq[])
{
int i, n1;
float x[2];
float err[2];
float w[2];
const float *codebook1 = ge_cb[0].cb;
int nb_entries = ge_cb[0].m;
int ndim = ge_cb[0].k;
float Wo_min = TWO_PI/P_MAX;
float Wo_max = TWO_PI/P_MIN;
x[0] = log10((model->Wo/PI)*4000.0/50.0)/log10(2);
x[1] = 10.0*log10(1e-4 + *e);
compute_weights2(x, xq, w, ndim);
for (i=0;iWo = pow(2.0, xq[0])*(PI*50.0)/4000.0;
/* bit errors can make us go out of range leading to all sorts of
probs like seg faults */
if (model->Wo > Wo_max) model->Wo = Wo_max;
if (model->Wo < Wo_min) model->Wo = Wo_min;
model->L = PI/model->Wo; /* if we quantise Wo re-compute L */
*e = pow(10.0, xq[1]/10.0);
}
/*---------------------------------------------------------------------------*\
FUNCTION....: encode_WoE()
AUTHOR......: Jean-Marc Valin & David Rowe
DATE CREATED: 11 May 2012
Joint Wo and LPC energy vector quantiser developed my Jean-Marc
Valin. Returns index, and updated states xq[].
\*---------------------------------------------------------------------------*/
int encode_WoE(MODEL *model, float e, float xq[])
{
int i, n1;
float x[2];
float err[2];
float w[2];
const float *codebook1 = ge_cb[0].cb;
int nb_entries = ge_cb[0].m;
int ndim = ge_cb[0].k;
assert((1<Wo/PI)*4000.0/50.0)/log10(2);
x[1] = 10.0*log10(1e-4 + e);
compute_weights2(x, xq, w, ndim);
for (i=0;iWo = pow(2.0, xq[0])*(PI*50.0)/4000.0;
/* bit errors can make us go out of range leading to all sorts of
probs like seg faults */
if (model->Wo > Wo_max) model->Wo = Wo_max;
if (model->Wo < Wo_min) model->Wo = Wo_min;
model->L = PI/model->Wo; /* if we quantise Wo re-compute L */
*e = pow(10.0, xq[1]/10.0);
}