Imported Upstream version 3.0
[debian/gnuradio] / gnuradio-examples / python / usrp / tvrx_am_rcv_gui.py
1 #!/usr/bin/env python
2 #
3 # Copyright 2004 Free Software Foundation, Inc.
4
5 # This file is part of GNU Radio
6
7 # GNU Radio 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 2, or (at your option)
10 # any later version.
11
12 # GNU Radio 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 GNU Radio; 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 # Demodulate an AM signal from the TVRX or a recorded file.
24 # The file format must be 256 ksps, complex data.
25 #
26
27 from gnuradio import gr, gru, eng_notation
28 from gnuradio import audio_oss as audio
29 from gnuradio import usrp
30 from gnuradio import tv_rx
31 from gnuradio.eng_option import eng_option
32 from optparse import OptionParser
33 import sys
34 import math
35 from gnuradio.wxgui import stdgui, fftsink, scopesink
36 import wx
37
38 #
39 # return a gr.flow_graph
40 #
41 class wfm_rx_graph (stdgui.gui_flow_graph):
42   def __init__(self,frame,panel,vbox,argv):
43     stdgui.gui_flow_graph.__init__ (self,frame,panel,vbox,argv)
44     
45     #set rf freq
46     rf_freq = 120.e6
47     
48     # Decimation rate from USRP ADC to IF.
49     usrp_decim = 100
50     
51     # Calculate the sampling rate of the USRP and capture file.
52     # Decimate the IF sampling rate down by 4 to 64 ksps
53     # This is a flow graph that has an input (capture file) and output (audio channel).
54     #self = gr.flow_graph ()
55   
56     # Signal source is assumed to be 256 kspb / complex data stream.
57     which_side = 0
58     # usrp is data source
59     if which_side == 0:
60         src = usrp.source_c (0, usrp_decim, 1, gru.hexint(0xf0f0f0f0), 0)
61     else:
62         src = usrp.source_c (0, usrp_decim, 1, gru.hexint(0xf0f0f0f2), 0)
63
64     if_rate = 640e3 # src.adc_freq() / usrp_decim
65     if_decim = 5
66     demod_rate = if_rate / if_decim
67     
68     audio_decimation = 4
69     audio_rate = demod_rate / audio_decimation
70
71     # set up frontend
72     dboard = tv_rx.tv_rx (src, which_side)
73     self.dboard = dboard
74     (success, actual_freq) = dboard.set_freq(rf_freq)
75     assert success
76
77     if_freq = rf_freq - actual_freq
78     src.set_rx_freq (0, -if_freq)
79
80     print "actual freq ", actual_freq
81     print "IF freq ", if_freq
82         
83     dboard.set_gain(50)
84     
85     #src = gr.file_source (gr.sizeof_gr_complex, "samples/atis_ffz_am_baseband_256k_complex.dat")
86     #src = gr.file_source (gr.sizeof_gr_complex, "samples/garagedoor1.dat", True)
87     
88     #channel_coeffs = gr.firdes.band_pass (
89     #    1.0,    # gain
90     #    if_rate,
91     #    10,   # center of low transition band
92     #    10000,   # center of hi transition band
93     #    200,    # width of transition band
94     #    gr.firdes.WIN_HAMMING)
95     
96     channel_coeffs = gr.firdes.low_pass (1.0, if_rate, 10e3, 4e3, gr.firdes.WIN_HANN)
97     print "len(channel_coeffs) = ", len(channel_coeffs)
98
99     # Tune to the desired frequency.
100     ddc = gr.freq_xlating_fir_filter_ccf (if_decim, channel_coeffs, -20e3, if_rate)
101
102     # Demodule with classic sqrt (I*I + Q*Q)
103     magblock = gr.complex_to_mag()
104
105     # Scale the audio
106     volumecontrol = gr.multiply_const_ff(.1)
107
108     #band-pass
109     audio_coeffs = gr.firdes.band_pass (
110         1.0,    # gain
111         demod_rate,
112         10,   # center of low transition band
113         6000,   # center of hi transition band
114         200,    # width of transition band
115         gr.firdes.WIN_HAMMING)
116     
117     
118     # Low pass filter the demodulator output
119     #audio_coeffs = gr.firdes.low_pass (1.0, demod_rate, 500, 200, gr.firdes.WIN_HANN)
120     print "len(audio_coeffs) = ", len(audio_coeffs)
121
122     # input: float; output: float
123     audio_filter = gr.fir_filter_fff (audio_decimation, audio_coeffs)
124
125     # sound card as final sink
126     audio_sink = audio.sink (int (audio_rate))
127
128     # now wire it all together
129     self.connect (src, ddc)
130     self.connect (ddc, magblock)
131     self.connect (magblock, volumecontrol)
132     self.connect (volumecontrol, audio_filter)
133     self.connect (audio_filter, (audio_sink, 0))
134     
135     d_win = fftsink.fft_sink_c (self, panel, title="RF", fft_size=512, sample_rate=if_rate)
136     self.connect (src,d_win)
137     vbox.Add (d_win.win, 4, wx.EXPAND)
138        
139     p_win = fftsink.fft_sink_c (self, panel, title="IF", fft_size=512, sample_rate=demod_rate)
140     self.connect (ddc,p_win)
141     vbox.Add (p_win.win, 4, wx.EXPAND)
142        
143     r_win = fftsink.fft_sink_f (self, panel, title="Audio", fft_size=512, sample_rate=audio_rate)
144     self.connect (audio_filter,r_win)
145     vbox.Add (r_win.win, 4, wx.EXPAND)
146     
147     #audio_oscope = scopesink.scope_sink_f (self, panel, "Oscope Data", audio_rate)
148     #self.connect (audio_filter, audio_oscope)
149     #vbox.Add (audio_oscope.win, 4, wx.EXPAND) 
150                 
151 if __name__ == '__main__':
152     
153     app = stdgui.stdapp (wfm_rx_graph, "TVRX AM RX")
154     app.MainLoop ()