cleaning up and putting much better code in. Step 1 of 2
[debian/gnuradio] / gr-msdd6000 / src / python_test / capture_tcp_one_set.py
1 #!/usr/bin/python
2
3 from socket import *
4 import string
5 import time
6 import struct;
7 import random;
8 import array;
9 import cmath;
10 from numpy import *;
11 from numpy.fft import *;
12 from pylab import *;
13
14 myport = random.randint(1025,65535);
15 filename = "output.dat";
16
17 port = 10000
18 host = "10.45.4.46"
19 #host = "10.45.4.41"
20 myaddr = ('',myport);
21
22 buf = 100000;
23
24 TCPSock = socket(AF_INET,SOCK_STREAM);
25 TCPSock.bind(myaddr);
26 TCPSock.connect((host,port));
27
28 #f_mhz = 2647;  # roof ofdm
29 if(len(sys.argv)!= 3):
30         print "usage: %s  fc_ghz   decim_pow2_exponent"%(sys.argv[0]);
31         sys.exit(-1);
32
33 f_mhz = float(sys.argv[1])*1000;
34 decim = int(sys.argv[2]);
35
36 #f_mhz = 3500;
37 #f_mhz = 2600;
38 f_hz = 0;       # offset        
39 gain = 0;       
40 window = 3;     #0=rect, 1=hanning, 2=hamming, 3=blackman
41
42 samples = 65536;
43 #samples = 16777216;
44 samples = samples*4;    #bytes of data we are requesting
45 samples=samples*2;
46 #decim = 2;     #0-8   (3 => 2^3 = 8)  
47 decim = decim+16;       # +16 to use 16bit floats instead of 32 bit floats
48 mode = 0;       #0=IQ, 1=MAG, 2=MAGDB
49 sets = 1;
50
51 raw_data = struct.pack("<IIIIIIII", 0x01, 0x18, f_mhz, f_hz, gain, samples, decim, sets);
52
53 data = raw_data;
54
55 TCPSock.send(data);
56
57 print "sent"
58
59
60
61 count = 0;
62
63 total_data = [];
64
65 state = 0;
66
67 vals = [];
68 mags = [];
69 re = [];
70
71 sample_count = 0;
72 IQ_bytes=0;
73 while(TCPSock):
74         if(state==0):
75                 data = TCPSock.recv(4);
76                 [opcode] = struct.unpack("<I", data);
77                 print "Opcode = %d"%(opcode);
78                 if(opcode==1):
79                         state = 1;
80
81         elif(state==1):
82                 data = TCPSock.recv(7*4);
83                 args = struct.unpack("<IIIIIII", data);
84                 print ["reply_len", "freq_mhz", "offset_hz", "gain", "sample_bytes", "decim", "sets_remain"];
85                 print args;
86                 IQ_bytes = args[0] - 7*4;
87                 state =2;
88
89         elif(state==2):
90                 data = TCPSock.recv(4);
91                 [i,q] = struct.unpack("<hh", data);
92                 tmp = complex(i,q);
93
94                 re.append(i);
95                 vals.append(tmp);
96                 mags.append(abs(tmp));
97
98
99                 sample_count = sample_count + 1;
100 #               print "sample count %d"%(sample_count)  
101
102                 IQ_bytes = IQ_bytes - 4;
103                 if(IQ_bytes < 4):
104                         print "got all data (total %d)"%(sample_count);
105                         print "remaining: %d"%(IQ_bytes);
106                         break;
107
108
109 TCPSock.close();
110
111 print "done"
112 nmags = []
113 for i in mags:
114         if i == 0:
115                 i=1;
116         nmags.append(i);
117
118
119 subplot(2,1,1);
120 plot(nmags);
121 #plot(10*log10(nmags));
122
123 dlen = len(vals);
124 fftlen = (dlen-1024)/1024;
125
126 fft_data = []
127 for i in range(1, dlen-1025, 1024):
128
129         t_in = [];
130         for ind in range(i, i+1024):
131                 t_in.append(vals[ind]);
132
133         #tmp = 20*log10(fftshift(fft(t_in)));
134         tmp = (fftshift(fft(t_in)));
135
136         if(len(fft_data) == 0):
137                 for ind in range(0,1024):
138                         fft_data.append( tmp[ind] );
139         else:
140                 for ind in range(0,1024):
141                         fft_data[ind] = fft_data[ind] + tmp[ind];
142
143 #fft_data = 20*log10(fftshift(fft(vals)));
144
145
146 subplot(2,1,2);
147 plot(fft_data);
148 show();
149
150 f = open(filename, "w");
151 for sample in vals:
152         binchunk = struct.pack("<ff",float(sample.real), float(sample.imag) );
153         f.write(binchunk);
154 f.close();
155
156