1 //
2 // firinterp_crcf_example.c
3 //
4 // This example demonstrates the firinterp object (interpolator) interface.
5 // Data symbols are generated and then interpolated according to a
6 // finite impulse response Nyquist filter.
7 //
8 
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <math.h>
12 #include <getopt.h>
13 
14 #include "liquid.h"
15 
16 #define OUTPUT_FILENAME "firinterp_crcf_example.m"
17 
18 // print usage/help message
usage()19 void usage()
20 {
21     printf("firinterp_crcf_example:\n");
22     printf("  -h         : print usage/help\n");
23     printf("  -k <s/sym> : samples/symbol (interp factor), k > 1, default: 4\n");
24     printf("  -m <delay> : filter delay (symbols), m > 0,         default: 3\n");
25     printf("  -s <atten> : filter stop-band attenuation [dB],     default: 60\n");
26     printf("  -n <num>   : number of data symbols,                default: 16\n");
27 }
28 
29 
main(int argc,char * argv[])30 int main(int argc, char*argv[]) {
31     // options
32     unsigned int    k        = 4;       // samples/symbol
33     unsigned int    m        = 3;       // filter delay
34     float           As       = 60.0f;   // filter stop-band attenuation
35     unsigned int    num_syms = 16;      // number of data symbols
36 
37     int dopt;
38     while ((dopt = getopt(argc,argv,"uhk:m:s:n:")) != EOF) {
39         switch (dopt) {
40         case 'u':
41         case 'h': usage();                          return 0;
42         case 'k': k = atoi(optarg);                 break;
43         case 'm': m = atoi(optarg);                 break;
44         case 's': As = atof(optarg);                break;
45         case 'n': num_syms = atoi(optarg);  break;
46         default:
47             exit(1);
48         }
49     }
50 
51     // validate options
52     if (k < 2) {
53         fprintf(stderr,"error: %s, interp factor must be greater than 1\n", argv[0]);
54         exit(1);
55     } else if (m < 1) {
56         fprintf(stderr,"error: %s, filter delay must be greater than 0\n", argv[0]);
57         exit(1);
58     } else if (num_syms < 1) {
59         fprintf(stderr,"error: %s, must have at least one data symbol\n", argv[0]);
60         usage();
61         return 1;
62     }
63 
64     // derived values
65     unsigned int num_syms_total = num_syms + 2*m;   // total symbols (w/ delay)
66     unsigned int num_samples    = k*num_syms_total; // total samples
67 
68     // create interpolator from prototype
69     firinterp_crcf q = firinterp_crcf_create_kaiser(k,m,As);
70 
71     // generate input signal and interpolate
72     float complex x[num_syms_total];   // input symbols
73     float complex y[num_samples];   // output samples
74     unsigned int i;
75     for (i=0; i<num_syms; i++) {
76         x[i] = (rand() % 2 ? 1.0f : -1.0f) +
77                (rand() % 2 ? 1.0f : -1.0f) * _Complex_I;
78     }
79 
80     // pad end of sequence with zeros
81     for (i=num_syms; i<num_syms_total; i++)
82         x[i] = 0.0f;
83 
84     // interpolate symbols
85     for (i=0; i<num_syms_total; i++)
86         firinterp_crcf_execute(q, x[i], &y[k*i]);
87 
88     // destroy interpolator object
89     firinterp_crcf_destroy(q);
90 
91     // print results to screen
92     printf("x(t) :\n");
93     for (i=0; i<num_syms_total; i++)
94         printf("  x(%4u) = %8.4f + j*%8.4f;\n", i, crealf(x[i]), cimagf(x[i]));
95 
96     printf("y(t) :\n");
97     for (i=0; i<num_samples; i++) {
98         printf("  y(%4u) = %8.4f + j*%8.4f;", i, crealf(y[i]), cimagf(y[i]));
99         if ( (i >= k*m) && ((i%k)==0))
100             printf(" **\n");
101         else
102             printf("\n");
103     }
104 
105     //
106     // export output file
107     //
108     FILE * fid = fopen(OUTPUT_FILENAME,"w");
109     fprintf(fid,"%% %s: auto-generated file\n\n", OUTPUT_FILENAME);
110     fprintf(fid,"clear all;\n");
111     fprintf(fid,"close all;\n");
112     fprintf(fid,"k = %u;\n", k);
113     fprintf(fid,"m = %u;\n", m);
114     fprintf(fid,"num_syms_total = %u;\n", num_syms_total);
115     fprintf(fid,"num_samples = k*num_syms_total;\n");
116     fprintf(fid,"x = zeros(1,num_syms_total);\n");
117     fprintf(fid,"y = zeros(1,num_samples);\n");
118 
119     for (i=0; i<num_syms_total; i++)
120         fprintf(fid,"x(%4u) = %12.4e + j*%12.4e;\n", i+1, crealf(x[i]), cimagf(x[i]));
121 
122     for (i=0; i<num_samples; i++)
123         fprintf(fid,"y(%4u) = %12.4e + j*%12.4e;\n", i+1, crealf(y[i]), cimagf(y[i]));
124 
125     fprintf(fid,"\n\n");
126     fprintf(fid,"tx = [0:(num_syms_total-1)];\n");
127     fprintf(fid,"ty = [0:(num_samples-1)]/k - m;\n");
128     fprintf(fid,"figure;\n");
129     fprintf(fid,"subplot(2,1,1);\n");
130     fprintf(fid,"    plot(ty,real(y),'-',tx,real(x),'s');\n");
131     fprintf(fid,"    xlabel('time');\n");
132     fprintf(fid,"    ylabel('real');\n");
133     fprintf(fid,"    grid on;\n");
134     fprintf(fid,"subplot(2,1,2);\n");
135     fprintf(fid,"    plot(ty,imag(y),'-',tx,imag(x),'s');\n");
136     fprintf(fid,"    xlabel('time');\n");
137     fprintf(fid,"    ylabel('imag');\n");
138     fprintf(fid,"    grid on;\n");
139 
140     fclose(fid);
141     printf("results written to %s.\n",OUTPUT_FILENAME);
142 
143     printf("done.\n");
144     return 0;
145 }
146