1 //
2 // firpfbch2_crcf_example.c
3 //
4 // Example of the finite impulse response (FIR) polyphase filterbank
5 // (PFB) channelizer with an output rate of 2 Fs / M as an (almost)
6 // perfect reconstructive system.
7 //
8 
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <math.h>
12 #include <getopt.h>
13 #include <assert.h>
14 
15 #include "liquid.h"
16 
17 #define OUTPUT_FILENAME "firpfbch2_crcf_example.m"
18 
19 // print usage/help message
usage()20 void usage()
21 {
22     printf("%s [options]\n", __FILE__);
23     printf("  h     : print help\n");
24     printf("  M     : number of channels, default: 6\n");
25     printf("  m     : prototype filter semi-length, default: 4\n");
26     printf("  s     : prototype filter stop-band attenuation, default: 80\n");
27     printf("  n     : number of 'symbols' to analyze, default: 20\n");
28 }
29 
main(int argc,char * argv[])30 int main(int argc, char*argv[])
31 {
32     // options
33     unsigned int num_channels=6;    // number of channels
34     unsigned int m = 4;             // filter semi-length (symbols)
35     unsigned int num_symbols=20;    // number of symbols
36     float As = 80.0f;               // filter stop-band attenuation
37 
38     int dopt;
39     while ((dopt = getopt(argc,argv,"hM:m:s:n:")) != EOF) {
40         switch (dopt) {
41         case 'h':   usage();                     return 0;
42         case 'M':   num_channels = atoi(optarg); break;
43         case 'm':   m            = atoi(optarg); break;
44         case 's':   As           = atof(optarg); break;
45         case 'n':   num_symbols  = atof(optarg); break;
46         default:
47             exit(1);
48         }
49     }
50 
51     unsigned int i;
52 
53     // validate input
54     if (num_channels < 2 || num_channels % 2) {
55         fprintf(stderr,"error: %s, number of channels must be greater than 2 and even\n", argv[0]);
56         exit(1);
57     } else if (m == 0) {
58         fprintf(stderr,"error: %s, filter semi-length must be greater than zero\n", argv[0]);
59         exit(1);
60     } else if (num_symbols == 0) {
61         fprintf(stderr,"error: %s, number of symbols must be greater than zero", argv[0]);
62         exit(1);
63     }
64 
65     // derived values
66     unsigned int num_samples = num_channels * num_symbols;
67 
68     // allocate arrays
69     float complex x[num_samples];
70     float complex y[num_samples];
71 
72     // generate input signal
73     for (i=0; i<num_samples; i++) {
74         //x[i] = (i==0) ? 1.0f : 0.0f;
75         x[i] = cexpf( (-0.05f + 0.07f*_Complex_I)*i );  // decaying complex exponential
76     }
77 
78     // create filterbank objects from prototype
79     firpfbch2_crcf qa = firpfbch2_crcf_create_kaiser(LIQUID_ANALYZER,    num_channels, m, As);
80     firpfbch2_crcf qs = firpfbch2_crcf_create_kaiser(LIQUID_SYNTHESIZER, num_channels, m, As);
81     firpfbch2_crcf_print(qa);
82     firpfbch2_crcf_print(qs);
83 
84     // run channelizer
85     float complex Y[num_channels];
86     for (i=0; i<num_samples; i+=num_channels/2) {
87         // run analysis filterbank
88         firpfbch2_crcf_execute(qa, &x[i], Y);
89 
90         // run synthesis filterbank
91         firpfbch2_crcf_execute(qs, Y, &y[i]);
92     }
93 
94     // destroy filterbank objects
95     firpfbch2_crcf_destroy(qa); // analysis filterbank
96     firpfbch2_crcf_destroy(qs); // synthesis filterbank
97 
98     // print output
99     for (i=0; i<num_samples; i++)
100         printf("%4u : %12.8f + %12.8fj\n", i, crealf(y[i]), cimagf(y[i]));
101 
102     // compute RMSE
103     float rmse = 0.0f;
104     unsigned int delay = 2*num_channels*m - num_channels/2 + 1;
105     for (i=0; i<num_samples; i++) {
106         float complex err = y[i] - (i < delay ? 0.0f : x[i-delay]);
107         rmse += crealf( err*conjf(err) );
108     }
109     rmse = sqrtf( rmse/(float)num_samples );
110     printf("rmse : %12.4e\n", rmse);
111 
112     //
113     // EXPORT DATA TO FILE
114     //
115     FILE * fid = fopen(OUTPUT_FILENAME,"w");
116     fprintf(fid,"%% %s: auto-generated file\n\n", OUTPUT_FILENAME);
117     fprintf(fid,"clear all;\n");
118     fprintf(fid,"close all;\n");
119     fprintf(fid,"num_channels=%u;\n", num_channels);
120     fprintf(fid,"m = %u;\n", m);
121     fprintf(fid,"num_symbols=%u;\n",  num_symbols);
122     fprintf(fid,"num_samples = num_channels*num_symbols;\n");
123 
124     fprintf(fid,"x = zeros(1,num_samples);\n");
125     fprintf(fid,"y = zeros(1,num_samples);\n");
126 
127     // save input and output arrays
128     for (i=0; i<num_samples; i++) {
129         fprintf(fid,"x(%4u) = %12.4e + j*%12.4e;\n", i+1, crealf(x[i]), cimag(x[i]));
130         fprintf(fid,"y(%4u) = %12.4e + j*%12.4e;\n", i+1, crealf(y[i]), cimag(y[i]));
131     }
132 
133     // save error vector
134     for (i=delay; i<num_samples; i++) {
135         float complex e = y[i] - x[i-delay];
136         fprintf(fid,"e(%4u) = %12.4e + j*%12.4e;\n", i+1, crealf(e), cimag(e));
137     }
138 
139     // plot results
140     fprintf(fid,"t = 0:(num_samples-1);\n");
141     fprintf(fid,"delay = %u;\n", delay);
142     fprintf(fid,"figure;\n");
143     fprintf(fid,"title('composite');\n");
144     fprintf(fid,"subplot(3,1,1);\n");
145     fprintf(fid,"    plot(t,real(x), t-delay,real(y),'s','MarkerSize',1);\n");
146     fprintf(fid,"    axis([-2 num_samples -0.3 1.1]);\n");
147     fprintf(fid,"    ylabel('real');\n");
148     fprintf(fid,"    legend('original','reconstructed','location','northeast');\n");
149     fprintf(fid,"    grid on;\n");
150     fprintf(fid,"subplot(3,1,2);\n");
151     fprintf(fid,"    plot(t,imag(x), t-delay,imag(y),'s','MarkerSize',1);\n");
152     fprintf(fid,"    axis([-2 num_samples -0.3 1.1]);\n");
153     fprintf(fid,"    ylabel('imag');\n");
154     fprintf(fid,"    grid on;\n");
155     fprintf(fid,"subplot(3,1,3);\n");
156     fprintf(fid,"    plot(t-delay,real(e), t-delay,imag(e));\n");
157     fprintf(fid,"    emax = 1.2*max(abs(e));\n");
158     fprintf(fid,"    axis([-2 num_samples -emax emax]);\n");
159     fprintf(fid,"    legend('real','imag','location','northeast');\n");
160     fprintf(fid,"    xlabel('time');\n");
161     fprintf(fid,"    ylabel('error');\n");
162     fprintf(fid,"    grid on;\n");
163 
164     fclose(fid);
165     printf("results written to '%s'\n", OUTPUT_FILENAME);
166 
167     printf("done.\n");
168     return 0;
169 }
170