1 //
2 // iirfilt_crcf_example.c
3 //
4 // Complex infinite impulse response filter example. Demonstrates
5 // the functionality of iirfilt by designing a low-order
6 // prototype (e.g. Butterworth) and using it to filter a noisy
7 // signal.  The filter coefficients are real, but the input and
8 // output arrays are complex.  The filter order and cutoff
9 // frequency are specified at the beginning.
10 //
11 
12 #include <stdio.h>
13 #include <math.h>
14 #include <complex.h>
15 
16 #include "liquid.h"
17 
18 #define OUTPUT_FILENAME "iirfilt_crcf_example.m"
19 
main()20 int main() {
21     // options
22     unsigned int order =   4;       // filter order
23     float        fc    =   0.1f;    // cutoff frequency
24     float        f0    =   0.0f;    // center frequency
25     float        Ap    =   1.0f;    // pass-band ripple
26     float        As    =  40.0f;    // stop-band attenuation
27     unsigned int n     = 128;       // number of samples
28     liquid_iirdes_filtertype ftype  = LIQUID_IIRDES_ELLIP;
29     liquid_iirdes_bandtype   btype  = LIQUID_IIRDES_LOWPASS;
30     liquid_iirdes_format     format = LIQUID_IIRDES_SOS;
31 
32     // design filter from prototype
33     iirfilt_crcf q = iirfilt_crcf_create_prototype(
34                         ftype, btype, format, order, fc, f0, Ap, As);
35     iirfilt_crcf_print(q);
36 
37     unsigned int i;
38 
39     // allocate memory for data arrays
40     float complex x[n];
41     float complex y[n];
42 
43     // generate input signal (noisy sine wave with decaying amplitude)
44     for (i=0; i<n; i++) {
45         x[i] = cexpf((2*M_PI*0.057f*_Complex_I - 0.04f)*i);
46         x[i] += 0.1f*(randnf() + _Complex_I*randnf());
47 
48         // run filter
49         iirfilt_crcf_execute(q, x[i], &y[i]);
50     }
51 
52     // compute response
53     unsigned int nfft=512;
54     float complex H[nfft];
55     for (i=0; i<nfft; i++) {
56         float freq = 0.5f * (float)i / (float)nfft;
57         iirfilt_crcf_freqresponse(q, freq, &H[i]);
58     }
59 
60     // destroy filter object
61     iirfilt_crcf_destroy(q);
62 
63     //
64     // plot results to output file
65     //
66     FILE * fid = fopen(OUTPUT_FILENAME,"w");
67     fprintf(fid,"%% %s : auto-generated file\n", OUTPUT_FILENAME);
68     fprintf(fid,"clear all;\n");
69     fprintf(fid,"close all;\n");
70     fprintf(fid,"\n");
71     fprintf(fid,"order=%u;\n", order);
72     fprintf(fid,"n=%u;\n",n);
73     fprintf(fid,"nfft=%u;\n",nfft);
74     fprintf(fid,"x=zeros(1,n);\n");
75     fprintf(fid,"y=zeros(1,n);\n");
76     fprintf(fid,"H=zeros(1,nfft);\n");
77 
78     for (i=0; i<n; i++) {
79         //printf("%4u : %12.8f + j*%12.8f\n", i, crealf(y), cimagf(y));
80         fprintf(fid,"x(%4u) = %12.4e + j*%12.4e;\n", i+1, crealf(x[i]), cimagf(x[i]));
81         fprintf(fid,"y(%4u) = %12.4e + j*%12.4e;\n", i+1, crealf(y[i]), cimagf(y[i]));
82     }
83 
84     for (i=0; i<nfft; i++)
85         fprintf(fid,"H(%4u) = %12.8f + j*%12.8f;\n", i+1, crealf(H[i]), cimagf(H[i]));
86 
87     // plot output
88     fprintf(fid,"t=0:(n-1);\n");
89     fprintf(fid,"figure;\n");
90     fprintf(fid,"subplot(2,1,1);\n");
91     fprintf(fid,"  plot(t,real(x),'-','Color',[1 1 1]*0.5,'LineWidth',1,...\n");
92     fprintf(fid,"       t,real(y),'-','Color',[0 0.5 0.25],'LineWidth',2);\n");
93     fprintf(fid,"  xlabel('time');\n");
94     fprintf(fid,"  ylabel('real');\n");
95     fprintf(fid,"  legend('input','filtered output','location','northeast');\n");
96     fprintf(fid,"  grid on;\n");
97     fprintf(fid,"subplot(2,1,2);\n");
98     fprintf(fid,"  plot(t,imag(x),'-','Color',[1 1 1]*0.5,'LineWidth',1,...\n");
99     fprintf(fid,"       t,imag(y),'-','Color',[0 0.25 0.5],'LineWidth',2);\n");
100     fprintf(fid,"  xlabel('time');\n");
101     fprintf(fid,"  ylabel('imag');\n");
102     fprintf(fid,"  legend('input','filtered output','location','northeast');\n");
103     fprintf(fid,"  grid on;\n");
104 
105     // plot frequency response
106     fprintf(fid,"f=0.5*[0:(nfft-1)]/nfft;\n");
107     fprintf(fid,"figure;\n");
108     fprintf(fid,"subplot(3,1,1);\n");
109     fprintf(fid,"  plot(f,20*log10(abs(H)),'Color',[0 0.25 0.5],'LineWidth',2);\n");
110     fprintf(fid,"  axis([0 0.5 -3 0]);\n");
111     fprintf(fid,"  grid on;\n");
112     fprintf(fid,"  ylabel('Pass band [dB]');\n");
113     fprintf(fid,"subplot(3,1,2);\n");
114     fprintf(fid,"  plot(f,20*log10(abs(H)),'Color',[0 0.25 0.5],'LineWidth',2);\n");
115     fprintf(fid,"  axis([0 0.5 -100 0]);\n");
116     fprintf(fid,"  grid on;\n");
117     fprintf(fid,"  ylabel('Stop band [dB]');\n");
118     fprintf(fid,"subplot(3,1,3);\n");
119     fprintf(fid,"  plot(f,180/pi*arg(H),'Color',[0 0.25 0.5],'LineWidth',2);\n");
120     //fprintf(fid,"  axis([0 0.5 -100 0]);\n");
121     fprintf(fid,"  grid on;\n");
122     fprintf(fid,"  ylabel('Phase [degrees]');\n");
123     fprintf(fid,"  xlabel('Normalized Frequency [f/F_s]');\n");
124     fclose(fid);
125     printf("results written to %s.\n", OUTPUT_FILENAME);
126 
127     printf("done.\n");
128     return 0;
129 }
130 
131