blob: a693e6aebc2cb7b7a596e6430a4f5169a66cadd8 [file] [log] [blame]
from optparse import OptionParser
import sys
from gnuradio import blocks
from gnuradio import gr
from gnuradio import uhd
class SignalGenerator(gr.top_block):
def __init__(self):
super(SignalGenerator, self).__init__('Signal Generator')
sample_rate = 30e6
# Initialize the SDR
stream_args = uhd.stream_args(cpu_format='fc32', otw_format='sc8',
self.usrp_sink = uhd.usrp_sink(',', stream_args)
# Create a list of the file source streams so we can add them as we read
# arguments.
self.sources = []
# Adjust parameters
# Initialize and connect the signal adder.
self.add_block = blocks.add_vcc(1)
self.connect((self.add_block, 0), (self.usrp_sink, 0))
# Adjust gain value on the USRP.
def set_gain(self, gain):
# Adjust center frequency value of the USRP.
def set_freq(self, freq):
self.usrp_sink.set_center_freq(freq, 0)
# Add a new signal to be broadcasted.
def add_file_source(self, filename, factor):
# Create and add our file sources paired with their multiplier block to the
# sources list.
src = blocks.file_source(gr.sizeof_gr_complex, filename, True)
multiply_block = blocks.multiply_const_vcc((factor,))
self.connect((src, 0), (multiply_block, 0))
def run(self):
# Connect all the sources to the adder.
for i in range(len(self.sources)):
self.connect((self.sources[i], 0), (self.add_block, i))
if __name__ == '__main__':
# Get command line arguments.
parser = OptionParser()
parser.add_option('-f', '--file', dest='file', help='Add signal from file '
'(raw 64 bit complex) which can be generated using the '
'\'\' script. The raw file can be read '
'using scipy.fromfile. If factor is not included, it defaults '
'to 50.0', metavar='FILENAME,FACTOR', action='append')
parser.add_option('-u', '--frequency', dest='frequency',
help='Specify the desired frequency in KHz, defaults to'
'2412000KHz (Wifi Channel 1)', metavar='FREQUENCY',
parser.add_option('-g', '--gain', dest='gain',
help='Specify the db-gain amplification, defaults to 50db',
metavar='DB_GAIN', default='50')
options, args = parser.parse_args()
# Initialize the top_block
signal_gen_block = SignalGenerator()
signal_gen_block.set_freq(float(options.frequency) * 1000.0)
# Add the signals to the top_block
if options.file is not None:
for signal in options.file:
args = signal.split(',')
# Check for number of arguments
if len(args) > 1:
# Verify that the second argument is a float.
factor = float(args[1])
print 'Invalid arguments: {0}'.format(signal)
factor = 50.0
# Add the signal to the top_block.
signal_gen_block.add_file_source(args[0], factor)
print '\nAt least 1 signal is required to run\n'