1#!/usr/bin/env python 2# 3# Copyright 2009,2012,2013 Free Software Foundation, Inc. 4# 5# This file is part of GNU Radio 6# 7# GNU Radio is free software; you can redistribute it and/or modify 8# it under the terms of the GNU General Public License as published by 9# the Free Software Foundation; either version 3, or (at your option) 10# any later version. 11# 12# GNU Radio is distributed in the hope that it will be useful, 13# but WITHOUT ANY WARRANTY; without even the implied warranty of 14# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15# GNU General Public License for more details. 16# 17# You should have received a copy of the GNU General Public License 18# along with GNU Radio; see the file COPYING. If not, write to 19# the Free Software Foundation, Inc., 51 Franklin Street, 20# Boston, MA 02110-1301, USA. 21# 22 23from __future__ import print_function 24from __future__ import division 25from __future__ import unicode_literals 26from gnuradio import gr 27from gnuradio import blocks 28from gnuradio import filter 29from gnuradio import analog 30from gnuradio import channels 31import sys, math, time 32import numpy 33 34try: 35 import pylab 36except ImportError: 37 print("Error: Program requires matplotlib (see: matplotlib.sourceforge.net).") 38 sys.exit(1) 39 40 41class fmtx(gr.hier_block2): 42 def __init__(self, lo_freq, audio_rate, if_rate): 43 44 gr.hier_block2.__init__(self, "build_fm", 45 gr.io_signature(1, 1, gr.sizeof_float), 46 gr.io_signature(1, 1, gr.sizeof_gr_complex)) 47 48 fmtx = analog.nbfm_tx(audio_rate, if_rate, max_dev=5e3, 49 tau=75e-6, fh=0.925*if_rate/2.0) 50 51 # Local oscillator 52 lo = analog.sig_source_c(if_rate, # sample rate 53 analog.GR_SIN_WAVE, # waveform type 54 lo_freq, # frequency 55 1.0, # amplitude 56 0) # DC Offset 57 mixer = blocks.multiply_cc() 58 59 self.connect(self, fmtx, (mixer, 0)) 60 self.connect(lo, (mixer, 1)) 61 self.connect(mixer, self) 62 63class fmtest(gr.top_block): 64 def __init__(self): 65 gr.top_block.__init__(self) 66 67 self._nsamples = 1000000 68 self._audio_rate = 8000 69 70 # Set up N channels with their own baseband and IF frequencies 71 self._N = 5 72 chspacing = 16000 73 freq = [10, 20, 30, 40, 50] 74 f_lo = [0, 1*chspacing, -1*chspacing, 2*chspacing, -2*chspacing] 75 76 self._if_rate = 4*self._N*self._audio_rate 77 78 # Create a signal source and frequency modulate it 79 self.sum = blocks.add_cc() 80 for n in range(self._N): 81 sig = analog.sig_source_f(self._audio_rate, analog.GR_SIN_WAVE, freq[n], 0.5) 82 fm = fmtx(f_lo[n], self._audio_rate, self._if_rate) 83 self.connect(sig, fm) 84 self.connect(fm, (self.sum, n)) 85 86 self.head = blocks.head(gr.sizeof_gr_complex, self._nsamples) 87 self.snk_tx = blocks.vector_sink_c() 88 self.channel = channels.channel_model(0.1) 89 90 self.connect(self.sum, self.head, self.channel, self.snk_tx) 91 92 93 # Design the channlizer 94 self._M = 10 95 bw = chspacing / 2.0 96 t_bw = chspacing / 10.0 97 self._chan_rate = self._if_rate / self._M 98 self._taps = filter.firdes.low_pass_2(1, self._if_rate, bw, t_bw, 99 attenuation_dB=100, 100 window=filter.firdes.WIN_BLACKMAN_hARRIS) 101 tpc = math.ceil(float(len(self._taps)) / float(self._M)) 102 103 print("Number of taps: ", len(self._taps)) 104 print("Number of channels: ", self._M) 105 print("Taps per channel: ", tpc) 106 107 self.pfb = filter.pfb.channelizer_ccf(self._M, self._taps) 108 109 self.connect(self.channel, self.pfb) 110 111 # Create a file sink for each of M output channels of the filter and connect it 112 self.fmdet = list() 113 self.squelch = list() 114 self.snks = list() 115 for i in range(self._M): 116 self.fmdet.append(analog.nbfm_rx(self._audio_rate, self._chan_rate)) 117 self.squelch.append(analog.standard_squelch(self._audio_rate*10)) 118 self.snks.append(blocks.vector_sink_f()) 119 self.connect((self.pfb, i), self.fmdet[i], self.squelch[i], self.snks[i]) 120 121 def num_tx_channels(self): 122 return self._N 123 124 def num_rx_channels(self): 125 return self._M 126 127def main(): 128 129 fm = fmtest() 130 131 tstart = time.time() 132 fm.run() 133 tend = time.time() 134 135 if 1: 136 fig1 = pylab.figure(1, figsize=(12,10), facecolor="w") 137 fig2 = pylab.figure(2, figsize=(12,10), facecolor="w") 138 fig3 = pylab.figure(3, figsize=(12,10), facecolor="w") 139 140 Ns = 10000 141 Ne = 100000 142 143 fftlen = 8192 144 winfunc = numpy.blackman 145 146 # Plot transmitted signal 147 fs = fm._if_rate 148 149 d = fm.snk_tx.data()[Ns:Ns+Ne] 150 sp1_f = fig1.add_subplot(2, 1, 1) 151 152 X,freq = sp1_f.psd(d, NFFT=fftlen, noverlap=fftlen / 4, Fs=fs, 153 window = lambda d: d*winfunc(fftlen), 154 visible=False) 155 X_in = 10.0*numpy.log10(abs(numpy.fft.fftshift(X))) 156 f_in = numpy.arange(-fs / 2.0, fs / 2.0, fs / float(X_in.size)) 157 p1_f = sp1_f.plot(f_in, X_in, "b") 158 sp1_f.set_xlim([min(f_in), max(f_in)+1]) 159 sp1_f.set_ylim([-120.0, 20.0]) 160 161 sp1_f.set_title("Input Signal", weight="bold") 162 sp1_f.set_xlabel("Frequency (Hz)") 163 sp1_f.set_ylabel("Power (dBW)") 164 165 Ts = 1.0 / fs 166 Tmax = len(d)*Ts 167 168 t_in = numpy.arange(0, Tmax, Ts) 169 x_in = numpy.array(d) 170 sp1_t = fig1.add_subplot(2, 1, 2) 171 p1_t = sp1_t.plot(t_in, x_in.real, "b-o") 172 #p1_t = sp1_t.plot(t_in, x_in.imag, "r-o") 173 sp1_t.set_ylim([-5, 5]) 174 175 # Set up the number of rows and columns for plotting the subfigures 176 Ncols = int(numpy.floor(numpy.sqrt(fm.num_rx_channels()))) 177 Nrows = int(numpy.floor(fm.num_rx_channels() / Ncols)) 178 if(fm.num_rx_channels() % Ncols != 0): 179 Nrows += 1 180 181 # Plot each of the channels outputs. Frequencies on Figure 2 and 182 # time signals on Figure 3 183 fs_o = fm._audio_rate 184 for i in range(len(fm.snks)): 185 # remove issues with the transients at the beginning 186 # also remove some corruption at the end of the stream 187 # this is a bug, probably due to the corner cases 188 d = fm.snks[i].data()[Ns:Ne] 189 190 sp2_f = fig2.add_subplot(Nrows, Ncols, 1+i) 191 X,freq = sp2_f.psd(d, NFFT=fftlen, noverlap=fftlen / 4, Fs=fs_o, 192 window = lambda d: d*winfunc(fftlen), 193 visible=False) 194 #X_o = 10.0*numpy.log10(abs(numpy.fft.fftshift(X))) 195 X_o = 10.0*numpy.log10(abs(X)) 196 #f_o = numpy.arange(-fs_o/2.0, fs_o/2.0, fs_o/float(X_o.size)) 197 f_o = numpy.arange(0, fs_o / 2.0, fs_o/2.0/float(X_o.size)) 198 p2_f = sp2_f.plot(f_o, X_o, "b") 199 sp2_f.set_xlim([min(f_o), max(f_o)+0.1]) 200 sp2_f.set_ylim([-120.0, 20.0]) 201 sp2_f.grid(True) 202 203 sp2_f.set_title(("Channel %d" % i), weight="bold") 204 sp2_f.set_xlabel("Frequency (kHz)") 205 sp2_f.set_ylabel("Power (dBW)") 206 207 208 Ts = 1.0 / fs_o 209 Tmax = len(d)*Ts 210 t_o = numpy.arange(0, Tmax, Ts) 211 212 x_t = numpy.array(d) 213 sp2_t = fig3.add_subplot(Nrows, Ncols, 1+i) 214 p2_t = sp2_t.plot(t_o, x_t.real, "b") 215 p2_t = sp2_t.plot(t_o, x_t.imag, "r") 216 sp2_t.set_xlim([min(t_o), max(t_o)+1]) 217 sp2_t.set_ylim([-1, 1]) 218 219 sp2_t.set_xlabel("Time (s)") 220 sp2_t.set_ylabel("Amplitude") 221 222 223 pylab.show() 224 225 226if __name__ == "__main__": 227 main() 228