cleaning up and putting much better code in. Step 1 of 2
[debian/gnuradio] / gr-msdd6000 / src / python_test / newtest.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 msdd_port = 10001
18 msdd_host = "10.45.4.43"
19
20 buf = 100000;
21
22 my_udp_addr = ('',10001);
23 my_udp_addr = ('10.45.1.229 ',10001);
24
25 UDPSock = socket(AF_INET,SOCK_DGRAM);
26 UDPSock.bind(my_udp_addr);
27
28 #f_mhz = 3500;  
29 #f_mhz = 3500;  
30 f_mhz = 100;    
31 f_hz = 0;       
32 gain = 0;       
33 window = 3;     #0=rect, 1=hanning, 2=hamming, 3=blackman
34
35 samples = 12000;
36 samples = samples*4;    #bytes of data we are requesting
37
38 decim = 2;      #0-8   (3 => 2^3 = 8)  
39 decim = decim+16;       # +16 to use 16bit floats instead of 32 bit floats
40 mode = 0;       #0=IQ, 1=MAG, 2=MAGDB
41 #sets = 0;
42 sets = 0xffffffff;
43
44 size_int = 4;
45 request_len = 6*size_int;       # 6 int items not including the 8 bytes for opcode and length fields
46 print "request len = %d"%(request_len);
47
48 #raw_data = struct.pack("<IIIIIIII", 0x01, 0x18, f_mhz, f_hz, gain, samples, decim, sets);
49 raw_data = struct.pack("<IIIIIIII", 0x01, request_len, f_mhz, f_hz, gain, samples, decim, sets);
50
51 data = raw_data;
52
53 UDPSock.sendto(data, (msdd_host, msdd_port));
54
55 print "sent"
56
57
58
59 count = 0;
60
61 total_data = [];
62
63 state = 0;
64
65 vals = [];
66 mags = [];
67 re = [];
68
69 sample_count = 0;
70 IQ_bytes=0;
71
72 while(True):
73         print state;
74         if(state==0):
75                 data = UDPSock.recv(4);
76                 [opcode] = struct.unpack("<I", data);
77                 print "Opcode = %d"%(opcode);
78                 if(opcode==1):
79
80                         # if UDP mode and sets_stream requested,
81                         # we do not get a header reply back, only data
82                         if(sets == 0):
83                                 state = 1;
84                         else:
85                                 state = 2;
86
87         elif(state==1):
88                 data = UDPSock.recv(7*4);
89                 args = struct.unpack("<IIIIIII", data);
90                 print ["reply_len", "freq_mhz", "offset_hz", "gain", "sample_bytes", "decim", "sets_remain"];
91                 print args;
92                 IQ_bytes = args[0] - 7*4;
93                 state =2;
94
95         elif(state==2):
96                 data = UDPSock.recv(4);
97                 [i,q] = struct.unpack("<hh", data);
98                 tmp = complex(i,q);
99
100                 re.append(i);
101                 vals.append(tmp);
102                 mags.append(abs(tmp));
103
104
105                 sample_count = sample_count + 1;
106 #               print "sample count %d"%(sample_count)  
107
108                 IQ_bytes = IQ_bytes - 4;
109                 if(IQ_bytes < 4):
110                         print "got all data (total %d)"%(sample_count);
111                         print "remaining: %d"%(IQ_bytes);
112                         break;
113
114
115 UDPSock.close();
116
117 print "done"
118 nmags = []
119 for i in mags:
120         if i == 0:
121                 i=1;
122         nmags.append(i);
123
124
125 subplot(2,1,1);
126 plot(20*log10(nmags));
127
128 fft_data = 20*log10(fftshift(fft(vals)));
129
130 subplot(2,1,2);
131 plot(fft_data);
132 show();
133
134 f = open(filename, "w");
135 for sample in vals:
136         binchunk = struct.pack("<ff",float(sample.real), float(sample.imag) );
137         f.write(binchunk);
138 f.close();
139
140