Wednesday, April 24, 2013

An awesome program to understand the block diagram translated into a flow graph

** Although this is for old versions of GNU Radio but still very informative !!

#!/usr/bin/env python
#
#                    Weaver SSB demodulation
#
#
#                                         rf_loi                      af_loi
#                                           |                               |
#                                  |-----(X)---[lpf1i]--[lpf2i]---(X)-----|
#                                  |   rf_mixi                       af_mixi |
#                                  |                                                 |
#                 signal ---- |                                               (+)----- ssb_demod
#                                 |                                                 |
#                                 |   rf_mixq                    af_mixq |
#                                 |-----(X)---[lpf1q]--[lpf2q]---(X)---|
#                                          |                                 |
#                                      rf_loq                        af_loq
#

from gnuradio import gr
import sys

def build_graph ():

   rf_LO = 501.8e3 // set frequency for RF local oscillator
   af_LO = 1.8e3 // set frequency for audio local oscillator

   signal = 501.5e3  # passband is 500-503.6 Khz USB // set frequency for the signal

   rf_sample_rate = 8e6 // rf sampling rate
   af_sample_rate = 32e3 // audio sampling rate

   fir_decimation = (long)(rf_sample_rate / af_sample_rate) // decimation to tackle the sampling // rate conversion

   fg = gr.flow_graph () // instantiate the flow graph

   src = gr.sig_source_f (rf_sample_rate,gr.GR_SIN_WAVE,signal,1.0,0) // make a signal source for input

   rf_loi = gr.sig_source_f (rf_sample_rate,gr.GR_COS_WAVE,rf_LO,1.0,0)// for mixing at RF I
   rf_loq = gr.sig_source_f (rf_sample_rate,gr.GR_SIN_WAVE,rf_LO,1.0,0) // for mixing at RF Q
   af_loi = gr.sig_source_f (af_sample_rate,gr.GR_COS_WAVE,af_LO,1.0,0) // for mixing at AF I
   af_loq = gr.sig_source_f (af_sample_rate,gr.GR_SIN_WAVE,af_LO,1.0,0)// for mixing at AF Q

   rf_mixi = gr.multiply_ff () // for multiplication or mixing
   rf_mixq = gr.multiply_ff () // for multiplication or mixing

   lpf1_taps = gr.firdes.low_pass ( \
          1.0, rf_sample_rate, 160e3, 100e3, gr.firdes.WIN_HAMMING)// making filter taps for lpf1
   lpf1i = gr.fir_filter_fff (fir_decimation, lpf1_taps) // generating lpf1 I with some decimation as it is going to a less sample rated lpf21
   lpf1q = gr.fir_filter_fff (fir_decimation, lpf1_taps) // generating lpf1Q with some decimation as it is going to a less sample rated lpf2Q
   lpf2_taps = gr.firdes.low_pass ( \
          1.0, af_sample_rate, 1.8e3, 100, gr.firdes.WIN_HAMMING) // making filter taps for lpf2
   lpf2i = gr.fir_filter_fff (1, lpf2_taps) // generating lpf2I
   lpf2q = gr.fir_filter_fff (1, lpf2_taps) // generating lpf2Q

   af_mixi = gr.multiply_ff () // for mixing
   af_mixq = gr.multiply_ff () // for mixing

   sum = gr.add_ff () // for addition

   out = gr.file_sink (gr.sizeof_float, "ssb_demod") // collect output in a file

   fg.connect (src, (rf_mixi, 0)) // connect src to port 0 of multiplier rf_mixi
   fg.connect (rf_loi, (rf_mixi, 1)) // connect rf_loi to the port 1 of the multiplier rf_mixi
   fg.connect (rf_mixi, lpf1i) // connect output of rf_mixi to lpf1i
   fg.connect (lpf1i, lpf2i) // connect output of lpf1i to lpf2i
   fg.connect (lpf2i, (af_mixi, 0)) //
   fg.connect (af_loi, (af_mixi, 1))
   fg.connect (af_mixi, (sum, 0))

   fg.connect (src, (rf_mixq, 0))
   fg.connect (rf_loq, (rf_mixq, 1))
   fg.connect (rf_mixq, lpf1q)
   fg.connect (lpf1q, lpf2q)
   fg.connect (lpf2q, (af_mixq, 0))
   fg.connect (af_loq, (af_mixq, 1))
   fg.connect (af_mixq, (sum, 1))

   fg.connect (sum, out) // sum them up

   return fg

def main ():

   fg = build_graph ()


   # fg.run ()

   fg.start ()
   raw_input ('Press Enter to quit: ')
   fg.stop ()

if __name__ == '__main__':
   main ()


~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

**** With this connection you will get upper side band. To get the lower side band you need to use subtraction instead of addition. :) Thats it .
 

No comments:

Post a Comment