1 //
2 // nyquist_filter_example.c
3 //
4
5 #include <stdio.h>
6 #include <stdlib.h>
7 #include <string.h>
8 #include <getopt.h>
9 #include <math.h>
10
11 #include "liquid.h"
12
13 #define OUTPUT_FILENAME "nyquist_filter_example.m"
14
15 // print usage/help message
usage()16 void usage()
17 {
18 printf("nyquist_filter_example options:\n");
19 printf(" u/h : print usage/help\n");
20 printf(" t : filter type: [kaiser], pm, rcos, fexp, fsech, farcsech\n");
21 printf(" k : filter samples/symbol, k >= 2, default: 2\n");
22 printf(" m : filter delay (symbols), m >= 1, default: 4\n");
23 printf(" b : filter excess bandwidth factor, 0 < b < 1, default: 0.33\n");
24 }
25
26
main(int argc,char * argv[])27 int main(int argc, char*argv[]) {
28 // options
29 unsigned int k=2; // samples/symbol
30 unsigned int m=4; // symbol delay
31 float beta=0.33f; // excess bandwidth factor
32 int ftype = LIQUID_FIRFILT_RCOS;
33
34 int dopt;
35 while ((dopt = getopt(argc,argv,"uht:k:m:b:")) != EOF) {
36 switch (dopt) {
37 case 'u':
38 case 'h':
39 usage();
40 return 0;
41 case 't':
42 if (strcmp(optarg,"kaiser")==0) {
43 ftype = LIQUID_FIRFILT_KAISER;
44 } else if (strcmp(optarg,"pm")==0) {
45 ftype = LIQUID_FIRFILT_PM;
46 } else if (strcmp(optarg,"rcos")==0) {
47 ftype = LIQUID_FIRFILT_RCOS;
48 } else if (strcmp(optarg,"fexp")==0) {
49 ftype = LIQUID_FIRFILT_FEXP;
50 } else if (strcmp(optarg,"fsech")==0) {
51 ftype = LIQUID_FIRFILT_FSECH;
52 } else if (strcmp(optarg,"farcsech")==0) {
53 ftype = LIQUID_FIRFILT_FARCSECH;
54 } else {
55 fprintf(stderr,"error: %s, unknown filter type '%s'\n", argv[0], optarg);
56 exit(1);
57 }
58 break;
59 case 'k': k = atoi(optarg); break;
60 case 'm': m = atoi(optarg); break;
61 case 'b': beta = atof(optarg); break;
62 default:
63 exit(1);
64 }
65 }
66
67 if (k < 2) {
68 fprintf(stderr,"error: %s, k must be at least 2\n", argv[0]);
69 exit(1);
70 } else if (m < 1) {
71 fprintf(stderr,"error: %s, m must be at least 1\n", argv[0]);
72 exit(1);
73 } else if (beta <= 0.0f || beta >= 1.0f) {
74 fprintf(stderr,"error: %s, beta must be in (0,1)\n", argv[0]);
75 exit(1);
76 }
77
78 // initialize objects
79 unsigned int h_len = 2*k*m+1;
80 float h[h_len];
81
82 // design the filter
83 liquid_firdes_prototype(ftype,k,m,beta,0,h);
84
85 // print the coefficients to the screen
86 unsigned int i;
87 for (i=0; i<h_len; i++)
88 printf("h(%3u) = %12.8f\n", i+1, h[i]);
89
90 //
91 // export output file
92 //
93 FILE * fid = fopen(OUTPUT_FILENAME,"w");
94 fprintf(fid,"%% %s : auto-generated file\n\n", OUTPUT_FILENAME);
95 fprintf(fid,"clear all;\n");
96 fprintf(fid,"close all;\n");
97 fprintf(fid,"k = %u;\n", k);
98 fprintf(fid,"m = %u;\n", m);
99 fprintf(fid,"beta = %12.8f;\n", beta);
100 fprintf(fid,"h_len = 2*k*m+1;\n");
101
102 fprintf(fid,"h = zeros(1,h_len);\n");
103 for (i=0; i<h_len; i++)
104 fprintf(fid,"h(%3u) = %20.8e;\n", i+1, h[i]);
105 fprintf(fid,"nfft=1024;\n");
106 fprintf(fid,"f = [0:(nfft-1)]/nfft - 0.5;\n");
107 fprintf(fid,"H = 20*log10(abs(fftshift(fft(h/k,nfft))));\n");
108 fprintf(fid,"figure;\n");
109 fprintf(fid,"plot(f,H,'-','LineWidth',2,...\n");
110 fprintf(fid," [0.5/k],[-20*log10(2)],'or',...\n");
111 fprintf(fid," [0.5/k*(1-beta) 0.5/k*(1-beta)],[-100 10],'-r',...\n");
112 fprintf(fid," [0.5/k*(1+beta) 0.5/k*(1+beta)],[-100 10],'-r');\n");
113 fprintf(fid,"xlabel('normalized frequency');\n");
114 fprintf(fid,"ylabel('PSD');\n");
115 fprintf(fid,"axis([-0.5 0.5 -100 10]);\n");
116 fprintf(fid,"grid on;\n");
117
118 fclose(fid);
119 printf("results written to %s.\n", OUTPUT_FILENAME);
120
121 printf("done.\n");
122 return 0;
123 }
124
125