1 //
2 // resamp2_cccf_example.c
3 //
4 // This example demonstrates the halfband resampler cenetered at the
5 // quarter sample rate to split the signal into positive and negative
6 // frequency bands. Two distinct narrow-band signals are generated; one
7 // at a positive frequency and one at a negative frequency. The resamp2
8 // object is run as a filter to separate the two about the zero-
9 // frequency center point.
10 //
11
12 #include <stdio.h>
13 #include <complex.h>
14 #include <math.h>
15
16 #include "liquid.h"
17
18 #define OUTPUT_FILENAME "resamp2_cccf_example.m"
19
main()20 int main() {
21 unsigned int m = 12; // filter semi-length (actual length: 4*m+1)
22 float As = 60.0f; // stop-band attenuation [dB]
23 unsigned int num_samples = 400; // number of input samples
24
25 unsigned int i;
26
27 // allocate memory for data arrays
28 float complex x [num_samples]; // input signal
29 float complex y0[num_samples]; //
30 float complex y1[num_samples]; //
31
32 // generate the two signals
33 iirfilt_crcf lowpass = iirfilt_crcf_create_lowpass(6,0.02);
34 for (i=0; i<num_samples; i++) {
35 // signal at negative frequency: tone
36 float complex x_neg = cexpf(-_Complex_I*2*M_PI*0.059f*i);
37
38 // signal at positive frequency: filtered noise
39 float complex v;
40 iirfilt_crcf_execute(lowpass, 4*randnf(), &v);
41 float complex x_pos = v * cexpf(_Complex_I*2*M_PI*0.073f*i);
42
43 // compsite
44 x[i] = (x_neg + x_pos) * hamming(i,num_samples);
45 }
46 iirfilt_crcf_destroy(lowpass);
47
48 // create/print the half-band resampler, with a specified
49 // stopband attenuation level
50 resamp2_cccf q = resamp2_cccf_create(m,-0.25f,As);
51 resamp2_cccf_print(q);
52
53 // run filter
54 for (i=0; i<num_samples; i++)
55 resamp2_cccf_filter_execute(q,x[i],&y0[i],&y1[i]);
56
57 //
58 // print results to file
59 //
60 FILE*fid = fopen(OUTPUT_FILENAME,"w");
61 fprintf(fid,"%% %s: auto-generated file\n",OUTPUT_FILENAME);
62 fprintf(fid,"clear all;\n");
63 fprintf(fid,"close all;\n\n");
64 fprintf(fid,"num_samples=%u;\n", num_samples);
65
66 // output results
67 for (i=0; i<num_samples; i++) {
68 fprintf(fid,"x( %3u) = %12.4e + j*%12.4e;\n", i+1, crealf( x[i]), cimagf( x[i]));
69 fprintf(fid,"y0(%3u) = %12.4e + j*%12.4e;\n", i+1, crealf(y0[i]), cimagf(y0[i]));
70 fprintf(fid,"y1(%3u) = %12.4e + j*%12.4e;\n", i+1, crealf(y1[i]), cimagf(y1[i]));
71 }
72
73 // plot temporal results
74 fprintf(fid,"\n\n");
75 fprintf(fid,"t = 0:(num_samples-1);\n");
76 fprintf(fid,"figure;\n");
77 fprintf(fid,"subplot(3,1,1);\n");
78 fprintf(fid," hold on;\n");
79 fprintf(fid," plot(t,real(x),'Color',[1 1 1]*0.7,'LineWidth',1);\n");
80 fprintf(fid," plot(t,imag(x),'Color',[1 1 1]*0.5,'LineWidth',2);\n");
81 fprintf(fid," hold off;\n");
82 fprintf(fid," grid on;\n");
83 fprintf(fid," legend('real','imag','location','northeast');\n");
84 fprintf(fid," axis([0 num_samples -2 2]);\n");
85 fprintf(fid," ylabel('original');\n");
86 fprintf(fid,"subplot(3,1,2);\n");
87 fprintf(fid," hold on;\n");
88 fprintf(fid," plot(t,real(y0),'Color',[1 1 1]*0.7,'LineWidth',1);\n");
89 fprintf(fid," plot(t,imag(y0),'Color',[0 0.5 0.2],'LineWidth',2);\n");
90 fprintf(fid," hold off;\n");
91 fprintf(fid," grid on;\n");
92 fprintf(fid," legend('real','imag','location','northeast');\n");
93 fprintf(fid," axis([0 num_samples -2 2]);\n");
94 fprintf(fid," ylabel('negative band');\n");
95 fprintf(fid,"subplot(3,1,3);\n");
96 fprintf(fid," hold on;\n");
97 fprintf(fid," plot(t,real(y1),'Color',[1 1 1]*0.7,'LineWidth',1);\n");
98 fprintf(fid," plot(t,imag(y1),'Color',[0 0.2 0.5],'LineWidth',2);\n");
99 fprintf(fid," hold off;\n");
100 fprintf(fid," grid on;\n");
101 fprintf(fid," legend('real','imag','location','northeast');\n");
102 fprintf(fid," axis([0 num_samples -2 2]);\n");
103 fprintf(fid," ylabel('positive band');\n");
104 fprintf(fid," xlabel('sample index');\n");
105
106 // plot spectrum results
107 fprintf(fid,"\n\n");
108 fprintf(fid,"nfft=2400;\n");
109 fprintf(fid,"f = [0:(nfft-1)]/nfft - 0.5;\n");
110 fprintf(fid,"w = hamming(num_samples)' / num_samples;\n");
111 fprintf(fid,"X = 20*log10(abs(fftshift(fft( x.*w, nfft))));\n");
112 fprintf(fid,"Y0 = 20*log10(abs(fftshift(fft(y0.*w, nfft))));\n");
113 fprintf(fid,"Y1 = 20*log10(abs(fftshift(fft(y1.*w, nfft))));\n");
114 fprintf(fid,"figure('Color','white');\n");
115 fprintf(fid," hold on;\n");
116 fprintf(fid," plot(f,X, 'Color',[1 1 1]*0.7,'LineWidth',2);\n");
117 fprintf(fid," plot(f,Y0,'Color',[0 0.2 0.5]);\n");
118 fprintf(fid," plot(f,Y1,'Color',[0 0.5 0.2]);\n");
119 fprintf(fid," hold off;\n");
120 fprintf(fid,"legend('original','negative','positive','location','northeast');\n");
121 fprintf(fid,"grid on;\n");
122 fprintf(fid,"axis([-0.5 0.5 -120 20]);\n");
123 fprintf(fid,"xlabel('Normalized Frequency [f/F_s]');\n");
124 fprintf(fid,"ylabel('Power Spectral Density [dB]');\n");
125
126 fclose(fid);
127 printf("results written to %s\n",OUTPUT_FILENAME);
128
129 printf("done.\n");
130 return 0;
131 }
132