~ubuntu-branches/ubuntu/trusty/gnuradio/trusty

« back to all changes in this revision

Viewing changes to gnuradio-examples/python/usrp/usrp_siggen.py

  • Committer: Bazaar Package Importer
  • Author(s): Kamal Mostafa
  • Date: 2010-03-13 07:46:01 UTC
  • mfrom: (2.1.2 sid)
  • Revision ID: james.westby@ubuntu.com-20100313074601-zjsa893a87bozyh7
Tags: 3.2.2.dfsg-1ubuntu1
* Fix build for Ubuntu lucid (LP: #260406)
  - add binary package dep for libusrp0, libusrp2-0: adduser
  - debian/rules clean: remove pre-built Qt moc files

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#!/usr/bin/env python
2
 
 
3
 
from gnuradio import gr, gru
4
 
from gnuradio import usrp
5
 
from gnuradio.eng_option import eng_option
6
 
from gnuradio import eng_notation
7
 
from optparse import OptionParser
8
 
import sys
9
 
 
10
 
 
11
 
class my_graph(gr.flow_graph):
12
 
    def __init__ (self):
13
 
        gr.flow_graph.__init__(self)
14
 
        
15
 
        # controllable values
16
 
        self.interp = 64
17
 
        self.waveform_type = gr.GR_SIN_WAVE
18
 
        self.waveform_ampl = 16000
19
 
        self.waveform_freq = 100.12345e3
20
 
        self.waveform_offset = 0
21
 
        self._instantiate_blocks ()
22
 
        self.set_waveform_type (self.waveform_type)
23
 
 
24
 
    def usb_freq (self):
25
 
        return self.u.dac_freq() / self.interp
26
 
 
27
 
    def usb_throughput (self):
28
 
        return self.usb_freq () * 4
29
 
        
30
 
    def set_waveform_type (self, type):
31
 
        '''
32
 
        valid waveform types are: gr.GR_SIN_WAVE, gr.GR_CONST_WAVE,
33
 
        gr.GR_UNIFORM and gr.GR_GAUSSIAN
34
 
        '''
35
 
        self._configure_graph (type)
36
 
        self.waveform_type = type
37
 
 
38
 
    def set_waveform_ampl (self, ampl):
39
 
        self.waveform_ampl = ampl
40
 
        self.siggen.set_amplitude (ampl)
41
 
        self.noisegen.set_amplitude (ampl)
42
 
 
43
 
    def set_waveform_freq (self, freq):
44
 
        self.waveform_freq = freq
45
 
        self.siggen.set_frequency (freq)
46
 
        
47
 
    def set_waveform_offset (self, offset):
48
 
        self.waveform_offset = offset
49
 
        self.siggen.set_offset (offset)
50
 
 
51
 
    def set_interpolator (self, interp):
52
 
        self.interp = interp
53
 
        self.siggen.set_sampling_freq (self.usb_freq ())
54
 
        self.u.set_interp_rate (interp)
55
 
 
56
 
    def _instantiate_blocks (self):
57
 
        self.src = None
58
 
        self.u = usrp.sink_c (0, self.interp)
59
 
        
60
 
        self.siggen = gr.sig_source_c (self.usb_freq (),
61
 
                                       gr.GR_SIN_WAVE,
62
 
                                       self.waveform_freq,
63
 
                                       self.waveform_ampl,
64
 
                                       self.waveform_offset)
65
 
 
66
 
        self.noisegen = gr.noise_source_c (gr.GR_UNIFORM,
67
 
                                           self.waveform_ampl)
68
 
 
69
 
        # self.file_sink = gr.file_sink (gr.sizeof_gr_complex, "siggen.dat")
70
 
 
71
 
    def _configure_graph (self, type):
72
 
        was_running = self.is_running ()
73
 
        if was_running:
74
 
            self.stop ()
75
 
        self.disconnect_all ()
76
 
        if type == gr.GR_SIN_WAVE or type == gr.GR_CONST_WAVE:
77
 
            self.connect (self.siggen, self.u)
78
 
            # self.connect (self.siggen, self.file_sink)
79
 
            self.siggen.set_waveform (type)
80
 
            self.src = self.siggen
81
 
        elif type == gr.GR_UNIFORM or type == gr.GR_GAUSSIAN:
82
 
            self.connect (self.noisegen, self.u)
83
 
            self.noisegen.set_type (type)
84
 
            self.src = self.noisegen
85
 
        else:
86
 
            raise ValueError, type
87
 
        if was_running:
88
 
            self.start ()
89
 
 
90
 
    def set_freq(self, target_freq):
91
 
        """
92
 
        Set the center frequency we're interested in.
93
 
 
94
 
        @param target_freq: frequency in Hz
95
 
        @rypte: bool
96
 
 
97
 
        Tuning is a two step process.  First we ask the front-end to
98
 
        tune as close to the desired frequency as it can.  Then we use
99
 
        the result of that operation and our target_frequency to
100
 
        determine the value for the digital up converter.
101
 
        """
102
 
        r = self.u.tune(self.subdev._which, self.subdev, target_freq)
103
 
        if r:
104
 
            #print "r.baseband_freq =", eng_notation.num_to_str(r.baseband_freq)
105
 
            #print "r.dxc_freq      =", eng_notation.num_to_str(r.dxc_freq)
106
 
            #print "r.residual_freq =", eng_notation.num_to_str(r.residual_freq)
107
 
            #print "r.inverted      =", r.inverted
108
 
            return True
109
 
 
110
 
        return False
111
 
 
112
 
 
113
 
 
114
 
def main ():
115
 
    parser = OptionParser (option_class=eng_option)
116
 
    parser.add_option ("-T", "--tx-subdev-spec", type="subdev", default=(0, 0),
117
 
                       help="select USRP Tx side A or B")
118
 
    parser.add_option ("-f", "--rf-freq", type="eng_float", default=None,
119
 
                       help="set RF center frequency to FREQ")
120
 
    parser.add_option ("-i", "--interp", type="int", default=64,
121
 
                       help="set fgpa interpolation rate to INTERP [default=%default]")
122
 
 
123
 
    parser.add_option ("--sine", dest="type", action="store_const", const=gr.GR_SIN_WAVE,
124
 
                       help="generate a complex sinusoid [default]", default=gr.GR_SIN_WAVE)
125
 
    parser.add_option ("--const", dest="type", action="store_const", const=gr.GR_CONST_WAVE, 
126
 
                       help="generate a constant output")
127
 
    parser.add_option ("--gaussian", dest="type", action="store_const", const=gr.GR_GAUSSIAN,
128
 
                       help="generate Gaussian random output")
129
 
    parser.add_option ("--uniform", dest="type", action="store_const", const=gr.GR_UNIFORM,
130
 
                       help="generate Uniform random output")
131
 
 
132
 
    parser.add_option ("-w", "--waveform-freq", type="eng_float", default=100e3,
133
 
                       help="set waveform frequency to FREQ [default=%default]")
134
 
    parser.add_option ("-a", "--amplitude", type="eng_float", default=16e3,
135
 
                       help="set waveform amplitude to AMPLITUDE [default=%default]", metavar="AMPL")
136
 
    parser.add_option ("-o", "--offset", type="eng_float", default=0,
137
 
                       help="set waveform offset to OFFSET [default=%default]")
138
 
    (options, args) = parser.parse_args ()
139
 
 
140
 
    if len(args) != 0:
141
 
        parser.print_help()
142
 
        raise SystemExit
143
 
 
144
 
    if options.rf_freq is None:
145
 
        sys.stderr.write("usrp_siggen: must specify RF center frequency with -f RF_FREQ\n")
146
 
        parser.print_help()
147
 
        raise SystemExit
148
 
 
149
 
    fg = my_graph()
150
 
    fg.set_interpolator (options.interp)
151
 
    fg.set_waveform_type (options.type)
152
 
    fg.set_waveform_freq (options.waveform_freq)
153
 
    fg.set_waveform_ampl (options.amplitude)
154
 
    fg.set_waveform_offset (options.offset)
155
 
 
156
 
    # determine the daughterboard subdevice we're using
157
 
    if options.tx_subdev_spec is None:
158
 
        options.tx_subdev_spec = usrp.pick_tx_subdevice(fg.u)
159
 
 
160
 
    m = usrp.determine_tx_mux_value(fg.u, options.tx_subdev_spec)
161
 
    #print "mux = %#04x" % (m,)
162
 
    fg.u.set_mux(m)
163
 
    fg.subdev = usrp.selected_subdev(fg.u, options.tx_subdev_spec)
164
 
    print "Using TX d'board %s" % (fg.subdev.side_and_name(),)
165
 
    
166
 
    fg.subdev.set_gain(fg.subdev.gain_range()[1])    # set max Tx gain
167
 
 
168
 
    if not fg.set_freq(options.rf_freq):
169
 
        sys.stderr.write('Failed to set RF frequency\n')
170
 
        raise SystemExit
171
 
    
172
 
    fg.subdev.set_enable(True)                       # enable transmitter
173
 
 
174
 
    try:
175
 
        fg.run()
176
 
    except KeyboardInterrupt:
177
 
        pass
178
 
 
179
 
if __name__ == '__main__':
180
 
    main ()