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