summaryrefslogtreecommitdiff
path: root/gr-msdd6000/src/non_gr_snapshot_tool
diff options
context:
space:
mode:
Diffstat (limited to 'gr-msdd6000/src/non_gr_snapshot_tool')
-rw-r--r--gr-msdd6000/src/non_gr_snapshot_tool/Makefile13
-rw-r--r--gr-msdd6000/src/non_gr_snapshot_tool/client5.cc128
-rw-r--r--gr-msdd6000/src/non_gr_snapshot_tool/msdd6000.cc146
-rw-r--r--gr-msdd6000/src/non_gr_snapshot_tool/msdd6000.h52
-rwxr-xr-xgr-msdd6000/src/non_gr_snapshot_tool/plot_psd.py48
-rwxr-xr-xgr-msdd6000/src/non_gr_snapshot_tool/spectrogram.py88
6 files changed, 475 insertions, 0 deletions
diff --git a/gr-msdd6000/src/non_gr_snapshot_tool/Makefile b/gr-msdd6000/src/non_gr_snapshot_tool/Makefile
new file mode 100644
index 000000000..213d68e0b
--- /dev/null
+++ b/gr-msdd6000/src/non_gr_snapshot_tool/Makefile
@@ -0,0 +1,13 @@
+CCFLAGS = -O3
+LDFLAGS = -lpthread
+FFTW_LDFLAGS = -lfftw3f
+
+
+all:
+ g++ $(CCFLAGS) -c client5.cc
+ g++ $(CCFLAGS) -c msdd6000.cc
+ g++ $(CCFLAGS) -o cap4 client5.o msdd6000.o $(LDFLAGS)
+
+
+clean:
+ rm test.out*
diff --git a/gr-msdd6000/src/non_gr_snapshot_tool/client5.cc b/gr-msdd6000/src/non_gr_snapshot_tool/client5.cc
new file mode 100644
index 000000000..fe753beda
--- /dev/null
+++ b/gr-msdd6000/src/non_gr_snapshot_tool/client5.cc
@@ -0,0 +1,128 @@
+#include "msdd6000.h"
+#include <stdio.h>
+#include <stdlib.h>
+#include <pthread.h>
+#include <semaphore.h>
+#include <string.h>
+#include <unistd.h>
+#include <sys/time.h>
+#include <sys/resource.h>
+
+
+#define MSDD_HOST "192.168.1.200"
+int decim = 2;
+float fc = 3500000000.0;
+int gain = 1;
+
+//long target_buf_size = 256*1024*1024; // 2 gig output when converted to floats
+long target_buf_size = 1024*1024;
+#define CHUNK_SIZE (366*2)*sizeof(short)+6
+#define OUTPUT_FILE "CAPTURE_FILE.DAT"
+
+char* buffer;
+long int buffer_size;
+sem_t buf_sem;
+sem_t lock;
+
+long int in_ptr;
+long int out_ptr;
+char** argvg;
+
+
+int main(int argc, char* argv[]){
+
+ int prio = getpriority(PRIO_PROCESS, getpid());
+ printf("prio = %d\n", prio);
+
+ // renice to -20
+ setpriority(PRIO_PROCESS, getpid(), -20);
+
+ prio = getpriority(PRIO_PROCESS, getpid());
+ printf("new prio = %d\n", prio);
+
+
+ argvg = argv;
+
+ // instantiate our reciever instance
+ MSDD6000 rcv((char*)MSDD_HOST);
+
+ // set up desired rcv parameters
+
+ int tune_mhz = long(fc)/1000000;
+ int tune_hz = long(fc)%1000000;
+ printf("mhz = %d hz = %d\n", tune_mhz, tune_hz);
+
+ rcv.set_decim(decim);
+ rcv.set_fc(tune_mhz, tune_hz); // tune frequency in mhz, and ddc fine tuning in hz
+ rcv.set_ddc_gain(gain); // careful, too much gain will cause bit-clipping (this simply chooses which 16 bits to map in 0=high order)
+ rcv.set_rf_attn(10); // adjusted variable attenuator in front of adc (0-32dB i think)
+
+ // send start command
+ rcv.start();
+
+ // allocate our recieve buffer
+
+ buffer_size = ((long)CHUNK_SIZE) * (target_buf_size/CHUNK_SIZE);
+
+ printf("Allocating Intermediate Buffer. %f MB\n", ((float)buffer_size)/(1024.0*1024));
+ buffer = (char*)malloc(buffer_size);
+ in_ptr = out_ptr = 0;
+ printf("malloc returns %x.\n", buffer);
+
+ while(in_ptr + CHUNK_SIZE < buffer_size){
+ rcv.read( &buffer[in_ptr], CHUNK_SIZE );
+ in_ptr+=CHUNK_SIZE;
+ }
+
+ printf("done.\n");
+
+ int lastseq = -1;
+ int discont = 0;
+
+ float fbuf[366*2];
+
+ FILE* fd = fopen(OUTPUT_FILE, "w");
+
+ for(long long i=0;i<buffer_size;i += CHUNK_SIZE){
+ int seq = *((int*) &buffer[i + 2] );
+ if(seq == 0){
+// printf( "seq = %d, lastseq = %d at i = %d\n", seq, lastseq, i);
+ }
+ if(lastseq == -1){
+ if(seq==0){
+// printf("found start... starting...\n");
+ lastseq = seq;
+ }
+ } else {
+ if(seq != lastseq+366){
+// printf("seq gap, %d -> %d\n", lastseq, seq);
+ if(seq==0){
+// printf("stopping at i=%d\n", i);
+ break;
+ } else {
+// printf("this is a bad discontinuity!!! :{\n");
+ discont++;
+ }
+ } else {
+ // this is good data, and we are started
+ for(int j = 0; j<366*2; j++){
+ long ptr = i+6+j*2;
+// printf("%x %x \n", buffer[ptr], buffer[ptr+1]);
+ short int sample_data = (*(( signed short*) &buffer[ptr]));
+// printf("%d\n", sample_data);
+ fbuf[j] = (float) sample_data;
+// printf("%f\n", fbuf[j]);
+
+ }
+ fwrite(fbuf, sizeof(float), 366*2, fd);
+ }
+ lastseq = seq;
+ }
+ }
+
+ fclose(fd);
+
+ printf("total discontinuities = %d\n", discont);
+
+}
+
diff --git a/gr-msdd6000/src/non_gr_snapshot_tool/msdd6000.cc b/gr-msdd6000/src/non_gr_snapshot_tool/msdd6000.cc
new file mode 100644
index 000000000..a2055877e
--- /dev/null
+++ b/gr-msdd6000/src/non_gr_snapshot_tool/msdd6000.cc
@@ -0,0 +1,146 @@
+#include "msdd6000.h"
+
+#include <stdio.h>
+#include <netinet/in.h>
+#include <sys/socket.h>
+#include <string.h>
+#include <unistd.h>
+
+void optimize_socket(int socket);
+
+MSDD6000::MSDD6000(char* addr){
+ d_sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_IP);
+
+ optimize_socket(d_sock);
+
+
+ // set up remote sockaddr
+// int s = inet_aton(addr, &d_adx);
+ d_sockaddr.sin_family = AF_INET;
+ d_sockaddr.sin_port = htons(10000);
+ int s = inet_aton(addr, &d_sockaddr.sin_addr);
+
+ // set up local sockaddr
+ short int port = 10010;
+ d_myadx.s_addr = INADDR_ANY;
+ d_mysockaddr.sin_family = AF_INET;
+ d_mysockaddr.sin_port = htons(port);
+ memcpy(&d_mysockaddr.sin_addr.s_addr, &d_myadx.s_addr, sizeof(in_addr));
+ //d_sockaddr.sin_addr = INADDR_ANY;
+ s = bind(d_sock, (const sockaddr*) &d_mysockaddr, sizeof(d_mysockaddr));
+
+ // set default values
+ d_decim = 2;
+ d_ddc_gain = 2;
+ d_rf_attn = 0;
+ d_state = STATE_STOPPED;
+}
+
+
+void optimize_socket(int socket){
+#define BANDWIDTH 1000000000/8
+#define DELAY 0.5
+ int ret;
+
+ int sock_buf_size = 2*BANDWIDTH*DELAY;
+ char textbuf[512];
+ sprintf(textbuf, "%d", sock_buf_size);
+ printf("sock_buf_size = %d\n", sock_buf_size);
+
+ ret = setsockopt( socket, SOL_SOCKET, SO_SNDBUF,
+ (char *)&sock_buf_size, sizeof(sock_buf_size) );
+
+ ret = setsockopt( socket, SOL_SOCKET, SO_RCVBUF,
+ (char *)&sock_buf_size, sizeof(sock_buf_size) );
+
+ int uid = getuid();
+ if(uid!=0){
+ printf(" ****** COULD NOT OPTIMIZE SYSTEM NETWORK PARAMETERS BECAUSE YOU ARE NOT RUNNING AS ROOT *******\n ****** YOUR MSDD6000 RECIEVER PERFORMANCE IS GOING TO BE TERRIBLE *******\n");
+ return;
+ }
+
+
+ // SET UP SOME SYSTEM WIDE TCP SOCKET PARAMETERS
+ FILE* fd = fopen("/proc/sys/net/core/netdev_max_backlog", "w");
+ fwrite("10000", 1, strlen("10000"), fd);
+ fclose(fd);
+
+ fd = fopen("/proc/sys/net/core/rmem_max", "w");
+ fwrite(textbuf, 1, strlen(textbuf), fd);
+ fclose(fd);
+
+ fd = fopen("/proc/sys/net/core/wmem_max", "w");
+ fwrite(textbuf, 1, strlen(textbuf), fd);
+ fclose(fd);
+
+ // just incase these were rejected before because of max sizes...
+
+ ret = setsockopt( socket, SOL_SOCKET, SO_SNDBUF,
+ (char *)&sock_buf_size, sizeof(sock_buf_size) );
+
+ ret = setsockopt( socket, SOL_SOCKET, SO_RCVBUF,
+ (char *)&sock_buf_size, sizeof(sock_buf_size) );
+
+}
+
+
+void MSDD6000::set_decim(int decim_pow2){
+ DEBUG("SETTING NEW DECIM");
+ d_decim = decim_pow2;
+
+ if(d_state==STATE_STARTED)
+ send_request(d_fc_mhz, d_rf_attn, d_ddc_gain, d_decim, d_offset_hz);
+}
+
+void MSDD6000::set_rf_attn(int attn){
+ DEBUG("SETTING NEW RF ATTN");
+ d_rf_attn = attn;
+ if(d_state==STATE_STARTED)
+ send_request(d_fc_mhz, d_rf_attn, d_ddc_gain, d_decim, d_offset_hz);
+}
+
+void MSDD6000::set_ddc_gain(int gain){
+ DEBUG("SETTING NEW DDC GAIN");
+ d_ddc_gain = gain;
+ if(d_state==STATE_STARTED)
+ send_request(d_fc_mhz, d_rf_attn, d_ddc_gain, d_decim, d_offset_hz);
+}
+
+void MSDD6000::set_fc(int center_mhz, int offset_hz){
+ DEBUG("SETTING NEW FC");
+ d_fc_mhz = center_mhz;
+ d_offset_hz = offset_hz;
+
+ if(d_state==STATE_STARTED)
+ send_request(d_fc_mhz, d_rf_attn, d_ddc_gain, d_decim, d_offset_hz);
+}
+
+
+void MSDD6000::start(){
+ send_request(d_fc_mhz, d_rf_attn, d_ddc_gain, d_decim, d_offset_hz);
+ d_state = STATE_STARTED;
+ }
+
+
+void MSDD6000::stop(){
+ // new request with 0 decim tells it to halt
+ send_request(d_fc_mhz, d_rf_attn, d_ddc_gain, 0, d_offset_hz);
+ d_state = STATE_STOPPED;
+ }
+
+
+void MSDD6000::send_request(float freq_mhz, float rf_attn, float ddc_gain, float ddc_dec, float ddc_offset_hz){
+ static char buff[512];
+ sprintf(buff, "%f %f %f %f %f\n",freq_mhz, rf_attn, ddc_gain, ddc_dec, ddc_offset_hz);
+ printf("sending: %s\n", buff);
+ int flags = 0;
+ sendto( d_sock, buff, strlen(buff)+1, flags, (const sockaddr*)&d_sockaddr, sizeof(d_sockaddr));
+ }
+
+
+int MSDD6000::read(char* buf, int size){
+ int flags = 0;
+ return recv(d_sock, buf, size, flags);
+ }
+
+
diff --git a/gr-msdd6000/src/non_gr_snapshot_tool/msdd6000.h b/gr-msdd6000/src/non_gr_snapshot_tool/msdd6000.h
new file mode 100644
index 000000000..06fedccf6
--- /dev/null
+++ b/gr-msdd6000/src/non_gr_snapshot_tool/msdd6000.h
@@ -0,0 +1,52 @@
+#ifndef MSDD6000_H
+#define MSDD6000_H
+
+#include <netinet/in.h>
+#include <arpa/inet.h>
+#include <linux/socket.h>
+
+#define DEBUG(A) printf("=debug=> %s\n", A)
+
+#define STATE_STOPPED 0
+#define STATE_STARTED 1
+
+class MSDD6000 {
+ public:
+ MSDD6000(char* addr);
+
+ void set_decim(int decim_pow2);
+ void set_fc(int center_mhz, int offset_hz);
+ void set_ddc_gain(int gain);
+ void set_rf_attn(int attn);
+
+ void set_output(int mode, void* arg);
+
+ void start();
+ void stop();
+
+ void send_request(float,float,float,float,float);
+ int read(char*, int);
+
+ private:
+ int d_decim;
+ int d_fc_mhz;
+ int d_offset_hz;
+ int d_rf_attn;
+ int d_ddc_gain;
+
+// in_addr d_adx;
+ in_addr d_myadx;
+
+ struct sockaddr_in d_sockaddr;
+ struct sockaddr_in d_mysockaddr;
+
+ int d_sock;
+ int d_state;
+};
+
+
+
+
+
+
+#endif
diff --git a/gr-msdd6000/src/non_gr_snapshot_tool/plot_psd.py b/gr-msdd6000/src/non_gr_snapshot_tool/plot_psd.py
new file mode 100755
index 000000000..66d5ae6e2
--- /dev/null
+++ b/gr-msdd6000/src/non_gr_snapshot_tool/plot_psd.py
@@ -0,0 +1,48 @@
+#!/usr/bin/python
+import struct;
+from pylab import *;
+import sys;
+
+#a = fft([1,2,3,4]);
+#print a;
+
+if(len(sys.argv)<2):
+ print "usage ./plot_psd.py <filename>"
+ sys.exit(-1);
+
+filename = sys.argv[1];
+
+#a = open("test.out_002", "r");
+a = open(filename, "r");
+samples = [];
+i_arr = [];
+q_arr = [];
+while(True):
+ d = a.read(1024*1024*4);
+ if(len(d) <= 0):
+ break;
+
+ for i in range(0,(len(d))/8):
+ [s_i, s_q] = struct.unpack_from("<ff", d, i*8);
+ samples.append(complex(s_i, s_q));
+ i_arr.append(s_i);
+ q_arr.append(s_q);
+ break;
+
+fft_data = fftshift(fft(samples));
+for i in range(0,len(fft_data)):
+ if(fft_data[i] == 0):
+ fft_data[i] = 0.01;
+
+plot(20*log10(abs(fft_data)));
+figure();
+subplot(2,1,1);
+plot(i_arr);
+subplot(2,1,2);
+plot(q_arr);
+show();
+
+
+a.close();
+
+
diff --git a/gr-msdd6000/src/non_gr_snapshot_tool/spectrogram.py b/gr-msdd6000/src/non_gr_snapshot_tool/spectrogram.py
new file mode 100755
index 000000000..5ce40de7a
--- /dev/null
+++ b/gr-msdd6000/src/non_gr_snapshot_tool/spectrogram.py
@@ -0,0 +1,88 @@
+#!/usr/bin/python
+
+fft_bins = 1024;
+stride = 256;
+
+#filename = "output.dat";
+#decim = 4;
+#Fs = (102.4/decim) * 1e6;
+
+
+from gnuradio import gr;
+from Numeric import *;
+import FFT;
+import numpy.fft;
+from numpy import *;
+from pylab import *;
+import sys;
+
+if len(sys.argv) <2:
+ print "usage: %s <filename> (sample_rate_in_MSPS) (stride_samples)"%(sys.argv[0]);
+ sys.exit(-1);
+
+filename = sys.argv[1];
+fs = 0;
+if(len(sys.argv)>2):
+ fs = float(sys.argv[2])*1000000;
+print "opening %s.\n"%(filename);
+
+if(len(sys.argv)>=4):
+ stride = int(sys.argv[3]);
+ print "using stride = %d"%(stride);
+
+
+tb = gr.top_block();
+src = gr.file_source(gr.sizeof_gr_complex, filename, False)
+sink = gr.vector_sink_c();
+tb.connect(src,sink);
+tb.run();
+
+data = sink.data();
+dataa = array(data);
+datalen = len( data );
+
+time_bins = (datalen - fft_bins) / stride;
+
+print "output vector :: fft_bins = %d, time_bins = %d\n"%(fft_bins,time_bins);
+
+start_idx = 0;
+
+b = numpy.zeros((time_bins, fft_bins), complex);
+l = [];
+
+window = numpy.blackman(fft_bins);
+
+for i in range(0,time_bins):
+
+ time_chunk = take( dataa, range(start_idx,start_idx + fft_bins), 0);
+ time_chunk = time_chunk * window;
+ fft_chunk = numpy.fft.fftshift(numpy.fft.fft(time_chunk));
+ psd = 10*log10(fft_chunk * conj(fft_chunk)+0.001);
+
+ b[i] = psd.real;
+ l.append( psd.real.tolist() );
+
+ start_idx = start_idx + stride;
+
+#c = array(b, 10);
+
+print b[0];
+c = array(b);
+#l = c.tolist();
+print size(l);
+
+x = range(0,time_bins);
+print size(x);
+y = range(0,fft_bins);
+print size(y);
+
+print size(l);
+
+contourf(l);
+#contourf([x,y], l);
+colorbar();
+show();
+
+#print c[1,1];
+
+