summaryrefslogtreecommitdiff
path: root/gr-utils/src/python/usrp2_siggen.py
blob: 9ade933c7c3c33a6ecf4811858a3cad1f393eba0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
#!/usr/bin/env python
#
# Copyright 2008,2009 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, eng_notation, usrp2
from gnuradio.eng_option import eng_option
from optparse import OptionParser
import sys
import math

n2s = eng_notation.num_to_str

waveforms = { gr.GR_SIN_WAVE   : "Complex Sinusoid",
              gr.GR_CONST_WAVE : "Constant",
              gr.GR_GAUSSIAN   : "Gaussian Noise",
              gr.GR_UNIFORM    : "Uniform Noise",
              "2tone"          : "Two Tone",
              "sweep"          : "Sweep" }

#
# GUI-unaware GNU Radio flowgraph.  This may be used either with command
# line applications or GUI applications.
#
class top_block(gr.top_block):
    def __init__(self, options, args):
        gr.top_block.__init__(self)
        self._verbose = options.verbose

        self._interp = 0 
        self._gain = 0
        self._freq = None       # Indicates frequency hasn't been successfully set yet
        self._bb_freq = 0
        self._ddc_freq = 0
        self._amplitude = 0
        self._type = None       # Indicates waveform flowgraph not created yet
        self._offset = options.offset

        self.set_usrp2(options.interface, options.mac_addr)
        self.set_interp(options.interp)
        self.set_gain(options.gain)
        self.set_freq(options.tx_freq, options.lo_offset)
        self.set_amplitude(options.amplitude)

        self.set_waveform_freq(options.waveform_freq)
        self.set_waveform2_freq(options.waveform2_freq)
        self.set_waveform(options.type)

    def set_usrp2(self, interface, mac_addr):
        self._u = usrp2.sink_32fc(interface, mac_addr)
        self._dac_rate = self._u.dac_rate()
        if self._verbose:
            print "Network interface:", interface
            print "Network address:", self._u.mac_addr()
            print "Daughterboard ID:", hex(self._u.daughterboard_id())

    def set_interp(self, interp):
        if interp < 4 or interp > 512: # FIXME get from flowgraph
            if self._verbose: print "Interpolation rate out of range:", interp
            return False

        if not self._u.set_interp(interp):
            raise RuntimeError("Failed to set interpolation rate %i" % (interp,))

        self._interp = interp
        self._eth_rate = self._dac_rate/self._interp
        if self._verbose:
            print "USRP2 interpolation rate:", self._interp
            print "USRP2 IF bandwidth: %sHz" % (n2s(self._eth_rate),)

        if (self._type == gr.GR_SIN_WAVE or 
            self._type == gr.GR_CONST_WAVE):
            self._src.set_sampling_freq(self._eth_rate)
        elif self._type == "2tone":
            self._src1.set_sampling_freq(self._eth_rate)
            self._src1.set_sampling_freq(self._eth_rate)
        elif self._type == "sweep":
            self._src1.set_sampling_freq(self._eth_rate)
            self._src1.set_sampling_freq(self._waveform_freq*2*math.pi/self._eth_rate)
        else:
            return True # Waveform not yet set
        
        if self._verbose: print "Set interpolation rate to:", interp
        return True

    def set_gain(self, gain):
        if gain is None:
            g = self._u.gain_range()
            gain = float(g[0]+g[1])/2
            if self._verbose:
                print "Using auto-calculated mid-point TX gain"
        self._u.set_gain(gain)
        self._gain = gain
        if self._verbose:
            print "Set TX gain to:", self._gain

    def set_freq(self, target_freq, lo_offset=None):
        if lo_offset is not None:
            self._lo_offset = lo_offset
            self._u.set_lo_offset(self._lo_offset)
            if self._verbose:
                print "Set LO offset frequency to: %sHz" % (n2s(lo_offset),)

        if target_freq is None:
            f = self._u.freq_range()
            target_freq = float(f[0]+f[1])/2.0
            if self._verbose:
                print "Using auto-calculated mid-point frequency"

        tr = self._u.set_center_freq(target_freq)
        fs = "%sHz" % (n2s(target_freq),)
        if tr is not None:
            self._freq = target_freq

        else:
            return True # Waveform not yet set
        
        if self._verbose: print "Set amplitude to:", amplitude
        return True

    def set_gain(self, gain):
        if gain is None:
            g = self._u.gain_range()
            gain = float(g[0]+g[1])/2
            if self._verbose:
                print "Using auto-calculated mid-point TX gain"
        self._u.set_gain(gain)
        self._gain = gain
        if self._verbose:
            print "Set TX gain to:", self._gain

    def set_freq(self, target_freq, lo_offset=None):
        if lo_offset is not None:
            self._lo_offset = lo_offset
            self._u.set_lo_offset(self._lo_offset)
            if self._verbose:
                print "Set LO offset frequency to: %sHz" % (n2s(lo_offset),)

        if target_freq is None:
            f = self._u.freq_range()
            target_freq = float(f[0]+f[1])/2.0
            if self._verbose:
                print "Using auto-calculated mid-point frequency"

        tr = self._u.set_center_freq(target_freq)
        fs = "%sHz" % (n2s(target_freq),)
        if tr is not None:
            self._freq = target_freq
            self._ddc_freq = tr.dxc_freq
            self._bb_freq = tr.baseband_freq
            if self._verbose:
                print "Set center frequency to", fs
                print "Tx baseband frequency: %sHz" % (n2s(tr.baseband_freq),)
                print "Tx DDC frequency: %sHz" % (n2s(tr.dxc_freq),)
                print "Tx residual frequency: %sHz" % (n2s(tr.residual_freq),)
                
        return tr

    def set_waveform_freq(self, freq):
        self._waveform_freq = freq
        if self._type == gr.GR_SIN_WAVE:
            self._src.set_frequency(freq)
        elif self._type == "2tone" or self._type == "sweep":
            self._src1.set_frequency(freq)
        return True

    def set_waveform2_freq(self, freq):
        self._waveform2_freq = freq
        if self._type == "2tone":
            self._src2.set_frequency(freq)
        elif self._type == "sweep":
            self._src1.set_frequency(freq)
        return True

    def set_waveform(self, type):
        self.lock()
        self.disconnect_all()

        if type == gr.GR_SIN_WAVE or type == gr.GR_CONST_WAVE:
            self._src = gr.sig_source_c(self._eth_rate,      # Sample rate
                                        type,                # Waveform type
                                        self._waveform_freq, # Waveform frequency
                                        self._amplitude,     # Waveform amplitude
                                        self._offset)        # Waveform offset
        elif type == gr.GR_GAUSSIAN or type == gr.GR_UNIFORM:
            self._src = gr.noise_source_c(type, self._amplitude)
        elif type == "2tone":
            self._src1 = gr.sig_source_c(self._eth_rate,
                                         gr.GR_SIN_WAVE,
                                         self._waveform_freq,
                                         self._amplitude/2.0,
                                         0)
            if(self._waveform2_freq is None):
                self._waveform2_freq = -self._waveform_freq

            self._src2 = gr.sig_source_c(self._eth_rate,
                                         gr.GR_SIN_WAVE,
                                         self._waveform2_freq,
                                         self._amplitude/2.0,
                                         0)
            self._src = gr.add_cc()
            self.connect(self._src1,(self._src,0))
            self.connect(self._src2,(self._src,1))
        elif type == "sweep":
            # rf freq is center frequency
            # waveform_freq is total swept width
            # waveform2_freq is sweep rate
            # will sweep from (rf_freq-waveform_freq/2) to (rf_freq+waveform_freq/2)
            if self._waveform2_freq is None:
                self._waveform2_freq = 0.1

            self._src1 = gr.sig_source_f(self._eth_rate,
                                         gr.GR_TRI_WAVE,
                                         self._waveform2_freq,
                                         1.0,
                                         -0.5)
            self._src2 = gr.frequency_modulator_fc(self._waveform_freq*2*math.pi/self._eth_rate)
            self._src = gr.multiply_const_cc(self._amplitude)
            self.connect(self._src1,self._src2,self._src)
        else:
            raise RuntimeError("Unknown waveform type")

        self.connect(self._src, self._u)
        self._type = type
        self.unlock()

        if self._verbose:
            print "Set baseband modulation to:", waveforms[self._type]
            if type == gr.GR_SIN_WAVE:
                print "Modulation frequency: %sHz" % (n2s(self._waveform_freq),)
                print "Initial phase:", self._offset
            elif type == "2tone":
                print "Tone 1: %sHz" % (n2s(self._waveform_freq),)
                print "Tone 2: %sHz" % (n2s(self._waveform2_freq),)
            elif type == "sweep":
                print "Sweeping across %sHz to %sHz" % (n2s(-self._waveform_freq/2.0),n2s(self._waveform_freq/2.0))
                print "Sweep rate: %sHz" % (n2s(self._waveform2_freq),)
            print "TX amplitude:", self._amplitude


    def set_amplitude(self, amplitude):
        if amplitude < 0.0 or amplitude > 1.0:
            if self._verbose: print "Amplitude out of range:", amplitude
            return False

        self._amplitude = amplitude

        if (self._type == gr.GR_SIN_WAVE or 
            self._type == gr.GR_CONST_WAVE or
            self._type == gr.GR_GAUSSIAN or
            self._type == gr.GR_UNIFORM):
            self._src.set_amplitude(amplitude)
        elif self._type == "2tone":
            self._src1.set_amplitude(amplitude/2.0)
            self._src2.set_amplitude(amplitude/2.0)
        elif self._type == "sweep":
            self._src.set_k(amplitude)
        else:
            return True # Waveform not yet set
        
        if self._verbose: print "Set amplitude to:", amplitude
        return True


    # Property getters

    def mac_addr(self):
        return self._u.mac_addr()

    def interface_name(self):
        return self._u.interface_name()

    def daughterboard_id(self):
        return self._u.daughterboard_id()

    def interp_rate(self):
        return self._interp

    def eth_rate(self):
        return self._eth_rate

    def freq(self):
        return self._freq

    def freq_range(self):
        return self._u.freq_range()

    def ddc_freq(self):
        return self._ddc_freq

    def baseband_freq(self):
        return self._bb_freq

    def amplitude(self):
        return self._amplitude

    def waveform_type(self):
        return self._type

    def waveform_freq(self):
        return self._waveform_freq

    def waveform2_freq(self):
        if self._waveform2_freq is None:
            return -self._waveform_freq
        else:
            return self._waveform2_freq

