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
12 class fmtx(gr.hier_block2):
13 def __init__(self, lo_freq, audio_rate, if_rate):
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
19 fmtx = blks2.nbfm_tx (audio_rate, if_rate, max_dev=5e3, tau=75e-6)
22 lo = gr.sig_source_c (if_rate, # sample rate
23 gr.GR_SIN_WAVE, # waveform type
27 mixer = gr.multiply_cc ()
29 self.connect (self, fmtx, (mixer, 0))
30 self.connect (lo, (mixer, 1))
31 self.connect (mixer, self)
33 class fmtest(gr.top_block):
35 gr.top_block.__init__(self)
37 self._nsamples = 1000000
38 self._audio_rate = 8000
40 # Set up N channels with their own baseband and IF frequencies
43 freq = [10, 20, 30, 40, 50]
44 f_lo = [0, 1*chspacing, -1*chspacing, 2*chspacing, -2*chspacing]
46 self._if_rate = 4*self._N*self._audio_rate
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)
54 self.connect(fm, (self.sum, n))
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)
60 self.connect(self.sum, self.head, self.channel, self.snk_tx)
63 # Design the channlizer
67 self._chan_rate = self._if_rate / self._M
68 self._taps = gr.firdes.low_pass_2(1, self._if_rate, bw, t_bw,
70 window=gr.firdes.WIN_BLACKMAN_hARRIS)
71 tpc = math.ceil(float(len(self._taps)) / float(self._M))
73 print "Number of taps: ", len(self._taps)
74 print "Number of channels: ", self._M
75 print "Taps per channel: ", tpc
77 self.pfb = blks2.pfb_channelizer_ccf(self._M, self._taps)
79 self.connect(self.channel, self.pfb)
81 # Create a file sink for each of M output channels of the filter and connect it
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])
91 def num_tx_channels(self):
94 def num_rx_channels(self):
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")
114 winfunc = scipy.blackman
116 # Plot transmitted signal
119 d = fm.snk_tx.data()[Ns:Ns+Ne]
120 sp1_f = fig1.add_subplot(2, 1, 1)
122 X,freq = sp1_f.psd(d, NFFT=fftlen, noverlap=fftlen/4, Fs=fs,
123 window = lambda d: d*winfunc(fftlen),
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])
131 sp1_f.set_title("Input Signal", weight="bold")
132 sp1_f.set_xlabel("Frequency (Hz)")
133 sp1_f.set_ylabel("Power (dBW)")
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])
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):
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]
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),
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])
173 sp2_f.set_title(("Channel %d" % i), weight="bold")
174 sp2_f.set_xlabel("Frequency (kHz)")
175 sp2_f.set_ylabel("Power (dBW)")
180 t_o = scipy.arange(0, Tmax, Ts)
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])
189 sp2_t.set_xlabel("Time (s)")
190 sp2_t.set_ylabel("Amplitude")
196 if __name__ == "__main__":