📄 dsk_app.c
字号:
createTrig(transmit_Handle, FRAMELEN);
copyData2x2(temp, transmit_Handle, 124*4, 48);
DSK6713_LED_toggle(1);
}
#define SEARCH_INTERVAL 160
//function to get the time index (first symbol)
int sync(float *in , const float *training_seq)
{
int i;
int k;
float sum;
int index = 0;
float max = 0;
for (i = 0; i < (SEARCH_INTERVAL); i++) // we assume the delay is not larger than twice the padding
{
sum = 0;
for(k = 0; k < TRAININGSIZE; k++)
{
sum += in[k*(UPFACTOR)+i]*training_seq[k];
}
if (sum > max) {
max = sum;
index = i;
}
}
return index;
}
void extractFrame(short *framebuffer, short length)
{
int i;
// you receive rx : size upfactor*buffsize+(filter_length-1)
memset(temp,0,(RECIEVELEN+160)*sizeof(float));
for(i=0;i<RECIEVELEN;i++)
temp[i+80] = (float)framebuffer[i];
DSPF_sp_convol(temp, cos_filter, rx, FILTER_LENGTH-1, RECIEVELEN+(FILTER_LENGTH-1));
// Remove Cyclic Prefix, downsample, stuff Zeros at imaginary slots, and do extraction.
for (i=0;i<256;i++)
{
temp[i*2]=rx[RefIndex+i*UPFACTOR+32*UPFACTOR]; // 32 should be multiplied by 8 also
temp[i*2+1] = 0; // the imaginary numbers..
}
// Run FFT
dft_DSP(temp, workbuffer); // workbuffer contains 254 values now.
div_element(workbuffer, h_hat, temp, 2*SUBCARSIZE);
demodulation_shart(temp, finalData); // Just to decode the data..
loopIndex++;
}
// in should have downfactor more values than out. output_size defines the size of out.
void downsampling(short *out, const short *in, short output_size, short downfactor)
{
short i;
for (i=0; i<output_size; i++)
{
// Take value 3 and 4, of 5 values, and take the mean.
out[i] = (in[i*downfactor+3]+in[i*downfactor+2]) >> 1;
}
}
/*
* copy2Data() - Copy one buffer with length elements to another.
* And remove every second value, which belongs to the other input channel.
*/
void copy2Data(const short *inbuf, short *outbuf, const short length)
{ // length is length of outbuffer, == 1/2 inbuf
short i;
for (i = 0; i < length; i++) {
outbuf[i] = inbuf[i*2];
}
}
/*
* copyData2x2() - Copy one buffer with length elements to another.
* Upsamples by 8(?), signal starts from offset*2, to keep track of which channel is used.
*/
void copyData2x2(const float *inbuf, short *outbuf, const short length, const short offset)
{ // length is length of inbuffer, == 1/2 outbuf
short i, j;
short pulse[4] = {0x5a81, 0x7ffd, 0x5a81, 0x0000};
for (i = 0; i < length; i++) { //fills up 4072 numbers in outbuf
for(j = 0; j < (4); j++)
outbuf[i*8+j*2+offset*2] = pulse[j]*(short)inbuf[i];
}
}
void createTrig(short *databuf, const short length)
{
short i; // k=0;
// Length of sine wave table *
const short sin_size = 48; // 72
for(i=48; i<BUFFSIZE; i++)
databuf[i]=0;
for(i=0; i<(sin_size); i++)
{
databuf[i*2]=sinetable[i];
//k++;
}
}
void framesync(short *databuf, short *startindex, short length)
{
// The databuf might contain a trigger signal, the same as the defined sinetable here.
/* Length of sine wave table */
const short sin_size = 48; // 72
int res[2];
correlation(sinetable, databuf, sin_size, length, res);
startindex[0] = res[1];
if(res[0] < 7000000){ // is the correlation over minimum treshold.
startindex[0] = 0; // 35000000 is possible treshold for no channel..
}else{
DSK6713_LED_toggle(0);
}
}
void correlation(const short *reference, const short *input, const short len_ref, const short len_inp, int *res)
{
short i,k;
int sum;
res[0]=0;
for (i = 0; i < (len_inp - len_ref); i++){
sum = 0;
for (k = i; k < (len_ref+i); k++){
sum = sum + (((input[k] >> 4) * (reference[k-i] >> 4)));
if(sum<-10000) // The sum should always be positive over the sine, this test can give an important speedup in noisy channels.
break;
}
if(res[0]<(sum)) // Find and store the point with largest correlation.
{
res[0] = (sum);
res[1] = i;
}
}
}
/* function y = demodulation(x,b,e,h)
* find the symbol in the defined modulation scheme map which has the minimum distance to the received symbol,
* output the binary code corresponding to the index
*
* b is the subcarrier bit allocation, b(i) is the number of bits allocated to ith subcarrier
*
* ------- demodulation.m -----------------------------------
* Black team, Erik
* April-11-05
* ----------------------------------------------------------
****************************************************************/
#define LEN (128-1-3) // Assuming framelength always vill be 128, might be adjusted later.
void demodulation_shart(const float *input, unsigned int *output)
{
float k[2];
short i, tmp = 0; // tmp gives the index of the d var.
// short t[LEN];
// short b2; // which int[x] from input should be used.
// short b3; // which bit in the int is our first bit to consider.
// short b4[LEN]; // real bit position from start of input
// b2=0; // '0' indicates the first int.
// b3=0; // '0' indicates the first bit of an int.
for(i=0; i<LEN; i++)
{
if(b[i] == 2)
{
if(input[i*2]<0) {
if(input[i*2+1]<0)
tmp = 2;
else
tmp = 0;
} else {
if(input[i*2+1]<0)
tmp = 3;
else
tmp = 1;
}
output[b2] = output[b2] | (tmp << b3);
b3+=2;
if(b3==32)
{
b3=0;
b2++;
if(b2>=INTS_IN_FILE)
break;
}
} else if(b[i] == 4) {
k[0] = input[i*2];
k[1] = input[i*2+1];
if(k[0]<0)
tmp = 0; // Init tmp
else
tmp = 0x08;
if(k[0] > -0.6330 && k[0] < 0.6330)
tmp += 0x04;
if(k[1]>0)
tmp += 0x02;
if(k[1] > -0.6330 && k[1] < 0.6330)
tmp += 0x01;
if((32-b3)<4) { // Last bit of integer, goto next.
output[b2] = output[b2] | (tmp << b3);
output[b2+1] = (tmp >> (32-b3));
b3 = (b3+4)%32;
b2++;
if(b2>=INTS_IN_FILE)
break;
} else {
output[b2] = output[b2] | (tmp << b3);
b3 += 4;
if(b3 == 32) {
b3 = 0;
b2++;
}
}
} else if(b[i] == 6) {
k[0] = input[i*2];
k[1] = input[i*2+1];
if(k[1]>0)
tmp = 0; // Init tmp
else
tmp = 0x20;
if(k[0]>0)
tmp += 0x04;
if(k[0] > -0.3086 && k[0] < 0.3086) // case +-0.15
tmp += 0x02;
if(k[1] > -0.3086 && k[1] < 0.3086)
tmp += 0x10;
if(k[0] > -0.6172 && k[0] <= -0.3086 || k[0] < 0.6172 && k[0] >= 0.3086) // case +-0.46
tmp += 0x03;
if(k[1] > -0.6172 && k[1] <= -0.3086 || k[1] < 0.6172 && k[1] >= 0.3086) // case +-0.46
tmp += 0x18;
if(k[0] > -0.9258 && k[0] <= -0.6172 || k[0] < 0.9258 && k[0] >= 0.6172) // case +-0.77
tmp += 0x01;
if(k[1] > -0.9258 && k[1] <= -0.6172 || k[1] < 0.9258 && k[1] >= 0.6172) // case +-0.77
tmp += 0x08;
if((32-b3)<6) { // Last bit of integer, goto next.
output[b2] = output[b2] | (tmp << b3);
output[b2+1] = (tmp >> (32-b3));
b3 = (b3+6)%32;
b2++;
if(b2>=INTS_IN_FILE)
break;
} else {
output[b2] = output[b2] | (tmp << b3);
b3 += 6;
if(b3 == 32) {
b3 = 0;
b2++;
}
}
} else if(b[i] == 8) {
k[0] = input[i*2];
k[1] = input[i*2+1];
// Halvera koden genom att k鰎a en forlop 鰒er index i k[]?
if(k[1]>0)
tmp = 0; // Init tmp
else
tmp = 0x80;
if(k[0]>0)
tmp += 0x08;
if(k[0] > -0.1534 && k[0] < 0.1534) // case +-0.0767
tmp += 0x04;
if(k[1] > -0.1534 && k[1] < 0.1534)
tmp += 0x40;
if(k[0] > -1.0738 && k[0] <= -0.9204 || k[0] < 1.0738 && k[0] >= 0.9204) // case +-0.9971
tmp += 0x01;
if(k[1] > -1.0738 && k[1] <= -0.9204 || k[1] < 1.0738 && k[1] >= 0.9204) // case +-0.9971
tmp += 0x10;
if(k[0] > -0.7670 && k[0] <= -0.6136 || k[0] < 0.7670 && k[0] >= 0.6136) // case +-0.6903
tmp += 0x02;
if(k[1] > -0.7670 && k[1] <= -0.6136 || k[1] < 0.7670 && k[1] >= 0.6136) // case +-0.6903
tmp += 0x20;
if(k[0] > -0.9204 && k[0] <= -0.7670 || k[0] < 0.9204 && k[0] >= 0.7670) // case +-0.8437
tmp += 0x03;
if(k[1] > -0.9204 && k[1] <= -0.7670 || k[1] < 0.9204 && k[1] >= 0.7670) // case +-0.8437
tmp += 0x30;
if(k[0] > -0.3068 && k[0] <= -0.1534 || k[0] < 0.3068 && k[0] >= 0.1534) // case +-0.2301
tmp += 0x05;
if(k[1] > -0.3068 && k[1] <= -0.1534 || k[1] < 0.3068 && k[1] >= 0.1534) // case +-0.2301
tmp += 0x50;
if(k[0] > -0.6136 && k[0] <= -0.4602 || k[0] < 0.6136 && k[0] >= 0.4602) // case +-0.5369
tmp += 0x06;
if(k[1] > -0.6136 && k[1] <= -0.4602 || k[1] < 0.6136 && k[1] >= 0.4602) // case +-0.5369
tmp += 0x60;
if(k[0] > -0.4602 && k[0] <= -0.3068 || k[0] < 0.4602 && k[0] >= 0.3068) // case +-0.3835
tmp += 0x07;
if(k[1] > -0.4602 && k[1] <= -0.3068 || k[1] < 0.4602 && k[1] >= 0.3068) // case +-0.3835
tmp += 0x70;
if((32-b3)<8) { // Last bit of integer, goto next.
output[b2] = output[b2] | (tmp << b3);
output[b2+1] = (tmp >> (32-b3));
b3 = (b3+8)%32;
b2++;
if(b2>=INTS_IN_FILE)
break;
} else {
output[b2] = output[b2] | (tmp << b3);
b3 += 8;
if(b3 == 32) {
b3 = 0;
b2++;
}
}
} else { // case b==0, no information in carrier
if(b2>=INTS_IN_FILE)
break;
// lalalala
}
}
}
void rtdx_send_result(void)
{
int status,i, k;
IRQ_globalDisable(); // Disable global interrupts during setup
for(i=0; i<124; i++) {
finalData[i+400] = (unsigned int)10*log10((double)gn[i]);
finalData[i+524] = b[i];
}
TARGET_INITIALIZE();
for(k=0; k<(200*3); k+=200){
for(i=0; i<(200);i++)
arraydata[i]= finalData[i+k];
LOG_printf(&trace, "Value %u was sent to the host\n", k );
// enable the output channel
RTDX_enableOutput( &ochan );
// send an array of integers to the host
status = RTDX_write( &ochan, arraydata, 200*4); //sizeof(arraydata) );
if ( status == 0 ) {
puts( "ERROR: RTDX_write failed!\n" );
exit( -1 );
}
DSK6713_LED_toggle(0);
while ( RTDX_writing != NULL );
}
// Send the last block which is less then 200 elements.
for(i=0; i<(48);i++)
arraydata[i]= finalData[i+k];
LOG_printf(&trace, "Value %u was sent to the host\n", k );
// enable the output channel
RTDX_enableOutput( &ochan );
// send an array of integers to the host
status = RTDX_write( &ochan, arraydata, 48*4); //sizeof(arraydata) );
if ( status == 0 ) {
puts( "ERROR: RTDX_write failed!\n" );
exit( -1 );
}
DSK6713_LED_toggle(0);
while ( RTDX_writing != NULL );
// disable the output channel
RTDX_disableOutput( &ochan );
exit(0);
}
void qpsk(float *output, short *input)
{
short im_p4[] = {-1, 1, 1, 1, -1, -1, 1, -1};
short i, tmp=0; // The tmp is the index number which defines the value of the bits defined by b.
short b_2=0; // which int[x] from input should be used.
short b_3=0; // which bit in the int is our first bit to consider.
// '0' indicates the first int.
// '0' indicates the first bit of an int.
for(i=0; i<(124*2); i++){
tmp = (input[b_2]&(0x03 << b_3)) >> b_3; // get the 2 bits and shift to become a correct index
output[i*2] = im_p4[tmp*2];
output[i*2+1] = im_p4[tmp*2+1];
b_3 +=2;
if(b_3==4){
b_3=0;
b_2++;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -