97df0e0f56202a2ea1c09949936252af0d0893e2
[debian/gnuradio] / gnuradio-examples / python / pfb / fmtest.py
1 #!/usr/bin/env python
2 #
3
4
5 from gnuradio import gr, eng_notation
6 from gnuradio import blks2
7 from gnuradio.eng_option import eng_option
8 from optparse import OptionParser
9 import math, time, sys, scipy, pylab
10 from scipy import fftpack
11
12 class fmtx(gr.hier_block2):
13     def __init__(self, lo_freq, audio_rate, if_rate):
14
15         gr.hier_block2.__init__(self, "build_fm",
16                                 gr.io_signature(1, 1, gr.sizeof_float),      # Input signature
17                                 gr.io_signature(1, 1, gr.sizeof_gr_complex)) # Output signature
18
19         fmtx = blks2.nbfm_tx (audio_rate, if_rate, max_dev=5e3, tau=75e-6)
20         
21         # Local oscillator
22         lo = gr.sig_source_c (if_rate,        # sample rate
23                               gr.GR_SIN_WAVE, # waveform type
24                               lo_freq,        #frequency
25                               1.0,            # amplitude
26                               0)              # DC Offset
27         mixer = gr.multiply_cc ()
28     
29         self.connect (self, fmtx, (mixer, 0))
30         self.connect (lo, (mixer, 1))
31         self.connect (mixer, self)
32
33 class fmtest(gr.top_block):
34     def __init__(self):
35         gr.top_block.__init__(self)
36
37         self._nsamples = 1000000
38         self._audio_rate = 8000
39
40         # Set up N channels with their own baseband and IF frequencies
41         self._N = 5
42         chspacing = 16000
43         freq = [10, 20, 30, 40, 50]
44         f_lo = [0, 1*chspacing, -1*chspacing, 2*chspacing, -2*chspacing]
45
46         self._if_rate = 4*self._N*self._audio_rate
47
48         # Create a signal source and frequency modulate it
49         self.sum = gr.add_cc ()
50         for n in xrange(self._N):
51             sig = gr.sig_source_f(self._audio_rate, gr.GR_SIN_WAVE, freq[n], 0.5)
52             fm = fmtx(f_lo[n], self._audio_rate, self._if_rate)
53             self.connect(sig, fm)
54             self.connect(fm, (self.sum, n))
55
56         self.head = gr.head(gr.sizeof_gr_complex, self._nsamples)
57         self.snk_tx = gr.vector_sink_c()
58         self.channel = blks2.channel_model(0.1)
59
60         self.connect(self.sum, self.head, self.channel, self.snk_tx)
61
62
63         # Design the channlizer
64         self._M = 10
65         bw = chspacing/2.0
66         t_bw = chspacing/10.0
67         self._chan_rate = self._if_rate / self._M
68         self._taps = gr.firdes.low_pass_2(1, self._if_rate, bw, t_bw, 
69                                           attenuation_dB=100,
70                                           window=gr.firdes.WIN_BLACKMAN_hARRIS)
71         tpc = math.ceil(float(len(self._taps)) /  float(self._M))
72
73         print "Number of taps:     ", len(self._taps)
74         print "Number of channels: ", self._M
75         print "Taps per channel:   ", tpc
76         
77         self.pfb = blks2.pfb_channelizer_ccf(self._M, self._taps)
78         
79         self.connect(self.channel, self.pfb)
80         
81         # Create a file sink for each of M output channels of the filter and connect it
82         self.fmdet = list()
83         self.squelch = list()
84         self.snks = list()
85         for i in xrange(self._M):
86             self.fmdet.append(blks2.nbfm_rx(self._audio_rate, self._chan_rate))
87             self.squelch.append(blks2.standard_squelch(self._audio_rate*10))
88             self.snks.append(gr.vector_sink_f())
89             self.connect((self.pfb, i), self.fmdet[i], self.squelch[i], self.snks[i])
90
91     def num_tx_channels(self):
92         return self._N
93
94     def num_rx_channels(self):
95         return self._M
96
97 def main():
98
99     fm = fmtest()
100
101     tstart = time.time()
102     fm.run()
103     tend = time.time()
104
105     if 1:
106         fig1 = pylab.figure(1, figsize=(12,10), facecolor="w")
107         fig2 = pylab.figure(2, figsize=(12,10), facecolor="w")
108         fig3 = pylab.figure(3, figsize=(12,10), facecolor="w")
109
110         Ns = 10000
111         Ne = 100000
112
113         fftlen = 8192
114         winfunc = scipy.blackman
115
116         # Plot transmitted signal
117         fs = fm._if_rate
118
119         d = fm.snk_tx.data()[Ns:Ns+Ne]
120         sp1_f = fig1.add_subplot(2, 1, 1)
121
122         X,freq = sp1_f.psd(d, NFFT=fftlen, noverlap=fftlen/4, Fs=fs,
123                            window = lambda d: d*winfunc(fftlen),
124                            visible=False)
125         X_in = 10.0*scipy.log10(abs(fftpack.fftshift(X)))
126         f_in = scipy.arange(-fs/2.0, fs/2.0, fs/float(X_in.size))
127         p1_f = sp1_f.plot(f_in, X_in, "b")
128         sp1_f.set_xlim([min(f_in), max(f_in)+1]) 
129         sp1_f.set_ylim([-120.0, 20.0]) 
130
131         sp1_f.set_title("Input Signal", weight="bold")
132         sp1_f.set_xlabel("Frequency (Hz)")
133         sp1_f.set_ylabel("Power (dBW)")
134
135         Ts = 1.0/fs
136         Tmax = len(d)*Ts
137         
138         t_in = scipy.arange(0, Tmax, Ts)
139         x_in = scipy.array(d)
140         sp1_t = fig1.add_subplot(2, 1, 2)
141         p1_t = sp1_t.plot(t_in, x_in.real, "b-o")
142         #p1_t = sp1_t.plot(t_in, x_in.imag, "r-o")
143         sp1_t.set_ylim([-5, 5])
144
145         # Set up the number of rows and columns for plotting the subfigures
146         Ncols = int(scipy.floor(scipy.sqrt(fm.num_rx_channels())))
147         Nrows = int(scipy.floor(fm.num_rx_channels() / Ncols))
148         if(fm.num_rx_channels() % Ncols != 0):
149             Nrows += 1
150
151         # Plot each of the channels outputs. Frequencies on Figure 2 and
152         # time signals on Figure 3
153         fs_o = fm._audio_rate
154         for i in xrange(len(fm.snks)):
155             # remove issues with the transients at the beginning
156             # also remove some corruption at the end of the stream
157             #    this is a bug, probably due to the corner cases
158             d = fm.snks[i].data()[Ns:Ne]
159
160             sp2_f = fig2.add_subplot(Nrows, Ncols, 1+i)
161             X,freq = sp2_f.psd(d, NFFT=fftlen, noverlap=fftlen/4, Fs=fs_o,
162                                window = lambda d: d*winfunc(fftlen),
163                                visible=False)
164             #X_o = 10.0*scipy.log10(abs(fftpack.fftshift(X)))
165             X_o = 10.0*scipy.log10(abs(X))
166             #f_o = scipy.arange(-fs_o/2.0, fs_o/2.0, fs_o/float(X_o.size))
167             f_o = scipy.arange(0, fs_o/2.0, fs_o/2.0/float(X_o.size))
168             p2_f = sp2_f.plot(f_o, X_o, "b")
169             sp2_f.set_xlim([min(f_o), max(f_o)+0.1]) 
170             sp2_f.set_ylim([-120.0, 20.0]) 
171             sp2_f.grid(True)
172
173             sp2_f.set_title(("Channel %d" % i), weight="bold")
174             sp2_f.set_xlabel("Frequency (kHz)")
175             sp2_f.set_ylabel("Power (dBW)")
176
177
178             Ts = 1.0/fs_o
179             Tmax = len(d)*Ts
180             t_o = scipy.arange(0, Tmax, Ts)
181
182             x_t = scipy.array(d)
183             sp2_t = fig3.add_subplot(Nrows, Ncols, 1+i)
184             p2_t = sp2_t.plot(t_o, x_t.real, "b")
185             p2_t = sp2_t.plot(t_o, x_t.imag, "r")
186             sp2_t.set_xlim([min(t_o), max(t_o)+1]) 
187             sp2_t.set_ylim([-1, 1]) 
188
189             sp2_t.set_xlabel("Time (s)")
190             sp2_t.set_ylabel("Amplitude")
191
192
193         pylab.show()
194
195
196 if __name__ == "__main__":
197     main()