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 | |
Piotr Krysik | 969c0ff | 2016-02-27 17:45:18 +0100 | [diff] [blame] | 59 | class grgsm_channelize(gr.top_block): |
Piotr Krysik | 45b8700 | 2017-09-11 10:32:23 +0200 | [diff] [blame] | 60 | def __init__(self, arfcns, resamp_rate, fc, samp_rate, input_file, dest_dir, data_type="complex"): |
Piotr Krysik | 969c0ff | 2016-02-27 17:45:18 +0100 | [diff] [blame] | 61 | gr.top_block.__init__(self, "grgsm_channelize") |
rpp | 29a39b2 | 2015-09-11 16:33:58 +0200 | [diff] [blame] | 62 | |
| 63 | ################################################## |
| 64 | # Parameters |
| 65 | ################################################## |
Piotr Krysik | 45b8700 | 2017-09-11 10:32:23 +0200 | [diff] [blame] | 66 | self.arfcns = arfcns |
Piotr Krysik | 969c0ff | 2016-02-27 17:45:18 +0100 | [diff] [blame] | 67 | self.resamp_rate = resamp_rate |
rpp | 29a39b2 | 2015-09-11 16:33:58 +0200 | [diff] [blame] | 68 | self.fc = fc |
rpp | 29a39b2 | 2015-09-11 16:33:58 +0200 | [diff] [blame] | 69 | self.samp_rate = samp_rate |
Piotr Krysik | 969c0ff | 2016-02-27 17:45:18 +0100 | [diff] [blame] | 70 | self.blocks_resamplers = {} |
| 71 | self.blocks_rotators = {} |
rpp | 29a39b2 | 2015-09-11 16:33:58 +0200 | [diff] [blame] | 72 | self.blocks_file_sinks = {} |
Piotr Krysik | 969c0ff | 2016-02-27 17:45:18 +0100 | [diff] [blame] | 73 | |
rpp | 29a39b2 | 2015-09-11 16:33:58 +0200 | [diff] [blame] | 74 | ################################################## |
| 75 | # Blocks and connections |
| 76 | ################################################## |
Piotr Krysik | 969c0ff | 2016-02-27 17:45:18 +0100 | [diff] [blame] | 77 | 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 Velichkov | fa2f784 | 2018-09-12 12:28:54 +0300 | [diff] [blame] | 82 | 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 Krysik | 969c0ff | 2016-02-27 17:45:18 +0100 | [diff] [blame] | 86 | elif data_type == "complex": |
| 87 | self.source = blocks.file_source(gr.sizeof_gr_complex, input_file, False) |
rpp | 29a39b2 | 2015-09-11 16:33:58 +0200 | [diff] [blame] | 88 | |
Steve Glass | d3b4049 | 2016-03-03 07:39:57 +1000 | [diff] [blame] | 89 | fc_str = eng_notation.num_to_str(fc) |
Piotr Krysik | 45b8700 | 2017-09-11 10:32:23 +0200 | [diff] [blame] | 90 | print("Extracting channels %s, given that the center frequency is at %s" % (str(arfcns), eng_notation.num_to_str(fc))) |
rpp | 29a39b2 | 2015-09-11 16:33:58 +0200 | [diff] [blame] | 91 | |
Piotr Krysik | 45b8700 | 2017-09-11 10:32:23 +0200 | [diff] [blame] | 92 | for channel in self.arfcns: |
leommxj | ece9fd7 | 2017-09-10 01:02:53 +0800 | [diff] [blame] | 93 | channel_freq = arfcn.arfcn2downlink(channel) |
rpp | 29a39b2 | 2015-09-11 16:33:58 +0200 | [diff] [blame] | 94 | freq_diff = channel_freq - fc |
Steve Glass | d3b4049 | 2016-03-03 07:39:57 +1000 | [diff] [blame] | 95 | 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)) |
rpp | 29a39b2 | 2015-09-11 16:33:58 +0200 | [diff] [blame] | 98 | |
Piotr Krysik | 969c0ff | 2016-02-27 17:45:18 +0100 | [diff] [blame] | 99 | 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) ) |
rpp | 29a39b2 | 2015-09-11 16:33:58 +0200 | [diff] [blame] | 103 | |
Piotr Krysik | 969c0ff | 2016-02-27 17:45:18 +0100 | [diff] [blame] | 104 | 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] | 105 | self.blocks_file_sinks[channel].set_unbuffered(False) |
Piotr Krysik | 969c0ff | 2016-02-27 17:45:18 +0100 | [diff] [blame] | 106 | self.connect((self.blocks_resamplers[channel], 0), (self.blocks_file_sinks[channel], 0)) |
rpp | 29a39b2 | 2015-09-11 16:33:58 +0200 | [diff] [blame] | 107 | |
| 108 | |
| 109 | if __name__ == '__main__': |
Piotr Krysik | 969c0ff | 2016-02-27 17:45:18 +0100 | [diff] [blame] | 110 | parser = ArgumentParser(formatter_class=RawDescriptionHelpFormatter, description='Split wideband a GSM capture into seperate files per ARFCN.', add_help=True, epilog=EXTRA_HELP) |
Piotr Krysik | 45b8700 | 2017-09-11 10:32:23 +0200 | [diff] [blame] | 111 | parser.add_argument(dest="arfcns", type=int, nargs='+', |
| 112 | help="List of ARFCNs (for PCS1900 add 0x8000 (2**15) to the ARFCN number)") |
Piotr Krysik | 13d9e7a | 2016-04-24 15:23:15 +0200 | [diff] [blame] | 113 | 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] | 114 | help="Sample rate of the wideband capture file [default=%(default)s]") |
Piotr Krysik | 969c0ff | 2016-02-27 17:45:18 +0100 | [diff] [blame] | 115 | 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]") |
rpp | 29a39b2 | 2015-09-11 16:33:58 +0200 | [diff] [blame] | 117 | 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 Krysik | 969c0ff | 2016-02-27 17:45:18 +0100 | [diff] [blame] | 119 | parser.add_argument("-i", "--input_file", dest="input_file", type=str, required=True, |
rpp | 29a39b2 | 2015-09-11 16:33:58 +0200 | [diff] [blame] | 120 | help="Path to wideband GSM capture file") |
Vasil Velichkov | fa2f784 | 2018-09-12 12:28:54 +0300 | [diff] [blame] | 121 | parser.add_argument("-t", "--data_type", dest="data_type", type=str, choices=["complex","ishort","ichar"], default="complex", |
Piotr Krysik | 969c0ff | 2016-02-27 17:45:18 +0100 | [diff] [blame] | 122 | 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 | |
rpp | 29a39b2 | 2015-09-11 16:33:58 +0200 | [diff] [blame] | 126 | args = parser.parse_args() |
Piotr Krysik | 45b8700 | 2017-09-11 10:32:23 +0200 | [diff] [blame] | 127 | |
| 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 Krysik | 969c0ff | 2016-02-27 17:45:18 +0100 | [diff] [blame] | 132 | if not os.path.exists(args.input_file): |
| 133 | raise IOError(args.input_file + " does not exist") |
rpp | 29a39b2 | 2015-09-11 16:33:58 +0200 | [diff] [blame] | 134 | |
Piotr Krysik | 969c0ff | 2016-02-27 17:45:18 +0100 | [diff] [blame] | 135 | input_filename, _ = os.path.splitext(os.path.basename(args.input_file)) |
rpp | 29a39b2 | 2015-09-11 16:33:58 +0200 | [diff] [blame] | 136 | |
Piotr Krysik | 969c0ff | 2016-02-27 17:45:18 +0100 | [diff] [blame] | 137 | 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 | |
rpp | 29a39b2 | 2015-09-11 16:33:58 +0200 | [diff] [blame] | 143 | |
| 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 Krysik | 969c0ff | 2016-02-27 17:45:18 +0100 | [diff] [blame] | 147 | resamp_rate = args.out_samp_rate / args.samp_rate |
rpp | 29a39b2 | 2015-09-11 16:33:58 +0200 | [diff] [blame] | 148 | 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 Krysik | 969c0ff | 2016-02-27 17:45:18 +0100 | [diff] [blame] | 150 | print("==> using resample rate of " + str(resamp_rate)) |
rpp | 29a39b2 | 2015-09-11 16:33:58 +0200 | [diff] [blame] | 151 | |
Piotr Krysik | 45b8700 | 2017-09-11 10:32:23 +0200 | [diff] [blame] | 152 | tb = grgsm_channelize(arfcns=args.arfcns, |
Piotr Krysik | 969c0ff | 2016-02-27 17:45:18 +0100 | [diff] [blame] | 153 | resamp_rate=resamp_rate, |
rpp | 29a39b2 | 2015-09-11 16:33:58 +0200 | [diff] [blame] | 154 | fc=args.fc, |
rpp | 29a39b2 | 2015-09-11 16:33:58 +0200 | [diff] [blame] | 155 | samp_rate=args.samp_rate, |
Piotr Krysik | 969c0ff | 2016-02-27 17:45:18 +0100 | [diff] [blame] | 156 | input_file=args.input_file, |
| 157 | dest_dir=args.dest_dir, |
| 158 | data_type=args.data_type |
| 159 | ) |
rpp | 29a39b2 | 2015-09-11 16:33:58 +0200 | [diff] [blame] | 160 | tb.start() |
| 161 | tb.wait() |
| 162 | print("Done!") |