1 //
2 // iirfilt_crcf_dcblocker_example.c
3 //
4 // This example demonstrates how to create a DC-blocking recursive
5 // (infinite impulse response) filter.
6 //
7
8 #include <stdio.h>
9 #include <math.h>
10 #include <complex.h>
11
12 #include "liquid.h"
13
14 #define OUTPUT_FILENAME "iirfilt_crcf_dcblocker_example.m"
15
main()16 int main() {
17 // options
18 unsigned int num_samples = 1200; // number of samples
19 float alpha = 0.10f; // filter cut-off
20
21 // design filter from prototype
22 iirfilt_crcf q = iirfilt_crcf_create_dc_blocker(alpha);
23 iirfilt_crcf_print(q);
24
25 // allocate memory for data arrays
26 float complex x[num_samples]; // original input
27 float complex y[num_samples]; // input with DC offset
28 float complex z[num_samples]; // DC-blocked result
29
30 // generate signals
31 unsigned int i;
32 for (i=0; i<num_samples; i++) {
33 // original input signal
34 x[i] = cexpf( (0.070f*i + 1e-4f*i*i)*_Complex_I );
35
36 // add DC offset
37 y[i] = x[i] + 2.0f*cexpf( 0.007f*_Complex_I*i );
38
39 // run filter to try to remove DC offset
40 iirfilt_crcf_execute(q, y[i], &z[i]);
41 }
42
43 // destroy filter object
44 iirfilt_crcf_destroy(q);
45
46 //
47 // plot results to output file
48 //
49 FILE * fid = fopen(OUTPUT_FILENAME,"w");
50 fprintf(fid,"%% %s : auto-generated file\n", OUTPUT_FILENAME);
51 fprintf(fid,"clear all;\n");
52 fprintf(fid,"close all;\n");
53 fprintf(fid,"\n");
54 fprintf(fid,"num_samples=%u;\n",num_samples);
55 fprintf(fid,"x=zeros(1,num_samples);\n");
56 fprintf(fid,"y=zeros(1,num_samples);\n");
57
58 // save input, output arrays
59 for (i=0; i<num_samples; i++) {
60 fprintf(fid,"x(%4u) = %12.4e + j*%12.4e;\n", i+1, crealf(x[i]), cimagf(x[i]));
61 fprintf(fid,"y(%4u) = %12.4e + j*%12.4e;\n", i+1, crealf(y[i]), cimagf(y[i]));
62 fprintf(fid,"z(%4u) = %12.4e + j*%12.4e;\n", i+1, crealf(z[i]), cimagf(z[i]));
63 }
64
65 // plot output
66 fprintf(fid,"t=0:(num_samples-1);\n");
67 fprintf(fid,"figure;\n");
68 fprintf(fid,"subplot(3,1,1);\n");
69 fprintf(fid," plot(t,real(x),'-','Color',[1 1 1]*0.5,'LineWidth',1,...\n");
70 fprintf(fid," t,imag(x),'-','Color',[0 0.2 0.5],'LineWidth',2);\n");
71 fprintf(fid," xlabel('time');\n");
72 fprintf(fid," ylabel('original input');\n");
73 fprintf(fid," legend('real','imag','location','southwest');\n");
74 fprintf(fid," axis([0 num_samples -3 3]);\n");
75 fprintf(fid," grid on;\n");
76 fprintf(fid,"subplot(3,1,2);\n");
77 fprintf(fid," plot(t,real(y),'-','Color',[1 1 1]*0.5,'LineWidth',1,...\n");
78 fprintf(fid," t,imag(y),'-','Color',[0 0.5 0.2],'LineWidth',2);\n");
79 fprintf(fid," xlabel('time');\n");
80 fprintf(fid," ylabel('input with DC offset');\n");
81 fprintf(fid," legend('real','imag','location','southwest');\n");
82 fprintf(fid," axis([0 num_samples -3 3]);\n");
83 fprintf(fid," grid on;\n");
84 fprintf(fid,"subplot(3,1,3);\n");
85 fprintf(fid," plot(t,real(z),'-','Color',[1 1 1]*0.5,'LineWidth',1,...\n");
86 fprintf(fid," t,imag(z),'-','Color',[0 0.2 0.5],'LineWidth',2);\n");
87 fprintf(fid," xlabel('time');\n");
88 fprintf(fid," ylabel('DC-blocked output');\n");
89 fprintf(fid," legend('real','imag','location','southwest');\n");
90 fprintf(fid," axis([0 num_samples -3 3]);\n");
91 fprintf(fid," grid on;\n");
92
93 // close output file
94 fclose(fid);
95 printf("results written to '%s'\n", OUTPUT_FILENAME);
96
97 printf("done.\n");
98 return 0;
99 }
100
101