blob: 9b00bd74d1c33727d64a231a1a8f0b17ac8a6579 [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))
82 elif data_type == "complex":
83 self.source = blocks.file_source(gr.sizeof_gr_complex, input_file, False)
rpp29a39b22015-09-11 16:33:58 +020084
Steve Glassd3b40492016-03-03 07:39:57 +100085 fc_str = eng_notation.num_to_str(fc)
Piotr Krysik45b87002017-09-11 10:32:23 +020086 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 +020087
Piotr Krysik45b87002017-09-11 10:32:23 +020088 for channel in self.arfcns:
leommxjece9fd72017-09-10 01:02:53 +080089 channel_freq = arfcn.arfcn2downlink(channel)
rpp29a39b22015-09-11 16:33:58 +020090 freq_diff = channel_freq - fc
Steve Glassd3b40492016-03-03 07:39:57 +100091 freq_diff_str = "+" if 0 <= freq_diff else ""
92 freq_diff_str += eng_notation.num_to_str(freq_diff)
93 print("ARFCN %d is at %sHz %sHz" % (channel, fc_str, freq_diff_str))
rpp29a39b22015-09-11 16:33:58 +020094
Piotr Krysik969c0ff2016-02-27 17:45:18 +010095 self.blocks_resamplers[channel] = pfb.arb_resampler_ccf( resamp_rate, taps=None, flt_size=32)
96 self.blocks_rotators[channel] = blocks.rotator_cc(-2*math.pi*(freq_diff)/samp_rate)
97 self.connect( (self.source, 0), (self.blocks_rotators[channel], 0) )
98 self.connect( (self.blocks_rotators[channel], 0), (self.blocks_resamplers[channel], 0) )
rpp29a39b22015-09-11 16:33:58 +020099
Piotr Krysik969c0ff2016-02-27 17:45:18 +0100100 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 +0200101 self.blocks_file_sinks[channel].set_unbuffered(False)
Piotr Krysik969c0ff2016-02-27 17:45:18 +0100102 self.connect((self.blocks_resamplers[channel], 0), (self.blocks_file_sinks[channel], 0))
rpp29a39b22015-09-11 16:33:58 +0200103
104
105if __name__ == '__main__':
Piotr Krysik969c0ff2016-02-27 17:45:18 +0100106 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 +0200107 parser.add_argument(dest="arfcns", type=int, nargs='+',
108 help="List of ARFCNs (for PCS1900 add 0x8000 (2**15) to the ARFCN number)")
Piotr Krysik13d9e7a2016-04-24 15:23:15 +0200109 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 +0200110 help="Sample rate of the wideband capture file [default=%(default)s]")
Piotr Krysik969c0ff2016-02-27 17:45:18 +0100111 parser.add_argument("-f", "--fc", dest="fc", type=eng_float, default=eng_notation.num_to_str(935e6), required=True,
112 help="Carrier frequency in Hz [default=%(default)s]")
rpp29a39b22015-09-11 16:33:58 +0200113 parser.add_argument("-o", "--out-samp-rate", dest="out_samp_rate", type=eng_float, default=eng_notation.num_to_str(1e6),
114 help="Sample rate of the output capture files [default=%(default)s]")
Piotr Krysik969c0ff2016-02-27 17:45:18 +0100115 parser.add_argument("-i", "--input_file", dest="input_file", type=str, required=True,
rpp29a39b22015-09-11 16:33:58 +0200116 help="Path to wideband GSM capture file")
Piotr Krysik969c0ff2016-02-27 17:45:18 +0100117 parser.add_argument("-t", "--data_type", dest="data_type", type=str, choices=["complex","ishort"], default="complex",
118 help="Type of the input file [default=%(default)s]")
119 parser.add_argument("-d", "--dest_dir", dest="dest_dir", type=str,
120 help="Destination directory - if not given defaults to input file name without extension")
121
rpp29a39b22015-09-11 16:33:58 +0200122 args = parser.parse_args()
Piotr Krysik45b87002017-09-11 10:32:23 +0200123
124 for ch in args.arfcns:
125 if not arfcn.is_valid_arfcn(ch):
126 parser.error("ARFCN "+str(ch)+" is not valid\n")
127
Piotr Krysik969c0ff2016-02-27 17:45:18 +0100128 if not os.path.exists(args.input_file):
129 raise IOError(args.input_file + " does not exist")
rpp29a39b22015-09-11 16:33:58 +0200130
Piotr Krysik969c0ff2016-02-27 17:45:18 +0100131 input_filename, _ = os.path.splitext(os.path.basename(args.input_file))
rpp29a39b22015-09-11 16:33:58 +0200132
Piotr Krysik969c0ff2016-02-27 17:45:18 +0100133 if args.dest_dir is None:
134 args.dest_dir = input_filename
135
136 if not os.path.exists("./"+args.dest_dir):
137 os.makedirs("./"+args.dest_dir)
138
rpp29a39b22015-09-11 16:33:58 +0200139
140 if args.samp_rate % args.out_samp_rate != 0:
141 raise Exception("Input sample rate should be multiple of output sample rate in order to get integer decimation.")
142
Piotr Krysik969c0ff2016-02-27 17:45:18 +0100143 resamp_rate = args.out_samp_rate / args.samp_rate
rpp29a39b22015-09-11 16:33:58 +0200144 print("Input sample rate: " + eng_notation.num_to_str(args.samp_rate))
145 print("Output sample rate: " + eng_notation.num_to_str(args.out_samp_rate))
Piotr Krysik969c0ff2016-02-27 17:45:18 +0100146 print("==> using resample rate of " + str(resamp_rate))
rpp29a39b22015-09-11 16:33:58 +0200147
Piotr Krysik45b87002017-09-11 10:32:23 +0200148 tb = grgsm_channelize(arfcns=args.arfcns,
Piotr Krysik969c0ff2016-02-27 17:45:18 +0100149 resamp_rate=resamp_rate,
rpp29a39b22015-09-11 16:33:58 +0200150 fc=args.fc,
rpp29a39b22015-09-11 16:33:58 +0200151 samp_rate=args.samp_rate,
Piotr Krysik969c0ff2016-02-27 17:45:18 +0100152 input_file=args.input_file,
153 dest_dir=args.dest_dir,
154 data_type=args.data_type
155 )
rpp29a39b22015-09-11 16:33:58 +0200156 tb.start()
157 tb.wait()
158 print("Done!")