-
Notifications
You must be signed in to change notification settings - Fork 0
/
ivq.c
124 lines (98 loc) · 2.29 KB
/
ivq.c
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
#include <stdio.h>
#include <math.h>
#ifndef M_PI
#define M_PI 3.141592653589792434
#endif
FILE *ifp; /* Input File Name */
#define COS_LENGTH 13
#define OUTPUT_LENGTH 85
#define VQ_SIZE 256
float CosTransform[OUTPUT_LENGTH][COS_LENGTH];
float InputGain;
int MaxSamples;
char *progname;
float CodeBook[VQ_SIZE][COS_LENGTH];
main(argc, argv)
int argc;
char **argv;
{
int f, j, ByteCount, i, FrameCount;
float *Data, ReadIeeeFloatHighLow();
float *Output, max, min, scale, result;
if (argc < 2){
fprintf(stderr, "Syntax: %s input codebook > output\n",
argv[0]);
exit(1);
}
progname = argv[0];
ifp = fopen(argv[1], "r");
ReadCodeBook(argv[2]);
for (f=0;f<COS_LENGTH;f++){
for (j=0;j<OUTPUT_LENGTH;j++){
CosTransform[j][f] = cos(M_PI*f*j/OUTPUT_LENGTH);
}
}
ByteCount = Read32BitsHighLow(ifp);
Output = (float *)malloc(ByteCount*OUTPUT_LENGTH*sizeof(*Output));
FrameCount = 0;
while (ByteCount > 0){
int code;
code = ReadByte(ifp) & 0xff;
if (code < 0 || code >= VQ_SIZE){
fprintf(stderr,
"%s: Got a code (%d) out of the codebook (0->%d)\n",
progname, code, VQ_SIZE);
exit(1);
}
Data = CodeBook[code];
for (j=0;j<OUTPUT_LENGTH;j++){
result = 0.0;
for (i=0;i<COS_LENGTH;i++)
result += Data[i] * CosTransform[j][i];
Output[FrameCount*OUTPUT_LENGTH+j] = result;
}
ByteCount--;
FrameCount++;
}
max = min = Output[0];
for (i=0;i<FrameCount*OUTPUT_LENGTH;i++){
if (Output[i] > max)
max = Output[i];
if (Output[i] < min)
min = Output[i];
}
fprintf(stderr, "Min is %g, Max is %g.\n", min, max);
scale = 255/(max-min);
for (i=0;i<FrameCount*OUTPUT_LENGTH;i++){
putchar((int)floor((Output[i]-min)*scale));
}
}
ReadCodeBook(name)
char *name;
{
int i;
FILE *fp;
fp = fopen(name, "r");
if (!fp) {
fprintf(stderr, "%s: Can't open codebook (%s).\n",
progname, name);
exit(1);
}
/* Note, unlike the ceptral files,
* the codebook files have a float
* count (instead of a byte count.)
*/
i = Read32BitsHighLow(fp);
if (i != COS_LENGTH*VQ_SIZE){
fprintf(stderr, "%s: Code is wrong size (%d), expected %d.\n",
progname, i, COS_LENGTH*VQ_SIZE);
exit(1);
}
for (i=0;i<VQ_SIZE;i++){
int j;
for (j=0;j<COS_LENGTH;j++){
CodeBook[i][j] = ReadIeeeFloatHighLow(fp);
}
}
fclose(fp);
}