diff options
Diffstat (limited to 'src/quantise.c')
| -rw-r--r-- | src/quantise.c | 1159 |
1 files changed, 526 insertions, 633 deletions
diff --git a/src/quantise.c b/src/quantise.c index 36af5f4..da1d821 100644 --- a/src/quantise.c +++ b/src/quantise.c @@ -24,26 +24,27 @@ */ +#include "quantise.h" + #include <assert.h> #include <ctype.h> +#include <math.h> #include <stdio.h> #include <stdlib.h> #include <string.h> -#include <math.h> +#include "codec2_fft.h" #include "defines.h" #include "dump.h" -#include "quantise.h" #include "lpc.h" #include "lsp.h" -#include "codec2_fft.h" -#include "phase.h" #include "mbest.h" +#include "phase.h" #undef PROFILE #include "machdep.h" -#define LSP_DELTA1 0.01 /* grid spacing for LSP root searches */ +#define LSP_DELTA1 0.01 /* grid spacing for LSP root searches */ /*---------------------------------------------------------------------------*\ @@ -52,7 +53,7 @@ \*---------------------------------------------------------------------------*/ float speech_to_uq_lsps(float lsp[], float ak[], float Sn[], float w[], - int m_pitch, int order); + int m_pitch, int order); /*---------------------------------------------------------------------------*\ @@ -60,17 +61,11 @@ float speech_to_uq_lsps(float lsp[], float ak[], float Sn[], float w[], \*---------------------------------------------------------------------------*/ -int lsp_bits(int i) { - return lsp_cb[i].log2m; -} +int lsp_bits(int i) { return lsp_cb[i].log2m; } -int lspd_bits(int i) { - return lsp_cbd[i].log2m; -} +int lspd_bits(int i) { return lsp_cbd[i].log2m; } -int lsp_pred_vq_bits(int i) { - return lsp_cbjmv[i].log2m; -} +int lsp_pred_vq_bits(int i) { return lsp_cbjmv[i].log2m; } /*---------------------------------------------------------------------------*\ @@ -82,7 +77,7 @@ int lsp_pred_vq_bits(int i) { \*---------------------------------------------------------------------------*/ -long quantise(const float * cb, float vec[], float w[], int k, int m, float *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 */ @@ -90,33 +85,31 @@ long quantise(const float * cb, float vec[], float w[], int k, int m, float *se) /* 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<m; j++) { - e = 0.0; - for(i=0; i<k; i++) { - diff = cb[j*k+i]-vec[i]; - e += (diff*w[i] * diff*w[i]); - } - if (e < beste) { - beste = e; - besti = j; - } - } - - *se += beste; - - return(besti); -} + 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 < m; j++) { + e = 0.0; + for (i = 0; i < k; i++) { + diff = cb[j * k + i] - vec[i]; + e += (diff * w[i] * diff * w[i]); + } + if (e < beste) { + beste = e; + besti = j; + } + } + + *se += beste; + return (besti); +} /*---------------------------------------------------------------------------*\ @@ -126,111 +119,89 @@ long quantise(const float * cb, float vec[], float w[], int k, int m, float *se) \*---------------------------------------------------------------------------*/ -void encode_lspds_scalar( - int indexes[], - float lsp[], - int order -) -{ - int i,k,m; - float lsp_hz[order]; - float lsp__hz[order]; - float dlsp[order]; - float dlsp_[order]; - float wt[order]; - const float *cb; - float se; - - for(i=0; i<order; i++) { - wt[i] = 1.0; - } - - /* convert from radians to Hz so we can use human readable - frequencies */ - - for(i=0; i<order; i++) - lsp_hz[i] = (4000.0/PI)*lsp[i]; +void encode_lspds_scalar(int indexes[], float lsp[], int order) { + int i, k, m; + float lsp_hz[order]; + float lsp__hz[order]; + float dlsp[order]; + float dlsp_[order]; + float wt[order]; + const float *cb; + float se; + + for (i = 0; i < order; i++) { + wt[i] = 1.0; + } - wt[0] = 1.0; - for(i=0; i<order; i++) { + /* convert from radians to Hz so we can use human readable + frequencies */ - /* find difference from previous quantised lsp */ + for (i = 0; i < order; i++) lsp_hz[i] = (4000.0 / PI) * lsp[i]; - if (i) - dlsp[i] = lsp_hz[i] - lsp__hz[i-1]; - else - dlsp[0] = lsp_hz[0]; + wt[0] = 1.0; + for (i = 0; i < order; i++) { + /* find difference from previous quantised lsp */ - k = lsp_cbd[i].k; - m = lsp_cbd[i].m; - cb = lsp_cbd[i].cb; - indexes[i] = quantise(cb, &dlsp[i], wt, k, m, &se); - dlsp_[i] = cb[indexes[i]*k]; + if (i) + dlsp[i] = lsp_hz[i] - lsp__hz[i - 1]; + else + dlsp[0] = lsp_hz[0]; - if (i) - lsp__hz[i] = lsp__hz[i-1] + dlsp_[i]; - else - lsp__hz[0] = dlsp_[0]; - } + k = lsp_cbd[i].k; + m = lsp_cbd[i].m; + cb = lsp_cbd[i].cb; + indexes[i] = quantise(cb, &dlsp[i], wt, k, m, &se); + dlsp_[i] = cb[indexes[i] * k]; + if (i) + lsp__hz[i] = lsp__hz[i - 1] + dlsp_[i]; + else + lsp__hz[0] = dlsp_[0]; + } } +void decode_lspds_scalar(float lsp_[], int indexes[], int order) { + int i, k; + float lsp__hz[order]; + float dlsp_[order]; + const float *cb; -void decode_lspds_scalar( - float lsp_[], - int indexes[], - int order -) -{ - int i,k; - float lsp__hz[order]; - float dlsp_[order]; - const float *cb; + for (i = 0; i < order; i++) { + k = lsp_cbd[i].k; + cb = lsp_cbd[i].cb; + dlsp_[i] = cb[indexes[i] * k]; - for(i=0; i<order; i++) { - - k = lsp_cbd[i].k; - cb = lsp_cbd[i].cb; - dlsp_[i] = cb[indexes[i]*k]; - - if (i) - lsp__hz[i] = lsp__hz[i-1] + dlsp_[i]; - else - lsp__hz[0] = dlsp_[0]; - - lsp_[i] = (PI/4000.0)*lsp__hz[i]; - } + if (i) + lsp__hz[i] = lsp__hz[i - 1] + dlsp_[i]; + else + lsp__hz[0] = dlsp_[0]; + lsp_[i] = (PI / 4000.0) * lsp__hz[i]; + } } -#define MIN(a,b) ((a)<(b)?(a):(b)) +#define MIN(a, b) ((a) < (b) ? (a) : (b)) #define MAX_ENTRIES 16384 -void compute_weights(const float *x, float *w, int ndim) -{ +void compute_weights(const float *x, float *w, int ndim) { int i; - w[0] = MIN(x[0], x[1]-x[0]); - for (i=1;i<ndim-1;i++) - w[i] = MIN(x[i]-x[i-1], x[i+1]-x[i]); - w[ndim-1] = MIN(x[ndim-1]-x[ndim-2], PI-x[ndim-1]); + w[0] = MIN(x[0], x[1] - x[0]); + for (i = 1; i < ndim - 1; i++) w[i] = MIN(x[i] - x[i - 1], x[i + 1] - x[i]); + w[ndim - 1] = MIN(x[ndim - 1] - x[ndim - 2], PI - x[ndim - 1]); - for (i=0;i<ndim;i++) - w[i] = 1./(.01+w[i]); + for (i = 0; i < ndim; i++) w[i] = 1. / (.01 + w[i]); } -int find_nearest(const float *codebook, int nb_entries, float *x, int ndim) -{ +int find_nearest(const float *codebook, int nb_entries, float *x, int ndim) { int i, j; float min_dist = 1e15; int nearest = 0; - for (i=0;i<nb_entries;i++) - { - float dist=0; - for (j=0;j<ndim;j++) - dist += (x[j]-codebook[i*ndim+j])*(x[j]-codebook[i*ndim+j]); - if (dist<min_dist) - { + for (i = 0; i < nb_entries; i++) { + float dist = 0; + for (j = 0; j < ndim; j++) + dist += (x[j] - codebook[i * ndim + j]) * (x[j] - codebook[i * ndim + j]); + if (dist < min_dist) { min_dist = dist; nearest = i; } @@ -238,19 +209,18 @@ int find_nearest(const float *codebook, int nb_entries, float *x, int ndim) return nearest; } -int find_nearest_weighted(const float *codebook, int nb_entries, float *x, const float *w, int ndim) -{ +int find_nearest_weighted(const float *codebook, int nb_entries, float *x, + const float *w, int ndim) { int i, j; float min_dist = 1e15; int nearest = 0; - for (i=0;i<nb_entries;i++) - { - float dist=0; - for (j=0;j<ndim;j++) - dist += w[j]*(x[j]-codebook[i*ndim+j])*(x[j]-codebook[i*ndim+j]); - if (dist<min_dist) - { + for (i = 0; i < nb_entries; i++) { + float dist = 0; + for (j = 0; j < ndim; j++) + dist += w[j] * (x[j] - codebook[i * ndim + j]) * + (x[j] - codebook[i * ndim + j]); + if (dist < min_dist) { min_dist = dist; nearest = i; } @@ -258,8 +228,7 @@ int find_nearest_weighted(const float *codebook, int nb_entries, float *x, const return nearest; } -void lspjmv_quantise(float *x, float *xq, int order) -{ +void lspjmv_quantise(float *x, float *xq, int order) { int i, n1, n2, n3; float err[order], err2[order], err3[order]; float w[order], w2[order], w3[order]; @@ -267,67 +236,60 @@ void lspjmv_quantise(float *x, float *xq, int order) const float *codebook2 = lsp_cbjmv[1].cb; const float *codebook3 = lsp_cbjmv[2].cb; - w[0] = MIN(x[0], x[1]-x[0]); - for (i=1;i<order-1;i++) - w[i] = MIN(x[i]-x[i-1], x[i+1]-x[i]); - w[order-1] = MIN(x[order-1]-x[order-2], PI-x[order-1]); + w[0] = MIN(x[0], x[1] - x[0]); + for (i = 1; i < order - 1; i++) w[i] = MIN(x[i] - x[i - 1], x[i + 1] - x[i]); + w[order - 1] = MIN(x[order - 1] - x[order - 2], PI - x[order - 1]); compute_weights(x, w, order); n1 = find_nearest(codebook1, lsp_cbjmv[0].m, x, order); - for (i=0;i<order;i++) - { - xq[i] = codebook1[order*n1+i]; + for (i = 0; i < order; i++) { + xq[i] = codebook1[order * n1 + i]; err[i] = x[i] - xq[i]; } - for (i=0;i<order/2;i++) - { - err2[i] = err[2*i]; - err3[i] = err[2*i+1]; - w2[i] = w[2*i]; - w3[i] = w[2*i+1]; + for (i = 0; i < order / 2; i++) { + err2[i] = err[2 * i]; + err3[i] = err[2 * i + 1]; + w2[i] = w[2 * i]; + w3[i] = w[2 * i + 1]; } - n2 = find_nearest_weighted(codebook2, lsp_cbjmv[1].m, err2, w2, order/2); - n3 = find_nearest_weighted(codebook3, lsp_cbjmv[2].m, err3, w3, order/2); + n2 = find_nearest_weighted(codebook2, lsp_cbjmv[1].m, err2, w2, order / 2); + n3 = find_nearest_weighted(codebook3, lsp_cbjmv[2].m, err3, w3, order / 2); - for (i=0;i<order/2;i++) - { - xq[2*i] += codebook2[order*n2/2+i]; - xq[2*i+1] += codebook3[order*n3/2+i]; + for (i = 0; i < order / 2; i++) { + xq[2 * i] += codebook2[order * n2 / 2 + i]; + xq[2 * i + 1] += codebook3[order * n3 / 2 + i]; } } -int check_lsp_order(float lsp[], int order) -{ - int i; - float tmp; - int swaps = 0; - - for(i=1; i<order; i++) - if (lsp[i] < lsp[i-1]) { - //fprintf(stderr, "swap %d\n",i); - swaps++; - tmp = lsp[i-1]; - lsp[i-1] = lsp[i]-0.1; - lsp[i] = tmp+0.1; - i = 1; /* start check again, as swap may have caused out of order */ - } - - return swaps; +int check_lsp_order(float lsp[], int order) { + int i; + float tmp; + int swaps = 0; + + for (i = 1; i < order; i++) + if (lsp[i] < lsp[i - 1]) { + // fprintf(stderr, "swap %d\n",i); + swaps++; + tmp = lsp[i - 1]; + lsp[i - 1] = lsp[i] - 0.1; + lsp[i] = tmp + 0.1; + i = 1; /* start check again, as swap may have caused out of order */ + } + + return swaps; } -void force_min_lsp_dist(float lsp[], int order) -{ - int i; +void force_min_lsp_dist(float lsp[], int order) { + int i; - for(i=1; i<order; i++) - if ((lsp[i]-lsp[i-1]) < 0.01) { - lsp[i] += 0.01; - } + for (i = 1; i < order; i++) + if ((lsp[i] - lsp[i - 1]) < 0.01) { + lsp[i] += 0.01; + } } - /*---------------------------------------------------------------------------*\ lpc_post_filter() @@ -358,104 +320,97 @@ void force_min_lsp_dist(float lsp[], int order) \*---------------------------------------------------------------------------*/ void lpc_post_filter(codec2_fftr_cfg fftr_fwd_cfg, float Pw[], float ak[], - int order, int dump, float beta, float gamma, int bass_boost, float E) -{ - int i; - float x[FFT_ENC]; /* input to FFTs */ - COMP Ww[FFT_ENC/2+1]; /* weighting spectrum */ - float Rw[FFT_ENC/2+1]; /* R = WA */ - float e_before, e_after, gain; - float Pfw; - float max_Rw, min_Rw; - float coeff; - PROFILE_VAR(tstart, tfft1, taw, tfft2, tww, tr); - - PROFILE_SAMPLE(tstart); - - /* Determine weighting filter spectrum W(exp(jw)) ---------------*/ - - for(i=0; i<FFT_ENC; i++) { - x[i] = 0.0; - } + int order, int dump, float beta, float gamma, + int bass_boost, float E) { + int i; + float x[FFT_ENC]; /* input to FFTs */ + COMP Ww[FFT_ENC / 2 + 1]; /* weighting spectrum */ + float Rw[FFT_ENC / 2 + 1]; /* R = WA */ + float e_before, e_after, gain; + float Pfw; + float max_Rw, min_Rw; + float coeff; + PROFILE_VAR(tstart, tfft1, taw, tfft2, tww, tr); - x[0] = ak[0]; - coeff = gamma; - for(i=1; i<=order; i++) { - x[i] = ak[i] * coeff; - coeff *= gamma; - } - codec2_fftr(fftr_fwd_cfg, x, Ww); + PROFILE_SAMPLE(tstart); - PROFILE_SAMPLE_AND_LOG(tfft2, taw, " fft2"); + /* Determine weighting filter spectrum W(exp(jw)) ---------------*/ - for(i=0; i<FFT_ENC/2; i++) { - Ww[i].real = Ww[i].real*Ww[i].real + Ww[i].imag*Ww[i].imag; - } + for (i = 0; i < FFT_ENC; i++) { + x[i] = 0.0; + } + + x[0] = ak[0]; + coeff = gamma; + for (i = 1; i <= order; i++) { + x[i] = ak[i] * coeff; + coeff *= gamma; + } + codec2_fftr(fftr_fwd_cfg, x, Ww); - PROFILE_SAMPLE_AND_LOG(tww, tfft2, " Ww"); + PROFILE_SAMPLE_AND_LOG(tfft2, taw, " fft2"); - /* Determined combined filter R = WA ---------------------------*/ + for (i = 0; i < FFT_ENC / 2; i++) { + Ww[i].real = Ww[i].real * Ww[i].real + Ww[i].imag * Ww[i].imag; + } - max_Rw = 0.0; min_Rw = 1E32; - for(i=0; i<FFT_ENC/2; i++) { - Rw[i] = sqrtf(Ww[i].real * Pw[i]); - if (Rw[i] > max_Rw) - max_Rw = Rw[i]; - if (Rw[i] < min_Rw) - min_Rw = Rw[i]; + PROFILE_SAMPLE_AND_LOG(tww, tfft2, " Ww"); - } + /* Determined combined filter R = WA ---------------------------*/ - PROFILE_SAMPLE_AND_LOG(tr, tww, " R"); + max_Rw = 0.0; + min_Rw = 1E32; + for (i = 0; i < FFT_ENC / 2; i++) { + Rw[i] = sqrtf(Ww[i].real * Pw[i]); + if (Rw[i] > max_Rw) max_Rw = Rw[i]; + if (Rw[i] < min_Rw) min_Rw = Rw[i]; + } - #ifdef DUMP - if (dump) - dump_Rw(Rw); - #endif + PROFILE_SAMPLE_AND_LOG(tr, tww, " R"); - /* create post filter mag spectrum and apply ------------------*/ +#ifdef DUMP + if (dump) dump_Rw(Rw); +#endif - /* measure energy before post filtering */ + /* create post filter mag spectrum and apply ------------------*/ - e_before = 1E-4; - for(i=0; i<FFT_ENC/2; i++) - e_before += Pw[i]; + /* measure energy before post filtering */ - /* apply post filter and measure energy */ + e_before = 1E-4; + for (i = 0; i < FFT_ENC / 2; i++) e_before += Pw[i]; - #ifdef DUMP - if (dump) - dump_Pwb(Pw); - #endif + /* apply post filter and measure energy */ +#ifdef DUMP + if (dump) dump_Pwb(Pw); +#endif - e_after = 1E-4; - for(i=0; i<FFT_ENC/2; i++) { - Pfw = powf(Rw[i], beta); - Pw[i] *= Pfw * Pfw; - e_after += Pw[i]; - } - gain = e_before/e_after; + e_after = 1E-4; + for (i = 0; i < FFT_ENC / 2; i++) { + Pfw = powf(Rw[i], beta); + Pw[i] *= Pfw * Pfw; + e_after += Pw[i]; + } + gain = e_before / e_after; - /* apply gain factor to normalise energy, and LPC Energy */ + /* apply gain factor to normalise energy, and LPC Energy */ - gain *= E; - for(i=0; i<FFT_ENC/2; i++) { - Pw[i] *= gain; - } + gain *= E; + for (i = 0; i < FFT_ENC / 2; i++) { + Pw[i] *= gain; + } - if (bass_boost) { - /* add 3dB to first 1 kHz to account for LP effect of PF */ + if (bass_boost) { + /* add 3dB to first 1 kHz to account for LP effect of PF */ - for(i=0; i<FFT_ENC/8; i++) { - Pw[i] *= 1.4*1.4; - } + for (i = 0; i < FFT_ENC / 8; i++) { + Pw[i] *= 1.4 * 1.4; } + } - PROFILE_SAMPLE_AND_LOG2(tr, " filt"); + PROFILE_SAMPLE_AND_LOG2(tr, " filt"); } - /*---------------------------------------------------------------------------*\ aks_to_M2() @@ -466,134 +421,125 @@ void lpc_post_filter(codec2_fftr_cfg fftr_fwd_cfg, float Pw[], float ak[], \*---------------------------------------------------------------------------*/ -void aks_to_M2( - codec2_fftr_cfg fftr_fwd_cfg, - 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 */ - int sim_pf, /* true to simulate a post filter */ - int pf, /* true to enable actual LPC post filter */ - int bass_boost, /* enable LPC filter 0-1kHz 3dB boost */ - float beta, - float gamma, /* LPC post filter parameters */ - COMP Aw[] /* output 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 */ +void aks_to_M2(codec2_fftr_cfg fftr_fwd_cfg, 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 */ + int sim_pf, /* true to simulate a post filter */ + int pf, /* true to enable actual LPC post filter */ + int bass_boost, /* enable LPC filter 0-1kHz 3dB boost */ + float beta, float gamma, /* LPC post filter parameters */ + COMP Aw[] /* output 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; PROFILE_VAR(tstart, tfft, tpw, tpf); PROFILE_SAMPLE(tstart); - r = TWO_PI/(FFT_ENC); + r = TWO_PI / (FFT_ENC); /* Determine DFT of A(exp(jw)) --------------------------------------------*/ { - float a[FFT_ENC]; /* input to FFT for power spectrum */ + float a[FFT_ENC]; /* input to FFT for power spectrum */ - for(i=0; i<FFT_ENC; i++) { - a[i] = 0.0; - } + for (i = 0; i < FFT_ENC; i++) { + a[i] = 0.0; + } - for(i=0; i<=order; i++) - a[i] = ak[i]; - codec2_fftr(fftr_fwd_cfg, a, Aw); + for (i = 0; i <= order; i++) a[i] = ak[i]; + codec2_fftr(fftr_fwd_cfg, a, Aw); } PROFILE_SAMPLE_AND_LOG(tfft, tstart, " fft"); /* Determine power spectrum P(w) = E/(A(exp(jw))^2 ------------------------*/ - float Pw[FFT_ENC/2]; + float Pw[FFT_ENC / 2]; #ifndef FDV_ARM_MATH - for(i=0; i<FFT_ENC/2; i++) { - Pw[i] = 1.0/(Aw[i].real*Aw[i].real + Aw[i].imag*Aw[i].imag + 1E-6); + for (i = 0; i < FFT_ENC / 2; i++) { + Pw[i] = 1.0 / (Aw[i].real * Aw[i].real + Aw[i].imag * Aw[i].imag + 1E-6); } #else - // this difference may seem strange, but the gcc for STM32F4 generates almost 5 times - // faster code with the two loops: 1120 ms -> 242 ms - // so please leave it as is or improve further - // since this code is called 4 times it results in almost 4ms gain (21ms -> 17ms per audio frame decode @ 1300 ) + // this difference may seem strange, but the gcc for STM32F4 generates almost + // 5 times faster code with the two loops: 1120 ms -> 242 ms so please leave + // it as is or improve further since this code is called 4 times it results in + // almost 4ms gain (21ms -> 17ms per audio frame decode @ 1300 ) - for(i=0; i<FFT_ENC/2; i++) - { - Pw[i] = Aw[i].real * Aw[i].real + Aw[i].imag * Aw[i].imag + 1E-6; + for (i = 0; i < FFT_ENC / 2; i++) { + Pw[i] = Aw[i].real * Aw[i].real + Aw[i].imag * Aw[i].imag + 1E-6; } - for(i=0; i<FFT_ENC/2; i++) { - Pw[i] = 1.0/(Pw[i]); + for (i = 0; i < FFT_ENC / 2; i++) { + Pw[i] = 1.0 / (Pw[i]); } #endif PROFILE_SAMPLE_AND_LOG(tpw, tfft, " Pw"); if (pf) - lpc_post_filter(fftr_fwd_cfg, Pw, ak, order, dump, beta, gamma, bass_boost, E); + lpc_post_filter(fftr_fwd_cfg, Pw, ak, order, dump, beta, gamma, bass_boost, + E); else { - for(i=0; i<FFT_ENC/2; i++) { - Pw[i] *= E; - } + for (i = 0; i < FFT_ENC / 2; i++) { + Pw[i] *= E; + } } PROFILE_SAMPLE_AND_LOG(tpf, tpw, " LPC post filter"); - #ifdef DUMP - if (dump) - dump_Pw(Pw); - #endif +#ifdef DUMP + if (dump) dump_Pw(Pw); +#endif /* Determine magnitudes from P(w) ----------------------------------------*/ /* when used just by decoder {A} might be all zeroes so init signal and noise to prevent log(0) errors */ - signal = 1E-30; noise = 1E-32; - - for(m=1; m<=model->L; m++) { - am = (int)((m - 0.5)*model->Wo/r + 0.5); - bm = (int)((m + 0.5)*model->Wo/r + 0.5); - - // FIXME: With arm_rfft_fast_f32 we have to use this - // otherwise sometimes a to high bm is calculated - // which causes trouble later in the calculation - // chain - // it seems for some reason model->Wo is calculated somewhat too high - if (bm>FFT_ENC/2) - { - bm = FFT_ENC/2; - } - Em = 0.0; - - for(i=am; i<bm; i++) - Em += Pw[i]; - Am = sqrtf(Em); - - signal += model->A[m]*model->A[m]; - noise += (model->A[m] - Am)*(model->A[m] - Am); - - /* 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 - suppresses 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; + signal = 1E-30; + noise = 1E-32; + + for (m = 1; m <= model->L; m++) { + am = (int)((m - 0.5) * model->Wo / r + 0.5); + bm = (int)((m + 0.5) * model->Wo / r + 0.5); + + // FIXME: With arm_rfft_fast_f32 we have to use this + // otherwise sometimes a to high bm is calculated + // which causes trouble later in the calculation + // chain + // it seems for some reason model->Wo is calculated somewhat too high + if (bm > FFT_ENC / 2) { + bm = FFT_ENC / 2; + } + Em = 0.0; + + for (i = am; i < bm; i++) Em += Pw[i]; + Am = sqrtf(Em); + + signal += model->A[m] * model->A[m]; + noise += (model->A[m] - Am) * (model->A[m] - Am); + + /* 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 + suppresses 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*log10f(signal/noise); + *snr = 10.0 * log10f(signal / noise); PROFILE_SAMPLE_AND_LOG2(tpf, " rec"); } @@ -608,19 +554,18 @@ void aks_to_M2( \*---------------------------------------------------------------------------*/ -int encode_Wo(C2CONST *c2const, float Wo, int bits) -{ - int index, Wo_levels = 1<<bits; - float Wo_min = c2const->Wo_min; - float Wo_max = c2const->Wo_max; - float norm; +int encode_Wo(C2CONST *c2const, float Wo, int bits) { + int index, Wo_levels = 1 << bits; + float Wo_min = c2const->Wo_min; + float Wo_max = c2const->Wo_max; + float norm; - norm = (Wo - Wo_min)/(Wo_max - Wo_min); - index = floorf(Wo_levels * norm + 0.5); - if (index < 0 ) index = 0; - if (index > (Wo_levels-1)) index = Wo_levels-1; + norm = (Wo - Wo_min) / (Wo_max - Wo_min); + index = floorf(Wo_levels * norm + 0.5); + if (index < 0) index = 0; + if (index > (Wo_levels - 1)) index = Wo_levels - 1; - return index; + return index; } /*---------------------------------------------------------------------------*\ @@ -633,18 +578,17 @@ int encode_Wo(C2CONST *c2const, float Wo, int bits) \*---------------------------------------------------------------------------*/ -float decode_Wo(C2CONST *c2const, int index, int bits) -{ - float Wo_min = c2const->Wo_min; - float Wo_max = c2const->Wo_max; - float step; - float Wo; - int Wo_levels = 1<<bits; +float decode_Wo(C2CONST *c2const, int index, int bits) { + float Wo_min = c2const->Wo_min; + float Wo_max = c2const->Wo_max; + float step; + float Wo; + int Wo_levels = 1 << bits; - step = (Wo_max - Wo_min)/Wo_levels; - Wo = Wo_min + step*(index); + step = (Wo_max - Wo_min) / Wo_levels; + Wo = Wo_min + step * (index); - return Wo; + return Wo; } /*---------------------------------------------------------------------------*\ @@ -657,19 +601,18 @@ float decode_Wo(C2CONST *c2const, int index, int bits) \*---------------------------------------------------------------------------*/ -int encode_log_Wo(C2CONST *c2const, float Wo, int bits) -{ - int index, Wo_levels = 1<<bits; - float Wo_min = c2const->Wo_min; - float Wo_max = c2const->Wo_max; - float norm; +int encode_log_Wo(C2CONST *c2const, float Wo, int bits) { + int index, Wo_levels = 1 << bits; + float Wo_min = c2const->Wo_min; + float Wo_max = c2const->Wo_max; + float norm; - norm = (log10f(Wo) - log10f(Wo_min))/(log10f(Wo_max) - log10f(Wo_min)); - index = floorf(Wo_levels * norm + 0.5); - if (index < 0 ) index = 0; - if (index > (Wo_levels-1)) index = Wo_levels-1; + norm = (log10f(Wo) - log10f(Wo_min)) / (log10f(Wo_max) - log10f(Wo_min)); + index = floorf(Wo_levels * norm + 0.5); + if (index < 0) index = 0; + if (index > (Wo_levels - 1)) index = Wo_levels - 1; - return index; + return index; } /*---------------------------------------------------------------------------*\ @@ -682,18 +625,17 @@ int encode_log_Wo(C2CONST *c2const, float Wo, int bits) \*---------------------------------------------------------------------------*/ -float decode_log_Wo(C2CONST *c2const, int index, int bits) -{ - float Wo_min = c2const->Wo_min; - float Wo_max = c2const->Wo_max; - float step; - float Wo; - int Wo_levels = 1<<bits; +float decode_log_Wo(C2CONST *c2const, int index, int bits) { + float Wo_min = c2const->Wo_min; + float Wo_max = c2const->Wo_max; + float step; + float Wo; + int Wo_levels = 1 << bits; - step = (log10f(Wo_max) - log10f(Wo_min))/Wo_levels; - Wo = log10f(Wo_min) + step*(index); + step = (log10f(Wo_max) - log10f(Wo_min)) / Wo_levels; + Wo = log10f(Wo_min) + step * (index); - return POW10F(Wo); + return POW10F(Wo); } /*---------------------------------------------------------------------------*\ @@ -708,56 +650,46 @@ float decode_log_Wo(C2CONST *c2const, int index, int bits) \*---------------------------------------------------------------------------*/ -float speech_to_uq_lsps(float lsp[], - float ak[], - float Sn[], - float w[], - int m_pitch, - int order -) -{ - int i, roots; - float Wn[m_pitch]; - float R[order+1]; - float e, E; - - e = 0.0; - for(i=0; i<m_pitch; i++) { - Wn[i] = Sn[i]*w[i]; - e += Wn[i]*Wn[i]; - } +float speech_to_uq_lsps(float lsp[], float ak[], float Sn[], float w[], + int m_pitch, int order) { + int i, roots; + float Wn[m_pitch]; + float R[order + 1]; + float e, E; + + e = 0.0; + for (i = 0; i < m_pitch; i++) { + Wn[i] = Sn[i] * w[i]; + e += Wn[i] * Wn[i]; + } - /* trap 0 energy case as LPC analysis will fail */ + /* trap 0 energy case as LPC analysis will fail */ - if (e == 0.0) { - for(i=0; i<order; i++) - lsp[i] = (PI/order)*(float)i; - return 0.0; - } + if (e == 0.0) { + for (i = 0; i < order; i++) lsp[i] = (PI / order) * (float)i; + return 0.0; + } - autocorrelate(Wn, R, m_pitch, order); - levinson_durbin(R, ak, order); + autocorrelate(Wn, R, m_pitch, order); + levinson_durbin(R, ak, order); - E = 0.0; - for(i=0; i<=order; i++) - E += ak[i]*R[i]; + E = 0.0; + for (i = 0; i <= order; i++) E += ak[i] * R[i]; - /* 15 Hz BW expansion as I can't hear the difference and it may help - help occasional fails in the LSP root finding. Important to do this - after energy calculation to avoid -ve energy values. - */ + /* 15 Hz BW expansion as I can't hear the difference and it may help + help occasional fails in the LSP root finding. Important to do this + after energy calculation to avoid -ve energy values. + */ - for(i=0; i<=order; i++) - ak[i] *= powf(0.994,(float)i); + for (i = 0; i <= order; i++) ak[i] *= powf(0.994, (float)i); - roots = lpc_to_lsp(ak, order, lsp, 5, LSP_DELTA1); - if (roots != order) { - /* if root finding fails use some benign LSP values instead */ - for(i=0; i<order; i++) - lsp[i] = (PI/order)*(float)i; - } + roots = lpc_to_lsp(ak, order, lsp, 5, LSP_DELTA1); + if (roots != order) { + /* if root finding fails use some benign LSP values instead */ + for (i = 0; i < order; i++) lsp[i] = (PI / order) * (float)i; + } - return E; + return E; } /*---------------------------------------------------------------------------*\ @@ -771,29 +703,27 @@ float speech_to_uq_lsps(float lsp[], \*---------------------------------------------------------------------------*/ -void encode_lsps_scalar(int indexes[], float lsp[], int order) -{ - int i,k,m; - float wt[1]; - float lsp_hz[order]; - const float * cb; - float se; - - /* convert from radians to Hz so we can use human readable - frequencies */ - - for(i=0; i<order; i++) - lsp_hz[i] = (4000.0/PI)*lsp[i]; - - /* scalar quantisers */ - - wt[0] = 1.0; - for(i=0; i<order; i++) { - k = lsp_cb[i].k; - m = lsp_cb[i].m; - cb = lsp_cb[i].cb; - indexes[i] = quantise(cb, &lsp_hz[i], wt, k, m, &se); - } +void encode_lsps_scalar(int indexes[], float lsp[], int order) { + int i, k, m; + float wt[1]; + float lsp_hz[order]; + const float *cb; + float se; + + /* convert from radians to Hz so we can use human readable + frequencies */ + + for (i = 0; i < order; i++) lsp_hz[i] = (4000.0 / PI) * lsp[i]; + + /* scalar quantisers */ + + wt[0] = 1.0; + for (i = 0; i < order; i++) { + k = lsp_cb[i].k; + m = lsp_cb[i].m; + cb = lsp_cb[i].cb; + indexes[i] = quantise(cb, &lsp_hz[i], wt, k, m, &se); + } } /*---------------------------------------------------------------------------*\ @@ -807,22 +737,20 @@ void encode_lsps_scalar(int indexes[], float lsp[], int order) \*---------------------------------------------------------------------------*/ -void decode_lsps_scalar(float lsp[], int indexes[], int order) -{ - int i,k; - float lsp_hz[order]; - const float * cb; - - for(i=0; i<order; i++) { - k = lsp_cb[i].k; - cb = lsp_cb[i].cb; - lsp_hz[i] = cb[indexes[i]*k]; - } +void decode_lsps_scalar(float lsp[], int indexes[], int order) { + int i, k; + float lsp_hz[order]; + const float *cb; - /* convert back to radians */ + for (i = 0; i < order; i++) { + k = lsp_cb[i].k; + cb = lsp_cb[i].cb; + lsp_hz[i] = cb[indexes[i] * k]; + } + + /* convert back to radians */ - for(i=0; i<order; i++) - lsp[i] = (PI/4000.0)*lsp_hz[i]; + for (i = 0; i < order; i++) lsp[i] = (PI / 4000.0) * lsp_hz[i]; } /*---------------------------------------------------------------------------*\ @@ -835,8 +763,7 @@ void decode_lsps_scalar(float lsp[], int indexes[], int order) \*---------------------------------------------------------------------------*/ -void encode_lsps_vq(int *indexes, float *x, float *xq, int order) -{ +void encode_lsps_vq(int *indexes, float *x, float *xq, int order) { int i, n1, n2, n3; float err[order], err2[order], err3[order]; float w[order], w2[order], w3[order]; @@ -844,36 +771,32 @@ void encode_lsps_vq(int *indexes, float *x, float *xq, int order) const float *codebook2 = lsp_cbjmv[1].cb; const float *codebook3 = lsp_cbjmv[2].cb; - w[0] = MIN(x[0], x[1]-x[0]); - for (i=1;i<order-1;i++) - w[i] = MIN(x[i]-x[i-1], x[i+1]-x[i]); - w[order-1] = MIN(x[order-1]-x[order-2], PI-x[order-1]); + w[0] = MIN(x[0], x[1] - x[0]); + for (i = 1; i < order - 1; i++) w[i] = MIN(x[i] - x[i - 1], x[i + 1] - x[i]); + w[order - 1] = MIN(x[order - 1] - x[order - 2], PI - x[order - 1]); compute_weights(x, w, order); n1 = find_nearest(codebook1, lsp_cbjmv[0].m, x, order); - for (i=0;i<order;i++) - { - xq[i] = codebook1[order*n1+i]; + for (i = 0; i < order; i++) { + xq[i] = codebook1[order * n1 + i]; err[i] = x[i] - xq[i]; } - for (i=0;i<order/2;i++) - { - err2[i] = err[2*i]; - err3[i] = err[2*i+1]; - w2[i] = w[2*i]; - w3[i] = w[2*i+1]; + for (i = 0; i < order / 2; i++) { + err2[i] = err[2 * i]; + err3[i] = err[2 * i + 1]; + w2[i] = w[2 * i]; + w3[i] = w[2 * i + 1]; } - n2 = find_nearest_weighted(codebook2, lsp_cbjmv[1].m, err2, w2, order/2); - n3 = find_nearest_weighted(codebook3, lsp_cbjmv[2].m, err3, w3, order/2); + n2 = find_nearest_weighted(codebook2, lsp_cbjmv[1].m, err2, w2, order / 2); + n3 = find_nearest_weighted(codebook3, lsp_cbjmv[2].m, err3, w3, order / 2); indexes[0] = n1; indexes[1] = n2; indexes[2] = n3; } - /*---------------------------------------------------------------------------*\ FUNCTION....: decode_lsps_vq() @@ -882,8 +805,7 @@ void encode_lsps_vq(int *indexes, float *x, float *xq, int order) \*---------------------------------------------------------------------------*/ -void decode_lsps_vq(int *indexes, float *xq, int order, int stages) -{ +void decode_lsps_vq(int *indexes, float *xq, int order, int stages) { int i, n1, n2, n3; const float *codebook1 = lsp_cbjmv[0].cb; const float *codebook2 = lsp_cbjmv[1].cb; @@ -893,20 +815,18 @@ void decode_lsps_vq(int *indexes, float *xq, int order, int stages) n2 = indexes[1]; n3 = indexes[2]; - for (i=0;i<order;i++) { - xq[i] = codebook1[order*n1+i]; + for (i = 0; i < order; i++) { + xq[i] = codebook1[order * n1 + i]; } if (stages != 1) { - for (i=0;i<order/2;i++) { - xq[2*i] += codebook2[order*n2/2+i]; - xq[2*i+1] += codebook3[order*n3/2+i]; - } + for (i = 0; i < order / 2; i++) { + xq[2 * i] += codebook2[order * n2 / 2 + i]; + xq[2 * i + 1] += codebook3[order * n3 / 2 + i]; + } } - } - /*---------------------------------------------------------------------------*\ FUNCTION....: bw_expand_lsps() @@ -920,50 +840,43 @@ void decode_lsps_vq(int *indexes, float *xq, int order, int stages) \*---------------------------------------------------------------------------*/ -void bw_expand_lsps(float lsp[], int order, float min_sep_low, float min_sep_high) -{ - int i; - - for(i=1; i<4; i++) { - - if ((lsp[i] - lsp[i-1]) < min_sep_low*(PI/4000.0)) - lsp[i] = lsp[i-1] + min_sep_low*(PI/4000.0); +void bw_expand_lsps(float lsp[], int order, float min_sep_low, + float min_sep_high) { + int i; - } + for (i = 1; i < 4; i++) { + if ((lsp[i] - lsp[i - 1]) < min_sep_low * (PI / 4000.0)) + lsp[i] = lsp[i - 1] + min_sep_low * (PI / 4000.0); + } - /* As quantiser gaps increased, larger BW expansion was required - to prevent twinkly noises. This may need more experiment for - different quanstisers. - */ + /* As quantiser gaps increased, larger BW expansion was required + to prevent twinkly noises. This may need more experiment for + different quanstisers. + */ - for(i=4; i<order; i++) { - if (lsp[i] - lsp[i-1] < min_sep_high*(PI/4000.0)) - lsp[i] = lsp[i-1] + min_sep_high*(PI/4000.0); - } + for (i = 4; i < order; i++) { + if (lsp[i] - lsp[i - 1] < min_sep_high * (PI / 4000.0)) + lsp[i] = lsp[i - 1] + min_sep_high * (PI / 4000.0); + } } -void bw_expand_lsps2(float lsp[], - int order -) -{ - int i; - - for(i=1; i<4; i++) { - - if ((lsp[i] - lsp[i-1]) < 100.0*(PI/4000.0)) - lsp[i] = lsp[i-1] + 100.0*(PI/4000.0); +void bw_expand_lsps2(float lsp[], int order) { + int i; - } + for (i = 1; i < 4; i++) { + if ((lsp[i] - lsp[i - 1]) < 100.0 * (PI / 4000.0)) + lsp[i] = lsp[i - 1] + 100.0 * (PI / 4000.0); + } - /* As quantiser gaps increased, larger BW expansion was required - to prevent twinkly noises. This may need more experiment for - different quanstisers. - */ + /* As quantiser gaps increased, larger BW expansion was required + to prevent twinkly noises. This may need more experiment for + different quanstisers. + */ - for(i=4; i<order; i++) { - if (lsp[i] - lsp[i-1] < 200.0*(PI/4000.0)) - lsp[i] = lsp[i-1] + 200.0*(PI/4000.0); - } + for (i = 4; i < order; i++) { + if (lsp[i] - lsp[i - 1] < 200.0 * (PI / 4000.0)) + lsp[i] = lsp[i - 1] + 200.0 * (PI / 4000.0); + } } /*---------------------------------------------------------------------------*\ @@ -977,11 +890,10 @@ void bw_expand_lsps2(float lsp[], \*---------------------------------------------------------------------------*/ -void apply_lpc_correction(MODEL *model) -{ - if (model->Wo < (PI*150.0/4000)) { - model->A[1] *= 0.032; - } +void apply_lpc_correction(MODEL *model) { + if (model->Wo < (PI * 150.0 / 4000)) { + model->A[1] *= 0.032; + } } /*---------------------------------------------------------------------------*\ @@ -994,20 +906,19 @@ void apply_lpc_correction(MODEL *model) \*---------------------------------------------------------------------------*/ -int encode_energy(float e, int bits) -{ - int index, e_levels = 1<<bits; - float e_min = E_MIN_DB; - float e_max = E_MAX_DB; - float norm; - - e = 10.0*log10f(e); - norm = (e - e_min)/(e_max - e_min); - index = floorf(e_levels * norm + 0.5); - if (index < 0 ) index = 0; - if (index > (e_levels-1)) index = e_levels-1; - - return index; +int encode_energy(float e, int bits) { + int index, e_levels = 1 << bits; + float e_min = E_MIN_DB; + float e_max = E_MAX_DB; + float norm; + + e = 10.0 * log10f(e); + norm = (e - e_min) / (e_max - e_min); + index = floorf(e_levels * norm + 0.5); + if (index < 0) index = 0; + if (index > (e_levels - 1)) index = e_levels - 1; + + return index; } /*---------------------------------------------------------------------------*\ @@ -1020,65 +931,56 @@ int encode_energy(float e, int bits) \*---------------------------------------------------------------------------*/ -float decode_energy(int index, int bits) -{ - float e_min = E_MIN_DB; - float e_max = E_MAX_DB; - float step; - float e; - int e_levels = 1<<bits; +float decode_energy(int index, int bits) { + float e_min = E_MIN_DB; + float e_max = E_MAX_DB; + float step; + float e; + int e_levels = 1 << bits; - step = (e_max - e_min)/e_levels; - e = e_min + step*(index); - e = POW10F(e/10.0); + step = (e_max - e_min) / e_levels; + e = e_min + step * (index); + e = POW10F(e / 10.0); - return e; + return e; } - static float ge_coeff[2] = {0.8, 0.9}; -void compute_weights2(const float *x, const float *xp, float *w) -{ +void compute_weights2(const float *x, const float *xp, float *w) { w[0] = 30; w[1] = 1; - if (x[1]<0) - { - w[0] *= .6; - w[1] *= .3; + if (x[1] < 0) { + w[0] *= .6; + w[1] *= .3; } - if (x[1]<-10) - { - w[0] *= .3; - w[1] *= .3; + if (x[1] < -10) { + w[0] *= .3; + w[1] *= .3; } /* Higher weight if pitch is stable */ - if (fabsf(x[0]-xp[0])<.2) - { - w[0] *= 2; - w[1] *= 1.5; - } else if (fabsf(x[0]-xp[0])>.5) /* Lower if not stable */ + if (fabsf(x[0] - xp[0]) < .2) { + w[0] *= 2; + w[1] *= 1.5; + } else if (fabsf(x[0] - xp[0]) > .5) /* Lower if not stable */ { - w[0] *= .5; + w[0] *= .5; } /* Lower weight for low energy */ - if (x[1] < xp[1]-10) - { - w[1] *= .5; + if (x[1] < xp[1] - 10) { + w[1] *= .5; } - if (x[1] < xp[1]-20) - { - w[1] *= .5; + if (x[1] < xp[1] - 20) { + w[1] *= .5; } - //w[0] = 30; - //w[1] = 1; + // 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]; - } /*---------------------------------------------------------------------------*\ @@ -1103,15 +1005,14 @@ void compute_weights2(const float *x, const float *xp, float *w) \*---------------------------------------------------------------------------*/ -void quantise_WoE(C2CONST *c2const, MODEL *model, float *e, float xq[]) -{ - int i, n1; - float x[2]; - float err[2]; - float w[2]; +void quantise_WoE(C2CONST *c2const, 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; + int nb_entries = ge_cb[0].m; + int ndim = ge_cb[0].k; float Wo_min = c2const->Wo_min; float Wo_max = c2const->Wo_max; float Fs = c2const->Fs; @@ -1120,18 +1021,16 @@ void quantise_WoE(C2CONST *c2const, MODEL *model, float *e, float xq[]) assert(Fs == 8000); - x[0] = log10f((model->Wo/PI)*4000.0/50.0)/log10f(2); - x[1] = 10.0*log10f(1e-4 + *e); + x[0] = log10f((model->Wo / PI) * 4000.0 / 50.0) / log10f(2); + x[1] = 10.0 * log10f(1e-4 + *e); compute_weights2(x, xq, w); - for (i=0;i<ndim;i++) - err[i] = x[i]-ge_coeff[i]*xq[i]; + for (i = 0; i < ndim; i++) err[i] = x[i] - ge_coeff[i] * xq[i]; n1 = find_nearest_weighted(codebook1, nb_entries, err, w, ndim); - for (i=0;i<ndim;i++) - { - xq[i] = ge_coeff[i]*xq[i] + codebook1[ndim*n1+i]; - err[i] -= codebook1[ndim*n1+i]; + for (i = 0; i < ndim; i++) { + xq[i] = ge_coeff[i] * xq[i] + codebook1[ndim * n1 + i]; + err[i] -= codebook1[ndim * n1 + i]; } /* @@ -1140,7 +1039,7 @@ void quantise_WoE(C2CONST *c2const, MODEL *model, float *e, float xq[]) Wo = (2^x)*(PI*50)/4000; */ - model->Wo = powf(2.0, xq[0])*(PI*50.0)/4000.0; + model->Wo = powf(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 */ @@ -1148,9 +1047,9 @@ void quantise_WoE(C2CONST *c2const, MODEL *model, float *e, float xq[]) 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 */ + model->L = PI / model->Wo; /* if we quantise Wo re-compute L */ - *e = POW10F(xq[1]/10.0); + *e = POW10F(xq[1] / 10.0); } /*---------------------------------------------------------------------------*\ @@ -1164,39 +1063,36 @@ void quantise_WoE(C2CONST *c2const, MODEL *model, float *e, float xq[]) \*---------------------------------------------------------------------------*/ -int encode_WoE(MODEL *model, float e, float xq[]) -{ - int i, n1; - float x[2]; - float err[2]; - float w[2]; +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; + int nb_entries = ge_cb[0].m; + int ndim = ge_cb[0].k; - assert((1<<WO_E_BITS) == nb_entries); + assert((1 << WO_E_BITS) == nb_entries); - if (e < 0.0) e = 0; /* occasional small negative energies due LPC round off I guess */ + if (e < 0.0) + e = 0; /* occasional small negative energies due LPC round off I guess */ - x[0] = log10f((model->Wo/PI)*4000.0/50.0)/log10f(2); - x[1] = 10.0*log10f(1e-4 + e); + x[0] = log10f((model->Wo / PI) * 4000.0 / 50.0) / log10f(2); + x[1] = 10.0 * log10f(1e-4 + e); compute_weights2(x, xq, w); - for (i=0;i<ndim;i++) - err[i] = x[i]-ge_coeff[i]*xq[i]; + for (i = 0; i < ndim; i++) err[i] = x[i] - ge_coeff[i] * xq[i]; n1 = find_nearest_weighted(codebook1, nb_entries, err, w, ndim); - for (i=0;i<ndim;i++) - { - xq[i] = ge_coeff[i]*xq[i] + codebook1[ndim*n1+i]; - err[i] -= codebook1[ndim*n1+i]; + for (i = 0; i < ndim; i++) { + xq[i] = ge_coeff[i] * xq[i] + codebook1[ndim * n1 + i]; + err[i] -= codebook1[ndim * n1 + i]; } - //printf("enc: %f %f (%f)(%f) \n", xq[0], xq[1], e, 10.0*log10(1e-4 + e)); + // printf("enc: %f %f (%f)(%f) \n", xq[0], xq[1], e, 10.0*log10(1e-4 + e)); return n1; } - /*---------------------------------------------------------------------------*\ FUNCTION....: decode_WoE() @@ -1209,21 +1105,19 @@ int encode_WoE(MODEL *model, float e, float xq[]) \*---------------------------------------------------------------------------*/ -void decode_WoE(C2CONST *c2const, MODEL *model, float *e, float xq[], int n1) -{ - int i; +void decode_WoE(C2CONST *c2const, MODEL *model, float *e, float xq[], int n1) { + int i; const float *codebook1 = ge_cb[0].cb; - int ndim = ge_cb[0].k; + int ndim = ge_cb[0].k; float Wo_min = c2const->Wo_min; float Wo_max = c2const->Wo_max; - for (i=0;i<ndim;i++) - { - xq[i] = ge_coeff[i]*xq[i] + codebook1[ndim*n1+i]; + for (i = 0; i < ndim; i++) { + xq[i] = ge_coeff[i] * xq[i] + codebook1[ndim * n1 + i]; } - //printf("dec: %f %f\n", xq[0], xq[1]); - model->Wo = powf(2.0, xq[0])*(PI*50.0)/4000.0; + // printf("dec: %f %f\n", xq[0], xq[1]); + model->Wo = powf(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 */ @@ -1231,8 +1125,7 @@ void decode_WoE(C2CONST *c2const, MODEL *model, float *e, float xq[], int n1) 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 */ + model->L = PI / model->Wo; /* if we quantise Wo re-compute L */ - *e = POW10F(xq[1]/10.0); + *e = POW10F(xq[1] / 10.0); } - |
