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