⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 dsk_app.c

📁 详细的OFDM设计过程
💻 C
📖 第 1 页 / 共 3 页
字号:
   	}
   	
   	switch(State){
   	
   		case START: 
   			memset(upsampBufferInterleaved,0,BUFFSIZE*sizeof(Int16));
   			sine_sync(upsampBufferInterleaved,sinetable, SINE_TABLE_SIZE, BUFFSIZE);
			memcpy(pBufferXmt,upsampBufferInterleaved, BUFFSIZE*sizeof(Int16));
			memset(maxtime,0,TOTFRAMES*sizeof(float));
			memset(constellation,0,TOTFRAMES*sizeof(float));
			DSK6713_LED_toggle(2);
			frame_number = 0;						
 			data_frame_counter =0;
 			uBitsTransmitted = 0;
 			uTotalBits = (DATABUFFSIZE*32);
			uBitsRemainInFile = uTotalBits;
			nextFrame =0;
			State = TRAINING; 			
			break;
		
		case TRAINING:
			frame_number++;
			dft_DSP(trainingBuffer, idftBuffer);
			//memset(upBuffer, 0, (LASTBUFF*UPFACTOR+160)*sizeof(float));
			upsampling3(idftBuffer, upBuffer, LASTBUFF, UPFACTOR);
			DSPF_sp_convol(upBuffer, cosfilter, upsampBuffer ,FILTER_LENGTH-1, LASTBUFF*UPFACTOR+FILTER_LENGTH-1);
			interleavebuffer2(upsampBufferInterleaved, upsampBuffer, (LASTBUFF*UPFACTOR)+(FILTER_LENGTH-1));
			memcpy(pBufferXmt,upsampBufferInterleaved, BUFFSIZE*sizeof(Int16));
			if(data_frame_counter>0){
				State = RUNNING;
			}else{
				State = SYNC;	
			} 	
			
			break;	
		
		case SYNC:
			frame_number++;
			copy2Data(pBufferRcv, upsampBufferInterleaved, (BUFFSIZE>>1));
			DSK6713_LED_off(0);
			DSK6713_LED_toggle(1);
			if(!nextFrame){
				framesync(upsampBufferInterleaved, startindex, BUFFSIZE>>1);
				if(startindex[0]!=0){
					
					if((startindex[0]+48)<((BUFFSIZE>>1)-UPFACTOR2*SUBCARSIZE*4)){
						downsampling(downsampBuffer, &upsampBufferInterleaved[startindex[0]+48], 4*SUBCARSIZE, UPFACTOR2);
						//memset(bitAlloc,0,SUBCARSIZE*sizeof(short));
						//demodulation_shart(downsampBuffer, bitAlloc);
						State = RUNNING;
						DSK6713_LED_on(0);
					} else {  //The frame overlaps the buffer borders..
						copyData(&upsampBufferInterleaved[startindex[0]+48], downsampBuffer, ((BUFFSIZE>>1)-startindex[0]-48));
						nextFrame = 4*UPFACTOR2*SUBCARSIZE-((BUFFSIZE>>1)-startindex[0]-48);			
					}
				}
				else{
					if(frame_number==10){
					State = START;
					}
				}
			}else{
				copyData(upsampBufferInterleaved, &downsampBuffer[(4*UPFACTOR2*SUBCARSIZE)-nextFrame], (nextFrame));
				downsampling(upsampBufferInterleaved, downsampBuffer, 4*SUBCARSIZE, UPFACTOR2);
				//memset(bitAlloc,0,SUBCARSIZE*sizeof(short));
				//demodulation_shart(upsampBufferInterleaved, bitAlloc);
				State = RUNNING;
				DSK6713_LED_on(0);
			}
			//memset((void*)upsampBufferInterleaved, 0, BUFFSIZE*sizeof(Int16));
			break; 
		
		case RUNNING:
			frame_number++;
			data_frame_counter++;
			uBitsInCurrFrame = total_bits(bitAlloc, SUBCARSIZE); //254 for simple			
			uStartElement = (uBitsTransmitted>>5);
			index = uBitsTransmitted%32;
			uElementsInCurrFrame = (uBitsInCurrFrame>>5)+2;
			
			if(uBitsRemainInFile >= uBitsInCurrFrame){
				memcpy(tempBuffer, &dataBuffer[uStartElement], uElementsInCurrFrame*sizeof(Uint32));
				uBitsTransmitted += uBitsInCurrFrame;
			}
			else{
				uElementsInCurrFrame = (uBitsRemainInFile>>5)+1;//(Int16)ceil(index/32);
				memset(tempBuffer,0xFFFFFFFF,TEMPBUFFSIZE*sizeof(Uint32)); 
				memcpy(tempBuffer, &dataBuffer[uStartElement], uElementsInCurrFrame*sizeof(Uint32));
				uBitsTransmitted += uBitsRemainInFile;
			}
			
			modulation2(modulationBuffer, tempBuffer, bitAlloc, index);
			constellation[2*frame_number]=modulationBuffer[20];
			constellation[2*frame_number+1]=modulationBuffer[21];
			dft_DSP(modulationBuffer, idftBuffer);
			upsampling3(idftBuffer, upBuffer, LASTBUFF, UPFACTOR);
			DSPF_sp_convol(upBuffer, cosfilter, upsampBuffer ,FILTER_LENGTH-1, LASTBUFF*UPFACTOR+FILTER_LENGTH-1);
			/*for(i=0;i<2384;i++){
				if(upsampBuffer[i]>4){
					upsampBuffer[i] = 4;
				}
				else if(upsampBuffer[i]<-4){
					upsampBuffer[i] = -4;
				}
			}*/
			interleavebuffer2(upsampBufferInterleaved, upsampBuffer, LASTBUFF*UPFACTOR+FILTER_LENGTH-1);
			memcpy(pBufferXmt,upsampBufferInterleaved, BUFFSIZE*sizeof(Int16));
			
			
			/*Update index and counter to pick start of next modulation*/
			uBitsRemainInFile = (DATABUFFSIZE*32)-uBitsTransmitted;
			
			min_abs = _fabsf(DSPF_sp_minval(upsampBuffer, 2384));
			max_abs = _fabsf(DSPF_sp_maxval(upsampBuffer, 2384));
			maxtime[frame_number] = MAX(max_abs,min_abs);
			
			if((data_frame_counter%20)==0){
				State = TRAINING;
			}
			
			/*if(data_frame_counter==5){
				DSK6713_LED_on(0);
			}*/
			
			if(!(uBitsRemainInFile > 0)){
				uBitsTransmitted = 0;
				uBitsRemainInFile = (DATABUFFSIZE*32);
				State = LASTFRAME;
			}
			
			
			LOG_printf(&sys, "data frame: %d",data_frame_counter);
			
			DSK6713_LED_toggle(3);
			break;
			
		case LASTFRAME:
			frame_number++;
			memset(pBufferXmt,0,BUFFSIZE*sizeof(Int16));
			State = END;
			break;
		
		case END:
			frame_number++;
   			maximum = DSPF_sp_maxval(maxtime, TOTFRAMES);
   			LOG_printf(&sys, "Peak: %f",maximum);
			LOG_printf(&trace, "File Transmitted");
			frame_number=0;
			State = START;
			exit(1);
			break;
  	}
  	LOG_printf(&trace, "%d", frame_number);

}

/*
 *  blinkLED() - Periodic thread (PRD) that toggles LED #0 every 500ms if 
 *               DIP switch #0 is depressed.  The thread is configured
 *               in the DSP/BIOS configuration tool under Scheduling -->
 *               PRD --> PRD_blinkLed.  The period is set there at 500
 *               ticks, with each tick corresponding to 1ms in real
 *               time.
 */
void blinkLED(void)
{
    /* Toggle LED #0 if DIP switch #0 is off (depressed) */
    if (!DSK6713_DIP_get(0))
        DSK6713_LED_toggle(0);
}


/*
 *  load() - PRD that simulates a 20-25% dummy load on a 225MHz 6713 if
 *           DIP switch #1 is depressed.  The thread is configured in
 *           the DSP/BIOS configuration tool under Scheduling --> PRD
 *           PRD_load.  The period is set there at 10 ticks, which each tick
 *           corresponding to 1ms in real time.
 */
void load(void)
{
    volatile Uint32 i;  

    if (!DSK6713_DIP_get(1))
        for (i = 0; i < 30000; i++);
}

void readRDTX(void){
	
	//int arraydata[MAX_ELEMENTS]; 
	unsigned int length;
	int status,N_rtdx;
	int nr_last,i,k; 
	
	//TARGET_INITIALIZE(); 
	/* enable the input channel */ 
	RTDX_enableInput( &ichan ); 
 
/*	status = RTDX_read( &ichan, &length, sizeof(length) );
	if ( status != sizeof(length) 	 ) { 
		printf( "ERROR: RTDX_read failed!\n" ); 
		exit( -1 ); 
	} else 
	LOG_printf(&rtdx, "Length %d was received from the host\n", length );	*/
	
	length = 16654;
	N_rtdx = (int)floorf((float)length*0.005);			//0.02=length/MAX_ELEMENTS
	nr_last = length - N_rtdx * MAX_ELEMENTS ;
	
	// receive an array from host. 
	for(k=0;k<N_rtdx;k++){
		
	status = RTDX_read( &ichan, arraydata, sizeof(arraydata) ); 
	 	
	if ( status != sizeof(arraydata) 	 ) { 
		LOG_printf(&rtdx, "ERROR: RTDX_read failed!\n" ); 
		exit( -1 ); 
	} else 
		for( i = 0; i < MAX_ELEMENTS; i++) 
			dataBuffer[i + k * MAX_ELEMENTS] = arraydata[i];
//	printf( "Value %d was received from the host\n",dataBuffer[i]);
		
	}
	
	
	if(nr_last){
	status = RTDX_read( &ichan, arraydata, nr_last*sizeof(int) ); 
	 	
	if ( status != nr_last*sizeof(int) 	 ) { 
		printf( "ERROR: RTDX_read failed!\n" ); 
		exit( -1 ); 
	} else 
		for( i = 0; i < nr_last; i++) 
			dataBuffer[i + N_rtdx * MAX_ELEMENTS] = arraydata[i];
	}
	
//	for( i = 0; i < length; i++) 
	
	//	printf( "Value %d was received from the host\n",dataBuffer[i]);
		 
	
	/* disable the input channel */ 
	RTDX_disableInput( &ichan ); 
 
	LOG_printf(&rtdx, "Done!\n" );
}

void copy2Data(const short *inbuf, short *outbuf, const short length)
{	// length is length of outbuffer, == 1/2 inbuf
    short i = 0;
    
    for (i = 0; i < length; i++) {                      
        outbuf[i]  = inbuf[i*2];
    }
}

void framesync(short *databuf, short *startindex, short length)
{
	// The databuf might contain a trigger signal, the same as the defined sinetable here. 
	int res[2];
	
	correlation(sinetable, databuf, SINE_TABLE_SIZE, length, res);
	
	startindex[0] = res[1];
	
	if(res[0] < 7000000){		// is the correlation over minimum treshold. 
		startindex[0] = 0;
	}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)
				break;
		}
		if(res[0]<(sum))			// Find and store the point with largest correlation. 
		{
			res[0] = (sum);
			res[1] = i;
		}
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -