summaryrefslogtreecommitdiff
path: root/gr-radar/src/python/split_files.py
blob: a604c07793ef0c422d8edc627f4251d11489de62 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#!/usr/bin/env python

from gnuradio import gr, gru, eng_notation, optfir
from gnuradio.eng_option import eng_option
from optparse import OptionParser
import os.path
import re

plot = None

def split_filename(src_filename):
    s = os.path.splitext(src_filename)[0]     # drop extension
    date, time, freq, dec, nchan = s.split('-')
    return (date, time, freq, int(dec), int(nchan))


def make_filename(date, time, freq, n, short_output):
    if short_output:
        return '-'.join((date, time, freq)) + '.s%02d' % (n,)
    else:
        return '-'.join((date, time, freq)) + '.c%02d' % (n,)

class my_graph(gr.flow_graph):

    def __init__(self, src_filename, short_output):
        """
        Deinterleave file, filter and decimate by 4, and write out
        a separate file for each input channel.

        The input file is the raw output of nchannels of short USRP data.
        That is, they are interleaved short I&Q for each channel.
        """
        global plot
        
        gr.flow_graph.__init__(self)

        decim = 4

        (date, time, freq, dec, nchan) = split_filename(src_filename)

        src = gr.file_source(gr.sizeof_short, src_filename)

        # convert stream of interleaved shorts to a stream of complex
        s2c = gr.interleaved_short_to_complex()

        # deinterleave complexes into separate streams
        deint = gr.deinterleave(gr.sizeof_gr_complex)

        self.connect(src, s2c, deint)

        taps = optfir.low_pass(1,       # gain
                               1,       # sampling rate
                               0.100,   # passband cutoff
                               0.125,   # stopband cutoff
                               0.01,    # passband ripple (dB)
                               70)      # stopband atten (dB)

        print "len(taps) =", len(taps)
                               
        #plot = gru.gnuplot_freqz(gru.freqz(taps, 1), 1)
        #raw_input('Press Enter to continue: ')
        
        for n in xrange(nchan):
            #df = gr.fft_filter_ccc(decim, taps)
            df = gr.fir_filter_ccf(decim, taps)
            self.connect((deint, n), df)
            dst_filename = make_filename(date, time, freq, n, short_output)
            if short_output:
                c2s = gr.complex_to_interleaved_short()
                dst = gr.file_sink(gr.sizeof_short, dst_filename)
                self.connect(df, c2s, dst)
            else:
                dst = gr.file_sink(gr.sizeof_gr_complex, dst_filename)
                self.connect(df, dst)


def split_1_file(filename, short_output):
    my_graph(filename, short_output).run()


def main():
    usage="%prog: [options] file_to_split..."
    parser = OptionParser (option_class=eng_option, usage=usage)
    parser.add_option("-s", "--short", action="store_true", default=False,
                      help="short output if set, else complex")
    (options, args) = parser.parse_args()

    for filename in args:
        split_1_file(filename, options.short)


if __name__ == '__main__':
    main()