📄 rxfunctionsok.c
字号:
/*****************************************************************************
Module : External Functions
******************************************************************************
Function : External Functions used by the receiver.
Procedures :
Author : $Author: Adrian $
Revision : $Revision: 10 $
Modified : $Modtime: 05-05-29 23:22 $
File : $Workfile: rxfunctions.c $
******************************************************************************
KTH, Royal Institute of Technology, S3, Stockholm
*****************************************************************************/
/*--- Include files --------------------------------------------------------*/
/* import */
#include <csl_stdinc.h>
#include "../common/commondef.h"
#include "rxmain.h"
#include "../common/crc16.h"
/* export */
#include "rxfunctions.h"
/*=== End of include files =================================================*/
/*--- Global variables definition ------------------------------------------*/
typRX_ENERGYDETSTATE EnergyDetState;
float Sig_Nois_Ratio; //the receive sigal to noise ratio.
/*=== End of global variables definition ===================================*/
/*--- Global constants definition ------------------------------------------*/
/*=== End of global constants definition ===================================*/
/*--- Local defines --------------------------------------------------------*/
/*=== End of local defines =================================================*/
/*--- Local types declaration ----------------------------------------------*/
/*=== End of local types declaration =======================================*/
/*--- Local variables definition -------------------------------------------*/
/*=== End of local variables definition ====================================*/
/*--- Local constants definition -------------------------------------------*/
/*=== End of local constants definition ====================================*/
/*--- Local functions definition -------------------------------------------*/
/*=== End of local functions definition ====================================*/
/*--- Global functions definition ------------------------------------------*/
/****************************************************************************
Function : computeEnergy
****************************************************************************
Description : compute the energy of signal to detect frame start point
Inputs : received signal and threshold
Outputs : -start point and noise power per sample
By : 2005-05-05 Jinfeng Du
2005-05-11 modified
****************************************************************************/
void computeEnergy(Int16 *pInBuff, const unsigned int uiInBuffSize, typRX_ENERGYDETSTATE *pEnergyDetState)
{
int n, i;
float channelA, channelB;
float alfa = 0.95; //forgetting factor
float factor = _rcpsp((float)pEnergyDetState->unEnergyBlock);
n = 0;
pEnergyDetState->iStartIndex = -1; //if fail to detect the signal
while(n < uiInBuffSize)
{
i = 0;//count the length of the block
channelA = 0.0;
channelB = 0.0;
while(i < pEnergyDetState->unEnergyBlock)
{
channelA += pInBuff[n]*pInBuff[n];
n++;
channelB += pInBuff[n]*pInBuff[n];
n++;
i++;
}//end while i
if (pEnergyDetState->fEnergyThrs < 0) //initialize the threshold
{
pEnergyDetState->fEnergyThrs = 10 * channelA * 10 * channelB;
}
if (channelA > pEnergyDetState->fEnergyThrs || channelB > pEnergyDetState->fEnergyThrs)
{
pEnergyDetState->iStartIndex = n - 2*pEnergyDetState->unEnergyBlock;
n = uiInBuffSize; // break;
//compute the SNR in ChannelA for use later
Sig_Nois_Ratio = (factor*channelA)*_rcpsp(pEnergyDetState->fNoisePower1);
}
else
{
pEnergyDetState->fNoisePower1 = factor*channelA;
pEnergyDetState->fNoisePower2 = factor*channelB;
//update the threshold
pEnergyDetState->fEnergyThrs = alfa*pEnergyDetState->fEnergyThrs
+ (1-alfa)*5*(channelA+channelB); //Thrs = 10*Noise_Power
}//end if
}//end if n
}// end computeEnergy
/****************************************************************************
Function : decodeStartFrame()
****************************************************************************
Description : Decode the received data in the first frame and set the
state variables according to the information (if correct).
Inputs : pointer to the data buffer
buffer length
receiver states (information to set/recover)
Outputs : 1 if successful, 0 if no information could be recovered
By : 2005-05-19 Adrian Schumacher
****************************************************************************/
int decodeStartFrame(Uint8 *pDataBuffer, const unsigned int buflen,
typRX_RECEIVESTATE *pReceiverState)
{
int i, success;
char tmp8;
Uint16 tmp16, crc16;
unsigned int tmp32, buf;
// test checksum to find uncorrupted data
success = FALSE;
i = 0;
while ((i < DATASYMBSYNC) && (success == FALSE))
{
// get checksum from the received data
tmp8 = pDataBuffer[i+NOOFINFOBYTES];
tmp16 = ((Uint16)tmp8) & 0x00FF;
tmp16 = (tmp16 << 8) & 0xFF00;
tmp8 = pDataBuffer[i+NOOFINFOBYTES+1];
tmp16 |= ((Uint16)tmp8) & 0x00FF;
// compute the checksum over the received data
crc16 = calc_crc(&pDataBuffer[i],NOOFINFOBYTES);
// compare the checksum
if (tmp16 == crc16)
success = TRUE; // leave the loop
else
i += NOOFINFOBYTES+2; // number of information bytes + 2 bytes CRC checksum
}
if (success == TRUE)
{
/*
At the moment, these values are set by the host program and available from the beginning of a transmission.
So there is no need to reproduce them from what was sent with the transmitter.
If this receiver would be used as a "stand-alone" receiver, a few changes have to be made:
Since we need to know the frame-format of the second frame before the data is recovered from the first,
some conventions have to be made, i.e. the format of the second frame has also to be fixed (sync or regular/data).
This will affect the "switch manager" which controls the time instants when the switching has to occure.
*/
// read SyncRep (1 byte)
tmp8 = pDataBuffer[i++];
pReceiverState->iSyncRep = (int)tmp8;
// read RxMethod (1 byte)
tmp8 = pDataBuffer[i++];
pReceiverState->RxMethod = (typCOM_eCommMethod)tmp8;
// read FileSize (4 bytes)
tmp8 = pDataBuffer[i++];
buf = (unsigned int)tmp8;
tmp32 = ((buf << 24) & 0xFF000000);
tmp8 = pDataBuffer[i++];
buf = (unsigned int)tmp8;
tmp32 |= ((buf << 16) & 0x00FF0000);
tmp8 = pDataBuffer[i++];
buf = (unsigned int)tmp8;
tmp32 |= ((buf << 8) & 0x0000FF00);
tmp8 = pDataBuffer[i++];
buf = (unsigned int)tmp8;
tmp32 |= (buf & 0x000000FF);
pReceiverState->uFileSize = tmp32;
// read NoOfFrames/Blocks (4 bytes)
tmp8 = pDataBuffer[i++];
buf = (unsigned int)tmp8;
tmp32 = ((buf << 24) & 0xFF000000);
tmp8 = pDataBuffer[i++];
buf = (unsigned int)tmp8;
tmp32 |= ((buf << 16) & 0x00FF0000);
tmp8 = pDataBuffer[i++];
buf = (unsigned int)tmp8;
tmp32 |= ((buf << 8) & 0x0000FF00);
tmp8 = pDataBuffer[i++];
buf = (unsigned int)tmp8;
tmp32 |= (buf & 0x000000FF);
pReceiverState->uNoOfBlocks = tmp32;
if ((pReceiverState->uNoOfBlocks == 0) && (pReceiverState->uFileSize == 0))
// if (tmp32 == 0)
return FALSE;
else
return TRUE;
}
else
{ // could not read the information from the frame
return FALSE;
}
}
/*=== End of global functions definition ===================================*/
/*--- AUTOMATICALLY GENERATED VERSION HISTORY --------------------------------
$Log: /MIMO/Receiver/rxfunctions.c $
*
* 10 05-05-29 23:37 Adrian
* removed a bug in the function decodeStartFrame()
* the 16bit CRC was not correctly read from the frame data
*
* 9 05-05-28 15:01 Adrian
* comment added in decodeStartFrame()
*
* 8 05-05-23 10:39 Jinfeng
* return 0 instead of 64 when signal detected
*
* 7 05-05-22 16:03 Adrian
* changed the datatype for *pDataBuffer from char to Uint8
*
* 6 20.05.05 8:56 Adrian
* added the function decodeStartFrame()
*
* 5 05-05-17 13:34 Jinfeng
* Add i++ inside the i-loop to make sence.
*
* 4 05-05-17 13:23 Jinfeng
* initialize the threshold at the first call and update it timely
* according to the noise power
*
* 3 05-05-12 12:17 Jinfeng
* No modification.
*
* 2 05-05-12 12:13 Jinfeng
* Introduce a global variable: float Sig_Nois_Ratio; //the receive sigal
* to noise ratio.
*
* 1 05-05-11 17:00 Adrian
* created and added to VSS
===== END OF AUTOMATICALLY GENERATED VERSION HISTORY =======================*/
/**** End of file ***********************************************************/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -