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

📄 dsk_app_up4_pic.c

📁 详细的OFDM设计过程
💻 C
📖 第 1 页 / 共 3 页
字号:
    float min_abs,max_abs;
    float maximum;
    
            
    /* Get contents of mailbox posted by edmaHwi */
    pingPong =  SWI_getmbox();

    /* Copy data from transmit to receive, could process audio here */
    if (pingPong == PING) { 
 		pBufferXmt = gBufferXmtPing;
        pBufferRcv = gBufferRcvPing;
        } else {
	    pBufferXmt = gBufferXmtPong;
        pBufferRcv = gBufferRcvPong;
   	}
   	
   	switch(State){
   	
   		case START:
   			memset(upsampBufferInterleaved,0,BUFFSIZE*sizeof(Int16));
   			//send sine wave for synchronization at the end of frame
   			sine_sync(upsampBufferInterleaved,sinetable, SINE_TABLE_SIZE, BUFFSIZE);
			memcpy(pBufferXmt,upsampBufferInterleaved, BUFFSIZE*sizeof(Int16));
			//Intialize buffers and counters in the start state to be used later
			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;
 			training_frame_counter = 0;
 			uTotalBits = (DATABUFFSIZE*32);
			uBitsRemainInFile = uTotalBits;
			nextFrame =0;
			State = TRAINING; 			
			break;
		
		case TRAINING:
			frame_number++;
			training_frame_counter++;
			//Perform fft on the modulated training sequence
			dft_DSP(trainingBuffer, idftBuffer);
			//Upsample and filter with raised cosine pulse shape
			upsampling3(idftBuffer, upBuffer, LASTBUFF, UPFACTOR);
			DSPF_sp_convol(upBuffer, cosfilter, upsampBuffer ,FILTER_LENGTH-1, LASTBUFF*UPFACTOR+FILTER_LENGTH-1);
			//interleave buffer to be transmitted on one channel
			interleavebuffer2(upsampBufferInterleaved, upsampBuffer, (LASTBUFF*UPFACTOR)+(FILTER_LENGTH-1));
			memcpy(pBufferXmt,upsampBufferInterleaved, BUFFSIZE*sizeof(Int16));
			//If data frames have been sent -> RUNNING, else -> feedback sync
			if((data_frame_counter>0) && (training_frame_counter>5)){
				State = RUNNING;
			}else if((data_frame_counter==0) && (training_frame_counter==5)){
				State = SYNC;
			} 	
			
			break;	
		
		case SYNC:
			frame_number++;
			copy2Data(pBufferRcv, upsampBufferInterleaved, (BUFFSIZE>>1));
			DSK6713_LED_off(0);
			DSK6713_LED_toggle(1);
			if(!nextFrame){ //If next frame buffer has not been set
				framesync(upsampBufferInterleaved, startindex, BUFFSIZE>>1);
				if(startindex[0]!=0){ //We have synchronized
					
					if((startindex[0]+48)<((BUFFSIZE>>1)-UPFACTOR2*SUBCARSIZE*4)){
						downsampling2(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{ // We have no synchronization
					if(frame_number==10){
					State = START;
					}
				}
			}else{ //frames do not overlap buffer borders
				copyData(upsampBufferInterleaved, &downsampBuffer[(4*UPFACTOR2*SUBCARSIZE)-nextFrame], (nextFrame));
				downsampling2(upsampBufferInterleaved, downsampBuffer, 4*SUBCARSIZE, UPFACTOR2);
				memset(bitAlloc,0,SUBCARSIZE*sizeof(short));
				demodulation_shart(upsampBufferInterleaved, bitAlloc);
				State = RUNNING;
				DSK6713_LED_on(0);
			}
			break; 
		
		case RUNNING:
			frame_number++;
			data_frame_counter++;
			//Bits according to Bit allocation
			uBitsInCurrFrame = total_bits(bitAlloc, SUBCARSIZE); //254 for simple			
			uStartElement = (uBitsTransmitted>>5); //The first element to be transmitted
			index = uBitsTransmitted%32; //The index of the first bit
			uElementsInCurrFrame = (uBitsInCurrFrame>>5)+2; //Take more elements in case the required bits overlap integers
			
			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);
			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, 1192));
			max_abs = _fabsf(DSPF_sp_maxval(upsampBuffer, 1192));
			maxtime[frame_number] = MAX(max_abs,min_abs);
			
			if((data_frame_counter%5)==0){
				State = TRAINING;
			}
			
			
			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){
	
	unsigned int length;
	int status,N_rtdx;
	int nr_last,i,k; 
	
	/* enable the input channel */ 
	RTDX_enableInput( &ichan ); 
 
	
	length = 16654;
	N_rtdx = (int)floorf((float)length*0.005);			
	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];
	}
	
	
	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];
	}
	
	/* 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 + -