blob: 4098b7beba704a696506b638e4834100a911516d [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)
rpp29a39b22015-09-11 16:33:58 +020085
86 c0_arfcn = arfcn.downlink2arfcn(fc, band)
87 self.channels.insert(0, c0_arfcn)
88 print("Extracting channels %s, given that the center frequency is at ARFCN %d (%s)" % (str(channels), c0_arfcn, eng_notation.num_to_str(fc)))
89
90 for channel in channels:
91 channel_freq = arfcn.arfcn2downlink(channel, band)
92 if channel_freq is None:
93 print("Warning: invalid ARFCN %d for band %s" % (channel, band))
94 continue
95 freq_diff = channel_freq - fc
96 print("ARFCN %d is at C0 %+d KHz" % (channel, int(freq_diff / 1000.0)))
97
98 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)
Steve Glasse25e10e2015-11-13 18:29:07 +100099 self.connect((self.blocks_file_source, 0), (self.blocks_fir_filters[channel], 0))
rpp29a39b22015-09-11 16:33:58 +0200100
101 self.blocks_file_sinks[channel] = blocks.file_sink(gr.sizeof_gr_complex, "./gsm_channelizer/out_" + str(channel) + ".cfile", False)
102 self.blocks_file_sinks[channel].set_unbuffered(False)
103 self.connect((self.blocks_fir_filters[channel], 0), (self.blocks_file_sinks[channel], 0))
104
105
106if __name__ == '__main__':
107 parser = ArgumentParser(formatter_class=RawDescriptionHelpFormatter, description='Split wideband a GSM capture into seperate files per ARFCN.', epilog=EXTRA_HELP)
108 parser.add_argument(dest="channel", type=int, nargs='+',
109 help="List of ARFCNs")
110 parser.add_argument("-w", "--width", dest="width", type=eng_float, default=eng_notation.num_to_str(200000),
111 help="Channel lowpass width [default=%(default)s]")
112 parser.add_argument("-t", "--transition-width", dest="twidth", type=eng_float, default=eng_notation.num_to_str(50000),
113 help="Channel lowpass transition width [default=%(default)s]")
114 parser.add_argument("-s", "--samp-rate", dest="samp_rate", type=eng_float, default=eng_notation.num_to_str(2e7),
115 help="Sample rate of the wideband capture file [default=%(default)s]")
116 parser.add_argument("-o", "--out-samp-rate", dest="out_samp_rate", type=eng_float, default=eng_notation.num_to_str(1e6),
117 help="Sample rate of the output capture files [default=%(default)s]")
118 parser.add_argument("-b", "--band", dest="band", type=gsm_band, default='E-GSM',
119 help="GSM band [default=%(default)s]")
120 parser.add_argument("-f", "--cfile", dest="path", type=str, required=True,
121 help="Path to wideband GSM capture file")
122 parser.add_argument("-c", "--C0", dest="fc", type=eng_float, default=eng_notation.num_to_str(925.2e6), required=True,
123 help="C0 (BCCH) channel frequency in Hz [default=%(default)s]")
124 args = parser.parse_args()
125
126 if not os.path.exists("./gsm_channelizer"):
127 os.makedirs("./gsm_channelizer")
128
129 if not os.path.exists(args.path):
130 raise IOError(args.path + " does not exist")
131
132 if args.samp_rate % args.out_samp_rate != 0:
133 raise Exception("Input sample rate should be multiple of output sample rate in order to get integer decimation.")
134
135 decim = int(args.samp_rate / args.out_samp_rate)
136 print("Input sample rate: " + eng_notation.num_to_str(args.samp_rate))
137 print("Output sample rate: " + eng_notation.num_to_str(args.out_samp_rate))
138 print("==> using decimation of " + str(decim))
139
140 tb = gsm_channelize(channels=args.channel,
141 decim=decim,
142 ch_width=args.width,
143 ch_twidth=args.twidth,
144 fc=args.fc,
145 band=args.band,
146 samp_rate=args.samp_rate,
147 path=args.path)
148 tb.start()
149 tb.wait()
150 print("Done!")