Adding new example script for using the new PFB arbitrary resampler interface. One...
[debian/gnuradio] / gnuradio-examples / python / pfb / resampler.py
1 #!/usr/bin/env python
2
3 from gnuradio import gr, blks2
4 import scipy, pylab
5
6 class mytb(gr.top_block):
7     def __init__(self, fs_in, fs_out, fc, N=10000):
8         gr.top_block.__init__(self)
9         
10         rerate = float(fs_out) / float(fs_in)
11         print "Resampling from %f to %f by %f " %(fs_in, fs_out, rerate)
12
13         # Creating our own taps
14         taps = gr.firdes.low_pass_2(32, 32, 0.25, 0.1, 80)
15
16         self.src = gr.sig_source_c(fs_in, gr.GR_SIN_WAVE, fc, 1)
17         #self.src = gr.noise_source_c(gr.GR_GAUSSIAN, 1)
18         self.head = gr.head(gr.sizeof_gr_complex, N)
19
20         # A resampler with our taps
21         self.resamp_0 = blks2.pfb_arb_resampler_ccf(rerate, taps,
22                                                     flt_size=32)
23
24         # A resampler that just needs a resampling rate.
25         # Filter is created for us and designed to cover
26         # entire bandwidth of the input signal.
27         # An optional atten=XX rate can be used here to 
28         # specify the out-of-band rejection (default=80).
29         self.resamp_1 = blks2.pfb_arb_resampler_ccf(rerate)
30
31         self.snk_in = gr.vector_sink_c()
32         self.snk_0 = gr.vector_sink_c()
33         self.snk_1 = gr.vector_sink_c()
34
35         self.connect(self.src, self.head, self.snk_in)
36         self.connect(self.head, self.resamp_0, self.snk_0)
37         self.connect(self.head, self.resamp_1, self.snk_1)
38
39 def main():
40     fs_in = 8000
41     fs_out = 20000
42     fc = 1000
43     N = 10000
44
45     tb = mytb(fs_in, fs_out, fc, N)
46     tb.run()
47
48
49     # Plot PSD of signals
50     nfftsize = 2048
51     fig1 = pylab.figure(1, figsize=(10,10), facecolor="w")
52     sp1 = fig1.add_subplot(2,1,1)
53     sp1.psd(tb.snk_in.data(), NFFT=nfftsize,
54             noverlap=nfftsize/4, Fs = fs_in)
55     sp1.set_title(("Input Signal at f_s=%.2f kHz" % (fs_in/1000.0)))
56     sp1.set_xlim([-fs_in/2, fs_in/2])
57
58     sp2 = fig1.add_subplot(2,1,2)
59     sp2.psd(tb.snk_0.data(), NFFT=nfftsize,
60             noverlap=nfftsize/4, Fs = fs_out,
61             label="With our filter")
62     sp2.psd(tb.snk_1.data(), NFFT=nfftsize,
63             noverlap=nfftsize/4, Fs = fs_out,
64             label="With auto-generated filter")
65     sp2.set_title(("Output Signals at f_s=%.2f kHz" % (fs_out/1000.0)))
66     sp2.set_xlim([-fs_out/2, fs_out/2])
67     sp2.legend()
68
69     # Plot signals in time
70     Ts_in = 1.0/fs_in
71     Ts_out = 1.0/fs_out
72     t_in = scipy.arange(0, len(tb.snk_in.data())*Ts_in, Ts_in)
73     t_out = scipy.arange(0, len(tb.snk_0.data())*Ts_out, Ts_out)
74
75     fig2 = pylab.figure(2, figsize=(10,10), facecolor="w")
76     sp21 = fig2.add_subplot(2,1,1)
77     sp21.plot(t_in, tb.snk_in.data())
78     sp21.set_title(("Input Signal at f_s=%.2f kHz" % (fs_in/1000.0)))
79     sp21.set_xlim([t_in[100], t_in[200]])
80
81     sp22 = fig2.add_subplot(2,1,2)
82     sp22.plot(t_out, tb.snk_0.data(),
83               label="With our filter")
84     sp22.plot(t_out, tb.snk_1.data(),
85               label="With auto-generated filter")
86     sp22.set_title(("Output Signals at f_s=%.2f kHz" % (fs_out/1000.0)))
87     r = float(fs_out)/float(fs_in)
88     sp22.set_xlim([t_out[r * 100], t_out[r * 200]])
89     sp22.legend()
90
91     pylab.show()
92
93 if __name__ == "__main__":
94     main()
95