blob: c0190a5cd43cbc5d47435984e09802289c36e9ac [file] [log] [blame]
rpp29a39b22015-09-11 16:33:58 +02001#!/usr/bin/env python2
2# -*- coding: utf-8 -*-
3# @file
4# @author Pieter Robyns <pieter.robyns@uhasselt.be>
5# @section LICENSE
6#
7# Gr-gsm is free software; you can redistribute it and/or modify
8# it under the terms of the GNU General Public License as published by
9# the Free Software Foundation; either version 3, or (at your option)
10# any later version.
11#
12# Gr-gsm is distributed in the hope that it will be useful,
13# but WITHOUT ANY WARRANTY; without even the implied warranty of
14# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15# GNU General Public License for more details.
16#
17# You should have received a copy of the GNU General Public License
18# along with gr-gsm; see the file COPYING. If not, write to
19# the Free Software Foundation, Inc., 51 Franklin Street,
20# Boston, MA 02110-1301, USA.
21#
22#
23
24##################################################
25# gr-gsm channelizer
26#
27# Standalone application to channelize a wideband
28# GSM capture file into multiple seperate capture
29# files for the specified ARFCNs.
30##################################################
31
32from gnuradio import blocks
33from gnuradio import eng_notation
34from gnuradio import filter
35from gnuradio import gr
36from gnuradio.eng_option import eng_option
37from gnuradio.filter import firdes
38from argparse import ArgumentParser, ArgumentTypeError, RawDescriptionHelpFormatter
39import grgsm.arfcn as arfcn
40import os
41
42EXTRA_HELP = """
43Example usage:
44gsm_channelize.py -f my_wideband_capture.cfile -c 925.2e6 990 991 992 993 994 995 1019 1020 1021 1022 1023
45
46The above example will channelize my_wideband_capture.cfile, in this case a cfile captured at
47925.2 MHz centered (ARFCN 975) and 20 Msps. As a result, 12 files will be generated for
48ARFCNs 975 - 1023 at 1 Msps each.
49"""
50
51def eng_float(value):
52 try:
53 return eng_notation.str_to_num(value)
54 except:
55 raise ArgumentTypeError("invalid engineering notation value: {0}".format(value))
56
57def gsm_band(value):
58 choices = arfcn.get_bands()
59 if value in choices:
60 return value
61 else:
62 raise ArgumentTypeError("invalid GSM band: {0}. Possible choices are: {1}".format(value, choices))
63
64class gsm_channelize(gr.top_block):
65 def __init__(self, channels, decim, ch_width, ch_twidth, fc, band, samp_rate, path):
66 gr.top_block.__init__(self, "gsm_channelize")
67
68 ##################################################
69 # Parameters
70 ##################################################
71 self.channels = channels
72 self.decim = decim
73 self.ch_width = ch_width
74 self.ch_twidth = ch_twidth
75 self.fc = fc
76 self.band = band
77 self.samp_rate = samp_rate
78 self.blocks_fir_filters = {}
79 self.blocks_file_sinks = {}
80
81 ##################################################
82 # Blocks and connections
83 ##################################################
84 self.blocks_file_source = blocks.file_source(gr.sizeof_gr_complex, path, False)
85 self.blocks_throttle = blocks.throttle(gr.sizeof_gr_complex, samp_rate,True)
86
87 self.connect((self.blocks_file_source, 0), (self.blocks_throttle, 0))
88
89 c0_arfcn = arfcn.downlink2arfcn(fc, band)
90 self.channels.insert(0, c0_arfcn)
91 print("Extracting channels %s, given that the center frequency is at ARFCN %d (%s)" % (str(channels), c0_arfcn, eng_notation.num_to_str(fc)))
92
93 for channel in channels:
94 channel_freq = arfcn.arfcn2downlink(channel, band)
95 if channel_freq is None:
96 print("Warning: invalid ARFCN %d for band %s" % (channel, band))
97 continue
98 freq_diff = channel_freq - fc
99 print("ARFCN %d is at C0 %+d KHz" % (channel, int(freq_diff / 1000.0)))
100
101 self.blocks_fir_filters[channel] = filter.freq_xlating_fir_filter_ccc(decim, (firdes.low_pass(1, samp_rate, ch_width, ch_twidth)), freq_diff, samp_rate)
102 self.connect((self.blocks_throttle, 0), (self.blocks_fir_filters[channel], 0))
103
104 self.blocks_file_sinks[channel] = blocks.file_sink(gr.sizeof_gr_complex, "./gsm_channelizer/out_" + str(channel) + ".cfile", False)
105 self.blocks_file_sinks[channel].set_unbuffered(False)
106 self.connect((self.blocks_fir_filters[channel], 0), (self.blocks_file_sinks[channel], 0))
107
108
109if __name__ == '__main__':
110 parser = ArgumentParser(formatter_class=RawDescriptionHelpFormatter, description='Split wideband a GSM capture into seperate files per ARFCN.', epilog=EXTRA_HELP)
111 parser.add_argument(dest="channel", type=int, nargs='+',
112 help="List of ARFCNs")
113 parser.add_argument("-w", "--width", dest="width", type=eng_float, default=eng_notation.num_to_str(200000),
114 help="Channel lowpass width [default=%(default)s]")
115 parser.add_argument("-t", "--transition-width", dest="twidth", type=eng_float, default=eng_notation.num_to_str(50000),
116 help="Channel lowpass transition width [default=%(default)s]")
117 parser.add_argument("-s", "--samp-rate", dest="samp_rate", type=eng_float, default=eng_notation.num_to_str(2e7),
118 help="Sample rate of the wideband capture file [default=%(default)s]")
119 parser.add_argument("-o", "--out-samp-rate", dest="out_samp_rate", type=eng_float, default=eng_notation.num_to_str(1e6),
120 help="Sample rate of the output capture files [default=%(default)s]")
121 parser.add_argument("-b", "--band", dest="band", type=gsm_band, default='E-GSM',
122 help="GSM band [default=%(default)s]")
123 parser.add_argument("-f", "--cfile", dest="path", type=str, required=True,
124 help="Path to wideband GSM capture file")
125 parser.add_argument("-c", "--C0", dest="fc", type=eng_float, default=eng_notation.num_to_str(925.2e6), required=True,
126 help="C0 (BCCH) channel frequency in Hz [default=%(default)s]")
127 args = parser.parse_args()
128
129 if not os.path.exists("./gsm_channelizer"):
130 os.makedirs("./gsm_channelizer")
131
132 if not os.path.exists(args.path):
133 raise IOError(args.path + " does not exist")
134
135 if args.samp_rate % args.out_samp_rate != 0:
136 raise Exception("Input sample rate should be multiple of output sample rate in order to get integer decimation.")
137
138 decim = int(args.samp_rate / args.out_samp_rate)
139 print("Input sample rate: " + eng_notation.num_to_str(args.samp_rate))
140 print("Output sample rate: " + eng_notation.num_to_str(args.out_samp_rate))
141 print("==> using decimation of " + str(decim))
142
143 tb = gsm_channelize(channels=args.channel,
144 decim=decim,
145 ch_width=args.width,
146 ch_twidth=args.twidth,
147 fc=args.fc,
148 band=args.band,
149 samp_rate=args.samp_rate,
150 path=args.path)
151 tb.start()
152 tb.wait()
153 print("Done!")