blob: 5eaeb3bdad35f0a41dbc36954c9a166665a98ca4 [file] [log] [blame]
rpp29a39b22015-09-11 16:33:58 +02001#!/usr/bin/env python2
2# -*- coding: utf-8 -*-
3# @file
Piotr Krysika6268a52017-08-23 16:02:19 +02004# @author (C) 2015 by Pieter Robyns <pieter.robyns@uhasselt.be>
rpp29a39b22015-09-11 16:33:58 +02005# @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 Krysik13d9e7a2016-04-24 15:23:15 +020046grgsm_channelize.py -c my_wideband_capture.cfile -f 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
Piotr Krysik13d9e7a2016-04-24 15:23:15 +020049925.2 MHz centered (ARFCN 975) and 2 Msps. As a result, 12 files will be generated for
rpp29a39b22015-09-11 16:33:58 +020050ARFCNs 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
Piotr Krysik969c0ff2016-02-27 17:45:18 +010059class grgsm_channelize(gr.top_block):
Piotr Krysik45b87002017-09-11 10:32:23 +020060 def __init__(self, arfcns, resamp_rate, fc, samp_rate, input_file, dest_dir, data_type="complex"):
Piotr Krysik969c0ff2016-02-27 17:45:18 +010061 gr.top_block.__init__(self, "grgsm_channelize")
rpp29a39b22015-09-11 16:33:58 +020062
63 ##################################################
64 # Parameters
65 ##################################################
Piotr Krysik45b87002017-09-11 10:32:23 +020066 self.arfcns = arfcns
Piotr Krysik969c0ff2016-02-27 17:45:18 +010067 self.resamp_rate = resamp_rate
rpp29a39b22015-09-11 16:33:58 +020068 self.fc = fc
rpp29a39b22015-09-11 16:33:58 +020069 self.samp_rate = samp_rate
Piotr Krysik969c0ff2016-02-27 17:45:18 +010070 self.blocks_resamplers = {}
71 self.blocks_rotators = {}
rpp29a39b22015-09-11 16:33:58 +020072 self.blocks_file_sinks = {}
Piotr Krysik969c0ff2016-02-27 17:45:18 +010073
rpp29a39b22015-09-11 16:33:58 +020074 ##################################################
75 # Blocks and connections
76 ##################################################
Piotr Krysik969c0ff2016-02-27 17:45:18 +010077 self.source = None
78 if data_type == "ishort":
79 self.blocks_file_source = blocks.file_source(gr.sizeof_short, input_file, False)
80 self.source = blocks.interleaved_short_to_complex(False, False)
81 self.connect((self.blocks_file_source, 0), (self.source, 0))
Vasil Velichkovfa2f7842018-09-12 12:28:54 +030082 elif data_type == "ichar":
83 self.blocks_file_source = blocks.file_source(gr.sizeof_char, input_file, False)
84 self.source = blocks.interleaved_char_to_complex(False)
85 self.connect((self.blocks_file_source, 0), (self.source, 0))
Piotr Krysik969c0ff2016-02-27 17:45:18 +010086 elif data_type == "complex":
87 self.source = blocks.file_source(gr.sizeof_gr_complex, input_file, False)
rpp29a39b22015-09-11 16:33:58 +020088
Steve Glassd3b40492016-03-03 07:39:57 +100089 fc_str = eng_notation.num_to_str(fc)
Piotr Krysik45b87002017-09-11 10:32:23 +020090 print("Extracting channels %s, given that the center frequency is at %s" % (str(arfcns), eng_notation.num_to_str(fc)))
rpp29a39b22015-09-11 16:33:58 +020091
Piotr Krysik45b87002017-09-11 10:32:23 +020092 for channel in self.arfcns:
leommxjece9fd72017-09-10 01:02:53 +080093 channel_freq = arfcn.arfcn2downlink(channel)
rpp29a39b22015-09-11 16:33:58 +020094 freq_diff = channel_freq - fc
Steve Glassd3b40492016-03-03 07:39:57 +100095 freq_diff_str = "+" if 0 <= freq_diff else ""
96 freq_diff_str += eng_notation.num_to_str(freq_diff)
97 print("ARFCN %d is at %sHz %sHz" % (channel, fc_str, freq_diff_str))
rpp29a39b22015-09-11 16:33:58 +020098
Piotr Krysik969c0ff2016-02-27 17:45:18 +010099 self.blocks_resamplers[channel] = pfb.arb_resampler_ccf( resamp_rate, taps=None, flt_size=32)
100 self.blocks_rotators[channel] = blocks.rotator_cc(-2*math.pi*(freq_diff)/samp_rate)
101 self.connect( (self.source, 0), (self.blocks_rotators[channel], 0) )
102 self.connect( (self.blocks_rotators[channel], 0), (self.blocks_resamplers[channel], 0) )
rpp29a39b22015-09-11 16:33:58 +0200103
Piotr Krysik969c0ff2016-02-27 17:45:18 +0100104 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 +0200105 self.blocks_file_sinks[channel].set_unbuffered(False)
Piotr Krysik969c0ff2016-02-27 17:45:18 +0100106 self.connect((self.blocks_resamplers[channel], 0), (self.blocks_file_sinks[channel], 0))
rpp29a39b22015-09-11 16:33:58 +0200107
108
109if __name__ == '__main__':
Piotr Krysik969c0ff2016-02-27 17:45:18 +0100110 parser = ArgumentParser(formatter_class=RawDescriptionHelpFormatter, description='Split wideband a GSM capture into seperate files per ARFCN.', add_help=True, epilog=EXTRA_HELP)
Piotr Krysik45b87002017-09-11 10:32:23 +0200111 parser.add_argument(dest="arfcns", type=int, nargs='+',
112 help="List of ARFCNs (for PCS1900 add 0x8000 (2**15) to the ARFCN number)")
Piotr Krysik13d9e7a2016-04-24 15:23:15 +0200113 parser.add_argument("-s", "--samp-rate", dest="samp_rate", type=eng_float, default=eng_notation.num_to_str(2e6),
rpp29a39b22015-09-11 16:33:58 +0200114 help="Sample rate of the wideband capture file [default=%(default)s]")
Piotr Krysik969c0ff2016-02-27 17:45:18 +0100115 parser.add_argument("-f", "--fc", dest="fc", type=eng_float, default=eng_notation.num_to_str(935e6), required=True,
116 help="Carrier frequency in Hz [default=%(default)s]")
rpp29a39b22015-09-11 16:33:58 +0200117 parser.add_argument("-o", "--out-samp-rate", dest="out_samp_rate", type=eng_float, default=eng_notation.num_to_str(1e6),
118 help="Sample rate of the output capture files [default=%(default)s]")
Piotr Krysik969c0ff2016-02-27 17:45:18 +0100119 parser.add_argument("-i", "--input_file", dest="input_file", type=str, required=True,
rpp29a39b22015-09-11 16:33:58 +0200120 help="Path to wideband GSM capture file")
Vasil Velichkovfa2f7842018-09-12 12:28:54 +0300121 parser.add_argument("-t", "--data_type", dest="data_type", type=str, choices=["complex","ishort","ichar"], default="complex",
Piotr Krysik969c0ff2016-02-27 17:45:18 +0100122 help="Type of the input file [default=%(default)s]")
123 parser.add_argument("-d", "--dest_dir", dest="dest_dir", type=str,
124 help="Destination directory - if not given defaults to input file name without extension")
125
rpp29a39b22015-09-11 16:33:58 +0200126 args = parser.parse_args()
Piotr Krysik45b87002017-09-11 10:32:23 +0200127
128 for ch in args.arfcns:
129 if not arfcn.is_valid_arfcn(ch):
130 parser.error("ARFCN "+str(ch)+" is not valid\n")
131
Piotr Krysik969c0ff2016-02-27 17:45:18 +0100132 if not os.path.exists(args.input_file):
133 raise IOError(args.input_file + " does not exist")
rpp29a39b22015-09-11 16:33:58 +0200134
Piotr Krysik969c0ff2016-02-27 17:45:18 +0100135 input_filename, _ = os.path.splitext(os.path.basename(args.input_file))
rpp29a39b22015-09-11 16:33:58 +0200136
Piotr Krysik969c0ff2016-02-27 17:45:18 +0100137 if args.dest_dir is None:
138 args.dest_dir = input_filename
139
140 if not os.path.exists("./"+args.dest_dir):
141 os.makedirs("./"+args.dest_dir)
142
rpp29a39b22015-09-11 16:33:58 +0200143
144 if args.samp_rate % args.out_samp_rate != 0:
145 raise Exception("Input sample rate should be multiple of output sample rate in order to get integer decimation.")
146
Piotr Krysik969c0ff2016-02-27 17:45:18 +0100147 resamp_rate = args.out_samp_rate / args.samp_rate
rpp29a39b22015-09-11 16:33:58 +0200148 print("Input sample rate: " + eng_notation.num_to_str(args.samp_rate))
149 print("Output sample rate: " + eng_notation.num_to_str(args.out_samp_rate))
Piotr Krysik969c0ff2016-02-27 17:45:18 +0100150 print("==> using resample rate of " + str(resamp_rate))
rpp29a39b22015-09-11 16:33:58 +0200151
Piotr Krysik45b87002017-09-11 10:32:23 +0200152 tb = grgsm_channelize(arfcns=args.arfcns,
Piotr Krysik969c0ff2016-02-27 17:45:18 +0100153 resamp_rate=resamp_rate,
rpp29a39b22015-09-11 16:33:58 +0200154 fc=args.fc,
rpp29a39b22015-09-11 16:33:58 +0200155 samp_rate=args.samp_rate,
Piotr Krysik969c0ff2016-02-27 17:45:18 +0100156 input_file=args.input_file,
157 dest_dir=args.dest_dir,
158 data_type=args.data_type
159 )
rpp29a39b22015-09-11 16:33:58 +0200160 tb.start()
161 tb.wait()
162 print("Done!")