提交 1e61fab6 编写于 作者: M manjaro-xfce

Detect Ok

上级 37a155eb
...@@ -27,8 +27,8 @@ int runSend() ...@@ -27,8 +27,8 @@ int runSend()
char dev_args[] = ""; char dev_args[] = "";
char error_string[4096]; char error_string[4096];
//Sample rate in Hz //Sample rate in Hz
double sprate = 240000 ; double sprate = SIGRATE ;
double tx_freq = 610.0e6; double tx_freq = 200.0e6;
double tx_sprate = sprate; double tx_sprate = sprate;
double tx_gain = 70; double tx_gain = 70;
double tx_bw = sprate; double tx_bw = sprate;
...@@ -85,7 +85,7 @@ int runSend() ...@@ -85,7 +85,7 @@ int runSend()
fprintf(stderr, "Actual TX Rate: %f...\n\n", tx_sprate); fprintf(stderr, "Actual TX Rate: %f...\n\n", tx_sprate);
// Set gain // Set gain
fprintf(stderr, "Setting TX Gain: %f db...\n", tx_gain); fprintf(stderr, "Setting TX Gain: %f db...\n", tx_gain);
UHD_DO(uhd_usrp_set_tx_gain(usrp, tx_gain, 0, "")); UHD_DO(uhd_usrp_set_tx_gain(usrp, tx_gain, tx_channel[0], ""));
// See what gain actually is // See what gain actually is
UHD_DO(uhd_usrp_get_tx_gain(usrp, tx_channel[0], "", &tx_gain)); UHD_DO(uhd_usrp_get_tx_gain(usrp, tx_channel[0], "", &tx_gain));
fprintf(stderr, "Actual TX Gain: %f...\n", tx_gain); fprintf(stderr, "Actual TX Gain: %f...\n", tx_gain);
...@@ -145,7 +145,7 @@ int runSend() ...@@ -145,7 +145,7 @@ int runSend()
1, &num_samps_sent)); 1, &num_samps_sent));
total_sent += num_samps_sent; total_sent += num_samps_sent;
} }
std::this_thread::sleep_for(std::chrono::milliseconds(100)); std::this_thread::sleep_for(std::chrono::milliseconds(10));
} }
......
...@@ -8,6 +8,7 @@ ...@@ -8,6 +8,7 @@
#include <memory> #include <memory>
#include <atomic> #include <atomic>
#include <math.h> #include <math.h>
#include <fftw3.h>
#include "../waveform.h" #include "../waveform.h"
static bool stop_signal_called = false; static bool stop_signal_called = false;
#define UHD_DO(X) \ #define UHD_DO(X) \
...@@ -23,15 +24,12 @@ void sigint_handler(int code){ ...@@ -23,15 +24,12 @@ void sigint_handler(int code){
(void)code; (void)code;
stop_signal_called = true; stop_signal_called = true;
} }
#define READ_BUF_SIZE 1024*1024 static const size_t rxbuf_points = 1000*1000*16;
std::shared_ptr<SPTYPE[][2]> readbuf[2]; static std::shared_ptr<short [][2]> rx_buf_ptr(new short[rxbuf_points+1024*1024][2]{{0,0}});
std::atomic<int> curr_active = 0; static std::atomic< unsigned long long> deal_count (0);
std::atomic<int> curr_deal = 0; static std::atomic< unsigned long long> rx_count (0);
//Sample rate in Hz //Sample rate in Hz
double sprate = 240000 ; double sprate = 240000 ;
const int searchRange = 1001;
std::atomic<int> syn = searchRange/10;
//消费者线程,for rx //消费者线程,for rx
void dealer(); void dealer();
int runRecieve() int runRecieve()
...@@ -43,11 +41,11 @@ int runRecieve() ...@@ -43,11 +41,11 @@ int runRecieve()
char error_string[4096]; char error_string[4096];
//接收频率 //接收频率
double rx_freq = 610e6; double rx_freq = 200e6;
double rx_sprate = sprate; double rx_sprate = SIGRATE;
double rx_gain = 0.0; double rx_gain = 0.0;
bool rx_agc = false; bool rx_agc = true;
double rx_bw = 240000; double rx_bw = SIGRATE;
//接收信号。MIMO时,可以指定0,1 //接收信号。MIMO时,可以指定0,1
size_t rx_channel[] = {0}; size_t rx_channel[] = {0};
//设备句柄 //设备句柄
...@@ -143,51 +141,28 @@ int runRecieve() ...@@ -143,51 +141,28 @@ int runRecieve()
// Issue stream command // Issue stream command
fprintf(stderr, "Issuing stream command.\n"); fprintf(stderr, "Issuing stream command.\n");
readbuf[0] = std::shared_ptr<SPTYPE[][2]> (new SPTYPE[READ_BUF_SIZE+rx_sps_buff*4][2]); short (*pBufRx)[2] = rx_buf_ptr.get();
readbuf[1] = std::shared_ptr<SPTYPE[][2]> (new SPTYPE[READ_BUF_SIZE+rx_sps_buff*4][2]); rx_count = 0;
size_t total_red = 0; deal_count = 0;
UHD_DO(uhd_rx_streamer_issue_stream_cmd(rx_streamer, &rx_stream_cmd)); UHD_DO(uhd_rx_streamer_issue_stream_cmd(rx_streamer, &rx_stream_cmd));
//Read, RX in Main Thread //Read, RX in Main Thread
while (!stop_signal_called) { while (!stop_signal_called)
size_t num_rx_samps = 0; {
SPTYPE * rx_buff = (SPTYPE *) (readbuf[curr_active].get() + total_red); size_t red = 0;
void ** rx_buff_ptr = (void **)&rx_buff; void * rx_buff_ptr = (void *)(pBufRx[rx_count % rxbuf_points]);
// Handle data UHD_DO(uhd_rx_streamer_recv(rx_streamer,&rx_buff_ptr, rx_sps_buff, &rx_meta, 1, false, &red));
UHD_DO(uhd_rx_streamer_recv(rx_streamer, rx_buff_ptr, rx_sps_buff, &rx_meta, 1, false, &num_rx_samps)); rx_count += red;
const size_t newBg = rx_count % rxbuf_points;
if (newBg < rx_sps_buff && newBg > 0)
memcpy(pBufRx[0], pBufRx[rxbuf_points], sizeof(short) * 2 * newBg);
uhd_rx_metadata_error_code_t error_code; uhd_rx_metadata_error_code_t error_code;
UHD_DO(uhd_rx_metadata_error_code(rx_meta, &error_code)); UHD_DO(uhd_rx_metadata_error_code(rx_meta, &error_code));
if(error_code != UHD_RX_METADATA_ERROR_CODE_NONE){ if(error_code != UHD_RX_METADATA_ERROR_CODE_NONE){
fprintf(stderr, "Warning: Error code 0x%x was returned during streaming.\n", error_code); fprintf(stderr, "Warning: Error code 0x%x was returned during streaming.\n", error_code);
puts(error_string);
}
total_red += num_rx_samps;
//切换缓存
if (total_red > READ_BUF_SIZE)
{
fprintf(stderr, "Switch Cache to %d\n", (int) curr_active);
size_t start_move = READ_BUF_SIZE - WAVSIZE;
size_t end_move = total_red-1;
size_t copyto = end_move - start_move + 1;
SPTYPE (* dest)[2] = readbuf[1-curr_active].get();
SPTYPE (* src)[2] = readbuf[curr_active].get();
src += start_move;
memcpy(dest, src , copyto * sizeof(SPTYPE) * 2 );
total_red = copyto;
curr_active = 1 - curr_active;
if (syn>3)
{
rx_stream_cmd.stream_mode = UHD_STREAM_MODE_STOP_CONTINUOUS;
UHD_DO(uhd_rx_streamer_issue_stream_cmd(rx_streamer, &rx_stream_cmd));
while (curr_active != curr_deal)
{
std::this_thread::sleep_for(std::chrono::milliseconds(1000));
fprintf(stderr,"Waiting for syn.\n");
}
rx_stream_cmd.stream_mode = UHD_STREAM_MODE_START_CONTINUOUS;
UHD_DO(uhd_rx_streamer_issue_stream_cmd(rx_streamer, &rx_stream_cmd));
}
} }
} }
if (rx_streamer) uhd_rx_streamer_free(&rx_streamer); if (rx_streamer) uhd_rx_streamer_free(&rx_streamer);
if (rx_meta) uhd_rx_metadata_free(&rx_meta); if (rx_meta) uhd_rx_metadata_free(&rx_meta);
if(return_code != EXIT_SUCCESS && usrp != NULL){ if(return_code != EXIT_SUCCESS && usrp != NULL){
...@@ -203,139 +178,121 @@ int runRecieve() ...@@ -203,139 +178,121 @@ int runRecieve()
void runDeal() void runDeal()
{ {
const int DealEnd = READ_BUF_SIZE - WAVSIZE; unsigned long long next_test = WAVSIZE;
float (*cross[searchRange])[2][2] ; const unsigned int half_sz = WAVSIZE/2;
for (int i=0;i<searchRange;++i) short (*pBufRx)[2] = rx_buf_ptr.get();
cross[i] = new float[DealEnd][2][2]; double (* ampwin)[2] = new double[WAVSIZE][2];
float (*judge[searchRange])[2] ;
for (int i=0;i<searchRange;++i)
judge[i] = new float[DealEnd][2];
float (*freq_mv[searchRange])[2]; fftw_complex *in[2], *out[2];
for (int i=0;i<searchRange;++i) fftw_plan p[2];
freq_mv[i] = new float[DealEnd][2];
float (*sig[searchRange])[2] ; in[0] = (fftw_complex*) fftw_malloc(sizeof(fftw_complex) * WAVSIZE);
for (int i=0;i<searchRange;++i) out[0] = (fftw_complex*) fftw_malloc(sizeof(fftw_complex) * WAVSIZE);
sig[i] = new float[DealEnd][2]; p[0] = fftw_plan_dft_1d(WAVSIZE, in[0], out[0], FFTW_FORWARD, FFTW_ESTIMATE);
in[1] = (fftw_complex*) fftw_malloc(sizeof(fftw_complex) * WAVSIZE);
out[1] = (fftw_complex*) fftw_malloc(sizeof(fftw_complex) * WAVSIZE);
p[1] = fftw_plan_dft_1d(WAVSIZE, in[1], out[1], FFTW_FORWARD, FFTW_ESTIMATE);
//fftw_execute(p); /* repeat as needed */
double globalMax = 0;
while (!stop_signal_called)
{
if (deal_count + WAVSIZE >= rx_count || rx_count < WAVSIZE )
{
std::this_thread::sleep_for(std::chrono::milliseconds(1));
continue;
}
if (deal_count < next_test)
{
ampwin[deal_count % WAVSIZE][0] = 0;
ampwin[deal_count % WAVSIZE][1] = 0;
++deal_count;
continue;
}
unsigned long long start_x = deal_count - WAVSIZE;
//xorr
for (size_t i=0;i<WAVSIZE;++i)
{
const int xb = (start_x + i) % rxbuf_points;
in[0][i][0] = (pBufRx[xb][0] * wav_xorr[0][i][0] - pBufRx[xb][1] * wav_xorr[0][i][1])/16384.0;
in[0][i][1] = (pBufRx[xb][0] * wav_xorr[0][i][1] + pBufRx[xb][1] * wav_xorr[0][i][0])/16384.0;
in[1][i][0] = (pBufRx[xb][0] * wav_xorr[1][i][0] - pBufRx[xb][1] * wav_xorr[1][i][1])/16384.0;
in[1][i][1] = (pBufRx[xb][0] * wav_xorr[1][i][1] + pBufRx[xb][1] * wav_xorr[1][i][0])/16384.0;
}
fftw_execute(p[0]);
fftw_execute(p[1]);
double max_abs0 = 0, max_abs1 = 0;
for (size_t i=0;i<WAVSIZE;++i)
{
const double a0 = out[0][i][0]*out[0][i][0] + out[0][i][1]*out[0][i][1];
const double a1 = out[1][i][0]*out[1][i][0] + out[1][i][1]*out[1][i][1];
if (a0 > max_abs0)
max_abs0 = a0;
if (a1 > max_abs1)
max_abs1 = a1;
}
const float pi = 3.1415927; ampwin[deal_count % WAVSIZE][0] = max_abs0;
ampwin[deal_count % WAVSIZE][1] = max_abs1;
#pragma omp parallel for if (globalMax < max_abs0)
for (int df = 0;df<searchRange;++df)
{
float f = (df-searchRange/2.0)*10;
for (int pv = 0;pv<DealEnd;++pv)
{ {
float t = pv / sprate; //printf("%lf,%lf\n",max_abs0,max_abs1);
freq_mv[df][pv][0] = cos(2*pi*f*t); globalMax = max_abs0;
freq_mv[df][pv][1] = sin(2*pi*f*t);
} }
} if (globalMax < max_abs1)
int startDF =0, endDF = searchRange;
while (!stop_signal_called) {
if ((int)curr_active==(int)curr_deal)
{ {
std::this_thread::sleep_for(std::chrono::milliseconds(1)); //printf("%lf,%lf\n",max_abs0,max_abs1);
continue; globalMax = max_abs1;
} }
int deal = 1-curr_active;
short (*pBufRx)[2] = readbuf[deal].get();
fprintf(stderr,"Dealing %d\n",deal); bool best0 = true, best1=true;
float maxAbs = 0;
int maxDF = 2; for (size_t i=0;i<WAVSIZE && (best0||best1);++i)
int stp = syn;
#pragma omp parallel for
for (int df = startDF;df<endDF;df+=stp)
{ {
//xorr if (i!=half_sz)
#pragma omp simd
for (size_t i=0;i<DealEnd;++i)
{
sig[df][i][0] = (pBufRx[i][0] * freq_mv[df][i][0] - pBufRx[i][1] * freq_mv[df][i][1]);
sig[df][i][1] = (pBufRx[i][0] * freq_mv[df][i][1] + pBufRx[i][1] * freq_mv[df][i][0]);
}
#pragma omp simd
for (size_t i=0;i<DealEnd;++i)
{ {
cross[df][i][0][0] = 0; if (ampwin[(deal_count +i - WAVSIZE )% WAVSIZE][0] > ampwin[(deal_count +half_sz - WAVSIZE )% WAVSIZE][0])
cross[df][i][0][1] = 0; best0 = false;
cross[df][i][1][0] = 0; if (ampwin[(deal_count +i - WAVSIZE )% WAVSIZE][1] > ampwin[(deal_count +half_sz - WAVSIZE )% WAVSIZE][1])
cross[df][i][1][1] = 0; best1 = false;
for (size_t j=0;j<WAVSIZE;++j)
{
cross[df][i][0][0] += (sig[df][i+j][0] * wav_xorr[0][j][0] - sig[df][i+j][1] * wav_xorr[0][j][1]);
cross[df][i][0][1] += (sig[df][i+j][0] * wav_xorr[0][j][1] + sig[df][i+j][1] * wav_xorr[0][j][0]);
cross[df][i][1][0] += (sig[df][i+j][0] * wav_xorr[1][j][0] - sig[df][i+j][1] * wav_xorr[1][j][1]);
cross[df][i][1][1] += (sig[df][i+j][0] * wav_xorr[1][j][1] + sig[df][i+j][1] * wav_xorr[1][j][0]);
}
judge[df][i][0] = (cross[df][i][0][0]/32768 * cross[df][i][0][0]/32768 + cross[df][i][0][1]/32768 * cross[df][i][0][1]/32768 );
judge[df][i][1] = (cross[df][i][1][0]/32768 * cross[df][i][1][0]/32768 + cross[df][i][1][1]/32768 * cross[df][i][1][1]/32768 );
} }
float maxv = 0; }
#pragma omp simd if (best0)
for (size_t i=0;i<DealEnd;++i) if (ampwin[(deal_count +half_sz - WAVSIZE )% WAVSIZE][1]*10 > ampwin[(deal_count +half_sz - WAVSIZE )% WAVSIZE][0])
{ {
if (maxv<judge[df][i][0]) best0 = false;
maxv = judge[df][i][0];
if (maxv<judge[df][i][1])
maxv = judge[df][i][1];
} }
#pragma omp critical if (best1)
if (ampwin[(deal_count +half_sz - WAVSIZE )% WAVSIZE][0]*10 > ampwin[(deal_count +half_sz - WAVSIZE )% WAVSIZE][1])
{ {
if (maxv > maxAbs) best1 = false;
{
maxAbs = maxv;
maxDF = df;
}
} }
}
printf ("MaxDF=%d\n",maxDF);
for (size_t i=0;i<DealEnd;++i)
{
if (judge[maxDF][i][0] >= 5000 || judge[maxDF][i][1] >= 5000 )
{
if (judge[maxDF][i][0] * 32 < judge[maxDF][i][1])
{
printf("%lu:0=%f, 1=%f, 1\n",i,judge[maxDF][i][0],judge[maxDF][i][1]);
fflush(stdout);
i+=WAVSIZE;
startDF = (maxDF - stp*2)<0?0:(maxDF - stp*2);
endDF = (maxDF + stp*2)>searchRange?searchRange:((maxDF + stp*2));
if (syn > 3)
syn = (endDF - startDF)/10;
if (syn<1)
syn = 1;
}
else if (judge[maxDF][i][1] * 32 < judge[maxDF][i][0])
{
printf("%lu:0=%f, 1=%f, 0\n",i,judge[maxDF][i][0],judge[maxDF][i][1]);
fflush(stdout);
i+=WAVSIZE;
startDF = (maxDF - stp*2)<0?0:(maxDF - stp*2);
endDF = (maxDF + stp*2)>searchRange?searchRange:((maxDF + stp*2));
if (syn > 3)
syn = (endDF - startDF)/10;
if (syn<1)
syn = 1;
}
}
if (best0 )
{
next_test = deal_count + WAVSIZE/2 - MODRATE_N * 16;
putchar('0');
fflush(stdout);
} }
//判决 else if (best1 )
curr_deal = 1-deal; {
} next_test = deal_count + WAVSIZE/2 - MODRATE_N * 16;
for (int i=0;i<searchRange;++i) putchar('1');
{ fflush(stdout);
delete [] cross[i]; }
delete [] judge[i]; ++deal_count;
delete [] freq_mv[i];
delete [] sig[i];
} }
delete [] ampwin;
fftw_destroy_plan(p[0]);
fftw_free(in[0]); fftw_free(out[0]);
fftw_destroy_plan(p[1]);
fftw_free(in[1]); fftw_free(out[1]);
} }
int main() int main()
{ {
......
...@@ -7,7 +7,7 @@ SOURCES += \ ...@@ -7,7 +7,7 @@ SOURCES += \
../waveform.cpp \ ../waveform.cpp \
main.cpp main.cpp
LIBS += -luhd -lpthread -lgomp LIBS += -luhd -lpthread -lgomp -lfftw3
QMAKE_CXXFLAGS_RELEASE += -fopenmp -O3 -mavx2 -fopt-info QMAKE_CXXFLAGS_RELEASE += -fopenmp -O3 -mavx2 -fopt-info
QMAKE_CFLAGS_RELEASE += -fopenmp -O3 -mavx2 -fopt-info QMAKE_CFLAGS_RELEASE += -fopenmp -O3 -mavx2 -fopt-info
......
#ifndef WAVEFORM_H #ifndef WAVEFORM_H
#define WAVEFORM_H #define WAVEFORM_H
typedef short SPTYPE; typedef short SPTYPE;
#define MODRATE_N 4 #define MODRATE_N 1
#define C_AMP 30000 #define C_AMP 8192
#define SPREAD_RAT 128 #define SPREAD_RAT 256
#define WAVSIZE SPREAD_RAT*MODRATE_N #define WAVSIZE SPREAD_RAT*MODRATE_N
#define SIGRATE 240000
extern SPTYPE wav_spread[2][WAVSIZE][2]; extern SPTYPE wav_spread[2][WAVSIZE][2];
extern float wav_xorr[2][WAVSIZE][2]; extern float wav_xorr[2][WAVSIZE][2];
void init_wavform(); void init_wavform();
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册