Advertisement
Guest User

Untitled

a guest
Jun 15th, 2015
558
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.43 KB | None | 0 0
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. """
  4. Created on Sun Apr 5 19:06:56 2015
  5.  
  6. @author: madengr
  7. """
  8.  
  9. from gnuradio import gr
  10. from gnuradio import uhd
  11. from gnuradio import filter # Warning: redefining built-in filter()
  12. from gnuradio import blocks
  13. import time
  14. import numpy as np
  15.  
  16. def str_to_floats(data):
  17. """Convert string of floats to list of floats
  18.  
  19. Used for converting GNU Radio message queue strings
  20. """
  21. import struct
  22.  
  23. floats = []
  24. for i in range(0,len(data),4):
  25. floats.append(struct.unpack_from('f',data[i:i+4])[0])
  26. return np.array(floats)
  27.  
  28. class ScannerTopBlock(gr.top_block):
  29. """ Class for the GNU Radio GSM channel scanner flow
  30. """
  31.  
  32. def __init__(self):
  33. # Call the initialization method from the parent class
  34. gr.top_block.__init__(self, "GSM Channel Scanner")
  35.  
  36. # Define the constants
  37. uhd_args = "type=b200, master_clock_rate=50E6"
  38. src_samp_rate = 1E6
  39. rx_freq = 1830.2E6
  40. src_gain_dB = 20
  41. rssi_cal_offset_dB = -24.2
  42.  
  43. # Initialize a queue
  44. self.sink_queue = gr.msg_queue()
  45.  
  46. # Setup the USRP source
  47. self.src = uhd.usrp_source(uhd_args, uhd.io_type_t.COMPLEX_FLOAT32, 1)
  48. self.src.set_samp_rate(src_samp_rate)
  49. self.src.set_gain(src_gain_dB, 0)
  50. self.src.set_center_freq(rx_freq, 0)
  51.  
  52. # Generate taps for frequency translating FIR filter
  53. filter_taps = filter.firdes.low_pass(gain=1.0,
  54. sampling_freq=src_samp_rate,
  55. cutoff_freq=100E3,
  56. transition_width=25E3,
  57. window=filter.firdes.WIN_HAMMING)
  58.  
  59. # Channel filter
  60. channel_filter = filter.fir_filter_ccc(1, filter_taps)
  61.  
  62. # Calculate power for RSSI
  63. c2magsqr = blocks.complex_to_mag_squared(1)
  64.  
  65. # Integrate for mean power and decimate down to 100 Hz
  66. integrator = blocks.integrate_ff(10000)
  67.  
  68. # Take 10*Log10() and offset for calibrated power and src gain
  69. logten = blocks.nlog10_ff(10, 1, rssi_cal_offset_dB-src_gain_dB)
  70.  
  71. # Message sink
  72. self.msg_sink = blocks.message_sink(gr.sizeof_float, self.sink_queue, False)
  73.  
  74. # Connect the blocks for the RSSI
  75. self.connect(self.src, channel_filter, c2magsqr, integrator, logten, self.msg_sink)
  76.  
  77.  
  78. def main():
  79. """ Start the flow """
  80. # Set a threshold in dBm to log the channel
  81. threshold_dBm = -75
  82.  
  83. # Lets offset tune to avoid DC spike
  84. lo_offset_freq = 1.1E6
  85.  
  86. # Open a file for logging
  87. filename = 'log.txt'
  88. f = open(filename, 'w')
  89.  
  90. # Start the flow
  91. tb = ScannerTopBlock()
  92. tb.start()
  93.  
  94. # Get the time so we know how long it takes to scan
  95. print "Starting GSM channel scan"
  96. t0 = time.time()
  97.  
  98. # Scan thru ARFCN
  99. for arfcn in range(512, 811, 1):
  100.  
  101. channel_freq = 1850.2E6 + 80E6 + 0.2E6*(arfcn-512)
  102. if not tb.src.set_center_freq(uhd.tune_request(channel_freq, lo_offset_freq)):
  103. print '[ERROR] Failed to set base frequency.'
  104. raise SystemExit, 1
  105.  
  106. # Flush the queue
  107. tb.sink_queue.flush()
  108.  
  109. # Wait for queue to fill and flush again to clean integrator
  110. while tb.sink_queue.count() == 0:
  111. pass
  112. tb.sink_queue.flush()
  113.  
  114. # Wait for queue to fill then pull a sample
  115. while tb.sink_queue.count() == 0:
  116. pass
  117. rssi_str = tb.sink_queue.delete_head_nowait().to_string()
  118.  
  119. # Convert to float value and log
  120. rssi = str_to_floats(rssi_str)[0]
  121.  
  122. # Log if above threshold and not a spur (10 MHz clock harmonic)
  123. if rssi >= threshold_dBm and channel_freq % 10E6 != 0:
  124. log_string = str(arfcn) + " " + str(channel_freq/1E6) + " " + str(rssi)
  125. f.write(log_string + '\n')
  126. print(log_string)
  127.  
  128. #Print the elapsed time it took to scan
  129. print str(time.time() - t0) + " seconds to scan"
  130.  
  131. # Stop the flow
  132. tb.stop()
  133. tb.wait()
  134. f.close()
  135.  
  136. if __name__ == '__main__':
  137. try:
  138. main()
  139. except KeyboardInterrupt:
  140. pass
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement