Advertisement
Guest User

Untitled

a guest
Sep 3rd, 2012
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.50 KB | None | 0 0
  1. #!/usr/bin/env python
  2. ##################################################
  3. # Gnuradio Python Flow Graph
  4. # Title: Top Block
  5. # Generated: Thu Aug 23 14:36:34 2012
  6. ##################################################
  7. from scipy import *
  8. from scipy.linalg import toeplitz
  9. import numpy as np
  10. import scipy as sp
  11. import matplotlib as mpl
  12. import matplotlib.pyplot as plt
  13. from scipy.signal import lfilter
  14. from numpy import genfromtxt
  15. from time import sleep
  16. import threading
  17. import time
  18. import array
  19.  
  20. from gnuradio import digital
  21. from gnuradio import eng_notation
  22. from gnuradio import filter
  23. from gnuradio import gr
  24. from gnuradio.eng_option import eng_option
  25. from gnuradio.filter import firdes
  26. from gnuradio.gr import firdes
  27. #from grc_gnuradio import wxgui as grc_wxgui
  28. from optparse import OptionParser
  29. import wx
  30.  
  31.  
  32. #LMS Filter Data
  33. def process(received):
  34.  
  35. preamble = genfromtxt('/home/traviscollins/data/pypreamble.txt', delimiter=',')
  36. #Correlator
  37. xcorr=np.correlate(received,preamble)
  38. max_val=np.max(xcorr)
  39. locations= np.where(xcorr==max_val)
  40. index= locations[0][0]
  41. received_saved=received
  42.  
  43. received=received[index:index+len(preamble)]
  44.  
  45. #Equalizer
  46. n=15
  47. delta=0 #delay
  48. p = len(received)-delta
  49. r1=received[n:p]
  50. r2=received[n::-1]
  51. R=toeplitz(r1,r2)
  52. S=preamble[n-delta:p-delta]
  53. # S=S.conj().transpose()
  54.  
  55. f1=np.real(np.linalg.inv(dot(R.conj().transpose(),R)))
  56. f2=dot(R.conj().transpose(),S)
  57. f=dot(f1,f2)
  58. output=sp.signal.lfilter(f,1,received_saved)#filter with filter calculated coeffs
  59.  
  60. #Calculate Error
  61. # error=0
  62. # error2=0
  63. # for i in range(0,len(output)):
  64. # error+=np.abs(np.real(output[i]))-np.abs(np.real(preamble[i]))
  65. # error2+=np.abs(np.imag(output[i]))-np.abs(np.imag(preamble[i]))
  66. # print "Error: ",error," | Error2: ",error2
  67.  
  68. return output
  69.  
  70.  
  71.  
  72. class top_block(gr.top_block): #(grc_wxgui.top_block_gui):
  73.  
  74. def __init__(self):
  75.  
  76. gr.top_block.__init__(self, 'Message Blocks Test')
  77.  
  78. ##################################################
  79. # Variables
  80. samp_rate=32000
  81. queue_size=900
  82.  
  83. ##################################################
  84. # Message Queues
  85. ##################################################
  86. self.sink_queue = gr.msg_queue(queue_size)
  87. self.source_queue = gr.msg_queue()
  88.  
  89.  
  90. ##################################################
  91. # Blocks
  92. ##################################################
  93. self.gr_throttle_0 = gr.throttle(gr.sizeof_char*1, samp_rate)
  94. self.gr_message_source_0 = gr.message_source(gr.sizeof_gr_complex*1, self.source_queue)
  95. self.gr_message_sink_0 = gr.message_sink(gr.sizeof_gr_complex*1, self.sink_queue, False)
  96. self.gr_file_source_0 = gr.file_source(gr.sizeof_char*1, "/home/traviscollins/data/input.txt", True)
  97. self.gr_file_sink_0_0_0 = gr.file_sink(gr.sizeof_gr_complex*1, "/home/traviscollins/data/post.txt")
  98. self.gr_file_sink_0_0_0.set_unbuffered(False)
  99. self.gr_file_sink_0_0 = gr.file_sink(gr.sizeof_gr_complex*1, "/home/traviscollins/data/pre.txt")
  100. self.gr_file_sink_0_0.set_unbuffered(False)
  101. self.gr_file_sink_0 = gr.file_sink(gr.sizeof_char*1, "/home/traviscollins/data/sig.txt")
  102. self.gr_file_sink_0.set_unbuffered(False)
  103. self.digital_dxpsk_mod_0 = digital.dbpsk_mod(
  104. samples_per_symbol=2,
  105. excess_bw=0.35,
  106. gray_coded=False,
  107. verbose=False,
  108. log=False)
  109.  
  110. self.digital_dxpsk_demod_0 = digital.dbpsk_demod(
  111. samples_per_symbol=2,
  112. excess_bw=0.35,
  113. freq_bw=6.28/100.0,
  114. phase_bw=6.28/100.0,
  115. timing_bw=6.28/100.0,
  116. gray_coded=False,
  117. verbose=False,
  118. log=False
  119. )
  120. self.channel_model_0 = filter.channel_model(
  121. noise_voltage=0.0,
  122. frequency_offset=0.0,
  123. epsilon=1.0,
  124. taps=(1.0 + 1.0j, ),
  125. noise_seed=0,
  126. )
  127.  
  128. #Extras
  129. self.gr_null = gr.null_sink(gr.sizeof_gr_complex*1)
  130. self.gr_file_sink_0_0_0_0 = gr.file_sink(gr.sizeof_gr_complex*1, "/home/traviscollins/data/dump.txt")
  131. self.gr_file_sink_0_0_0_0.set_unbuffered(False)
  132.  
  133.  
  134. ##################################################
  135. # Worker Threads
  136. ##################################################
  137. def _probe_probe():
  138. while True:
  139. time.sleep(1.0/(1))
  140. print "Source: ",self.source_queue.count()
  141. print "Sink: ",self.sink_queue.count()
  142.  
  143. _probe_thread = threading.Thread(target=_probe_probe)
  144. _probe_thread.daemon = True
  145. _probe_thread.start()
  146.  
  147.  
  148. ##################################################
  149. # Connections
  150. ##################################################
  151. self.connect((self.gr_file_source_0, 0), (self.gr_throttle_0, 0))
  152. self.connect((self.digital_dxpsk_mod_0, 0), (self.gr_file_sink_0_0, 0))
  153. self.connect((self.gr_throttle_0, 0), (self.digital_dxpsk_mod_0, 0))
  154. self.connect((self.digital_dxpsk_mod_0, 0), (self.channel_model_0, 0))
  155.  
  156. self.connect((self.channel_model_0, 0), (self.gr_file_sink_0_0_0, 0))
  157. self.connect((self.channel_model_0, 0), (self.gr_message_sink_0, 0))
  158. #self.connect((self.channel_model_0, 0), (self.digital_dxpsk_demod_0, 0))
  159.  
  160. self.connect((self.gr_message_source_0, 0), (self.digital_dxpsk_demod_0, 0))
  161. self.connect((self.gr_message_source_0, 0), (self.gr_file_sink_0_0_0_0, 0))
  162. self.connect((self.digital_dxpsk_demod_0, 0), (self.gr_file_sink_0, 0))
  163.  
  164. def get_samp_rate(self):
  165. return self.samp_rate
  166.  
  167. def set_samp_rate(self, samp_rate):
  168. self.samp_rate = samp_rate
  169.  
  170.  
  171.  
  172. #MAIN
  173. if __name__ == '__main__':
  174. parser = OptionParser(option_class=eng_option, usage="%prog: [options]")
  175. (options, args) = parser.parse_args()
  176. tb = top_block()
  177. tb.start()
  178. #Process Data in queues
  179. while(1):
  180. if tb.sink_queue.count()>800:
  181. #Get enough data to be processed
  182. a_msg=tb.sink_queue.delete_head()
  183. raw_a=a_msg.to_string()
  184. for i in range(0,10): #tb.sink_queue.count()):
  185. a_msg=tb.sink_queue.delete_head()
  186. raw_a+=a_msg.to_string()
  187. #Convert message from string to Complex
  188. a_data = np.fromstring(raw_a, np.complex64, count = int(a_msg.arg2())) # converted amplitude data
  189. input_a=np.array(a_data)
  190. #Process Data
  191. if len(input_a)<400:#message not long enough to contain preamble
  192. continue
  193. processed=process(input_a)
  194. data=processed[0].tostring()
  195. for i in range(1,len(processed)):
  196. data+=processed[i].tostring()
  197. #OLD WAY msg = gr.message_from_string(processed.tostring(),gr.sizeof_gr_complex)
  198. msg = gr.message_from_string(data,gr.sizeof_gr_complex)
  199. tb.source_queue.insert_tail(msg)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement