185
194
indexes[i] = quantise(cb, &dlsp[i], wt, k, m, &se);
186
195
dlsp_[i] = cb[indexes[i]*k];
190
198
lsp__hz[i] = lsp__hz[i-1] + dlsp_[i];
192
200
lsp__hz[0] = dlsp_[0];
194
//printf("%d lsp %3.2f dlsp %3.2f dlsp_ %3.2f lsp_ %3.2f\n", i, lsp_hz[i], dlsp[i], dlsp_[i], lsp__hz[i]);
199
void decode_lspds_scalar(
206
float lsp__hz[LPC_MAX];
207
float dlsp_[LPC_MAX];
210
assert(order == LPC_ORD);
212
for(i=0; i<order; i++) {
216
dlsp_[i] = cb[indexes[i]*k];
219
lsp__hz[i] = lsp__hz[i-1] + dlsp_[i];
221
lsp__hz[0] = dlsp_[0];
203
lsp__hz[i] = lsp__hz[i-1] + dlsp[i];
205
/* convert back to radians */
207
for(i=0; i<order; i++)
223
208
lsp_[i] = (PI/4000.0)*lsp__hz[i];
225
//printf("%d dlsp_ %3.2f lsp_ %3.2f\n", i, dlsp_[i], lsp__hz[i]);
230
#ifdef __EXPERIMENTAL__
231
211
/*---------------------------------------------------------------------------*\
235
Vector LSP quantiser.
215
Vector lsp difference quantiser.
237
217
\*---------------------------------------------------------------------------*/
219
void lspdvq_quantise(
245
225
int i,k,m,ncb, nlsp;
246
float wt[LPC_ORD], lsp_hz[LPC_ORD];
251
for(i=0; i<LPC_ORD; i++) {
253
lsp_hz[i] = 4000.0*lsp[i]/PI;
256
/* scalar quantise LSPs 1,2,3,4 */
258
/* simple uniform scalar quantisers */
264
index = quantise(cb, &lsp_hz[i], wt, k, m, &se);
265
lsp_[i] = cb[index*k]*PI/4000.0;
271
wt[i] = 1.0/(lsp[i]-lsp[i-1]) + 1.0/(lsp[i+1]-lsp[i]);
272
//printf("wt[%d] = %f\n", i, wt[i]);
274
wt[9] = 1.0/(lsp[i]-lsp[i-1]);
277
/* VQ LSPs 5,6,7,8,9,10 */
281
k = lsp_cbjnd[ncb].k;
282
m = lsp_cbjnd[ncb].m;
283
cb = lsp_cbjnd[ncb].cb;
284
index = quantise(cb, &lsp_hz[nlsp], &wt[nlsp], k, m, &se);
285
for(i=4; i<LPC_ORD; i++) {
286
lsp_[i] = cb[index*k+i-4]*(PI/4000.0);
287
//printf("%4.f (%4.f) ", lsp_hz[i], cb[index*k+i-4]);
291
/*---------------------------------------------------------------------------*\
295
Experimental JND LSP quantiser.
297
\*---------------------------------------------------------------------------*/
299
void lspjnd_quantise(float lsps[], float lsps_[], int order)
302
float wt[LPC_ORD], lsps_hz[LPC_ORD];
307
for(i=0; i<LPC_ORD; i++) {
313
for(i=0; i<LPC_ORD; i++) {
314
lsps_hz[i] = lsps[i]*(4000.0/PI);
318
/* simple uniform scalar quantisers */
323
cb = lsp_cbjnd[i].cb;
324
index = quantise(cb, &lsps_hz[i], wt, k, m, &se);
325
lsps_[i] = cb[index*k]*(PI/4000.0);
328
/* VQ LSPs 5,6,7,8,9,10 */
332
cb = lsp_cbjnd[4].cb;
333
index = quantise(cb, &lsps_hz[4], &wt[4], k, m, &se);
334
//printf("k = %d m = %d c[0] %f cb[k] %f\n", k,m,cb[0],cb[k]);
335
//printf("index = %4d: ", index);
336
for(i=4; i<LPC_ORD; i++) {
337
lsps_[i] = cb[index*k+i-4]*(PI/4000.0);
338
//printf("%4.f (%4.f) ", lsps_hz[i], cb[index*k+i-4]);
343
void compute_weights(const float *x, float *w, int ndim);
345
/*---------------------------------------------------------------------------*\
349
LSP difference in time quantiser. Split VQ, encoding LSPs 1-4 with
350
one VQ, and LSPs 5-10 with a second. Update of previous lsp memory
351
is done outside of this function to handle dT between 10 or 20ms
357
LSPDT_ALL VQ LSPs 1-4 and 5-10
358
LSPDT_LOW Just VQ LSPs 1-4, for LSPs 5-10 just copy previous
359
LSPDT_HIGH Just VQ LSPs 5-10, for LSPs 1-4 just copy previous
361
\*---------------------------------------------------------------------------*/
363
void lspdt_quantise(float lsps[], float lsps_[], float lsps__prev[], int mode)
367
float lsps_dt[LPC_ORD];
373
#endif // TRY_LSPDT_VQ
375
//compute_weights(lsps, wt, LPC_ORD);
376
for(i=0; i<LPC_ORD; i++) {
380
//compute_weights(lsps, wt, LPC_ORD );
382
for(i=0; i<LPC_ORD; i++) {
383
lsps_dt[i] = lsps[i] - lsps__prev[i];
384
lsps_[i] = lsps__prev[i];
387
//#define TRY_LSPDT_VQ
389
/* this actually improves speech a bit, but 40ms updates works surprsingly well.... */
393
index = quantise(cb, lsps_dt, wt, k, m, &se);
394
for(i=0; i<LPC_ORD; i++) {
395
lsps_[i] += cb[index*k + i];
402
#define MIN(a,b) ((a)<(b)?(a):(b))
403
#define MAX_ENTRIES 16384
405
void compute_weights(const float *x, float *w, int ndim)
408
w[0] = MIN(x[0], x[1]-x[0]);
409
for (i=1;i<ndim-1;i++)
410
w[i] = MIN(x[i]-x[i-1], x[i+1]-x[i]);
411
w[ndim-1] = MIN(x[ndim-1]-x[ndim-2], PI-x[ndim-1]);
414
w[i] = 1./(.01+w[i]);
419
/* LSP weight calculation ported from m-file function kindly submitted
422
void compute_weights_anssi_mode2(const float *x, float *w, int ndim)
427
assert(ndim == LPC_ORD);
429
for(i=0; i<LPC_ORD; i++)
433
for (i=1; i<LPC_ORD-1; i++)
434
d[i] = x[i+1] - x[i-1];
435
d[LPC_ORD-1] = PI - x[8];
436
for (i=0; i<LPC_ORD; i++) {
437
if (x[i]<((400.0/4000.0)*PI))
438
w[i]=5.0/(0.01+d[i]);
439
else if (x[i]<((700.0/4000.0)*PI))
440
w[i]=4.0/(0.01+d[i]);
441
else if (x[i]<((1200.0/4000.0)*PI))
442
w[i]=3.0/(0.01+d[i]);
443
else if (x[i]<((2000.0/4000.0)*PI))
444
w[i]=2.0/(0.01+d[i]);
446
w[i]=1.0/(0.01+d[i]);
448
w[i]=pow(w[i]+0.3, 0.66);
452
int find_nearest(const float *codebook, int nb_entries, float *x, int ndim)
455
float min_dist = 1e15;
458
for (i=0;i<nb_entries;i++)
462
dist += (x[j]-codebook[i*ndim+j])*(x[j]-codebook[i*ndim+j]);
472
int find_nearest_weighted(const float *codebook, int nb_entries, float *x, const float *w, int ndim)
475
float min_dist = 1e15;
478
for (i=0;i<nb_entries;i++)
482
dist += w[j]*(x[j]-codebook[i*ndim+j])*(x[j]-codebook[i*ndim+j]);
492
void lspjvm_quantise(float *x, float *xq, int ndim)
495
float err[LPC_ORD], err2[LPC_ORD], err3[LPC_ORD];
496
float w[LPC_ORD], w2[LPC_ORD], w3[LPC_ORD];
497
const float *codebook1 = lsp_cbjvm[0].cb;
498
const float *codebook2 = lsp_cbjvm[1].cb;
499
const float *codebook3 = lsp_cbjvm[2].cb;
501
w[0] = MIN(x[0], x[1]-x[0]);
502
for (i=1;i<ndim-1;i++)
503
w[i] = MIN(x[i]-x[i-1], x[i+1]-x[i]);
504
w[ndim-1] = MIN(x[ndim-1]-x[ndim-2], PI-x[ndim-1]);
506
compute_weights(x, w, ndim);
508
n1 = find_nearest(codebook1, lsp_cbjvm[0].m, x, ndim);
512
xq[i] = codebook1[ndim*n1+i];
513
err[i] = x[i] - xq[i];
515
for (i=0;i<ndim/2;i++)
518
err3[i] = err[2*i+1];
522
n2 = find_nearest_weighted(codebook2, lsp_cbjvm[1].m, err2, w2, ndim/2);
523
n3 = find_nearest_weighted(codebook3, lsp_cbjvm[2].m, err3, w3, ndim/2);
525
for (i=0;i<ndim/2;i++)
527
xq[2*i] += codebook2[ndim*n2/2+i];
528
xq[2*i+1] += codebook3[ndim*n3/2+i];
532
#ifdef __EXPERIMENTAL__
534
#define MBEST_STAGES 4
537
int index[MBEST_STAGES]; /* index of each stage that lead us to this error */
542
int entries; /* number of entries in mbest list */
543
struct MBEST_LIST *list;
547
static struct MBEST *mbest_create(int entries) {
552
mbest = (struct MBEST *)malloc(sizeof(struct MBEST));
553
assert(mbest != NULL);
555
mbest->entries = entries;
556
mbest->list = (struct MBEST_LIST *)malloc(entries*sizeof(struct MBEST_LIST));
557
assert(mbest->list != NULL);
559
for(i=0; i<mbest->entries; i++) {
560
for(j=0; j<MBEST_STAGES; j++)
561
mbest->list[i].index[j] = 0;
562
mbest->list[i].error = 1E32;
569
static void mbest_destroy(struct MBEST *mbest) {
570
assert(mbest != NULL);
576
/*---------------------------------------------------------------------------*\
580
Insert the results of a vector to codebook entry comparison. The
581
list is ordered in order or error, so those entries with the
582
smallest error will be first on the list.
584
\*---------------------------------------------------------------------------*/
586
static void mbest_insert(struct MBEST *mbest, int index[], float error) {
588
struct MBEST_LIST *list = mbest->list;
589
int entries = mbest->entries;
592
for(i=0; i<entries && !found; i++)
593
if (error < list[i].error) {
595
for(j=entries-1; j>i; j--)
597
for(j=0; j<MBEST_STAGES; j++)
598
list[i].index[j] = index[j];
599
list[i].error = error;
604
static void mbest_print(char title[], struct MBEST *mbest) {
607
printf("%s\n", title);
608
for(i=0; i<mbest->entries; i++) {
609
for(j=0; j<MBEST_STAGES; j++)
610
printf(" %4d ", mbest->list[i].index[j]);
611
printf(" %f\n", mbest->list[i].error);
616
/*---------------------------------------------------------------------------*\
620
Searches vec[] to a codebbook of vectors, and maintains a list of the mbest
623
\*---------------------------------------------------------------------------*/
625
static void mbest_search(
626
const float *cb, /* VQ codebook to search */
627
float vec[], /* target vector */
628
float w[], /* weighting vector */
629
int k, /* dimension of vector */
630
int m, /* number on entries in codebook */
631
struct MBEST *mbest, /* list of closest matches */
632
int index[] /* indexes that lead us here */
642
diff = cb[j*k+i]-vec[i];
643
e += pow(diff*w[i],2.0);
646
mbest_insert(mbest, index, e);
651
/* 3 stage VQ LSP quantiser. Design and guidance kindly submitted by Anssi, OH3GDD */
653
void lspanssi_quantise(float *x, float *xq, int ndim, int mbest_entries)
655
int i, j, n1, n2, n3, n4;
657
const float *codebook1 = lsp_cbvqanssi[0].cb;
658
const float *codebook2 = lsp_cbvqanssi[1].cb;
659
const float *codebook3 = lsp_cbvqanssi[2].cb;
660
const float *codebook4 = lsp_cbvqanssi[3].cb;
661
struct MBEST *mbest_stage1, *mbest_stage2, *mbest_stage3, *mbest_stage4;
662
float target[LPC_ORD];
663
int index[MBEST_STAGES];
665
mbest_stage1 = mbest_create(mbest_entries);
666
mbest_stage2 = mbest_create(mbest_entries);
667
mbest_stage3 = mbest_create(mbest_entries);
668
mbest_stage4 = mbest_create(mbest_entries);
669
for(i=0; i<MBEST_STAGES; i++)
672
compute_weights_anssi_mode2(x, w, ndim);
675
dump_weights(w, ndim);
680
mbest_search(codebook1, x, w, ndim, lsp_cbvqanssi[0].m, mbest_stage1, index);
681
mbest_print("Stage 1:", mbest_stage1);
685
for (j=0; j<mbest_entries; j++) {
686
index[1] = n1 = mbest_stage1->list[j].index[0];
687
for(i=0; i<ndim; i++)
688
target[i] = x[i] - codebook1[ndim*n1+i];
689
mbest_search(codebook2, target, w, ndim, lsp_cbvqanssi[1].m, mbest_stage2, index);
691
mbest_print("Stage 2:", mbest_stage2);
695
for (j=0; j<mbest_entries; j++) {
696
index[2] = n1 = mbest_stage2->list[j].index[1];
697
index[1] = n2 = mbest_stage2->list[j].index[0];
698
for(i=0; i<ndim; i++)
699
target[i] = x[i] - codebook1[ndim*n1+i] - codebook2[ndim*n2+i];
700
mbest_search(codebook3, target, w, ndim, lsp_cbvqanssi[2].m, mbest_stage3, index);
702
mbest_print("Stage 3:", mbest_stage3);
706
for (j=0; j<mbest_entries; j++) {
707
index[3] = n1 = mbest_stage3->list[j].index[2];
708
index[2] = n2 = mbest_stage3->list[j].index[1];
709
index[1] = n3 = mbest_stage3->list[j].index[0];
710
for(i=0; i<ndim; i++)
711
target[i] = x[i] - codebook1[ndim*n1+i] - codebook2[ndim*n2+i] - codebook3[ndim*n3+i];
712
mbest_search(codebook4, target, w, ndim, lsp_cbvqanssi[3].m, mbest_stage4, index);
714
mbest_print("Stage 4:", mbest_stage4);
716
n1 = mbest_stage4->list[0].index[3];
717
n2 = mbest_stage4->list[0].index[2];
718
n3 = mbest_stage4->list[0].index[1];
719
n4 = mbest_stage4->list[0].index[0];
721
xq[i] = codebook1[ndim*n1+i] + codebook2[ndim*n2+i] + codebook3[ndim*n3+i] + codebook4[ndim*n4+i];
723
mbest_destroy(mbest_stage1);
724
mbest_destroy(mbest_stage2);
725
mbest_destroy(mbest_stage3);
726
mbest_destroy(mbest_stage4);
730
int check_lsp_order(float lsp[], int lpc_order)
227
float dlsp_[LPC_MAX];
234
for(i=1; i<order; i++)
235
dlsp[i] = lsp[i] - lsp[i-1];
237
for(i=0; i<order; i++)
240
for(i=0; i<order; i++)
243
/* scalar quantise dLSPs 1,2,3,4,5 */
247
dlsp[i] = (lsp[i] - lsp_[i-1])*4000.0/PI;
249
dlsp[0] = lsp[0]*4000.0/PI;
253
cb = lsp_cbdvq[i].cb;
254
index = quantise(cb, &dlsp[i], wt, k, m, &se);
255
dlsp_[i] = cb[index*k]*PI/4000.0;
258
lsp_[i] = lsp_[i-1] + dlsp_[i];
262
dlsp[i] = lsp[i] - lsp_[i-1];
265
//printf("lsp[0] %f lsp_[0] %f\n", lsp[0], lsp_[0]);
266
//printf("lsp[1] %f lsp_[1] %f\n", lsp[1], lsp_[1]);
273
k = lsp_cbdvq[ncb].k;
274
m = lsp_cbdvq[ncb].m;
275
cb = lsp_cbdvq[ncb].cb;
276
index = quantise(cb, &dlsp[nlsp], wt, k, m, &se);
277
dlsp_[nlsp] = cb[index*k];
278
dlsp_[nlsp+1] = cb[index*k+1];
279
dlsp_[nlsp+2] = cb[index*k+2];
283
lsp_[i] = lsp_[i-1] + dlsp_[i];
284
dlsp[i] = lsp[i] - lsp_[i-1];
287
/* VQ dLSPs 6,7,8,9,10 */
291
k = lsp_cbdvq[ncb].k;
292
m = lsp_cbdvq[ncb].m;
293
cb = lsp_cbdvq[ncb].cb;
294
index = quantise(cb, &dlsp[nlsp], wt, k, m, &se);
295
dlsp_[nlsp] = cb[index*k];
296
dlsp_[nlsp+1] = cb[index*k+1];
297
dlsp_[nlsp+2] = cb[index*k+2];
298
dlsp_[nlsp+3] = cb[index*k+3];
299
dlsp_[nlsp+4] = cb[index*k+4];
301
/* rebuild LSPs for dLSPs */
304
for(i=1; i<order; i++)
305
lsp_[i] = lsp_[i-1] + dlsp_[i];
308
void check_lsp_order(float lsp[], int lpc_order)
736
313
for(i=1; i<lpc_order; i++)
737
314
if (lsp[i] < lsp[i-1]) {
738
//fprintf(stderr, "swap %d\n",i);
315
printf("swap %d\n",i);
741
lsp[i-1] = lsp[i]-0.1;
743
i = 1; /* start check again, as swap may have caused out of order */
317
lsp[i-1] = lsp[i]-0.05;
749
322
void force_min_lsp_dist(float lsp[], int lpc_order)
760
332
/*---------------------------------------------------------------------------*\
764
Applies a post filter to the LPC synthesis filter power spectrum
765
Pw, which supresses the inter-formant energy.
767
The algorithm is from p267 (Section 8.6) of "Digital Speech",
768
edited by A.M. Kondoz, 1994 published by Wiley and Sons. Chapter 8
769
of this text is on the MBE vocoder, and this is a freq domain
770
adaptation of post filtering commonly used in CELP.
772
I used the Octave simulation lpcpf.m to get an understaing of the
775
Requires two more FFTs which is significantly more MIPs. However
776
it should be possible to implement this more efficiently in the
777
time domain. Just not sure how to handle relative time delays
778
between the synthesis stage and updating these coeffs. A smaller
779
FFT size might also be accetable to save CPU.
782
[ ] sync var names between Octave and C version
783
[ ] doc gain normalisation
784
[ ] I think the first FFT is not rqd as we do the same
785
thing in aks_to_M2().
336
Derive a LPC model for amplitude samples then estimate amplitude samples
337
from this model with optional LSP quantisation.
339
Returns the spectral distortion for this frame.
787
341
\*---------------------------------------------------------------------------*/
789
void lpc_post_filter(kiss_fft_cfg fft_fwd_cfg, MODEL *model, COMP Pw[], float ak[],
790
int order, int dump, float beta, float gamma, int bass_boost)
343
float lpc_model_amplitudes(
344
float Sn[], /* Input frame of speech samples */
346
MODEL *model, /* sinusoidal model parameters */
347
int order, /* LPC model order */
348
int lsp_quant, /* optional LSP quantisation if non-zero */
349
float ak[] /* output aks */
793
COMP x[FFT_ENC]; /* input to FFTs */
794
COMP Aw[FFT_ENC]; /* LPC analysis filter spectrum */
795
COMP Ww[FFT_ENC]; /* weighting spectrum */
796
float Rw[FFT_ENC]; /* R = WA */
797
float e_before, e_after, gain;
798
float Pfw[FFT_ENC]; /* Post filter mag spectrum */
799
float max_Rw, min_Rw;
801
TIMER_VAR(tstart, tfft1, taw, tfft2, tww, tr);
803
TIMER_SAMPLE(tstart);
805
/* Determine LPC inverse filter spectrum 1/A(exp(jw)) -----------*/
807
/* we actually want the synthesis filter A(exp(jw)) but the
808
inverse (analysis) filter is easier to find as it's FIR, we
809
just use the inverse of 1/A to get the synthesis filter
812
for(i=0; i<FFT_ENC; i++) {
817
for(i=0; i<=order; i++)
819
kiss_fft(fft_fwd_cfg, (kiss_fft_cpx *)x, (kiss_fft_cpx *)Aw);
821
TIMER_SAMPLE_AND_LOG(tfft1, tstart, " fft1");
823
for(i=0; i<FFT_ENC/2; i++) {
824
Aw[i].real = 1.0/(Aw[i].real*Aw[i].real + Aw[i].imag*Aw[i].imag);
827
TIMER_SAMPLE_AND_LOG(taw, tfft1, " Aw");
829
/* Determine weighting filter spectrum W(exp(jw)) ---------------*/
831
for(i=0; i<FFT_ENC; i++) {
838
for(i=1; i<=order; i++) {
839
x[i].real = ak[i] * coeff;
842
kiss_fft(fft_fwd_cfg, (kiss_fft_cpx *)x, (kiss_fft_cpx *)Ww);
844
TIMER_SAMPLE_AND_LOG(tfft2, taw, " fft2");
846
for(i=0; i<FFT_ENC/2; i++) {
847
Ww[i].real = Ww[i].real*Ww[i].real + Ww[i].imag*Ww[i].imag;
850
TIMER_SAMPLE_AND_LOG(tww, tfft2, " Ww");
852
/* Determined combined filter R = WA ---------------------------*/
854
max_Rw = 0.0; min_Rw = 1E32;
855
for(i=0; i<FFT_ENC/2; i++) {
856
Rw[i] = sqrtf(Ww[i].real * Aw[i].real);
864
TIMER_SAMPLE_AND_LOG(tr, tww, " R");
871
/* create post filter mag spectrum and apply ------------------*/
873
/* measure energy before post filtering */
876
for(i=0; i<FFT_ENC/2; i++)
877
e_before += Pw[i].real;
879
/* apply post filter and measure energy */
887
for(i=0; i<FFT_ENC/2; i++) {
888
Pfw[i] = powf(Rw[i], beta);
889
Pw[i].real *= Pfw[i] * Pfw[i];
890
e_after += Pw[i].real;
892
gain = e_before/e_after;
894
/* apply gain factor to normalise energy */
896
for(i=0; i<FFT_ENC/2; i++) {
901
/* add 3dB to first 1 kHz to account for LP effect of PF */
903
for(i=0; i<FFT_ENC/8; i++) {
904
Pw[i].real *= 1.4*1.4;
908
TIMER_SAMPLE_AND_LOG2(tr, " filt");
358
float lsp_hz[LPC_MAX];
360
int roots; /* number of LSP roots found */
369
autocorrelate(Wn,R,M,order);
370
levinson_durbin(R,ak,order);
373
for(i=0; i<=order; i++)
376
for(i=0; i<order; i++)
380
roots = lpc_to_lsp(ak, order, lsp, 5, LSP_DELTA1);
382
printf("LSP roots not found\n");
384
/* convert from radians to Hz to make quantisers more
387
for(i=0; i<order; i++)
388
lsp_hz[i] = (4000.0/PI)*lsp[i];
390
/* simple uniform scalar quantisers */
392
for(i=0; i<10; i++) {
396
index = quantise(cb, &lsp_hz[i], wt, k, m, &se);
397
lsp_hz[i] = cb[index*k];
400
/* experiment: simulating uniform quantisation error
401
for(i=0; i<order; i++)
402
lsp[i] += PI*(12.5/4000.0)*(1.0 - 2.0*(float)rand()/RAND_MAX);
405
for(i=0; i<order; i++)
406
lsp[i] = (PI/4000.0)*lsp_hz[i];
408
/* Bandwidth Expansion (BW). Prevents any two LSPs getting too
409
close together after quantisation. We know from experiment
410
that LSP quantisation errors < 12.5Hz (25Hz setp size) are
411
inaudible so we use that as the minimum LSP separation.
415
if (lsp[i] - lsp[i-1] < PI*(12.5/4000.0))
416
lsp[i] = lsp[i-1] + PI*(12.5/4000.0);
419
/* as quantiser gaps increased, larger BW expansion was required
420
to prevent twinkly noises */
423
if (lsp[i] - lsp[i-1] < PI*(25.0/4000.0))
424
lsp[i] = lsp[i-1] + PI*(25.0/4000.0);
426
for(i=8; i<order; i++) {
427
if (lsp[i] - lsp[i-1] < PI*(75.0/4000.0))
428
lsp[i] = lsp[i-1] + PI*(75.0/4000.0);
431
for(j=0; j<order; j++)
434
lsp_to_lpc(lsp_, ak, order);
444
/* simulated LPC energy quantisation */
446
float e = 10.0*log10(E);
447
e += 2.0*(1.0 - 2.0*(float)rand()/RAND_MAX);
448
E = pow(10.0,e/10.0);
452
aks_to_M2(ak,order,model,E,&snr, 1); /* {ak} -> {Am} LPC decode */
912
457
/*---------------------------------------------------------------------------*\
920
465
\*---------------------------------------------------------------------------*/
923
kiss_fft_cfg fft_fwd_cfg,
924
float ak[], /* LPC's */
926
MODEL *model, /* sinusoidal model parameters for this frame */
927
float E, /* energy term */
928
float *snr, /* signal to noise ratio for this frame in dB */
929
int dump, /* true to dump sample to dump file */
930
int sim_pf, /* true to simulate a post filter */
931
int pf, /* true to LPC post filter */
932
int bass_boost, /* enable LPC filter 0-1khz 3dB boost */
934
float gamma /* LPC post filter parameters */
468
float ak[], /* LPC's */
470
MODEL *model, /* sinusoidal model parameters for this frame */
471
float E, /* energy term */
472
float *snr, /* signal to noise ratio for this frame in dB */
473
int dump /* true to dump sample to dump file */
937
COMP pw[FFT_ENC]; /* input to FFT for power spectrum */
938
COMP Pw[FFT_ENC]; /* output power spectrum */
476
COMP Pw[FFT_DEC]; /* power spectrum */
939
477
int i,m; /* loop variables */
940
478
int am,bm; /* limits of current band */
941
479
float r; /* no. rads/bin */
942
480
float Em; /* energy in band */
943
481
float Am; /* spectral amplitude sample */
944
482
float signal, noise;
945
TIMER_VAR(tstart, tfft, tpw, tpf);
947
TIMER_SAMPLE(tstart);
949
r = TWO_PI/(FFT_ENC);
484
r = TWO_PI/(FFT_DEC);
951
486
/* Determine DFT of A(exp(jw)) --------------------------------------------*/
953
for(i=0; i<FFT_ENC; i++) {
488
for(i=0; i<FFT_DEC; i++) {
958
493
for(i=0; i<=order; i++)
960
kiss_fft(fft_fwd_cfg, (kiss_fft_cpx *)pw, (kiss_fft_cpx *)Pw);
962
TIMER_SAMPLE_AND_LOG(tfft, tstart, " fft");
495
fft(&Pw[0].real,FFT_DEC,1);
964
497
/* Determine power spectrum P(w) = E/(A(exp(jw))^2 ------------------------*/
966
for(i=0; i<FFT_ENC/2; i++)
499
for(i=0; i<FFT_DEC/2; i++)
967
500
Pw[i].real = E/(Pw[i].real*Pw[i].real + Pw[i].imag*Pw[i].imag);
969
TIMER_SAMPLE_AND_LOG(tpw, tfft, " Pw");
972
lpc_post_filter(fft_fwd_cfg, model, Pw, ak, order, dump, beta, gamma, bass_boost);
974
TIMER_SAMPLE_AND_LOG(tpf, tpw, " LPC post filter");
981
/* Determine magnitudes from P(w) ----------------------------------------*/
983
/* when used just by decoder {A} might be all zeroes so init signal
984
and noise to prevent log(0) errors */
986
signal = 1E-30; noise = 1E-32;
506
/* Determine magnitudes by linear interpolation of P(w) -------------------*/
508
signal = noise = 0.0;
988
509
for(m=1; m<=model->L; m++) {
989
am = (int)((m - 0.5)*model->Wo/r + 0.5);
990
bm = (int)((m + 0.5)*model->Wo/r + 0.5);
997
signal += model->A[m]*model->A[m];
998
noise += (model->A[m] - Am)*(model->A[m] - Am);
1000
/* This code significantly improves perf of LPC model, in
1001
particular when combined with phase0. The LPC spectrum tends
1002
to track just under the peaks of the spectral envelope, and
1003
just above nulls. This algorithm does the reverse to
1004
compensate - raising the amplitudes of spectral peaks, while
1005
attenuating the null. This enhances the formants, and
1006
supresses the energy between formants. */
1009
if (Am > model->A[m])
1011
if (Am < model->A[m])
510
am = floor((m - 0.5)*model->Wo/r + 0.5);
511
bm = floor((m + 0.5)*model->Wo/r + 0.5);
518
signal += pow(model->A[m],2.0);
519
noise += pow(model->A[m] - Am,2.0);
1017
*snr = 10.0*log10f(signal/noise);
1019
TIMER_SAMPLE_AND_LOG2(tpf, " rec");
522
*snr = 10.0*log10(signal/noise);
1022
525
/*---------------------------------------------------------------------------*\
1273
684
lsp[i] = (PI/4000.0)*lsp_hz[i];
1277
#ifdef __EXPERIMENTAL__
1279
/*---------------------------------------------------------------------------*\
1281
FUNCTION....: encode_lsps_diff_freq_vq()
1282
AUTHOR......: David Rowe
1283
DATE CREATED: 15 November 2011
1285
Twenty-five bit LSP quantiser. LSPs 1-4 are quantised with scalar
1286
LSP differences (in frequency, i.e difference from the previous
1287
LSP). LSPs 5-10 are quantised with a VQ trained generated using
1290
\*---------------------------------------------------------------------------*/
1292
void encode_lsps_diff_freq_vq(int indexes[], float lsp[], int order)
1295
float lsp_hz[LPC_MAX];
1296
float lsp__hz[LPC_MAX];
1297
float dlsp[LPC_MAX];
1298
float dlsp_[LPC_MAX];
1303
for(i=0; i<LPC_ORD; i++) {
1307
/* convert from radians to Hz so we can use human readable
1310
for(i=0; i<order; i++)
1311
lsp_hz[i] = (4000.0/PI)*lsp[i];
1313
/* scalar quantisers for LSP differences 1..4 */
1316
for(i=0; i<4; i++) {
1318
dlsp[i] = lsp_hz[i] - lsp__hz[i-1];
1320
dlsp[0] = lsp_hz[0];
1325
indexes[i] = quantise(cb, &dlsp[i], wt, k, m, &se);
1326
dlsp_[i] = cb[indexes[i]*k];
1329
lsp__hz[i] = lsp__hz[i-1] + dlsp_[i];
1331
lsp__hz[0] = dlsp_[0];
1334
/* VQ LSPs 5,6,7,8,9,10 */
1338
cb = lsp_cbjnd[4].cb;
1339
indexes[4] = quantise(cb, &lsp_hz[4], &wt[4], k, m, &se);
1343
/*---------------------------------------------------------------------------*\
1345
FUNCTION....: decode_lsps_diff_freq_vq()
1346
AUTHOR......: David Rowe
1347
DATE CREATED: 15 Nov 2011
1349
From a vector of quantised LSP indexes, returns the quantised
1350
(floating point) LSPs.
1352
\*---------------------------------------------------------------------------*/
1354
void decode_lsps_diff_freq_vq(float lsp_[], int indexes[], int order)
1357
float dlsp_[LPC_MAX];
1358
float lsp__hz[LPC_MAX];
1361
/* scalar LSP differences */
1363
for(i=0; i<4; i++) {
1365
dlsp_[i] = cb[indexes[i]];
1367
lsp__hz[i] = lsp__hz[i-1] + dlsp_[i];
1369
lsp__hz[0] = dlsp_[0];
1376
cb = lsp_cbjnd[4].cb;
1377
for(i=4; i<order; i++)
1378
lsp__hz[i] = cb[indexes[4]*k+i-4];
1380
/* convert back to radians */
1382
for(i=0; i<order; i++)
1383
lsp_[i] = (PI/4000.0)*lsp__hz[i];
1387
/*---------------------------------------------------------------------------*\
1389
FUNCTION....: encode_lsps_diff_time()
1390
AUTHOR......: David Rowe
1391
DATE CREATED: 12 Sep 2012
1393
Encode difference from preious frames's LSPs using
1394
3,3,2,2,2,2,1,1,1,1 scalar quantisers (18 bits total).
1396
\*---------------------------------------------------------------------------*/
1398
void encode_lsps_diff_time(int indexes[],
1404
float lsps_dt[LPC_ORD];
1409
/* Determine difference in time and convert from radians to Hz so
1410
we can use human readable frequencies */
1412
for(i=0; i<LPC_ORD; i++) {
1413
lsps_dt[i] = (4000/PI)*(lsps[i] - lsps__prev[i]);
1416
/* scalar quantisers */
1419
for(i=0; i<order; i++) {
1422
cb = lsp_cbdt[i].cb;
1423
indexes[i] = quantise(cb, &lsps_dt[i], wt, k, m, &se);
1429
/*---------------------------------------------------------------------------*\
1431
FUNCTION....: decode_lsps_diff_time()
1432
AUTHOR......: David Rowe
1433
DATE CREATED: 15 Nov 2011
1435
From a quantised LSP indexes, returns the quantised
1436
(floating point) LSPs.
1438
\*---------------------------------------------------------------------------*/
1440
void decode_lsps_diff_time(
1449
for(i=0; i<order; i++)
1450
lsps_[i] = lsps__prev[i];
1452
for(i=0; i<order; i++) {
1454
cb = lsp_cbdt[i].cb;
1455
lsps_[i] += (PI/4000.0)*cb[indexes[i]*k];
1461
/*---------------------------------------------------------------------------*\
1463
FUNCTION....: encode_lsps_vq()
1464
AUTHOR......: David Rowe
1465
DATE CREATED: 15 Feb 2012
1467
Multi-stage VQ LSP quantiser developed by Jean-Marc Valin.
1469
\*---------------------------------------------------------------------------*/
1471
void encode_lsps_vq(int *indexes, float *x, float *xq, int ndim)
1474
float err[LPC_ORD], err2[LPC_ORD], err3[LPC_ORD];
1475
float w[LPC_ORD], w2[LPC_ORD], w3[LPC_ORD];
1476
const float *codebook1 = lsp_cbjvm[0].cb;
1477
const float *codebook2 = lsp_cbjvm[1].cb;
1478
const float *codebook3 = lsp_cbjvm[2].cb;
1480
assert(ndim <= LPC_ORD);
1482
w[0] = MIN(x[0], x[1]-x[0]);
1483
for (i=1;i<ndim-1;i++)
1484
w[i] = MIN(x[i]-x[i-1], x[i+1]-x[i]);
1485
w[ndim-1] = MIN(x[ndim-1]-x[ndim-2], PI-x[ndim-1]);
1487
compute_weights(x, w, ndim);
1489
n1 = find_nearest(codebook1, lsp_cbjvm[0].m, x, ndim);
1491
for (i=0;i<ndim;i++)
1493
xq[i] = codebook1[ndim*n1+i];
1494
err[i] = x[i] - xq[i];
1496
for (i=0;i<ndim/2;i++)
1499
err3[i] = err[2*i+1];
1503
n2 = find_nearest_weighted(codebook2, lsp_cbjvm[1].m, err2, w2, ndim/2);
1504
n3 = find_nearest_weighted(codebook3, lsp_cbjvm[2].m, err3, w3, ndim/2);
1512
/*---------------------------------------------------------------------------*\
1514
FUNCTION....: decode_lsps_vq()
1515
AUTHOR......: David Rowe
1516
DATE CREATED: 15 Feb 2012
1518
\*---------------------------------------------------------------------------*/
1520
void decode_lsps_vq(int *indexes, float *xq, int ndim)
1523
const float *codebook1 = lsp_cbjvm[0].cb;
1524
const float *codebook2 = lsp_cbjvm[1].cb;
1525
const float *codebook3 = lsp_cbjvm[2].cb;
1531
for (i=0;i<ndim;i++)
1533
xq[i] = codebook1[ndim*n1+i];
1535
for (i=0;i<ndim/2;i++)
1537
xq[2*i] += codebook2[ndim*n2/2+i];
1538
xq[2*i+1] += codebook3[ndim*n3/2+i];
1543
687
/*---------------------------------------------------------------------------*\
1545
689
FUNCTION....: bw_expand_lsps()
1767
decode_lsps_scalar(lsps, lsp_indexes, LPC_ORD);
843
decode_lsps(lsps, lsp_indexes, LPC_ORD);
1768
844
bw_expand_lsps(lsps, LPC_ORD);
1769
845
lsp_to_lpc(lsps, ak, LPC_ORD);
1770
846
*e = decode_energy(energy_index);
1771
aks_to_M2(ak, LPC_ORD, model, *e, &snr, 1, 0, 0, 1);
847
aks_to_M2(ak, LPC_ORD, model, *e, &snr, 1);
1772
848
apply_lpc_correction(model);
1778
static float ge_coeff[2] = {0.8, 0.9};
1780
void compute_weights2(const float *x, const float *xp, float *w, int ndim)
1794
/* Higher weight if pitch is stable */
1795
if (fabsf(x[0]-xp[0])<.2)
1799
} else if (fabsf(x[0]-xp[0])>.5) /* Lower if not stable */
1804
/* Lower weight for low energy */
1805
if (x[1] < xp[1]-10)
1809
if (x[1] < xp[1]-20)
1817
/* Square the weights because it's applied on the squared error */
1823
/*---------------------------------------------------------------------------*\
1825
FUNCTION....: quantise_WoE()
1826
AUTHOR......: Jean-Marc Valin & David Rowe
1827
DATE CREATED: 29 Feb 2012
1829
Experimental joint Wo and LPC energy vector quantiser developed by
1830
Jean-Marc Valin. Exploits correlations between the difference in
1831
the log pitch and log energy from frame to frame. For example
1832
both the pitch and energy tend to only change by small amounts
1833
during voiced speech, however it is important that these changes be
1834
coded carefully. During unvoiced speech they both change a lot but
1835
the ear is less sensitve to errors so coarser quantisation is OK.
1837
The ear is sensitive to log energy and loq pitch so we quantise in
1838
these domains. That way the error measure used to quantise the
1839
values is close to way the ear senses errors.
1841
See http://jmspeex.livejournal.com/10446.html
1843
\*---------------------------------------------------------------------------*/
1845
void quantise_WoE(MODEL *model, float *e, float xq[])
1851
const float *codebook1 = ge_cb[0].cb;
1852
int nb_entries = ge_cb[0].m;
1853
int ndim = ge_cb[0].k;
1854
float Wo_min = TWO_PI/P_MAX;
1855
float Wo_max = TWO_PI/P_MIN;
1857
x[0] = log10f((model->Wo/PI)*4000.0/50.0)/log10f(2);
1858
x[1] = 10.0*log10f(1e-4 + *e);
1860
compute_weights2(x, xq, w, ndim);
1861
for (i=0;i<ndim;i++)
1862
err[i] = x[i]-ge_coeff[i]*xq[i];
1863
n1 = find_nearest_weighted(codebook1, nb_entries, err, w, ndim);
1865
for (i=0;i<ndim;i++)
1867
xq[i] = ge_coeff[i]*xq[i] + codebook1[ndim*n1+i];
1868
err[i] -= codebook1[ndim*n1+i];
1872
x = log2(4000*Wo/(PI*50));
1873
2^x = 4000*Wo/(PI*50)
1874
Wo = (2^x)*(PI*50)/4000;
1877
model->Wo = powf(2.0, xq[0])*(PI*50.0)/4000.0;
1879
/* bit errors can make us go out of range leading to all sorts of
1880
probs like seg faults */
1882
if (model->Wo > Wo_max) model->Wo = Wo_max;
1883
if (model->Wo < Wo_min) model->Wo = Wo_min;
1885
model->L = PI/model->Wo; /* if we quantise Wo re-compute L */
1887
*e = powf(10.0, xq[1]/10.0);
1890
/*---------------------------------------------------------------------------*\
1892
FUNCTION....: encode_WoE()
1893
AUTHOR......: Jean-Marc Valin & David Rowe
1894
DATE CREATED: 11 May 2012
1896
Joint Wo and LPC energy vector quantiser developed my Jean-Marc
1897
Valin. Returns index, and updated states xq[].
1899
\*---------------------------------------------------------------------------*/
1901
int encode_WoE(MODEL *model, float e, float xq[])
1907
const float *codebook1 = ge_cb[0].cb;
1908
int nb_entries = ge_cb[0].m;
1909
int ndim = ge_cb[0].k;
1911
assert((1<<WO_E_BITS) == nb_entries);
1913
if (e < 0.0) e = 0; /* occasional small negative energies due LPC round off I guess */
1915
x[0] = log10f((model->Wo/PI)*4000.0/50.0)/log10f(2);
1916
x[1] = 10.0*log10f(1e-4 + e);
1918
compute_weights2(x, xq, w, ndim);
1919
for (i=0;i<ndim;i++)
1920
err[i] = x[i]-ge_coeff[i]*xq[i];
1921
n1 = find_nearest_weighted(codebook1, nb_entries, err, w, ndim);
1923
for (i=0;i<ndim;i++)
1925
xq[i] = ge_coeff[i]*xq[i] + codebook1[ndim*n1+i];
1926
err[i] -= codebook1[ndim*n1+i];
1929
//printf("enc: %f %f (%f)(%f) \n", xq[0], xq[1], e, 10.0*log10(1e-4 + e));
1934
/*---------------------------------------------------------------------------*\
1936
FUNCTION....: decode_WoE()
1937
AUTHOR......: Jean-Marc Valin & David Rowe
1938
DATE CREATED: 11 May 2012
1940
Joint Wo and LPC energy vector quantiser developed my Jean-Marc
1941
Valin. Given index and states xq[], returns Wo & E, and updates
1944
\*---------------------------------------------------------------------------*/
1946
void decode_WoE(MODEL *model, float *e, float xq[], int n1)
1950
const float *codebook1 = ge_cb[0].cb;
1951
int ndim = ge_cb[0].k;
1952
float Wo_min = TWO_PI/P_MAX;
1953
float Wo_max = TWO_PI/P_MIN;
1955
for (i=0;i<ndim;i++)
1957
xq[i] = ge_coeff[i]*xq[i] + codebook1[ndim*n1+i];
1958
err[i] -= codebook1[ndim*n1+i];
1961
//printf("dec: %f %f\n", xq[0], xq[1]);
1962
model->Wo = powf(2.0, xq[0])*(PI*50.0)/4000.0;
1964
/* bit errors can make us go out of range leading to all sorts of
1965
probs like seg faults */
1967
if (model->Wo > Wo_max) model->Wo = Wo_max;
1968
if (model->Wo < Wo_min) model->Wo = Wo_min;
1970
model->L = PI/model->Wo; /* if we quantise Wo re-compute L */
1972
*e = powf(10.0, xq[1]/10.0);