1 //
2 // firinterp_firdecim_crcf_example.c
3 //
4 // This example demonstrates interpolation and decimation of a QPSK
5 // signal with a square-root Nyquist filter.
6 // Data symbols are generated and then interpolated according to a
7 // finite impulse response square-root Nyquist filter.  The resulting
8 // sequence is then decimated with the same filter, matched to the
9 // interpolator.
10 //
11 // SEE ALSO: firinterp_crcf_example.c
12 //
13 
14 #include <stdio.h>
15 #include <stdlib.h>
16 #include <math.h>
17 #include <getopt.h>
18 
19 #include "liquid.h"
20 
21 #define OUTPUT_FILENAME "firinterp_firdecim_crcf_example.m"
22 
23 // print usage/help message
usage()24 void usage()
25 {
26     printf("firinterp_firdecim_crcf_example:\n");
27     printf("  -h         : print usage/help\n");
28     printf("  -k <s/sym> : samples/symbol (interp factor), k > 1, default: 2\n");
29     printf("  -m <delay> : filter delay (symbols), m > 0,         default: 2\n");
30     printf("  -b <bw>    : excess bandwidth factor, 0 < beta < 1, default: 0.5\n");
31     printf("  -n <num>   : number of data symbols,                default: 8\n");
32 }
33 
34 
main(int argc,char * argv[])35 int main(int argc, char*argv[]) {
36     // options
37     unsigned int k        = 2;      // samples/symbol
38     unsigned int m        = 3;      // filter delay
39     float        dt       = 0.5f;   // filter fractional symbol delay
40     float        beta     = 0.5f;   // filter excess bandwidth
41     unsigned int num_syms = 8;      // number of data symbols
42 
43     int dopt;
44     while ((dopt = getopt(argc,argv,"hk:m:b:n:")) != EOF) {
45         switch (dopt) {
46         case 'h': usage();                          return 0;
47         case 'k': k = atoi(optarg);                 break;
48         case 'm': m = atoi(optarg);                 break;
49         case 'b': beta = atof(optarg);              break;
50         case 'n': num_syms = atoi(optarg);  break;
51         default:
52             usage();
53             return 1;
54         }
55     }
56 
57     // validate options
58     if (k < 2) {
59         fprintf(stderr,"error: %s, interp factor must be greater than 1\n", argv[0]);
60         return 1;
61     } else if (m < 1) {
62         fprintf(stderr,"error: %s, filter delay must be greater than 0\n", argv[0]);
63         return 1;
64     } else if (beta <= 0.0 || beta > 1.0f) {
65         fprintf(stderr,"error: %s, beta (excess bandwidth factor) must be in (0,1]\n", argv[0]);
66         return 1;
67     } else if (num_syms < 1) {
68         fprintf(stderr,"error: %s, must have at least one data symbol\n", argv[0]);
69         return 1;
70     }
71 
72     // derived values
73     unsigned int h_len          = 2*k*m + 1;        // prototype filter length
74     unsigned int num_syms_total = num_syms + 2*m;   // number of total symbols (w/ delay)
75     unsigned int num_samples    = k*num_syms_total; // number of samples
76 
77     // design filter and create interpolator and decimator objects
78     float h[h_len];     // transmit filter
79     float g[h_len];     // receive filter (reverse of h)
80     liquid_firdes_rrcos(k,m,beta,dt,h);
81     unsigned int i;
82     for (i=0; i<h_len; i++)
83         g[i] = h[h_len-i-1];
84     firinterp_crcf interp = firinterp_crcf_create(k,h,h_len);
85     firdecim_crcf  decim  = firdecim_crcf_create(k,g,h_len);
86     firdecim_crcf_set_scale(decim, 1.0f/(float)k);
87 
88     // allocate memory for buffers
89     float complex x[num_syms_total];   // input symbols
90     float complex y[num_samples];   // interpolated sequence
91     float complex z[num_syms_total];   // decimated (received) symbols
92 
93     // generate input symbols, padded with zeros at the end
94     for (i=0; i<num_syms_total; i++) {
95         float complex s = (rand() % 2 ? 1.0f : -1.0f) +
96                           (rand() % 2 ? 1.0f : -1.0f) * _Complex_I;
97         x[i] = i < num_syms ? s : 0;
98     }
99 
100     // run interpolator
101     for (i=0; i<num_syms_total; i++)
102         firinterp_crcf_execute(interp, x[i], &y[k*i]);
103 
104     // run decimator
105     for (i=0; i<num_syms_total; i++)
106         firdecim_crcf_execute(decim, &y[k*i], &z[i]);
107 
108     // destroy objects
109     firinterp_crcf_destroy(interp);
110     firdecim_crcf_destroy(decim);
111 
112     // print results to screen
113     printf("filter impulse response :\n");
114     for (i=0; i<h_len; i++)
115         printf("  [%4u] : %8.4f\n", i, h[i]);
116 
117     printf("input symbols\n");
118     for (i=0; i<num_syms_total; i++) {
119         printf("  [%4u] : %8.4f + j*%8.4f", i, crealf(x[i]), cimagf(x[i]));
120 
121         // highlight actual data symbols
122         if (i < num_syms) printf(" *\n");
123         else                      printf("\n");
124     }
125 
126     printf("interpolator output samples:\n");
127     for (i=0; i<num_samples; i++) {
128         printf("  [%4u] : %8.4f + j*%8.4f", i, crealf(y[i]), cimagf(y[i]));
129 
130         if ( (i >= k*m) && ((i%k)==0))  printf(" **\n");
131         else                            printf("\n");
132     }
133 
134     printf("output symbols:\n");
135     for (i=0; i<num_syms_total; i++) {
136         printf("  [%4u] : %8.4f + j*%8.4f", i, crealf(z[i]), cimagf(z[i]));
137 
138         // highlight symbols (compensate for filter delay)
139         if ( i < 2*m ) printf("\n");
140         else           printf(" *\n");
141     }
142 
143     //
144     // export results to file
145     //
146     FILE * fid = fopen(OUTPUT_FILENAME,"w");
147     fprintf(fid,"%% %s: auto-generated file\n\n", OUTPUT_FILENAME);
148     fprintf(fid,"clear all;\n");
149     fprintf(fid,"close all;\n");
150     fprintf(fid,"k = %u;\n", k);
151     fprintf(fid,"m = %u;\n", m);
152     fprintf(fid,"dt = %8.6f;\n", dt);
153     fprintf(fid,"h_len=%u;\n",h_len);
154     fprintf(fid,"num_syms_total = %u;\n", num_syms_total);
155     fprintf(fid,"num_samples = k*num_syms_total;\n");
156     fprintf(fid,"h = zeros(1,h_len);\n");
157     fprintf(fid,"x = zeros(1,num_syms_total);\n");
158     fprintf(fid,"y = zeros(1,num_samples);\n");
159 
160     for (i=0; i<h_len; i++)
161         fprintf(fid,"h(%4u) = %12.4e;\n", i+1, h[i]);
162 
163     for (i=0; i<num_syms_total; i++)
164         fprintf(fid,"x(%4u) = %12.4e + j*%12.4e;\n", i+1, crealf(x[i]), cimagf(x[i]));
165 
166     for (i=0; i<num_samples; i++)
167         fprintf(fid,"y(%4u) = %12.4e + j*%12.4e;\n", i+1, crealf(y[i]), cimagf(y[i]));
168 
169     for (i=0; i<num_syms_total; i++)
170         fprintf(fid,"z(%4u) = %12.4e + j*%12.4e;\n", i+1, crealf(z[i]), cimagf(z[i]));
171 
172     fprintf(fid,"\n\n");
173     fprintf(fid,"tx = [0:(num_syms_total-1)];\n");
174     fprintf(fid,"ty = [0:(num_samples-1)]/k - m + dt/k;\n");
175     fprintf(fid,"tz = [0:(num_syms_total-1)] - 2*m;\n");
176     fprintf(fid,"figure;\n");
177     fprintf(fid,"subplot(2,1,1);\n");
178     fprintf(fid,"    plot(tx,real(x),'s',ty,real(y),'-',tz,real(z),'x');\n");
179     fprintf(fid,"    xlabel('time');\n");
180     fprintf(fid,"    ylabel('real');\n");
181     fprintf(fid,"    grid on;\n");
182     fprintf(fid,"    legend('symbols in','interp','symbols out',0);\n");
183     fprintf(fid,"subplot(2,1,2);\n");
184     fprintf(fid,"    plot(tx,imag(x),'s',ty,imag(y),'-',tz,imag(z),'x');\n");
185     fprintf(fid,"    xlabel('time');\n");
186     fprintf(fid,"    ylabel('imag');\n");
187     fprintf(fid,"    grid on;\n");
188 
189     fclose(fid);
190     printf("results written to %s.\n",OUTPUT_FILENAME);
191 
192     printf("done.\n");
193     return 0;
194 }
195