def get_options():
    usage="%prog: [options]"

    parser = OptionParser(option_class=eng_option, usage=usage)

    parser.add_option("-e", "--interface", type="string", default="eth0",
                      help="Use specified Ethernet interface [default=%default]")
    parser.add_option("-m", "--mac-addr", type="string", default="",
                      help="Use USRP2 at specified MAC address [default=None]")  
    parser.add_option("-i", "--interp", type="int", default=16, metavar="INTERP",
                      help="Set FPGA interpolation rate of INTERP [default=%default]")
    parser.add_option("-f", "--tx-freq", type="eng_float", default=None,
                      help="Set carrier frequency to FREQ [default=mid-point]", metavar="FREQ")
    parser.add_option("--lo-offset", type="eng_float", default=None,
                      help="set daughterboard LO offset to OFFSET [default=hw default]")
    parser.add_option("-g", "--gain", type="eng_float", default=None,
                      help="Set TX gain to GAIN [default=mid-point]")
    parser.add_option("-w", "--waveform-freq", type="eng_float", default=0,
                      help="Set baseband waveform frequency to FREQ [default=%default]")
    parser.add_option("-x", "--waveform2-freq", type="eng_float", default=None,
                      help="Set 2nd waveform frequency to FREQ [default=%default]")
    parser.add_option("--sine", dest="type", action="store_const", const=gr.GR_SIN_WAVE,
                      help="Generate a carrier modulated by a complex sine wave", default=gr.GR_SIN_WAVE)
    parser.add_option("--const", dest="type", action="store_const", const=gr.GR_CONST_WAVE, 
                      help="Generate a constant carrier")
    parser.add_option("--offset", type="eng_float", default=0,
                      help="Set waveform phase offset to OFFSET [default=%default]")
    parser.add_option("--gaussian", dest="type", action="store_const", const=gr.GR_GAUSSIAN,
                      help="Generate Gaussian random output")
    parser.add_option("--uniform", dest="type", action="store_const", const=gr.GR_UNIFORM,
                      help="Generate Uniform random output")
    parser.add_option("--2tone", dest="type", action="store_const", const="2tone",
                      help="Generate Two Tone signal for IMD testing")
    parser.add_option("--sweep", dest="type", action="store_const", const="sweep",
                      help="Generate a swept sine wave")
    parser.add_option("-a", "--amplitude", type="eng_float", default=0.1,
                      help="Set output amplitude to AMPL (0.0-1.0) [default=%default]", metavar="AMPL")
    parser.add_option("-v", "--verbose", action="store_true", default=False,
                      help="Use verbose console output [default=%default]")

    (options, args) = parser.parse_args()

    return (options, args)

# If this script is executed, the following runs. If it is imported, the below does not run.
if __name__ == "__main__":
    if gr.enable_realtime_scheduling() != gr.RT_OK:
        print "Note: failed to enable realtime scheduling, continuing"
    
    # Grab command line options and create top block
    try:
        (options, args) = get_options()
        tb = top_block(options, args)

    except RuntimeError, e:
        print e
        sys.exit(1)

    # Run it
    try:
        tb.run()

    except KeyboardInterrupt:
        pass