Houston, we have a trunk.
[debian/gnuradio] / docs / exploring-gnuradio / fm_demod_example.xml
1 <?xml version="1.0" encoding="ISO-8859-1"?>
2 <example id="fm_demod_ex"><title>Broadcast FM Receiver</title>
3 <programlisting>
4 #!/usr/bin/env python
5
6 from gnuradio import gr
7 from gnuradio import audio
8 from gnuradio import mc4020
9 import sys
10
11 def high_speed_adc (fg, input_rate):
12     # return gr.file_source (gr.sizeof_short, "dummy.dat", False)
13     return mc4020.source (input_rate, mc4020.MCC_CH3_EN | mc4020.MCC_ALL_1V)
14
15 #
16 # return a gr.flow_graph
17 #
18 def build_graph (freq1, freq2):
19     input_rate = 20e6
20     cfir_decimation = 125
21     audio_decimation = 5
22
23     quad_rate = input_rate / cfir_decimation
24     audio_rate = quad_rate / audio_decimation
25
26     fg = gr.flow_graph ()
27     
28     # use high speed ADC as input source
29     src = high_speed_adc (fg, input_rate)
30     
31     # compute FIR filter taps for channel selection
32     channel_coeffs = \
33       gr.firdes.low_pass (1.0,          # gain
34                           input_rate,   # sampling rate
35                           250e3,        # low pass cutoff freq
36                           8*100e3,      # width of trans. band
37                           gr.firdes.WIN_HAMMING)
38
39     # input: short; output: complex
40     chan_filter1 = \
41       gr.freq_xlating_fir_filter_scf (cfir_decimation,
42                                       channel_coeffs,
43                                       freq1,        # 1st station freq
44                                       input_rate)
45     
46     (head1, tail1) = build_pipeline (fg, quad_rate, audio_decimation)
47     
48     # sound card as final sink
49     audio_sink = audio.sink (int (audio_rate))
50
51     # now wire it all together
52     fg.connect (src, chan_filter1)
53     fg.connect (chan_filter1, head1)
54     fg.connect (tail1, (audio_sink, 0))
55
56     return fg
57
58 def build_pipeline (fg, quad_rate, audio_decimation):
59     '''Given a flow_graph, fg, construct a pipeline
60     for demodulating a broadcast FM signal.  The
61     input is the downconverted complex baseband
62     signal. The output is the demodulated audio.
63
64     build_pipeline returns a two element tuple
65     containing the input and output endpoints.
66     '''
67     fm_demod_gain = 2200.0/32768.0
68     audio_rate = quad_rate / audio_decimation
69     volume = 1.0
70
71     # input: complex; output: float
72     fm_demod = gr.quadrature_demod_cf (volume*fm_demod_gain)
73
74     # compute FIR filter taps for audio filter
75     width_of_transition_band = audio_rate / 32
76     audio_coeffs = gr.firdes.low_pass (1.0,            # gain
77                                        quad_rate,      # sampling rate
78                                        audio_rate/2 - width_of_transition_band,
79                                        width_of_transition_band,
80                                        gr.firdes.WIN_HAMMING)
81
82     # input: float; output: float
83     audio_filter = gr.fir_filter_fff (audio_decimation, audio_coeffs)
84
85     fg.connect (fm_demod, audio_filter)
86     return ((fm_demod, 0), (audio_filter, 0))
87     
88
89 def main (args):
90     nargs = len (args)
91     if nargs == 1:
92         # get station frequency from command line
93         freq1 = float (args[0]) * 1e6
94     else:
95         sys.stderr.write ('usage: fm_demod freq\n')
96         sys.exit (1)
97
98     # connect to RF front end
99     rf_front_end = gr.microtune_4937_eval_board ()
100     if not rf_front_end.board_present_p ():
101         raise IOError, 'RF front end not found'
102
103     # set front end gain
104     rf_front_end.set_AGC (300)
105
106     # determine the front end's "Intermediate Frequency"
107     IF_freq = rf_front_end.get_output_freq () # 5.75e6
108
109     # Tell the front end to tune to freq1.  
110     # I.e., freq1 is translated down to the IF frequency
111     rf_front_end.set_RF_freq (freq1)
112
113     # build the flow graph
114     fg = build_graph (IF_freq, None)
115     
116     fg.start ()        # fork thread(s) and return
117     raw_input ('Press Enter to quit: ')
118     fg.stop ()
119
120 if __name__ == '__main__':
121     main (sys.argv[1:])
122 </programlisting>
123 </example>