summaryrefslogtreecommitdiff
path: root/gr-vocoder/lib/codec2/interp.c
blob: 64372b050f7b258256cf9861aeeda706923aa6ed (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
/*---------------------------------------------------------------------------*\

  FILE........: interp.c
  AUTHOR......: David Rowe
  DATE CREATED: 9/10/09

  Interpolation of 20ms frames to 10ms frames.

\*---------------------------------------------------------------------------*/

/*
  Copyright (C) 2009 David Rowe

  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 <http://www.gnu.org/licenses/>.
*/

#include <assert.h>
#include <math.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>

#include "defines.h"
#include "interp.h"
#include "lsp.h"
#include "quantise.h"
#include "dump.h"

float sample_log_amp(MODEL *model, float w);

/*---------------------------------------------------------------------------*\

  FUNCTION....: interp()
  AUTHOR......: David Rowe
  DATE CREATED: 22/8/10

  Given two frames decribed by model parameters 20ms apart, determines
  the model parameters of the 10ms frame between them.  Assumes
  voicing is available for middle (interpolated) frame.  Outputs are
  amplitudes and Wo for the interpolated frame.

  This version can interpolate the amplitudes between two frames of
  different Wo and L.

  This version works by log linear interpolation, but listening tests
  showed it creates problems in background noise, e.g. hts2a and mmt1.
  When this function is used (--dec mode) bg noise appears to be
  amplitude modulated, and gets louder.  The interp_lsp() function
  below seems to do a better job.

\*---------------------------------------------------------------------------*/

void interpolate(
  MODEL *interp,    /* interpolated model params                     */
  MODEL *prev,      /* previous frames model params                  */
  MODEL *next       /* next frames model params                      */
)
{
    int   l;
    float w,log_amp;

    /* Wo depends on voicing of this and adjacent frames */

    if (interp->voiced) {
	if (prev->voiced && next->voiced)
	    interp->Wo = (prev->Wo + next->Wo)/2.0;
	if (!prev->voiced && next->voiced)
	    interp->Wo = next->Wo;
	if (prev->voiced && !next->voiced)
	    interp->Wo = prev->Wo;
    }
    else {
	interp->Wo = TWO_PI/P_MAX;
    }
    interp->L = PI/interp->Wo;

    /* Interpolate amplitudes using linear interpolation in log domain */

    for(l=1; l<=interp->L; l++) {
	w = l*interp->Wo;
	log_amp = (sample_log_amp(prev, w) + sample_log_amp(next, w))/2.0;
	interp->A[l] = pow(10.0, log_amp);
    }
}

/*---------------------------------------------------------------------------*\

  FUNCTION....: sample_log_amp()
  AUTHOR......: David Rowe
  DATE CREATED: 22/8/10

  Samples the amplitude envelope at an arbitrary frequency w.  Uses
  linear interpolation in the log domain to sample between harmonic
  amplitudes.

\*---------------------------------------------------------------------------*/

float sample_log_amp(MODEL *model, float w)
{
    int   m;
    float f, log_amp;

    assert(w > 0.0); assert (w <= PI);

    m = 0;
    while ((m+1)*model->Wo < w) m++;
    f = (w - m*model->Wo)/model->Wo;
    assert(f <= 1.0);

    if (m < 1) {
	log_amp = f*log10(model->A[1] + 1E-6);
    }
    else if ((m+1) > model->L) {
	log_amp = (1.0-f)*log10(model->A[model->L] + 1E-6);
    }
    else {
	log_amp = (1.0-f)*log10(model->A[m] + 1E-6) +
                  f*log10(model->A[m+1] + 1E-6);
	//printf("m=%d A[m] %f A[m+1] %f x %f %f %f\n", m, model->A[m],
	//       model->A[m+1], pow(10.0, log_amp),
	//       (1-f), f);
    }

    return log_amp;
}

/*---------------------------------------------------------------------------*\

  FUNCTION....: sample_log_amp_quad()
  AUTHOR......: David Rowe
  DATE CREATED: 9 March 2011

  Samples the amplitude envelope at an arbitrary frequency w.  Uses
  quadratic interpolation in the log domain to sample between harmonic
  amplitudes.

  y(x) = ax*x + bx + c

  We assume three points are x=-1, x=0, x=1, which we map to m-1,m,m+1

  c = y(0)
  b = (y(1) - y(-1))/2
  a = y(-1) + b - y(0)

\*---------------------------------------------------------------------------*/

float sample_log_amp_quad(MODEL *model, float w)
{
    int   m;
    float a,b,c,x, log_amp;

    assert(w > 0.0); assert (w <= PI);

    m = floor(w/model->Wo + 0.5);
    if (m < 2) m = 2;
    if (m > (model->L-1)) m = model->L-1;
    c = log10(model->A[m]+1E-6);
    b = (log10(model->A[m+1]+1E-6) - log10(model->A[m-1]+1E-6))/2.0;
    a = log10(model->A[m-1]+1E-6) + b - c;
    x = (w - m*model->Wo)/model->Wo;

    log_amp = a*x*x + b*x + c;
    //printf("m=%d A[m-1] %f A[m] %f A[m+1] %f w %f x %f log_amp %f\n", m,
    //	   model->A[m-1],
    //	   model->A[m], model->A[m+1], w, x, pow(10.0, log_amp));
    return log_amp;
}

/*---------------------------------------------------------------------------*\

  FUNCTION....: sample_log_amp_quad_nl()
  AUTHOR......: David Rowe
  DATE CREATED: 10 March 2011

  Samples the amplitude envelope at an arbitrary frequency w.  Uses
  quadratic interpolation in the log domain to sample between harmonic
  amplitudes.  This version can handle non-linear steps along a freq
  axis defined by arbitrary steps.

  y(x) = ax*x + bx + c

  We assume three points are (x_1,y_1), (0,y0) and (x1,y1).

\*---------------------------------------------------------------------------*/

float sample_log_amp_quad_nl(
			     float w[],     /* frequency points            */
			     float A[],     /* for these amplitude samples */
			     int np,        /* number of frequency points  */
			     float w_sample /* frequency of new samples    */
)
{
    int   m,i;
    float a,b,c,x, log_amp, best_dist;
    float x_1, x1;
    float y_1, y0, y1;

    //printf("w_sample  %f\n", w_sample);
    assert(w_sample >= 0.0); assert (w_sample <= 1.1*PI);

    /* find closest point to centre quadratic interpolator */

    best_dist = 1E32;
    for (i=0; i<np; i++)
	if (fabs(w[i] - w_sample) < best_dist) {
	    best_dist = fabs(w[i] - w_sample);
	    m = i;
	}

    /* stay one point away from edge of array */

    if (m < 1) m = 1;
    if (m > (np-2)) m = np - 2;

    /* find polynomial coeffs */

    x_1 = w[m-1]- w[m]; x1 = w[m+1] - w[m];
    y_1 = log10(A[m-1]+1E-6);
    y0  = log10(A[m]+1E-6);
    y1  = log10(A[m+1]+1E-6);

    c = y0;
    a = (y_1*x1 - y1*x_1 + c*x_1 - c*x1)/(x_1*x_1*x1 - x1*x1*x_1);
    b = (y1 -a*x1*x1 - c)/x1;
    x = w_sample - w[m];

    //printf("%f   %f  %f\n", w[0], w[1], w[2]);
    //printf("%f %f  %f %f  %f %f\n", x_1, y_1, 0.0, y0, x1, y1);
    log_amp = a*x*x + b*x + c;
    //printf("a %f  b %f  c %f\n", a, b, c);
    //printf("m=%d A[m-1] %f A[m] %f A[m+1] %f w_sample %f w[m] %f x %f log_amp %f\n", m,
    //	   A[m-1],
    //	   A[m], A[m+1], w_sample, w[m], x, log_amp);
    //exit(0);
    return log_amp;
}

#define M_MAX 40

float fres[] = {100,   200,  300,  400,  500,  600,  700,  800,  900, 1000,
		1200, 1400, 1600, 1850, 2100, 2350, 2600, 2900, 3400, 3800};

/*---------------------------------------------------------------------------*\

  FUNCTION....: resample_amp_nl()
  AUTHOR......: David Rowe
  DATE CREATED: 7 March 2011

  Converts the current model with L {Am} samples spaced Wo apart to
  RES_POINTS samples spaced Wo/RES_POINTS apart.  Then subtracts
  from the previous frames samples to get the delta.

\*---------------------------------------------------------------------------*/

void resample_amp_fixed(MODEL *model,
			float w[], float A[],
			float wres[], float Ares[],
			float AresdB_prev[],
			float AresdB[],
			float deltat[])
{
    int   i;

    for(i=1; i<=model->L; i++) {
	w[i-1] = i*model->Wo;
	A[i-1] = model->A[i];
    }

    for(i=0; i<RES_POINTS; i++) {
	wres[i] = fres[i]*PI/4000.0;
    }

    for(i=0; i<RES_POINTS; i++) {
	Ares[i] = pow(10.0,sample_log_amp_quad_nl(w, A, model->L, wres[i]));
    }

    /* work out delta T vector for this frame */

    for(i=0; i<RES_POINTS; i++) {
	AresdB[i] = 20.0*log10(Ares[i]);
	deltat[i] = AresdB[i] - AresdB_prev[i];
    }

}

/*---------------------------------------------------------------------------*\

  FUNCTION....: resample_amp_nl()
  AUTHOR......: David Rowe
  DATE CREATED: 7 March 2011

  Converts the current model with L {Am} samples spaced Wo apart to M
  samples spaced Wo/M apart.  Then converts back to L {Am} samples.
  used to prototype constant rate Amplitude encoding ideas.

  Returns the SNR in dB.

\*---------------------------------------------------------------------------*/

float resample_amp_nl(MODEL *model, int m, float AresdB_prev[])
{
    int   i;
    float w[MAX_AMP], A[MAX_AMP];
    float wres[MAX_AMP], Ares[MAX_AMP], AresdB[MAX_AMP];
    float signal, noise, snr;
    float new_A;
    float deltat[MAX_AMP], deltat_q[MAX_AMP], AresdB_q[MAX_AMP];

    resample_amp_fixed(model, w, A, wres, Ares, AresdB_prev, AresdB, deltat);

    /* quantise delta T vector */

    for(i=0; i<RES_POINTS; i++) {
	noise = 3.0*(1.0 - 2.0*rand()/RAND_MAX);
	//noise = 0.0;
	deltat_q[i] = deltat[i] + noise;
    }

    /* recover Ares vector */

    for(i=0; i<RES_POINTS; i++) {
	AresdB_q[i] = AresdB_prev[i] + deltat_q[i];
	Ares[i] = pow(10.0, AresdB_q[i]/20.0);
	//printf("%d %f %f\n", i, AresdB[i], AresdB_q[i]);
    }

    /* update memory based on version at decoder */

    for(i=0; i<RES_POINTS; i++) {
	AresdB_prev[i] = AresdB_q[i];
    }

#ifdef DUMP
    dump_resample(wres,Ares,M_MAX);
#endif

    signal = noise = 0.0;

    for(i=1; i<model->L; i++) {
	new_A = pow(10.0,sample_log_amp_quad_nl(wres, Ares, RES_POINTS, model->Wo*i));
	signal += pow(model->A[i], 2.0);
	noise  += pow(model->A[i] - new_A, 2.0);
	//printf("%f %f\n", model->A[i], new_A);
	model->A[i] = new_A;
    }

    snr = 10.0*log10(signal/noise);
    printf("snr = %3.2f\n", snr);
    //exit(0);
    return snr;
}

/*---------------------------------------------------------------------------*\

  FUNCTION....: resample_amp()
  AUTHOR......: David Rowe
  DATE CREATED: 10 March 2011

  Converts the current model with L {Am} samples spaced Wo apart to M
  samples with a non-linear spacing.  Then converts back to L {Am}
  samples.  used to prototype constant rate Amplitude encoding ideas.

  Returns the SNR in dB.

\*---------------------------------------------------------------------------*/

float resample_amp(MODEL *model, int m)
{
    int   i;
    MODEL model_m;
    float new_A, signal, noise, snr, log_amp_dB;
    float n_db = 0.0;

    model_m.Wo = PI/(float)m;
    model_m.L = PI/model_m.Wo;

    for(i=1; i<=model_m.L; i++) {
	log_amp_dB    = 20.0*sample_log_amp_quad(model, i*model_m.Wo);
	log_amp_dB   += n_db*(1.0 - 2.0*rand()/RAND_MAX);
	model_m.A[i]  = pow(10,log_amp_dB/20.0);
    }

    //dump_resample(&model_m);

    signal = noise = 0.0;

    for(i=1; i<model->L/4; i++) {
	new_A = pow(10,sample_log_amp_quad(&model_m, i*model->Wo));
	signal += pow(model->A[i], 2.0);
	noise  += pow(model->A[i] - new_A, 2.0);
	//printf("%f %f\n", model->A[i], new_A);
	model->A[i] = new_A;
    }

    snr = 10.0*log10(signal/noise);
    //printf("snr = %3.2f\n", snr);
    //exit(0);
    return snr;
}

/*---------------------------------------------------------------------------*\

  FUNCTION....: interp_lsp()
  AUTHOR......: David Rowe
  DATE CREATED: 10 Nov 2010

  Given two frames decribed by model parameters 20ms apart, determines
  the model parameters of the 10ms frame between them.  Assumes
  voicing is available for middle (interpolated) frame.  Outputs are
  amplitudes and Wo for the interpolated frame.

  This version uses interpolation of LSPs, seems to do a better job
  with bg noise.

\*---------------------------------------------------------------------------*/

void interpolate_lsp(
  MODEL *interp,    /* interpolated model params                     */
  MODEL *prev,      /* previous frames model params                  */
  MODEL *next,      /* next frames model params                      */
  float *prev_lsps, /* previous frames LSPs                          */
  float  prev_e,    /* previous frames LPC energy                    */
  float *next_lsps, /* next frames LSPs                              */
  float  next_e,    /* next frames LPC energy                        */
  float *ak_interp  /* interpolated aks for this frame                */
		     )
{
    //int   l,i;
    int   i;
    float lsps[LPC_ORD],e;
    float snr;

    /* Wo depends on voicing of this and adjacent frames */

    if (interp->voiced) {
	if (prev->voiced && next->voiced)
	    interp->Wo = (prev->Wo + next->Wo)/2.0;
	if (!prev->voiced && next->voiced)
	    interp->Wo = next->Wo;
	if (prev->voiced && !next->voiced)
	    interp->Wo = prev->Wo;
    }
    else {
	interp->Wo = TWO_PI/P_MAX;
    }
    interp->L = PI/interp->Wo;

    /* interpolate LSPs */

    for(i=0; i<LPC_ORD; i++) {
	lsps[i] = (prev_lsps[i] + next_lsps[i])/2.0;
    }

    /* Interpolate LPC energy in log domain */

    e = pow(10.0, (log10(prev_e) + log10(next_e))/2.0);

    /* convert back to amplitudes */

    lsp_to_lpc(lsps, ak_interp, LPC_ORD);
    aks_to_M2(ak_interp, LPC_ORD, interp, e, &snr, 0);
}