rpp | 29a39b2 | 2015-09-11 16:33:58 +0200 | [diff] [blame] | 1 | #!/usr/bin/env python2 |
| 2 | # -*- coding: utf-8 -*- |
| 3 | # @file |
Piotr Krysik | a6268a5 | 2017-08-23 16:02:19 +0200 | [diff] [blame] | 4 | # @author (C) 2015 by Pieter Robyns <pieter.robyns@uhasselt.be> |
rpp | 29a39b2 | 2015-09-11 16:33:58 +0200 | [diff] [blame] | 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 | |
| 32 | from gnuradio import blocks |
| 33 | from gnuradio import eng_notation |
| 34 | from gnuradio import filter |
| 35 | from gnuradio import gr |
| 36 | from gnuradio.eng_option import eng_option |
| 37 | from gnuradio.filter import firdes |
| 38 | from argparse import ArgumentParser, ArgumentTypeError, RawDescriptionHelpFormatter |
| 39 | import grgsm.arfcn as arfcn |
Piotr Krysik | 969c0ff | 2016-02-27 17:45:18 +0100 | [diff] [blame] | 40 | from gnuradio.filter import pfb |
| 41 | import math |
rpp | 29a39b2 | 2015-09-11 16:33:58 +0200 | [diff] [blame] | 42 | import os |
| 43 | |
| 44 | EXTRA_HELP = """ |
| 45 | Example usage: |
Piotr Krysik | 13d9e7a | 2016-04-24 15:23:15 +0200 | [diff] [blame] | 46 | grgsm_channelize.py -c my_wideband_capture.cfile -f 925.2e6 990 991 992 993 994 995 1019 1020 1021 1022 1023 |
rpp | 29a39b2 | 2015-09-11 16:33:58 +0200 | [diff] [blame] | 47 | |
| 48 | The above example will channelize my_wideband_capture.cfile, in this case a cfile captured at |
Piotr Krysik | 13d9e7a | 2016-04-24 15:23:15 +0200 | [diff] [blame] | 49 | 925.2 MHz centered (ARFCN 975) and 2 Msps. As a result, 12 files will be generated for |
rpp | 29a39b2 | 2015-09-11 16:33:58 +0200 | [diff] [blame] | 50 | ARFCNs 975 - 1023 at 1 Msps each. |
| 51 | """ |
| 52 | |
| 53 | def 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 | |
| 59 | def 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 Krysik | 969c0ff | 2016-02-27 17:45:18 +0100 | [diff] [blame] | 66 | class 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") |
rpp | 29a39b2 | 2015-09-11 16:33:58 +0200 | [diff] [blame] | 69 | |
| 70 | ################################################## |
| 71 | # Parameters |
| 72 | ################################################## |
| 73 | self.channels = channels |
Piotr Krysik | 969c0ff | 2016-02-27 17:45:18 +0100 | [diff] [blame] | 74 | self.resamp_rate = resamp_rate |
rpp | 29a39b2 | 2015-09-11 16:33:58 +0200 | [diff] [blame] | 75 | self.fc = fc |
| 76 | self.band = band |
| 77 | self.samp_rate = samp_rate |
Piotr Krysik | 969c0ff | 2016-02-27 17:45:18 +0100 | [diff] [blame] | 78 | self.blocks_resamplers = {} |
| 79 | self.blocks_rotators = {} |
rpp | 29a39b2 | 2015-09-11 16:33:58 +0200 | [diff] [blame] | 80 | self.blocks_file_sinks = {} |
Piotr Krysik | 969c0ff | 2016-02-27 17:45:18 +0100 | [diff] [blame] | 81 | |
rpp | 29a39b2 | 2015-09-11 16:33:58 +0200 | [diff] [blame] | 82 | ################################################## |
| 83 | # Blocks and connections |
| 84 | ################################################## |
Piotr Krysik | 969c0ff | 2016-02-27 17:45:18 +0100 | [diff] [blame] | 85 | 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) |
rpp | 29a39b2 | 2015-09-11 16:33:58 +0200 | [diff] [blame] | 92 | |
Steve Glass | d3b4049 | 2016-03-03 07:39:57 +1000 | [diff] [blame] | 93 | fc_str = eng_notation.num_to_str(fc) |
Piotr Krysik | 30ea7dd | 2016-03-11 09:04:03 +0100 | [diff] [blame] | 94 | print("Extracting channels %s, given that the center frequency is at %s" % (str(channels), eng_notation.num_to_str(fc))) |
rpp | 29a39b2 | 2015-09-11 16:33:58 +0200 | [diff] [blame] | 95 | |
| 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 Glass | d3b4049 | 2016-03-03 07:39:57 +1000 | [diff] [blame] | 102 | 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)) |
rpp | 29a39b2 | 2015-09-11 16:33:58 +0200 | [diff] [blame] | 105 | |
Piotr Krysik | 969c0ff | 2016-02-27 17:45:18 +0100 | [diff] [blame] | 106 | 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) ) |
rpp | 29a39b2 | 2015-09-11 16:33:58 +0200 | [diff] [blame] | 110 | |
Piotr Krysik | 969c0ff | 2016-02-27 17:45:18 +0100 | [diff] [blame] | 111 | self.blocks_file_sinks[channel] = blocks.file_sink(gr.sizeof_gr_complex, dest_dir+"/out_" + str(channel) + ".cfile", False) |
rpp | 29a39b2 | 2015-09-11 16:33:58 +0200 | [diff] [blame] | 112 | self.blocks_file_sinks[channel].set_unbuffered(False) |
Piotr Krysik | 969c0ff | 2016-02-27 17:45:18 +0100 | [diff] [blame] | 113 | self.connect((self.blocks_resamplers[channel], 0), (self.blocks_file_sinks[channel], 0)) |
rpp | 29a39b2 | 2015-09-11 16:33:58 +0200 | [diff] [blame] | 114 | |
| 115 | |
| 116 | if __name__ == '__main__': |
Piotr Krysik | 969c0ff | 2016-02-27 17:45:18 +0100 | [diff] [blame] | 117 | parser = ArgumentParser(formatter_class=RawDescriptionHelpFormatter, description='Split wideband a GSM capture into seperate files per ARFCN.', add_help=True, epilog=EXTRA_HELP) |
rpp | 29a39b2 | 2015-09-11 16:33:58 +0200 | [diff] [blame] | 118 | parser.add_argument(dest="channel", type=int, nargs='+', |
| 119 | help="List of ARFCNs") |
Piotr Krysik | 13d9e7a | 2016-04-24 15:23:15 +0200 | [diff] [blame] | 120 | parser.add_argument("-s", "--samp-rate", dest="samp_rate", type=eng_float, default=eng_notation.num_to_str(2e6), |
rpp | 29a39b2 | 2015-09-11 16:33:58 +0200 | [diff] [blame] | 121 | help="Sample rate of the wideband capture file [default=%(default)s]") |
Piotr Krysik | 969c0ff | 2016-02-27 17:45:18 +0100 | [diff] [blame] | 122 | 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 |
rpp | 29a39b2 | 2015-09-11 16:33:58 +0200 | [diff] [blame] | 126 | 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 Krysik | 969c0ff | 2016-02-27 17:45:18 +0100 | [diff] [blame] | 128 | parser.add_argument("-i", "--input_file", dest="input_file", type=str, required=True, |
rpp | 29a39b2 | 2015-09-11 16:33:58 +0200 | [diff] [blame] | 129 | help="Path to wideband GSM capture file") |
Piotr Krysik | 969c0ff | 2016-02-27 17:45:18 +0100 | [diff] [blame] | 130 | 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 | |
rpp | 29a39b2 | 2015-09-11 16:33:58 +0200 | [diff] [blame] | 135 | args = parser.parse_args() |
Piotr Krysik | 969c0ff | 2016-02-27 17:45:18 +0100 | [diff] [blame] | 136 | |
| 137 | if not os.path.exists(args.input_file): |
| 138 | raise IOError(args.input_file + " does not exist") |
rpp | 29a39b2 | 2015-09-11 16:33:58 +0200 | [diff] [blame] | 139 | |
Piotr Krysik | 969c0ff | 2016-02-27 17:45:18 +0100 | [diff] [blame] | 140 | input_filename, _ = os.path.splitext(os.path.basename(args.input_file)) |
rpp | 29a39b2 | 2015-09-11 16:33:58 +0200 | [diff] [blame] | 141 | |
Piotr Krysik | 969c0ff | 2016-02-27 17:45:18 +0100 | [diff] [blame] | 142 | 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 | |
rpp | 29a39b2 | 2015-09-11 16:33:58 +0200 | [diff] [blame] | 148 | |
| 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 Krysik | 969c0ff | 2016-02-27 17:45:18 +0100 | [diff] [blame] | 152 | resamp_rate = args.out_samp_rate / args.samp_rate |
rpp | 29a39b2 | 2015-09-11 16:33:58 +0200 | [diff] [blame] | 153 | 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 Krysik | 969c0ff | 2016-02-27 17:45:18 +0100 | [diff] [blame] | 155 | print("==> using resample rate of " + str(resamp_rate)) |
rpp | 29a39b2 | 2015-09-11 16:33:58 +0200 | [diff] [blame] | 156 | |
Piotr Krysik | 969c0ff | 2016-02-27 17:45:18 +0100 | [diff] [blame] | 157 | tb = grgsm_channelize(channels=args.channel, |
| 158 | resamp_rate=resamp_rate, |
rpp | 29a39b2 | 2015-09-11 16:33:58 +0200 | [diff] [blame] | 159 | fc=args.fc, |
| 160 | band=args.band, |
| 161 | samp_rate=args.samp_rate, |
Piotr Krysik | 969c0ff | 2016-02-27 17:45:18 +0100 | [diff] [blame] | 162 | input_file=args.input_file, |
| 163 | dest_dir=args.dest_dir, |
| 164 | data_type=args.data_type |
| 165 | ) |
rpp | 29a39b2 | 2015-09-11 16:33:58 +0200 | [diff] [blame] | 166 | tb.start() |
| 167 | tb.wait() |
| 168 | print("Done!") |