cleaning up and putting much better code in. Step 1 of 2
[debian/gnuradio] / gr-msdd6000 / src / python_test / udp_stream_cap.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 = 1000;   
31 f_hz = 0;       
32 gain = 0;       
33 window = 3;     #0=rect, 1=hanning, 2=hamming, 3=blackman
34
35 #samples = 65535;
36 samples = 16384;
37 #samples = samples*4;   #bytes of data we are requesting
38
39 decim = 4;      #0-8   (3 => 2^3 = 8)  
40 decim = decim+16;       # +16 to use 16bit floats instead of 32 bit floats
41 mode = 0;       #0=IQ, 1=MAG, 2=MAGDB
42 #sets = 0;
43 sets = 0xffffffff;
44
45 size_int = 4;
46 request_len = 6*size_int;       # 6 int items not including the 8 bytes for opcode and length fields
47 print "request len = %d"%(request_len);
48
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
73 numtocap = 1000;
74 IQ_bytes = 4 * numtocap;
75
76 numbytes = 100 * 65536;
77
78 num_rx = 0;
79 start =  time.time();
80
81 d = [];
82 while(num_rx < numbytes):
83         data = UDPSock.recv(65536);
84         num_rx = num_rx + len(data);
85         d.append(data);
86
87 mags = [];
88 for i in range(0, len(d)/4):
89         v = struct.unpack_from("<f",d, i*4);
90         mags.append(abs(v));
91 plot(mags);
92 show();
93
94 end = time.time();
95 print "recieved %d bytes in %f sec"%(numbytes, end-start);
96
97 bytes_per_sec = numbytes / (end-start);
98 samples_per_sec = bytes_per_sec / 4;
99 MSPS = samples_per_sec / 1000000.0;
100
101 print "Got %f MSPS"%(MSPS);
102 print "Expected %f MSPS"%(102.4/math.pow(2,(1+decim-16)));
103         
104
105 halt_data = struct.pack("<II", 0x04, 0x00);
106 UDPSock.sendto(halt_data, (msdd_host, msdd_port));
107
108
109 UDPSock.close();
110