1#!/usr/bin/env python 2# 3# Copyright 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, filter 27from gnuradio import analog 28from gnuradio import blocks 29from gnuradio import eng_notation 30from gnuradio.eng_arg import eng_float, intx 31from argparse import ArgumentParser 32import sys 33import numpy 34 35try: 36 from matplotlib import pyplot 37except ImportError: 38 print("Error: could not from matplotlib import pyplot (http://matplotlib.sourceforge.net/)") 39 sys.exit(1) 40 41class example_fir_filter_ccc(gr.top_block): 42 def __init__(self, N, fs, bw, tw, atten, D): 43 gr.top_block.__init__(self) 44 45 self._nsamps = N 46 self._fs = fs 47 self._bw = bw 48 self._tw = tw 49 self._at = atten 50 self._decim = D 51 taps = filter.firdes.low_pass_2(1, self._fs, self._bw, self._tw, self._at) 52 print("Num. Taps: ", len(taps)) 53 54 self.src = analog.noise_source_c(analog.GR_GAUSSIAN, 1) 55 self.head = blocks.head(gr.sizeof_gr_complex, self._nsamps) 56 57 self.filt0 = filter.fir_filter_ccc(self._decim, taps) 58 59 self.vsnk_src = blocks.vector_sink_c() 60 self.vsnk_out = blocks.vector_sink_c() 61 62 self.connect(self.src, self.head, self.vsnk_src) 63 self.connect(self.head, self.filt0, self.vsnk_out) 64 65def main(): 66 parser = ArgumentParser(conflict_handler="resolve") 67 parser.add_argument("-N", "--nsamples", type=int, default=10000, 68 help="Number of samples to process [default=%(default)r]") 69 parser.add_argument("-s", "--samplerate", type=eng_float, default=8000, 70 help="System sample rate [default=%(default)r]") 71 parser.add_argument("-B", "--bandwidth", type=eng_float, default=1000, 72 help="Filter bandwidth [default=%(default)r]") 73 parser.add_argument("-T", "--transition", type=eng_float, default=100, 74 help="Transition band [default=%(default)r]") 75 parser.add_argument("-A", "--attenuation", type=eng_float, default=80, 76 help="Stopband attenuation [default=%(default)r]") 77 parser.add_argument("-D", "--decimation", type=int, default=1, 78 help="Decmation factor [default=%(default)r]") 79 args = parser.parse_args() 80 81 put = example_fir_filter_ccc(args.nsamples, 82 args.samplerate, 83 args.bandwidth, 84 args.transition, 85 args.attenuation, 86 args.decimation) 87 put.run() 88 89 data_src = numpy.array(put.vsnk_src.data()) 90 data_snk = numpy.array(put.vsnk_out.data()) 91 92 # Plot the signals PSDs 93 nfft = 1024 94 f1 = pyplot.figure(1, figsize=(12,10)) 95 s1 = f1.add_subplot(1,1,1) 96 s1.psd(data_src, NFFT=nfft, noverlap=nfft / 4, 97 Fs=args.samplerate) 98 s1.psd(data_snk, NFFT=nfft, noverlap=nfft / 4, 99 Fs=args.samplerate) 100 101 f2 = pyplot.figure(2, figsize=(12,10)) 102 s2 = f2.add_subplot(1,1,1) 103 s2.plot(data_src) 104 s2.plot(data_snk.real, 'g') 105 106 pyplot.show() 107 108if __name__ == "__main__": 109 try: 110 main() 111 except KeyboardInterrupt: 112 pass 113 114