1 //
2 // quantize_example.c
3 //
4 // Demonstrates the quantizer/compander combo.
5 //
6
7 #include <stdlib.h>
8 #include <stdio.h>
9 #include <getopt.h>
10 #include <math.h>
11
12 #include "liquid.h"
13
14 #define OUTPUT_FILENAME "quantize_example.m"
15
16 // print usage/help message
usage()17 void usage()
18 {
19 printf("quantize_example [options]\n");
20 printf(" u/h : print usage\n");
21 printf(" b : number of bits, 0 < b <= 16 [default: 4]\n");
22 printf(" m : mu, compression factor, mu > 0 [default: 255.0]\n");
23 }
24
25
main(int argc,char * argv[])26 int main(int argc, char*argv[]) {
27 unsigned int num_bits=4;
28 float mu = 255.0f;
29 unsigned int num_samples = 64;
30
31 int dopt;
32 while ((dopt = getopt(argc,argv,"uhb:m:")) != EOF) {
33 switch (dopt) {
34 case 'u':
35 case 'h': usage(); return 0;
36 case 'b': num_bits = atoi(optarg); break;
37 case 'm': mu = atof(optarg); break;
38 default:
39 exit(1);
40 }
41 }
42
43 // validate input
44 if (num_bits > 16 || num_bits < 1) {
45 fprintf(stderr,"error: %s, quantizer bits must be in [1,16]\n", argv[0]);
46 exit(1);
47 } else if (mu < 0.0f) {
48 fprintf(stderr,"error: %s, mu must be greater than 0\n", argv[0]);
49 exit(1);
50 }
51
52 unsigned int i;
53 float x[num_samples];
54 float y[num_samples];
55
56 float v; // compressed sample
57 unsigned int q; // quantized sample
58 float u; // uncompressed sample
59 float e; // error
60 float rmse = 0.0f;
61 printf(" input ADC output\n");
62 printf(" ----- --- ------\n");
63 float phi = 0.0f;
64 float dphi = 0.02f * 256.0f / (float) num_samples;
65 for (i=0; i<num_samples; i++) {
66 // generate windowed pulse
67 x[i] = 0.5f*cosf(2.0f*M_PI*phi) + 0.5f*cosf(2.0f*M_PI*phi*0.57f);
68 x[i] *= 0.5f * kaiser(i, num_samples, 10.0f, 0.0f);
69 phi += dphi;
70
71 // compress sample
72 v = compress_mulaw(x[i],mu);
73
74 // quantize: analog to digital converter
75 q = quantize_adc(v,num_bits);
76
77 // quantize: digital to analog converter
78 u = quantize_dac(q,num_bits);
79
80 // expand sample
81 y[i] = expand_mulaw(u,mu);
82
83 // compute error
84 e = y[i] - x[i];
85 rmse += e*e;
86
87 printf("%4u : %12.8f > 0x%4.4x > %12.8f\n", i, x[i], q, y[i]);
88 }
89 rmse = sqrtf(rmse / (float)num_samples);
90 printf("-----------------------\n");
91 printf("rmse : %12.4e\n", rmse);
92
93 // open debug file
94 FILE * fid = fopen(OUTPUT_FILENAME,"w");
95 fprintf(fid,"%% %s: auto-generated file\n\n", OUTPUT_FILENAME);
96 fprintf(fid,"clear all\n");
97 fprintf(fid,"close all\n");
98 fprintf(fid,"num_samples = %u;\n", num_samples);
99
100 for (i=0; i<num_samples; i++) {
101 fprintf(fid,"x(%4u) = %16.8e; y(%4u) = %16.8e;\n", i+1, x[i], i+1, y[i]);
102 }
103 fprintf(fid,"figure;\n");
104 fprintf(fid,"t = 0:(num_samples-1);\n");
105 fprintf(fid,"%% generate stairs-like plot\n");
106 fprintf(fid,"t0 = reshape([t(1:num_samples-1)(:) t(1:num_samples-1)(:)]',1,2*(num_samples-1)) + 0.5;\n");
107 fprintf(fid,"y0 = reshape([y(1:num_samples-1)(:) y(2:num_samples)(:)]', 1,2*(num_samples-1));\n");
108 fprintf(fid,"hold on;\n");
109 fprintf(fid,"plot(t,x,t0,y0);\n");
110 fprintf(fid,"xlabel('sample index');\n");
111 fprintf(fid,"ylabel('signal');\n");
112 fprintf(fid,"legend('original','reconstructed',1);\n");
113 fprintf(fid,"grid on;\n");
114
115 // close debug file
116 fclose(fid);
117 printf("results wrtten to %s\n", OUTPUT_FILENAME);
118
119 printf("done.\n");
120 return 0;
121 }
122
123