Houston, we have a trunk.
[debian/gnuradio] / gr-radar / src / python / split_files.py
1 #!/usr/bin/env python
2
3 from gnuradio import gr, gru, eng_notation, optfir
4 from gnuradio.eng_option import eng_option
5 from optparse import OptionParser
6 import os.path
7 import re
8
9 plot = None
10
11 def split_filename(src_filename):
12     s = os.path.splitext(src_filename)[0]     # drop extension
13     date, time, freq, dec, nchan = s.split('-')
14     return (date, time, freq, int(dec), int(nchan))
15
16
17 def make_filename(date, time, freq, n, short_output):
18     if short_output:
19         return '-'.join((date, time, freq)) + '.s%02d' % (n,)
20     else:
21         return '-'.join((date, time, freq)) + '.c%02d' % (n,)
22
23 class my_graph(gr.flow_graph):
24
25     def __init__(self, src_filename, short_output):
26         """
27         Deinterleave file, filter and decimate by 4, and write out
28         a separate file for each input channel.
29
30         The input file is the raw output of nchannels of short USRP data.
31         That is, they are interleaved short I&Q for each channel.
32         """
33         global plot
34         
35         gr.flow_graph.__init__(self)
36
37         decim = 4
38
39         (date, time, freq, dec, nchan) = split_filename(src_filename)
40
41         src = gr.file_source(gr.sizeof_short, src_filename)
42
43         # convert stream of interleaved shorts to a stream of complex
44         s2c = gr.interleaved_short_to_complex()
45
46         # deinterleave complexes into separate streams
47         deint = gr.deinterleave(gr.sizeof_gr_complex)
48
49         self.connect(src, s2c, deint)
50
51         taps = optfir.low_pass(1,       # gain
52                                1,       # sampling rate
53                                0.100,   # passband cutoff
54                                0.125,   # stopband cutoff
55                                0.01,    # passband ripple (dB)
56                                70)      # stopband atten (dB)
57
58         print "len(taps) =", len(taps)
59                                
60         #plot = gru.gnuplot_freqz(gru.freqz(taps, 1), 1)
61         #raw_input('Press Enter to continue: ')
62         
63         for n in xrange(nchan):
64             #df = gr.fft_filter_ccc(decim, taps)
65             df = gr.fir_filter_ccf(decim, taps)
66             self.connect((deint, n), df)
67             dst_filename = make_filename(date, time, freq, n, short_output)
68             if short_output:
69                 c2s = gr.complex_to_interleaved_short()
70                 dst = gr.file_sink(gr.sizeof_short, dst_filename)
71                 self.connect(df, c2s, dst)
72             else:
73                 dst = gr.file_sink(gr.sizeof_gr_complex, dst_filename)
74                 self.connect(df, dst)
75
76
77 def split_1_file(filename, short_output):
78     my_graph(filename, short_output).run()
79
80
81 def main():
82     usage="%prog: [options] file_to_split..."
83     parser = OptionParser (option_class=eng_option, usage=usage)
84     parser.add_option("-s", "--short", action="store_true", default=False,
85                       help="short output if set, else complex")
86     (options, args) = parser.parse_args()
87
88     for filename in args:
89         split_1_file(filename, options.short)
90
91
92 if __name__ == '__main__':
93     main()