/*---------------------------------------------------------------------------*\ 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 "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; } #if VECTOR_QUANTISATION /*---------------------------------------------------------------------------*\ quantise_uniform Simulates uniform quantising of a float. \*---------------------------------------------------------------------------*/ void quantise_uniform(float *val, float min, float max, int bits) { int levels = 1 << (bits-1); float norm; int index; /* hard limit to quantiser range */ printf("min: %f max: %f val: %f ", min, max, val[0]); if (val[0] < min) val[0] = min; if (val[0] > max) val[0] = max; norm = (*val - min)/(max-min); printf("%f norm: %f ", val[0], norm); index = fabs(levels*norm + 0.5); *val = min + index*(max-min)/levels; printf("index %d val_: %f\n", index, val[0]); } #endif /*---------------------------------------------------------------------------*\ 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; besti = 0; beste = 1E32; for(j=0; j {Am} LPC decode */ return snr; } /*---------------------------------------------------------------------------*\ aks_to_M2() Transforms the linear prediction coefficients to spectral amplitude samples. This function determines A(m) from the average energy per band using an FFT. \*---------------------------------------------------------------------------*/ void aks_to_M2( float ak[], /* LPC's */ int order, MODEL *model, /* sinusoidal model parameters for this frame */ float E, /* energy term */ float *snr, /* signal to noise ratio for this frame in dB */ int dump /* true to dump sample to dump file */ ) { COMP Pw[FFT_DEC]; /* power spectrum */ int i,m; /* loop variables */ int am,bm; /* limits of current band */ float r; /* no. rads/bin */ float Em; /* energy in band */ float Am; /* spectral amplitude sample */ float signal, noise; r = TWO_PI/(FFT_DEC); /* Determine DFT of A(exp(jw)) --------------------------------------------*/ 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); 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....: 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; 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 WO_BITS 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; } /*---------------------------------------------------------------------------*\ FUNCTION....: encode_amplitudes() AUTHOR......: David Rowe DATE CREATED: 22/8/2010 Time domain LPC is used model the amplitudes which are then converted to LSPs and quantised. So we don't actually encode the amplitudes directly, rather we derive an equivalent representation from the time domain speech. \*---------------------------------------------------------------------------*/ void encode_amplitudes(int lsp_indexes[], int *energy_index, MODEL *model, float Sn[], float w[]) { float lsps[LPC_ORD]; float ak[LPC_ORD+1]; float e; e = speech_to_uq_lsps(lsps, ak, Sn, w, LPC_ORD); encode_lsps(lsp_indexes, lsps, LPC_ORD); *energy_index = encode_energy(e); } /*---------------------------------------------------------------------------*\ FUNCTION....: decode_amplitudes() AUTHOR......: David Rowe DATE CREATED: 22/8/2010 Given the amplitude quantiser indexes recovers the harmonic amplitudes. \*---------------------------------------------------------------------------*/ float decode_amplitudes(MODEL *model, float ak[], int lsp_indexes[], int energy_index, float lsps[], float *e ) { float snr; decode_lsps(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); apply_lpc_correction(model); return snr; }