add cqpsk-demod-file.py

git-svn-id: http://op25.osmocom.org/svn/trunk@219 65a5c917-d112-43f1-993d-58c26a4786be
This commit is contained in:
max 2010-07-21 22:22:37 +00:00
parent 339814f732
commit b2e4d4dd9d
1 changed files with 130 additions and 0 deletions

130
python/cqpsk-demod-file.py Executable file
View File

@ -0,0 +1,130 @@
#!/usr/bin/env python
#
# (C) Copyright 2010 KA1RBI
#
# apply CQPSK demodulator and P25 decoder to a sample capture file.
#
# input file is a sampled complex signal, default rate=96k
#
# Example usage:
# cqpsk-demod-file.py -i samples/trunk-control-complex-96KSS.dat -c 25300 -g 22
#
# FIXME: many of the blocks in this program should be moved to a hier block
#
import sys
import os
import math
from gnuradio import gr, gru, audio, eng_notation, repeater
from gnuradio.eng_option import eng_option
from optparse import OptionParser
from math import pi
class my_top_block(gr.top_block):
def __init__(self):
gr.top_block.__init__(self)
parser = OptionParser(option_class=eng_option)
parser.add_option("-1", "--one-channel", action="store_true", default=False, help="software synthesized Q channel")
parser.add_option("-c", "--calibration", type="eng_float", default=0, help="freq offset")
parser.add_option("-d", "--debug", action="store_true", default=False, help="allow time at init to attach gdb")
parser.add_option("-C", "--costas-alpha", type="eng_float", default=0.125, help="Costas alpha")
parser.add_option("-g", "--gain", type="eng_float", default=1.0)
parser.add_option("-i", "--input-file", type="string", default="in.dat", help="specify the input file")
parser.add_option("-I", "--imbe", action="store_true", default=False, help="output IMBE codewords")
parser.add_option("-L", "--low-pass", type="eng_float", default=12.5e3, help="low pass cut-off", metavar="Hz")
parser.add_option("-o", "--output-file", type="string", default="out.dat", help="specify the output file")
parser.add_option("-p", "--polarity", action="store_true", default=False, help="use reversed polarity")
parser.add_option("-s", "--sample-rate", type="int", default=96000, help="input sample rate")
parser.add_option("-v", "--verbose", action="store_true", default=False, help="additional output")
(options, args) = parser.parse_args()
sample_rate = options.sample_rate
symbol_rate = 4800
samples_per_symbol = sample_rate // symbol_rate
IN = gr.file_source(gr.sizeof_gr_complex, options.input_file)
if options.one_channel:
C2F = gr.complex_to_float()
F2C = gr.float_to_complex()
# osc./mixer for mixing signal down to approx. zero IF
LO = gr.sig_source_c (sample_rate, gr.GR_COS_WAVE, options.calibration, 1.0, 0)
MIXER = gr.multiply_cc()
# get signal into normalized range (-1.0 - +1.0)
# FIXME: add AGC
AMP = gr.multiply_const_cc(options.gain)
lpf_taps = gr.firdes.low_pass(1.0, sample_rate, options.low_pass, options.low_pass * 0.1, gr.firdes.WIN_HANN)
# decim by 2 to get 48k rate
DECIM = gr.fir_filter_ccf (2, lpf_taps)
sample_rate /= 2 # for DECIM
samples_per_symbol /= 2 # for DECIM
# create Gardner/Costas loop
# the loop will not work if the sample levels aren't normalized (above)
timing_error_gain = 0.025 # loop error gain
gain_omega = 0.25 * timing_error_gain * timing_error_gain
alpha = options.costas_alpha
beta = 0.125 * alpha * alpha
fmin = -0.025 # fmin and fmax are in radians/s
fmax = 0.025
GARDNER_COSTAS = repeater.gardner_costas_cc(samples_per_symbol, timing_error_gain, gain_omega, alpha, beta, fmax, fmin)
# probably too much phase noise etc to attempt coherent demodulation
# so we use differential
DIFF = gr.diff_phasor_cc()
# take angle of the phase difference (in radians)
TOFLOAT = gr.complex_to_arg()
# convert from radians such that signal is in [-3, -1, +1, +3]
RESCALE = gr.multiply_const_ff(1 / (pi / 4.0))
# optional polarity reversal (should be unnec. - now autodetected)
p = 1.0
if options.polarity:
p = -1.0
POLARITY = gr.multiply_const_ff(p)
# hard decision at specified points
levels = [-2.0, 0.0, 2.0, 4.0 ]
SLICER = repeater.fsk4_slicer_fb(levels)
# assemble received frames and route to Wireshark via UDP
hostname = "127.0.0.1"
port = 23456
debug = 0
if options.verbose:
debug = 255
do_imbe = False
if options.imbe:
do_imbe = True
do_output = True # enable block's output stream
do_msgq = False # msgq output not yet implemented
msgq = gr.msg_queue(2)
DECODER = repeater.p25_frame_assembler(hostname, port, debug, do_imbe, do_output, do_msgq, msgq)
OUT = gr.file_sink(gr.sizeof_char, options.output_file)
if options.one_channel:
self.connect(IN, C2F, F2C, (MIXER, 0))
else:
self.connect(IN, (MIXER, 0))
self.connect(LO, (MIXER, 1))
self.connect(MIXER, AMP, DECIM, GARDNER_COSTAS, DIFF, TOFLOAT, RESCALE, POLARITY, SLICER, DECODER, OUT)
if options.debug:
print 'Ready for GDB to attach (pid = %d)' % (os.getpid(),)
raw_input("Press 'Enter' to continue...")
if __name__ == "__main__":
try:
my_top_block().run()
except KeyboardInterrupt:
tb.stop()