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
|
/********************************************************************
* *
* THIS FILE IS PART OF THE OggVorbis SOFTWARE CODEC SOURCE CODE. *
* USE, DISTRIBUTION AND REPRODUCTION OF THIS LIBRARY SOURCE IS *
* GOVERNED BY A BSD-STYLE SOURCE LICENSE INCLUDED WITH THIS SOURCE *
* IN 'COPYING'. PLEASE READ THESE TERMS BEFORE DISTRIBUTING. *
* *
* THE OggVorbis SOURCE CODE IS (C) COPYRIGHT 1994-2001 *
* by the Xiph.Org Foundation http://www.xiph.org/ *
* *
********************************************************************
function: function calls to collect codebook metrics
********************************************************************/
#include <stdlib.h>
#include <unistd.h>
#include <math.h>
#include "bookutil.h"
/* collect the following metrics:
mean and mean squared amplitude
mean and mean squared error
mean and mean squared error (per sample) by entry
worst case fit by entry
entry cell size
hits by entry
total bits
total samples
(average bits per sample)*/
/* set up metrics */
float meanamplitude_acc=0.f;
float meanamplitudesq_acc=0.f;
float meanerror_acc=0.f;
float meanerrorsq_acc=0.f;
float **histogram=NULL;
float **histogram_error=NULL;
float **histogram_errorsq=NULL;
float **histogram_hi=NULL;
float **histogram_lo=NULL;
float bits=0.f;
float count=0.f;
static float *_now(codebook *c, int i){
return c->valuelist+i*c->c->dim;
}
int books=0;
void process_preprocess(codebook **bs,char *basename){
int i;
while(bs[books])books++;
if(books){
histogram=_ogg_calloc(books,sizeof(float *));
histogram_error=_ogg_calloc(books,sizeof(float *));
histogram_errorsq=_ogg_calloc(books,sizeof(float *));
histogram_hi=_ogg_calloc(books,sizeof(float *));
histogram_lo=_ogg_calloc(books,sizeof(float *));
}else{
fprintf(stderr,"Specify at least one codebook\n");
exit(1);
}
for(i=0;i<books;i++){
codebook *b=bs[i];
histogram[i]=_ogg_calloc(b->entries,sizeof(float));
histogram_error[i]=_ogg_calloc(b->entries*b->dim,sizeof(float));
histogram_errorsq[i]=_ogg_calloc(b->entries*b->dim,sizeof(float));
histogram_hi[i]=_ogg_calloc(b->entries*b->dim,sizeof(float));
histogram_lo[i]=_ogg_calloc(b->entries*b->dim,sizeof(float));
}
}
static float _dist(int el,float *a, float *b){
int i;
float acc=0.f;
for(i=0;i<el;i++){
float val=(a[i]-b[i]);
acc+=val*val;
}
return acc;
}
void cell_spacing(codebook *c){
int j,k;
float min=-1.f,max=-1.f,mean=0.f,meansq=0.f;
long total=0;
/* minimum, maximum, mean, ms cell spacing */
for(j=0;j<c->c->entries;j++){
if(c->c->lengthlist[j]>0){
float localmin=-1.;
for(k=0;k<c->c->entries;k++){
if(c->c->lengthlist[k]>0){
float this=_dist(c->c->dim,_now(c,j),_now(c,k));
if(j!=k &&
(localmin==-1 || this<localmin))
localmin=this;
}
}
if(min==-1 || localmin<min)min=localmin;
if(max==-1 || localmin>max)max=localmin;
mean+=sqrt(localmin);
meansq+=localmin;
total++;
}
}
fprintf(stderr,"\tminimum cell spacing (closest side): %g\n",sqrt(min));
fprintf(stderr,"\tmaximum cell spacing (closest side): %g\n",sqrt(max));
fprintf(stderr,"\tmean closest side spacing: %g\n",mean/total);
fprintf(stderr,"\tmean sq closest side spacing: %g\n",sqrt(meansq/total));
}
void process_postprocess(codebook **bs,char *basename){
int i,k,book;
char *buffer=alloca(strlen(basename)+80);
fprintf(stderr,"Done. Processed %ld data points:\n\n",
(long)count);
fprintf(stderr,"Global statistics:******************\n\n");
fprintf(stderr,"\ttotal samples: %ld\n",(long)count);
fprintf(stderr,"\ttotal bits required to code: %ld\n",(long)bits);
fprintf(stderr,"\taverage bits per sample: %g\n\n",bits/count);
fprintf(stderr,"\tmean sample amplitude: %g\n",
meanamplitude_acc/count);
fprintf(stderr,"\tmean squared sample amplitude: %g\n\n",
sqrt(meanamplitudesq_acc/count));
fprintf(stderr,"\tmean code error: %g\n",
meanerror_acc/count);
fprintf(stderr,"\tmean squared code error: %g\n\n",
sqrt(meanerrorsq_acc/count));
for(book=0;book<books;book++){
FILE *out;
codebook *b=bs[book];
int n=b->c->entries;
int dim=b->c->dim;
fprintf(stderr,"Book %d statistics:------------------\n",book);
cell_spacing(b);
sprintf(buffer,"%s-%d-mse.m",basename,book);
out=fopen(buffer,"w");
if(!out){
fprintf(stderr,"Could not open file %s for writing\n",buffer);
exit(1);
}
for(i=0;i<n;i++){
for(k=0;k<dim;k++){
fprintf(out,"%d, %g, %g\n",
i*dim+k,(b->valuelist+i*dim)[k],
sqrt((histogram_errorsq[book]+i*dim)[k]/histogram[book][i]));
}
}
fclose(out);
sprintf(buffer,"%s-%d-me.m",basename,book);
out=fopen(buffer,"w");
if(!out){
fprintf(stderr,"Could not open file %s for writing\n",buffer);
exit(1);
}
for(i=0;i<n;i++){
for(k=0;k<dim;k++){
fprintf(out,"%d, %g, %g\n",
i*dim+k,(b->valuelist+i*dim)[k],
(histogram_error[book]+i*dim)[k]/histogram[book][i]);
}
}
fclose(out);
sprintf(buffer,"%s-%d-worst.m",basename,book);
out=fopen(buffer,"w");
if(!out){
fprintf(stderr,"Could not open file %s for writing\n",buffer);
exit(1);
}
for(i=0;i<n;i++){
for(k=0;k<dim;k++){
fprintf(out,"%d, %g, %g, %g\n",
i*dim+k,(b->valuelist+i*dim)[k],
(b->valuelist+i*dim)[k]+(histogram_lo[book]+i*dim)[k],
(b->valuelist+i*dim)[k]+(histogram_hi[book]+i*dim)[k]);
}
}
fclose(out);
}
}
float process_one(codebook *b,int book,float *a,int dim,int step,int addmul,
float base){
int j,entry;
float amplitude=0.f;
if(book==0){
float last=base;
for(j=0;j<dim;j++){
amplitude=a[j*step]-(b->c->q_sequencep?last:0);
meanamplitude_acc+=fabs(amplitude);
meanamplitudesq_acc+=amplitude*amplitude;
count++;
last=a[j*step];
}
}
if(b->c->q_sequencep){
float temp;
for(j=0;j<dim;j++){
temp=a[j*step];
a[j*step]-=base;
}
base=temp;
}
entry=vorbis_book_besterror(b,a,step,addmul);
if(entry==-1){
fprintf(stderr,"Internal error: _best returned -1.\n");
exit(1);
}
histogram[book][entry]++;
bits+=vorbis_book_codelen(b,entry);
for(j=0;j<dim;j++){
float error=a[j*step];
if(book==books-1){
meanerror_acc+=fabs(error);
meanerrorsq_acc+=error*error;
}
histogram_errorsq[book][entry*dim+j]+=error*error;
histogram_error[book][entry*dim+j]+=fabs(error);
if(histogram[book][entry]==0 || histogram_hi[book][entry*dim+j]<error)
histogram_hi[book][entry*dim+j]=error;
if(histogram[book][entry]==0 || histogram_lo[book][entry*dim+j]>error)
histogram_lo[book][entry*dim+j]=error;
}
return base;
}
void process_vector(codebook **bs,int *addmul,int inter,float *a,int n){
int bi;
int i;
for(bi=0;bi<books;bi++){
codebook *b=bs[bi];
int dim=b->dim;
float base=0.f;
if(inter){
for(i=0;i<n/dim;i++)
base=process_one(b,bi,a+i,dim,n/dim,addmul[bi],base);
}else{
for(i=0;i<=n-dim;i+=dim)
base=process_one(b,bi,a+i,dim,1,addmul[bi],base);
}
}
if((long)(count)%100)spinnit("working.... samples: ",count);
}
void process_usage(void){
fprintf(stderr,
"usage: vqmetrics [-i] +|*<codebook>.vqh [ +|*<codebook.vqh> ]... \n"
" datafile.vqd [datafile.vqd]...\n\n"
" data can be taken on stdin. -i indicates interleaved coding.\n"
" Output goes to output files:\n"
" basename-me.m: gnuplot: mean error by entry value\n"
" basename-mse.m: gnuplot: mean square error by entry value\n"
" basename-worst.m: gnuplot: worst error by entry value\n"
" basename-distance.m: gnuplot file showing distance probability\n"
"\n");
}
|