cleaning up and putting much better code in. Step 1 of 2
[debian/gnuradio] / gr-msdd6000 / src / python_test / udp_stream_rate_test_plot.py
1 #!/usr/bin/python
2
3 from socket import *
4 import string
5 import time
6 import struct;
7 from random import *;
8 import array;
9 import cmath;
10 from numpy import *;
11 from numpy.fft import *;
12 from pylab import *;
13
14 myport = 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 = ('',randint(1025,65535));
23
24 UDPSock = socket(AF_INET,SOCK_DGRAM);
25 UDPSock.bind(my_udp_addr);
26
27 f_mhz = 2500;   
28
29 print "fc = %d"%(f_mhz);
30
31 f_hz = 0;       
32 gain = 20;      # attenuation
33 window = 3;     #0=rect, 1=hanning, 2=hamming, 3=blackman
34
35 samples = 65535*4*2;
36 #samples = 16384;
37 #samples = 16*1024*1024;
38 #samples = samples*4;   #bytes of data we are requesting
39
40 # decim 0-8 ( 3 - 8 )
41 #decim = 5;     # rate ok
42 decim = 8;
43 decim = decim+16;       # +16 to use 16bit floats instead of 32 bit floats
44 mode = 0;       #0=IQ, 1=MAG, 2=MAGDB
45 #sets = 0;
46 sets = 0xffffffff;
47
48 size_int = 4;
49 request_len = 6*size_int;       # 6 int items not including the 8 bytes for opcode and length fields
50 print "request len = %d"%(request_len);
51
52 raw_data = struct.pack("<IIIIIIII", 0x01, request_len, f_mhz, f_hz, gain, samples, decim, sets);
53
54 data = raw_data;
55
56 UDPSock.sendto(data, (msdd_host, msdd_port));
57
58 print "sent"
59
60
61
62 count = 0;
63
64 total_data = [];
65
66 state = 0;
67
68 vals = [];
69 mags = [];
70 re = [];
71
72 sample_count = 0;
73 IQ_bytes=0;
74
75
76 numtocap = 1000;
77 IQ_bytes = 4 * numtocap;
78
79 numbytes = 65536*100;
80 #numbytes = 65536*2;
81 #numbytes = 1024;
82
83 num_rx = 0;
84 start =  time.time();
85 l = [];
86 arr = [];
87
88 while(num_rx < numbytes):
89         data = UDPSock.recv(1024);
90         l.append(data);
91         num_rx = num_rx + len(data);
92
93
94 end = time.time();
95
96 # send stop command
97 halt_data = struct.pack(">II", 0x04, 0x00);
98 UDPSock.sendto(halt_data, (msdd_host, msdd_port));
99
100 # perform timing analysis
101 print "recieved %d bytes in %f sec"%(numbytes, end-start);
102 bytes_per_sec = numbytes / (end-start);
103 samples_per_sec = bytes_per_sec / 4;
104 MSPS = samples_per_sec / 1000000.0;
105
106 print "Got %f MSPS"%(MSPS);
107 print "Expected %f MSPS"%(102.4/math.pow(2,(decim-16)));
108
109
110 # plot data
111 val_arr = [];
112 mag_arr = [];
113 mag_arr2 = [];
114
115 print "Repacking data..."
116 f = open("out.dat","w");
117 for li in l:
118         for p in range(0, len(li)/4):
119                 [i,q] = struct.unpack_from("<hh", li, p*4);
120                 val = complex(i,q);
121                 mag_arr.append((val*conj(val)).real);
122                 val_arr.append(val);
123                 binchunk = struct.pack("<ff",float(val.real), float(val.imag) );
124                 f.write(binchunk);
125 f.close();
126                 
127
128 dlen = len(val_arr)-1;
129 fft_data = [];
130 for i in range(1, dlen-1024, 1024*1024):
131
132         t_in = [];
133         for ind in range(i, i+1024):
134                 t_in.append(val_arr[ind]);
135
136         tmp = 20*log10(fftshift(fft(t_in)));
137         #tmp = (fftshift(fft(t_in)));
138
139         if(len(fft_data) == 0):
140                 for ind in range(0,1024):
141                         fft_data.append( tmp[ind] );
142         else:
143                 for ind in range(0,1024):
144                         fft_data[ind] = fft_data[ind] + tmp[ind];
145
146
147
148
149 print "Plotting..."
150 subplot(2,1,1);
151 plot(mag_arr);
152 title("T power");
153 subplot(2,1,2);
154 plot(10*log10(fft_data));
155 title("PSD");
156 show();
157
158
159
160 UDPSock.close();
161