1 //
2 // agc_crcf_example.c
3 //
4 // Automatic gain control example demonstrating its transient
5 // response.
6 //
7 
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include <math.h>
11 #include <complex.h>
12 #include <getopt.h>
13 #include "liquid.h"
14 
15 #define OUTPUT_FILENAME "agc_crcf_example.m"
16 
17 // print usage/help message
usage()18 void usage()
19 {
20     printf("agc_example [options]\n");
21     printf("  h     : print usage\n");
22     printf("  n     : number of samples, n >=100, default: 2000\n");
23     printf("  b     : AGC bandwidth,     b >=  0, default: 0.01\n");
24 }
25 
26 
main(int argc,char * argv[])27 int main(int argc, char*argv[])
28 {
29     // options
30     float        bt          = 0.01f;   // agc loop bandwidth
31     float        gamma       = 0.001f;  // initial signal level
32     unsigned int num_samples = 2000;    // number of samples
33 
34     int dopt;
35     while((dopt = getopt(argc,argv,"hn:N:s:b:")) != EOF){
36         switch (dopt) {
37         case 'h': usage();                      return 0;
38         case 'n': num_samples = atoi(optarg);   break;
39         case 'b': bt          = atof(optarg);   break;
40         default:
41             exit(1);
42         }
43     }
44 
45     // validate input
46     if (bt < 0.0f) {
47         fprintf(stderr,"error: %s, bandwidth must be positive\n", argv[0]);
48         exit(1);
49     } else if (num_samples == 0) {
50         fprintf(stderr,"error: %s, number of samples must be greater than zero\n", argv[0]);
51         exit(1);
52     }
53 
54     unsigned int i;
55 
56     // create objects
57     agc_crcf q = agc_crcf_create();
58     agc_crcf_set_bandwidth(q, bt);
59     //agc_crcf_set_scale(q, 0.5f);
60 
61     float complex x[num_samples];   // input
62     float complex y[num_samples];   // output
63     float rssi[num_samples];        // received signal strength
64 
65     // print info
66     agc_crcf_print(q);
67 
68     // generate signal
69     for (i=0; i<num_samples; i++)
70         x[i] = gamma * cexpf(_Complex_I*2*M_PI*0.0193f*i);
71 
72     // run agc
73     for (i=0; i<num_samples; i++) {
74         // apply gain
75         agc_crcf_execute(q, x[i], &y[i]);
76 
77         // retrieve signal level [dB]
78         rssi[i] = agc_crcf_get_rssi(q);
79     }
80 
81     // destroy AGC object
82     agc_crcf_destroy(q);
83 
84     //
85     // export results
86     //
87     FILE* fid = fopen(OUTPUT_FILENAME,"w");
88     if (!fid) {
89         fprintf(stderr,"error: %s, could not open '%s' for writing\n", argv[0], OUTPUT_FILENAME);
90         exit(1);
91     }
92     fprintf(fid,"%% %s: auto-generated file\n\n",OUTPUT_FILENAME);
93     fprintf(fid,"clear all;\nclose all;\n\n");
94     fprintf(fid,"n = %u;\n", num_samples);
95 
96     for (i=0; i<num_samples; i++) {
97         fprintf(fid,"x(%4u) = %12.4e + j*%12.4e;\n", i+1, crealf(x[i]), cimagf(x[i]));
98         fprintf(fid,"y(%4u) = %12.4e + j*%12.4e;\n", i+1, crealf(y[i]), cimagf(y[i]));
99         fprintf(fid,"rssi(%4u)  = %12.4e;\n", i+1, rssi[i]);
100     }
101 
102     // plot results
103     fprintf(fid,"\n");
104     fprintf(fid,"figure;\n");
105     fprintf(fid,"t = 0:(n-1);\n");
106     fprintf(fid,"subplot(3,1,1);\n");
107     fprintf(fid,"  plot(t, real(x), '-', 'Color',[0 0.2 0.5],...\n");
108     fprintf(fid,"       t, imag(x), '-', 'Color',[0 0.5 0.2]);\n");
109     fprintf(fid,"  grid on;\n");
110     fprintf(fid,"  xlabel('sample index');\n");
111     fprintf(fid,"  ylabel('input');\n");
112     fprintf(fid,"subplot(3,1,2);\n");
113     fprintf(fid,"  plot(t, real(y), '-', 'Color',[0 0.2 0.5],...\n");
114     fprintf(fid,"       t, imag(y), '-', 'Color',[0 0.5 0.2]);\n");
115     fprintf(fid,"  grid on;\n");
116     fprintf(fid,"  xlabel('sample index');\n");
117     fprintf(fid,"  ylabel('output');\n");
118     fprintf(fid,"subplot(3,1,3);\n");
119     fprintf(fid,"  plot(t,rssi,'-','LineWidth',1.2,'Color',[0.5 0 0]);\n");
120     fprintf(fid,"  grid on;\n");
121     fprintf(fid,"  xlabel('sample index');\n");
122     fprintf(fid,"  ylabel('rssi [dB]');\n");
123 
124     fclose(fid);
125     printf("results written to %s\n", OUTPUT_FILENAME);
126 
127     printf("done.\n");
128     return 0;
129 }
130 
131