Add op25 TX programs

git-svn-id: http://op25.osmocom.org/svn/trunk@188 65a5c917-d112-43f1-993d-58c26a4786be
This commit is contained in:
max 2009-11-30 18:28:31 +00:00
parent 52cd683fb5
commit 17b5e68b46
3 changed files with 529 additions and 0 deletions

168
python/op25_c4fm_mod.py Normal file
View File

@ -0,0 +1,168 @@
#
# Copyright 2005,2006,2007 Free Software Foundation, Inc.
#
# OP25 4-Level Modulator Block # Copyright 2009, KA1RBI
#
# coeffs for shaping and cosine filters from Eric Ramsey thesis
#
# This file is part of GNU Radio and part of OP25
#
# This 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.
#
# It 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 this; see the file COPYING. If not, write to
# the Free Software Foundation, Inc., 51 Franklin Street,
# Boston, MA 02110-1301, USA.
#
"""
P25 C4FM pre-modulation block.
"""
from gnuradio import gr, gru, audio, eng_notation, optfir, modulation_utils
from gnuradio import blks2
from gnuradio.eng_option import eng_option
from optparse import OptionParser
import math
# default values (used in __init__ and add_options)
_def_output_sample_rate = 48000
_def_excess_bw = 0.2
_def_reverse = False
_def_verbose = False
_def_log = False
# /////////////////////////////////////////////////////////////////////////////
# modulator
# /////////////////////////////////////////////////////////////////////////////
class p25_mod(gr.hier_block2):
def __init__(self,
output_sample_rate=_def_output_sample_rate,
excess_bw=_def_excess_bw,
reverse=_def_reverse,
verbose=_def_verbose,
log=_def_log):
"""
Hierarchical block for RRC-filtered P25 FM modulation.
The input is a dibit (P25 symbol) stream (char, not packed) and the
output is the float "C4FM" signal at baseband, suitable for application
to an FM modulator stage
Input is at the base symbol rate (4800), output sample rate is
typically either 32000 (USRP TX chain) or 48000 (sound card)
@param output_sample_rate: output sample rate
@type output_sample_rate: integer
@param excess_bw: Root-raised cosine filter excess bandwidth
@type excess_bw: float
@param reverse: reverse polarity flag
@type reverse: bool
@param verbose: Print information about modulator?
@type verbose: bool
@param debug: Print modualtion data to files?
@type debug: bool
"""
gr.hier_block2.__init__(self, "p25_fm_mod",
gr.io_signature(1, 1, gr.sizeof_char), # Input signature
gr.io_signature(1, 1, gr.sizeof_float)) # Output signature
input_sample_rate = 4800 # P25 baseband symbol rate
lcm = gru.lcm(input_sample_rate, output_sample_rate)
self._interp_factor = int(lcm // input_sample_rate)
self._decimation = int(lcm // output_sample_rate)
self._excess_bw = excess_bw
mod_map = [1.0/3.0, 1.0, -(1.0/3.0), -1.0]
self.C2S = gr.chunks_to_symbols_bf(mod_map)
if reverse:
self.polarity = gr.multiply_const_ff(-1)
else:
self.polarity = gr.multiply_const_ff( 1)
ntaps = 11 * self._interp_factor
rrc_taps = gr.firdes.root_raised_cosine(
self._interp_factor, # gain (since we're interpolating by sps)
lcm, # sampling rate
input_sample_rate, # symbol rate
self._excess_bw, # excess bandwidth (roll-off factor)
ntaps)
# rrc_coeffs work slightly differently: each input sample
# (from mod_map above) at 4800 rate, then 9 zeros are inserted
# to bring to 48000 rate, then this filter is applied:
# rrc_filter = gr.fir_filter_fff(1, rrc_coeffs)
# FIXME: how to insert the 9 zero samples using gr ?
# rrc_coeffs = [0, -0.003, -0.006, -0.009, -0.012, -0.014, -0.014, -0.013, -0.01, -0.006, 0, 0.007, 0.014, 0.02, 0.026, 0.029, 0.029, 0.027, 0.021, 0.012, 0, -0.013, -0.027, -0.039, -0.049, -0.054, -0.055, -0.049, -0.038, -0.021, 0, 0.024, 0.048, 0.071, 0.088, 0.098, 0.099, 0.09, 0.07, 0.039, 0, -0.045, -0.091, -0.134, -0.17, -0.193, -0.199, -0.184, -0.147, -0.085, 0, 0.105, 0.227, 0.36, 0.496, 0.629, 0.751, 0.854, 0.933, 0.983, 1, 0.983, 0.933, 0.854, 0.751, 0.629, 0.496, 0.36, 0.227, 0.105, 0, -0.085, -0.147, -0.184, -0.199, -0.193, -0.17, -0.134, -0.091, -0.045, 0, 0.039, 0.07, 0.09, 0.099, 0.098, 0.088, 0.071, 0.048, 0.024, 0, -0.021, -0.038, -0.049, -0.055, -0.054, -0.049, -0.039, -0.027, -0.013, 0, 0.012, 0.021, 0.027, 0.029, 0.029, 0.026, 0.02, 0.014, 0.007, 0, -0.006, -0.01, -0.013, -0.014, -0.014, -0.012, -0.009, -0.006, -0.003, 0]
self.rrc_filter = gr.interp_fir_filter_fff(self._interp_factor, rrc_taps)
# FM pre-emphasis filter
shaping_coeffs = [-0.018, 0.0347, 0.0164, -0.0064, -0.0344, -0.0522, -0.0398, 0.0099, 0.0798, 0.1311, 0.121, 0.0322, -0.113, -0.2499, -0.3007, -0.2137, -0.0043, 0.2825, 0.514, 0.604, 0.514, 0.2825, -0.0043, -0.2137, -0.3007, -0.2499, -0.113, 0.0322, 0.121, 0.1311, 0.0798, 0.0099, -0.0398, -0.0522, -0.0344, -0.0064, 0.0164, 0.0347, -0.018]
self.shaping_filter = gr.fir_filter_fff(1, shaping_coeffs)
if verbose:
self._print_verbage()
if log:
self._setup_logging()
self.connect(self, self.C2S, self.polarity, self.rrc_filter, self.shaping_filter)
if (self._decimation > 1):
self.decimator = blks2.rational_resampler_fff(1, self._decimation)
self.connect(self.shaping_filter, self.decimator, self)
else:
self.connect(self.shaping_filter, self)
def _print_verbage(self):
print "\nModulator:"
print "RRS roll-off factor: %f" % self._excess_bw
print "interpolation: %d decimation: %d" %(self._interp_factor, self._decimation)
def _setup_logging(self):
print "Modulation logging turned on."
self.connect(self.C2S,
gr.file_sink(gr.sizeof_float, "tx_chunks2symbols.dat"))
self.connect(self.polarity,
gr.file_sink(gr.sizeof_float, "tx_polarity.dat"))
self.connect(self.rrc_filter,
gr.file_sink(gr.sizeof_float, "tx_rrc_filter.dat"))
self.connect(self.shaping_filter,
gr.file_sink(gr.sizeof_float, "tx_shaping_filter.dat"))
if (self._decimation > 1):
self.connect(self.decimator,
gr.file_sink(gr.sizeof_float, "tx_decimator.dat"))
def add_options(parser):
"""
Adds QPSK modulation-specific options to the standard parser
"""
parser.add_option("", "--excess-bw", type="float", default=_def_excess_bw,
help="set RRC excess bandwith factor [default=%default] (PSK)")
add_options=staticmethod(add_options)
def extract_kwargs_from_options(options):
"""
Given command line options, create dictionary suitable for passing to __init__
"""
return modulation_utils.extract_kwargs_from_options(dqpsk_mod.__init__,
('self',), options)
extract_kwargs_from_options=staticmethod(extract_kwargs_from_options)
#
# Add these to the mod/demod registry
#
modulation_utils.add_type_1_mod('op25_c4fm', p25_mod)

90
python/op25_remote_tx.py Executable file
View File

@ -0,0 +1,90 @@
#!/usr/bin/env python
#
# Copyright 2005,2006,2007 Free Software Foundation, Inc.
#
# GNU Radio Multichannel APCO P25 Tx (c) Copyright 2009, KA1RBI
#
# This file is part of GNU Radio and part of OP25
#
# This program 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.
#
# It 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 this; see the file COPYING. If not, write to
# the Free Software Foundation, Inc., 51 Franklin Street,
# Boston, MA 02110-1301, USA.
"""
This program is used in conjunction with op25_tx.py.
When op25_tx.py is used with its -p option (selecting UDP input),
you should start this program beforehand, to start the data flow.
"""
import math
from gnuradio import gr, gru, audio, eng_notation, blks2, optfir
from gnuradio.eng_option import eng_option
from optparse import OptionParser
from gnuradio import op25_imbe
class app_top_block(gr.top_block):
def __init__(self, options, queue):
gr.top_block.__init__(self, "mhp")
self.audio_amps = []
self.converters = []
self.vocoders = []
input_audio_rate = 8000
self.audio_input = audio.source(input_audio_rate, options.audio_input)
for i in range (options.nchannels):
t = gr.multiply_const_ff(32767 * options.audio_gain)
self.audio_amps.append(t)
t = gr.float_to_short()
self.converters.append(t)
t = op25_imbe.vocoder(True, # 0=Decode,True=Encode
options.verbose, # Verbose flag
options.stretch, # flex amount
options.udp_addr, # udp ip address
options.udp_port + i, # udp port
False) # dump raw u vectors
self.vocoders.append(t)
for i in range (options.nchannels):
self.connect((self.audio_input, i), self.audio_amps[i], self.converters[i], self.vocoders[i])
def main():
parser = OptionParser(option_class=eng_option)
parser.add_option("-a", "--udp-addr", type="string", default="127.0.0.1", help="destination host IP address")
parser.add_option("-g", "--audio-gain", type="eng_float", default=1.0, help="gain factor")
parser.add_option("-n", "--nchannels", type="int", default=2, help="number of audio channels")
parser.add_option("-p", "--udp-port", type="int", default=2525, help="destination host port")
parser.add_option("-v", "--verbose", action="store_true", default=False, help="dump demodulation data")
parser.add_option("-I", "--audio-input", type="string", default="", help="pcm input device name. E.g., hw:0,0 or /dev/dsp")
parser.add_option("-S", "--stretch", type="int", default=0)
(options, args) = parser.parse_args()
queue = gr.msg_queue()
tb = app_top_block(options, queue)
try:
tb.start()
while 1:
if not queue.empty_p():
sys.stderr.write("main: q.delete_head()\n")
msg = queue.delete_head()
except KeyboardInterrupt:
tb.stop()
if __name__ == "__main__":
main()

271
python/op25_tx.py Executable file
View File

@ -0,0 +1,271 @@
#!/usr/bin/env python
#
# Copyright 2005,2006,2007 Free Software Foundation, Inc.
#
# GNU Radio Multichannel APCO P25 Tx (c) Copyright 2009, KA1RBI
#
# This file is part of GNU Radio and part of OP25
#
# This program 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.
#
# It 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 this; see the file COPYING. If not, write to
# the Free Software Foundation, Inc., 51 Franklin Street,
# Boston, MA 02110-1301, USA.
#
"""
Transmit N simultaneous narrow band P25 C4FM signals.
They will be centered at the frequency specified on the command line,
and are spaced at 25kHz steps from there.
There are three main ways to run this program:
A. Combined audio capture, speech coding, and USRP transmission [default]
B. USRP transmission only, receives P25 symbol input via UDP channel [-p]
C. USRP transmission only, P25 symbol input taken from file(s) [-i]
By using method B, the coding and transmission functions can be
split over separate machines. Run the op25_remote_tx script prior to
running this script when using method B.
"""
from gnuradio import gr, eng_notation, op25_imbe
from gnuradio import usrp
from gnuradio import audio
from gnuradio import blks2
from gnuradio.eng_option import eng_option
from optparse import OptionParser
from usrpm import usrp_dbid
import math
import sys
from gnuradio.wxgui import stdgui2, fftsink2
#from gnuradio import tx_debug_gui
import wx
import op25_c4fm_mod
########################################################
# instantiate one transmit chain for each call
class pipeline(gr.hier_block2):
def __init__(self, vocoder, lo_freq, audio_rate, if_rate):
gr.hier_block2.__init__(self, "pipeline",
gr.io_signature(0, 0, 0), # Input signature
gr.io_signature(1, 1, gr.sizeof_gr_complex)) # Output signature
c4fm = op25_c4fm_mod.p25_mod(output_sample_rate=audio_rate,
log=False,
verbose=True)
interp_factor = if_rate / audio_rate
low_pass = 2.88e3
interp_taps = gr.firdes.low_pass(1.0, if_rate, low_pass, low_pass * 0.1, gr.firdes.WIN_HANN)
interpolator = gr.interp_fir_filter_fff (int(interp_factor), interp_taps)
max_dev = 12.5e3
k = 2 * math.pi * max_dev / if_rate
adjustment = 1.5 # adjust for proper c4fm deviation level
modulator = gr.frequency_modulator_fc (k * adjustment)
# Local oscillator
lo = gr.sig_source_c (if_rate, # sample rate
gr.GR_SIN_WAVE, # waveform type
lo_freq, #frequency
1.0, # amplitude
0) # DC Offset
mixer = gr.multiply_cc ()
self.connect (vocoder, c4fm, interpolator, modulator, (mixer, 0))
self.connect (lo, (mixer, 1))
self.connect (mixer, self)
class fm_tx_block(stdgui2.std_top_block):
def __init__(self, frame, panel, vbox, argv):
MAX_CHANNELS = 7
stdgui2.std_top_block.__init__ (self, frame, panel, vbox, argv)
parser = OptionParser (option_class=eng_option)
parser.add_option("-T", "--tx-subdev-spec", type="subdev", default=None,
help="select USRP Tx side A or B")
parser.add_option("-e","--enable-fft", action="store_true", default=False,
help="enable spectrum plot (and use more CPU)")
parser.add_option("-f", "--freq", type="eng_float", default=None,
help="set Tx frequency to FREQ [required]", metavar="FREQ")
parser.add_option("-i","--file-input", action="store_true", default=False,
help="input from baseband-0.dat, baseband-1.dat ...")
parser.add_option("-g", "--audio-gain", type="eng_float", default=1.0,
help="input audio gain multiplier")
parser.add_option("-n", "--nchannels", type="int", default=2,
help="number of Tx channels [1,4]")
parser.add_option("-a", "--udp-addr", type="string", default="127.0.0.1",
help="UDP host IP address")
parser.add_option("-p", "--udp-port", type="int", default=0,
help="UDP port number")
parser.add_option("-r","--repeat", action="store_true", default=False,
help="continuously replay input file")
parser.add_option("-S", "--stretch", type="int", default=0,
help="elastic buffer trigger value")
parser.add_option("-v","--verbose", action="store_true", default=False,
help="print out stats")
parser.add_option("-I", "--audio-input", type="string", default="", help="pcm input device name. E.g., hw:0,0 or /dev/dsp")
(options, args) = parser.parse_args ()
if len(args) != 0:
parser.print_help()
sys.exit(1)
if options.nchannels < 1 or options.nchannels > MAX_CHANNELS:
sys.stderr.write ("op25_tx: nchannels out of range. Must be in [1,%d]\n" % MAX_CHANNELS)
sys.exit(1)
if options.freq is None:
sys.stderr.write("op25_tx: must specify frequency with -f FREQ\n")
parser.print_help()
sys.exit(1)
# ----------------------------------------------------------------
# Set up constants and parameters
self.u = usrp.sink_c () # the USRP sink (consumes samples)
self.dac_rate = self.u.dac_rate() # 128 MS/s
self.usrp_interp = 400
self.u.set_interp_rate(self.usrp_interp)
self.usrp_rate = self.dac_rate / self.usrp_interp # 320 kS/s
self.sw_interp = 10
self.audio_rate = self.usrp_rate / self.sw_interp # 32 kS/s
# determine the daughterboard subdevice we're using
if options.tx_subdev_spec is None:
options.tx_subdev_spec = usrp.pick_tx_subdevice(self.u)
m = usrp.determine_tx_mux_value(self.u, options.tx_subdev_spec)
#print "mux = %#04x" % (m,)
self.u.set_mux(m)
self.subdev = usrp.selected_subdev(self.u, options.tx_subdev_spec)
print "Using TX d'board %s" % (self.subdev.side_and_name(),)
self.subdev.set_gain(self.subdev.gain_range()[0]) # set min Tx gain
if not self.set_freq(options.freq):
freq_range = self.subdev.freq_range()
print "Failed to set frequency to %s. Daughterboard supports %s to %s" % (
eng_notation.num_to_str(options.freq),
eng_notation.num_to_str(freq_range[0]),
eng_notation.num_to_str(freq_range[1]))
raise SystemExit
self.subdev.set_enable(True) # enable transmitter
# instantiate vocoders
self.vocoders = []
if options.file_input:
for i in range (options.nchannels):
t = gr.file_source(gr.sizeof_char, "baseband-%d.dat" % i, options.repeat)
self.vocoders.append(t)
elif options.udp_port > 0:
self.udp_sources = []
for i in range (options.nchannels):
t = gr.udp_source(1, options.udp_addr, options.udp_port + i, 216)
self.udp_sources.append(t)
arity = 2
t = gr.packed_to_unpacked_bb(arity, gr.GR_MSB_FIRST)
self.vocoders.append(t)
self.connect(self.udp_sources[i], self.vocoders[i])
else:
self.audio_amps = []
self.converters = []
input_audio_rate = 8000
self.audio_input = audio.source(input_audio_rate, options.audio_input)
for i in range (options.nchannels):
t = gr.multiply_const_ff(32767 * options.audio_gain)
self.audio_amps.append(t)
t = gr.float_to_short()
self.converters.append(t)
t = op25_imbe.vocoder(True, # 0=Decode,True=Encode
options.verbose, # Verbose flag
options.stretch, # flex amount
"", # udp ip address
0, # udp port
False) # dump raw u vectors
self.vocoders.append(t)
self.connect((self.audio_input, i), self.audio_amps[i], self.converters[i], self.vocoders[i])
sum = gr.add_cc ()
# Instantiate N NBFM channels
step = 25e3
offset = (0 * step, 1 * step, -1 * step, 2 * step, -2 * step, 3 * step, -3 * step)
for i in range (options.nchannels):
t = pipeline(self.vocoders[i], offset[i],
self.audio_rate, self.usrp_rate)
self.connect(t, (sum, i))
gain = gr.multiply_const_cc (4000.0 / options.nchannels)
# connect it all
self.connect (sum, gain)
self.connect (gain, self.u)
# plot an FFT to verify we are sending what we want
if options.enable_fft:
post_mod = fftsink2.fft_sink_c(panel, title="Post Modulation",
fft_size=512, sample_rate=self.usrp_rate,
y_per_div=20, ref_level=40)
self.connect (sum, post_mod)
vbox.Add (post_mod.win, 1, wx.EXPAND)
#if options.debug:
# self.debugger = tx_debug_gui.tx_debug_gui(self.subdev)
# self.debugger.Show(True)
def set_freq(self, target_freq):
"""
Set the center frequency we're interested in.
@param target_freq: frequency in Hz
@rypte: bool
Tuning is a two step process. First we ask the front-end to
tune as close to the desired frequency as it can. Then we use
the result of that operation and our target_frequency to
determine the value for the digital up converter. Finally, we feed
any residual_freq to the s/w freq translater.
"""
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)
print "r.residual_freq =", eng_notation.num_to_str(r.residual_freq)
print "r.inverted =", r.inverted
# Could use residual_freq in s/w freq translator
return True
return False
def main ():
sys.stderr.write("GNU Radio Multichannel APCO P25 Tx (c) Copyright 2009, KA1RBI\n")
app = stdgui2.stdapp(fm_tx_block, "Multichannel APCO P25 Tx", nstatus=1)
app.MainLoop ()
if __name__ == '__main__':
main ()