Added Band Notch filter
[debian/gnuradio] / gr-utils / src / python / usrp_siggen.py
index af6eee16792534912a28bf32a884997130b70f14..8ae2fbfbfd93ade4518cff28311ab3ba946108d6 100755 (executable)
@@ -1,4 +1,24 @@
 #!/usr/bin/env python
+#
+# Copyright 2004,2005,2007,2008 Free Software Foundation, Inc.
+# 
+# This file is part of GNU Radio
+# 
+# GNU Radio is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 3, or (at your option)
+# any later version.
+# 
+# GNU Radio is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+# 
+# You should have received a copy of the GNU General Public License
+# along with GNU Radio; see the file COPYING.  If not, write to
+# the Free Software Foundation, Inc., 51 Franklin Street,
+# Boston, MA 02110-1301, USA.
+# 
 
 from gnuradio import gr, gru
 from gnuradio import usrp
@@ -9,7 +29,7 @@ import sys
 
 
 class my_top_block(gr.top_block):
-    def __init__ (self):
+    def __init__ (self, nsamples):
         gr.top_block.__init__(self)
         
         # controllable values
@@ -18,6 +38,7 @@ class my_top_block(gr.top_block):
         self.waveform_ampl = 16000
         self.waveform_freq = 100.12345e3
         self.waveform_offset = 0
+        self.nsamples = nsamples
         self._instantiate_blocks ()
         self.set_waveform_type (self.waveform_type)
 
@@ -66,26 +87,36 @@ class my_top_block(gr.top_block):
         self.noisegen = gr.noise_source_c (gr.GR_UNIFORM,
                                            self.waveform_ampl)
 
+        self.head = None
+        if self.nsamples > 0:
+            self.head = gr.head(gr.sizeof_gr_complex, int(self.nsamples))
+
         # self.file_sink = gr.file_sink (gr.sizeof_gr_complex, "siggen.dat")
 
     def _configure_graph (self, type):
-        was_running = self.is_running ()
-        if was_running:
-            self.stop ()
-        self.disconnect_all ()
-        if type == gr.GR_SIN_WAVE or type == gr.GR_CONST_WAVE:
-            self.connect (self.siggen, self.u)
-            # self.connect (self.siggen, self.file_sink)
-            self.siggen.set_waveform (type)
-            self.src = self.siggen
-        elif type == gr.GR_UNIFORM or type == gr.GR_GAUSSIAN:
-            self.connect (self.noisegen, self.u)
-            self.noisegen.set_type (type)
-            self.src = self.noisegen
-        else:
-            raise ValueError, type
-        if was_running:
-            self.start ()
+        try:
+            self.lock()
+            self.disconnect_all ()
+
+            if self.head:
+                self.connect(self.head, self.u)
+                tail = self.head
+            else:
+                tail = self.u
+                
+            if type == gr.GR_SIN_WAVE or type == gr.GR_CONST_WAVE:
+                self.connect (self.siggen, tail)
+                # self.connect (self.siggen, self.file_sink)
+                self.siggen.set_waveform (type)
+                self.src = self.siggen
+            elif type == gr.GR_UNIFORM or type == gr.GR_GAUSSIAN:
+                self.connect (self.noisegen, tail)
+                self.noisegen.set_type (type)
+                self.src = self.noisegen
+            else:
+                raise ValueError, type
+        finally:
+            self.unlock()
 
     def set_freq(self, target_freq):
         """
@@ -99,7 +130,7 @@ class my_top_block(gr.top_block):
         the result of that operation and our target_frequency to
         determine the value for the digital up converter.
         """
-        r = self.u.tune(self.subdev._which, self.subdev, target_freq)
+        r = self.u.tune(self.subdev.which(), self.subdev, target_freq)
         if r:
             #print "r.baseband_freq =", eng_notation.num_to_str(r.baseband_freq)
             #print "r.dxc_freq      =", eng_notation.num_to_str(r.dxc_freq)
@@ -129,7 +160,7 @@ def main ():
     parser.add_option ("--uniform", dest="type", action="store_const", const=gr.GR_UNIFORM,
                        help="generate Uniform random output")
 
-    parser.add_option ("-w", "--waveform-freq", type="eng_float", default=100e3,
+    parser.add_option ("-w", "--waveform-freq", type="eng_float", default=0,
                        help="set waveform frequency to FREQ [default=%default]")
     parser.add_option ("-a", "--amplitude", type="eng_float", default=16e3,
                        help="set waveform amplitude to AMPLITUDE [default=%default]", metavar="AMPL")
@@ -137,6 +168,8 @@ def main ():
                        help="set output gain to GAIN [default=%default]")
     parser.add_option ("-o", "--offset", type="eng_float", default=0,
                        help="set waveform offset to OFFSET [default=%default]")
+    parser.add_option ("-N", "--nsamples", type="eng_float", default=0,
+                       help="set number of samples to transmit [default=+inf]")
     (options, args) = parser.parse_args ()
 
     if len(args) != 0:
@@ -148,7 +181,7 @@ def main ():
         parser.print_help()
         raise SystemExit
 
-    tb = my_top_block()
+    tb = my_top_block(options.nsamples)
     tb.set_interpolator (options.interp)
     tb.set_waveform_type (options.type)
     tb.set_waveform_freq (options.waveform_freq)
@@ -166,7 +199,7 @@ def main ():
     print "Using TX d'board %s" % (tb.subdev.side_and_name(),)
     
     if options.gain is None:
-        tb.subdev.set_gain(fg.subdev.gain_range()[1])    # set max Tx gain
+        tb.subdev.set_gain(tb.subdev.gain_range()[1])    # set max Tx gain
     else:
         tb.subdev.set_gain(options.gain)    # set max Tx gain
 
@@ -181,5 +214,6 @@ def main ():
     except KeyboardInterrupt:
         pass
 
+
 if __name__ == '__main__':
     main ()