1 //
2 // firdes_kaiser_example.c
3 //
4 // This example demonstrates finite impulse response filter design
5 // using a Kaiser window.
6 // SEE ALSO: firdespm_example.c
7 //
8 
9 #include <stdlib.h>
10 #include <stdio.h>
11 #include <getopt.h>
12 
13 #include "liquid.h"
14 
15 #define OUTPUT_FILENAME "firdes_kaiser_example.m"
16 
17 // print usage/help message
usage()18 void usage()
19 {
20     printf("firdes_kaiser_example:\n");
21     printf("  u/h   : print usage/help\n");
22     printf("  f     : filter cutoff frequency,           0 < f < 0.5, default: 0.2\n");
23     printf("  s     : filter stop-band attenuation [dB], 0 < s,       default: 60\n");
24     printf("  m     : fractional sample delay,        -0.5 < m < 0.5, default: 0\n");
25     printf("  n     : filter length [taps],              n > 1,       default: 55\n");
26 }
27 
main(int argc,char * argv[])28 int main(int argc, char*argv[]) {
29     // options
30     float fc=0.2f;          // filter cutoff frequency
31     float As=60.0f;         // stop-band attenuation [dB]
32     float mu=0.0f;          // fractional timing offset
33     unsigned int h_len=55;  // filter length
34 
35     int dopt;
36     while ((dopt = getopt(argc,argv,"uhf:n:s:m:")) != EOF) {
37         switch (dopt) {
38         case 'u':
39         case 'h': usage();              return 0;
40         case 'f': fc    = atof(optarg); break;
41         case 'n': h_len = atoi(optarg); break;
42         case 's': As    = atof(optarg); break;
43         case 'm': mu    = atof(optarg); break;
44         default:
45             exit(1);
46         }
47     }
48 
49     // validate input
50     if ( fc <= 0.0f ) {
51         fprintf(stderr,"error: %s, filter cutoff frequency must be greater than zero\n", argv[0]);
52         exit(1);
53     } else if ( h_len == 0 ) {
54         fprintf(stderr,"error: %s, filter length must be greater than zero\n", argv[0]);
55         exit(1);
56     }
57 
58     printf("filter design parameters\n");
59     printf("    cutoff frequency            :   %8.4f\n", fc);
60     printf("    stop-band attenuation [dB]  :   %8.4f\n", As);
61     printf("    fractional sample offset    :   %8.4f\n", mu);
62     printf("    filter length               :   %u\n", h_len);
63 
64     // generate the filter
65     unsigned int i;
66     float h[h_len];
67     liquid_firdes_kaiser(h_len,fc,As,mu,h);
68 
69     // print coefficients
70     for (i=0; i<h_len; i++)
71         printf("h(%4u) = %16.12f;\n", i+1, h[i]);
72 
73     // output to file
74     FILE*fid = fopen(OUTPUT_FILENAME,"w");
75     fprintf(fid,"%% %s: auto-generated file\n\n", OUTPUT_FILENAME);
76     fprintf(fid,"clear all;\nclose all;\n\n");
77     fprintf(fid,"h_len=%u;\n",h_len);
78     fprintf(fid,"fc=%12.4e;\n",fc);
79     fprintf(fid,"As=%12.4e;\n",As);
80 
81     for (i=0; i<h_len; i++)
82         fprintf(fid,"h(%4u) = %12.4e;\n", i+1, h[i]);
83 
84     fprintf(fid,"nfft=1024;\n");
85     fprintf(fid,"H=20*log10(abs(fftshift(fft(h*2*fc,nfft))));\n");
86     fprintf(fid,"f=[0:(nfft-1)]/nfft-0.5;\n");
87     fprintf(fid,"figure; plot(f,H,'Color',[0 0.5 0.25],'LineWidth',2);\n");
88     fprintf(fid,"grid on;\n");
89     fprintf(fid,"xlabel('normalized frequency');\n");
90     fprintf(fid,"ylabel('PSD [dB]');\n");
91     fprintf(fid,"title(['Filter design/Kaiser window f_c: %f, S_L: %f, h: %u']);\n",
92             fc, -As, h_len);
93     fprintf(fid,"axis([-0.5 0.5 -As-40 10]);\n");
94 
95     fclose(fid);
96     printf("results written to %s\n", OUTPUT_FILENAME);
97 
98     printf("done.\n");
99     return 0;
100 }
101 
102