-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpredict.h
executable file
·157 lines (130 loc) · 5.43 KB
/
predict.h
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
#include "mppenc.h"
#define MAX_LPC_ORDER 35
#define log2(x) ( log (x) * (1./M_LN2) )
#define ORDER_PENALTY 0
static int // best prediction order model
CalculateLPCCoeffs ( Int32_t* buf, // Samples
size_t nbuf, // Number of samples
Int32_t offset, //
double* lpcout, // quantized prediction coefficients
int nlpc, // max. prediction order
float* psigbit, // expected number of bits per original signal sample
float* presbit ) // expected number of bits per residual signal sample
{
static double* fbuf = NULL;
static int nflpc = 0;
static int nfbuf = 0;
int nbit;
int i;
int j;
int bestnbit;
int bestnlpc;
double e;
double bestesize;
double ci;
double esize;
double acf [MAX_LPC_ORDER + 1];
double ref [MAX_LPC_ORDER + 1];
double lpc [MAX_LPC_ORDER + 1];
double tmp [MAX_LPC_ORDER + 1];
double escale = 0.5 * M_LN2 * M_LN2 / nbuf;
double sum;
if ( nlpc >= nbuf ) // if necessary, limit the LPC order to the number of samples available
nlpc = nbuf - 1;
if ( nlpc > nflpc || nbuf > nfbuf ) { // grab some space for a 'zero mean' buffer of floats if needed
if ( fbuf != NULL )
free ( fbuf - nflpc );
fbuf = nlpc + ((double*) calloc ( nlpc+nbuf, sizeof (*fbuf) ));
nfbuf = nbuf;
nflpc = nlpc;
}
e = 0.;
for ( j = 0; j < nbuf; j++ ) { // zero mean signal and compute energy
sum = fbuf [j] = buf[j] - (double)offset;
e += sum * sum;
}
esize = e > 0. ? 0.5 * log2 (escale * e) : 0.;
*psigbit = esize; // return the expected number of bits per original signal sample
acf [0] = e; // store the best values so far (the zeroth order predictor)
bestnlpc = 0;
bestnbit = nbuf * esize;
bestesize = esize;
for ( i = 1; i <= nlpc && e > 0. && i < bestnlpc + 4; i++ ) { // just check two more than bestnlpc
sum = 0.;
for ( j = i; j < nbuf; j++ ) // compute the jth autocorrelation coefficient
sum += fbuf [j] * fbuf [j-i];
acf [i] = sum;
ci = 0.; // compute the reflection and LP coeffients for order j predictor
for ( j = 1; j < i; j++ )
ci += lpc [j] * acf [i-j];
lpc [i] = ref [i] = ci = (acf [i] - ci) / e;
for ( j = 1; j < i; j++ )
tmp [j] = lpc [j] - ci * lpc [i-j];
for ( j = 1; j < i; j++ )
lpc [j] = tmp [j];
e *= 1 - ci*ci; // compute the new energy in the prediction residual
esize = e > 0. ? 0.5 * log2 (escale * e) : 0.;
nbit = nbuf * esize + i * ORDER_PENALTY;
if ( nbit < bestnbit ) { // store this model if it is the best so far
bestnlpc = i; // store best model order
bestnbit = nbit;
bestesize = esize;
for ( j = 0; j < bestnlpc; j++ ) // store the quantized LP coefficients
lpcout [j] = lpc [j+1];
}
}
*presbit = bestesize; // return the expected number of bits per residual signal sample
return bestnlpc; // return the best model order
}
static void
Pred ( const unsigned int* new,
unsigned int* old )
{
static Double DOUBLE [36];
Float org;
Float pred;
int i;
int j;
int sum = 18;
int order;
double oldeff = 0.;
double neweff = 0.;
for ( i = 0; i < 36; i++ )
sum += old [i];
sum = (int) floor (sum / 36.);
order = CalculateLPCCoeffs ( old, 36, sum*0, DOUBLE, 35, &org, &pred );
printf ("avg: %4u [%2u] %.2f %.2f\n\n", sum, order, org, pred );
if ( order < 1 )
return;
for ( i = 0; i < order; i++ )
printf ("%f ", DOUBLE[i] );
printf ("\n");
for ( i = 0; i < 36; i++ ) {
double sum = 0.;
for ( j = 1; j <= order; j++ ) {
sum += (i-j < 0 ? old[i-j+36] : new [i-j]) * DOUBLE [j-1];
}
printf ("%2u: %6.2f %3d\n", i, sum, new [i] );
oldeff += new[i] * new[i];
neweff += (sum-new[i]) * (sum-new[i]);
}
printf ("%6.2f %6.2f\n", sqrt(oldeff), sqrt(neweff) );
}
void
Predicate ( int Channel, int Band, unsigned int* x, int* scf )
{
static Int32_t OLD [CH] [32] [36];
int i;
printf ("=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=\n");
for ( i = 0; i < 36; i++ )
printf ("%2d ", OLD [Channel][Band][i] );
printf ("\n");
for ( i = 0; i < 36; i++ )
printf ("%2d ", x[i] );
printf ("\n");
printf ("%2u-%2u-%2u ", scf[0], scf[1], scf[2] );
Pred ( x, OLD [Channel][Band] );
for ( i = 0; i < 36; i++ )
OLD [Channel][Band][i] = x[i];
}
/* end of predict.c */