cleaning up and putting much better code in. Step 1 of 2
[debian/gnuradio] / gr-msdd6000 / src / python_test / udp_stream_test.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.46"
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 = 2500;   
31 f_hz = 0;       
32 gain = 0;       
33 window = 3;     #0=rect, 1=hanning, 2=hamming, 3=blackman
34
35 samples = 65535;
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, request_len, f_mhz, f_hz, gain, samples, decim, sets);
49
50 data = raw_data;
51
52 UDPSock.sendto(data, (msdd_host, msdd_port));
53
54 print "sent"
55
56
57
58 count = 0;
59
60 total_data = [];
61
62 state = 0;
63
64 vals = [];
65 mags = [];
66 re = [];
67
68 sample_count = 0;
69 IQ_bytes=0;
70
71
72 numtocap = 1000;
73 IQ_bytes = 4 * numtocap;
74
75 while(True):
76         data = UDPSock.recv(4);
77         [i,q] = struct.unpack("<hh", data);
78         tmp = complex(i,q);
79
80         re.append(i);
81         vals.append(tmp);
82         mags.append(abs(tmp));
83
84
85         sample_count = sample_count + 1;
86 #       print "sample count %d"%(sample_count)  
87
88         IQ_bytes = IQ_bytes - 4;
89         if(IQ_bytes % 200 == 0):
90                 print IQ_bytes;
91         if(IQ_bytes < 4):
92                 print "got all data (total %d)"%(sample_count);
93                 print "remaining: %d"%(IQ_bytes);
94                 break;
95
96
97 halt_data = struct.pack("<II", 0x04, 0x00);
98 UDPSock.sendto(halt_data, (msdd_host, msdd_port));
99
100
101
102 UDPSock.close();
103
104 print "done"
105 nmags = []
106 for i in mags:
107         if i == 0:
108                 i=1;
109         nmags.append(i);
110
111
112 subplot(2,1,1);
113 plot(20*log10(nmags));
114
115 fft_data = 20*log10(fftshift(fft(vals)));
116
117 subplot(2,1,2);
118 plot(fft_data);
119 show();
120
121 f = open(filename, "w");
122 for sample in vals:
123         binchunk = struct.pack("<ff",float(sample.real), float(sample.imag) );
124         f.write(binchunk);
125 f.close();
126
127