blob: 5d1184254ac1852545407b0e044f37534b03175d [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
Piotr Krysik969c0ff2016-02-27 17:45:18 +010040from gnuradio.filter import pfb
41import math
rpp29a39b22015-09-11 16:33:58 +020042import os
43
44EXTRA_HELP = """
45Example usage:
Piotr Krysik969c0ff2016-02-27 17:45:18 +010046grgsm_channelize.py -f my_wideband_capture.cfile -c 925.2e6 990 991 992 993 994 995 1019 1020 1021 1022 1023
rpp29a39b22015-09-11 16:33:58 +020047
48The above example will channelize my_wideband_capture.cfile, in this case a cfile captured at
49925.2 MHz centered (ARFCN 975) and 20 Msps. As a result, 12 files will be generated for
50ARFCNs 975 - 1023 at 1 Msps each.
51"""
52
53def eng_float(value):
54 try:
55 return eng_notation.str_to_num(value)
56 except:
57 raise ArgumentTypeError("invalid engineering notation value: {0}".format(value))
58
59def gsm_band(value):
60 choices = arfcn.get_bands()
61 if value in choices:
62 return value
63 else:
64 raise ArgumentTypeError("invalid GSM band: {0}. Possible choices are: {1}".format(value, choices))
65
Piotr Krysik969c0ff2016-02-27 17:45:18 +010066class grgsm_channelize(gr.top_block):
67 def __init__(self, channels, resamp_rate, fc, band, samp_rate, input_file, dest_dir, data_type="complex"):
68 gr.top_block.__init__(self, "grgsm_channelize")
rpp29a39b22015-09-11 16:33:58 +020069
70 ##################################################
71 # Parameters
72 ##################################################
73 self.channels = channels
Piotr Krysik969c0ff2016-02-27 17:45:18 +010074 self.resamp_rate = resamp_rate
rpp29a39b22015-09-11 16:33:58 +020075 self.fc = fc
76 self.band = band
77 self.samp_rate = samp_rate
Piotr Krysik969c0ff2016-02-27 17:45:18 +010078 self.blocks_resamplers = {}
79 self.blocks_rotators = {}
rpp29a39b22015-09-11 16:33:58 +020080 self.blocks_file_sinks = {}
Piotr Krysik969c0ff2016-02-27 17:45:18 +010081
rpp29a39b22015-09-11 16:33:58 +020082 ##################################################
83 # Blocks and connections
84 ##################################################
Piotr Krysik969c0ff2016-02-27 17:45:18 +010085 self.source = None
86 if data_type == "ishort":
87 self.blocks_file_source = blocks.file_source(gr.sizeof_short, input_file, False)
88 self.source = blocks.interleaved_short_to_complex(False, False)
89 self.connect((self.blocks_file_source, 0), (self.source, 0))
90 elif data_type == "complex":
91 self.source = blocks.file_source(gr.sizeof_gr_complex, input_file, False)
rpp29a39b22015-09-11 16:33:58 +020092
Steve Glassd3b40492016-03-03 07:39:57 +100093 fc_str = eng_notation.num_to_str(fc)
94 print("Extracting channels %s, given center frequency at %sHz (ARFCN %d)" % (str(ca), fc_str, center_arfcn))
rpp29a39b22015-09-11 16:33:58 +020095
96 for channel in channels:
97 channel_freq = arfcn.arfcn2downlink(channel, band)
98 if channel_freq is None:
99 print("Warning: invalid ARFCN %d for band %s" % (channel, band))
100 continue
101 freq_diff = channel_freq - fc
Steve Glassd3b40492016-03-03 07:39:57 +1000102 freq_diff_str = "+" if 0 <= freq_diff else ""
103 freq_diff_str += eng_notation.num_to_str(freq_diff)
104 print("ARFCN %d is at %sHz %sHz" % (channel, fc_str, freq_diff_str))
rpp29a39b22015-09-11 16:33:58 +0200105
Piotr Krysik969c0ff2016-02-27 17:45:18 +0100106 self.blocks_resamplers[channel] = pfb.arb_resampler_ccf( resamp_rate, taps=None, flt_size=32)
107 self.blocks_rotators[channel] = blocks.rotator_cc(-2*math.pi*(freq_diff)/samp_rate)
108 self.connect( (self.source, 0), (self.blocks_rotators[channel], 0) )
109 self.connect( (self.blocks_rotators[channel], 0), (self.blocks_resamplers[channel], 0) )
rpp29a39b22015-09-11 16:33:58 +0200110
Piotr Krysik969c0ff2016-02-27 17:45:18 +0100111 self.blocks_file_sinks[channel] = blocks.file_sink(gr.sizeof_gr_complex, dest_dir+"/out_" + str(channel) + ".cfile", False)
rpp29a39b22015-09-11 16:33:58 +0200112 self.blocks_file_sinks[channel].set_unbuffered(False)
Piotr Krysik969c0ff2016-02-27 17:45:18 +0100113 self.connect((self.blocks_resamplers[channel], 0), (self.blocks_file_sinks[channel], 0))
rpp29a39b22015-09-11 16:33:58 +0200114
115
116if __name__ == '__main__':
Piotr Krysik969c0ff2016-02-27 17:45:18 +0100117 parser = ArgumentParser(formatter_class=RawDescriptionHelpFormatter, description='Split wideband a GSM capture into seperate files per ARFCN.', add_help=True, epilog=EXTRA_HELP)
rpp29a39b22015-09-11 16:33:58 +0200118 parser.add_argument(dest="channel", type=int, nargs='+',
119 help="List of ARFCNs")
rpp29a39b22015-09-11 16:33:58 +0200120 parser.add_argument("-s", "--samp-rate", dest="samp_rate", type=eng_float, default=eng_notation.num_to_str(2e7),
121 help="Sample rate of the wideband capture file [default=%(default)s]")
Piotr Krysik969c0ff2016-02-27 17:45:18 +0100122 parser.add_argument("-f", "--fc", dest="fc", type=eng_float, default=eng_notation.num_to_str(935e6), required=True,
123 help="Carrier frequency in Hz [default=%(default)s]")
124 parser.add_argument("-b", "--band", dest="band", type=gsm_band, default='E-GSM',
125 help="GSM band [default=%(default)s]") #TODO: add automatic discovery based on fc
rpp29a39b22015-09-11 16:33:58 +0200126 parser.add_argument("-o", "--out-samp-rate", dest="out_samp_rate", type=eng_float, default=eng_notation.num_to_str(1e6),
127 help="Sample rate of the output capture files [default=%(default)s]")
Piotr Krysik969c0ff2016-02-27 17:45:18 +0100128 parser.add_argument("-i", "--input_file", dest="input_file", type=str, required=True,
rpp29a39b22015-09-11 16:33:58 +0200129 help="Path to wideband GSM capture file")
Piotr Krysik969c0ff2016-02-27 17:45:18 +0100130 parser.add_argument("-t", "--data_type", dest="data_type", type=str, choices=["complex","ishort"], default="complex",
131 help="Type of the input file [default=%(default)s]")
132 parser.add_argument("-d", "--dest_dir", dest="dest_dir", type=str,
133 help="Destination directory - if not given defaults to input file name without extension")
134
rpp29a39b22015-09-11 16:33:58 +0200135 args = parser.parse_args()
Piotr Krysik969c0ff2016-02-27 17:45:18 +0100136
137 if not os.path.exists(args.input_file):
138 raise IOError(args.input_file + " does not exist")
rpp29a39b22015-09-11 16:33:58 +0200139
Piotr Krysik969c0ff2016-02-27 17:45:18 +0100140 input_filename, _ = os.path.splitext(os.path.basename(args.input_file))
rpp29a39b22015-09-11 16:33:58 +0200141
Piotr Krysik969c0ff2016-02-27 17:45:18 +0100142 if args.dest_dir is None:
143 args.dest_dir = input_filename
144
145 if not os.path.exists("./"+args.dest_dir):
146 os.makedirs("./"+args.dest_dir)
147
rpp29a39b22015-09-11 16:33:58 +0200148
149 if args.samp_rate % args.out_samp_rate != 0:
150 raise Exception("Input sample rate should be multiple of output sample rate in order to get integer decimation.")
151
Piotr Krysik969c0ff2016-02-27 17:45:18 +0100152 resamp_rate = args.out_samp_rate / args.samp_rate
rpp29a39b22015-09-11 16:33:58 +0200153 print("Input sample rate: " + eng_notation.num_to_str(args.samp_rate))
154 print("Output sample rate: " + eng_notation.num_to_str(args.out_samp_rate))
Piotr Krysik969c0ff2016-02-27 17:45:18 +0100155 print("==> using resample rate of " + str(resamp_rate))
rpp29a39b22015-09-11 16:33:58 +0200156
Piotr Krysik969c0ff2016-02-27 17:45:18 +0100157 tb = grgsm_channelize(channels=args.channel,
158 resamp_rate=resamp_rate,
rpp29a39b22015-09-11 16:33:58 +0200159 fc=args.fc,
160 band=args.band,
161 samp_rate=args.samp_rate,
Piotr Krysik969c0ff2016-02-27 17:45:18 +0100162 input_file=args.input_file,
163 dest_dir=args.dest_dir,
164 data_type=args.data_type
165 )
rpp29a39b22015-09-11 16:33:58 +0200166 tb.start()
167 tb.wait()
168 print("Done!")