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

📄 frameofdm.c

📁 应用于无线通信的RS编码和OFDM调制源程序。64点FFT
💻 C
字号:
#include <stdio.h>
#include <math.h>
#include "Setting.H"
#include "RS.H"
#include "Function.H"
#include "asintable.h"

/* This script generates a COFDM waveform from an input data */ 
void OFDMModu(DType DataStr[], float BaseSignal[], DType RefPhase[])
/* DataStr:    RS coded data to transmit in the CodewordBit given,
 *             UsedCarrier * SymPerFrame in length;
 * BaseSignal: Final output modulated signal, SymbolSize * SymPerFrame in length;
 * RefPhase:   DPSK reference phase, UsedCarrier * 1 in length;
 */
{
	WordType i, j, k;
	DType DPSKdata[SymPerFrame * UsedCarrier];
	float TxSpecR[SymPerFrame * FFTSize], TxSpecI[SymPerFrame * FFTSize];
	
	/* Convert to DQPSK */
	for (j = 0; j < UsedCarrier; j++)
	{
		DPSKdata[j] = (DType) (DataStr[j] + RefPhase[j]) &
			(DType) ((1 << CodewordBit) - 1);
	}
	for (i = 1; i < SymPerFrame; i++)
	{
		for (j = 0; j < UsedCarrier; j++)
		{
			DPSKdata[j + i * UsedCarrier] = 
				(DType) (DataStr[j + i * UsedCarrier] + DPSKdata[j + (i - 1) * UsedCarrier]) &
				(DType) ((1 << CodewordBit) - 1);
		}
	}
	/* Store the last carrier data in RefPhase[] for next subframe */
	for (j = 0; j < UsedCarrier; j++)
	{
		RefPhase[j] = DPSKdata[j + (SymPerFrame - 1) * UsedCarrier];
	}

	/* Get the required spectrums */
	for (j = 0; j < SymPerFrame; j++)
	{
		for (i = 0; i < FFTSize; i++)
		{
			if ((i > CarrierJumpPos - 2) && (i < CarrierJumpPos + UsedCarrier - 1))
			{
				switch (DPSKdata[j * UsedCarrier + i + 1 - CarrierJumpPos])
				{
				case 0: TxSpecR[j * FFTSize + i] = 1;   TxSpecI[j * FFTSize + i] = 0;  break;
				case 1: TxSpecR[j * FFTSize + i] = 0;   TxSpecI[j * FFTSize + i] = 1;  break;
				case 2: TxSpecR[j * FFTSize + i] = -1;  TxSpecI[j * FFTSize + i] = 0;  break;
				case 3: TxSpecR[j * FFTSize + i] = 0;   TxSpecI[j * FFTSize + i] = -1;
				}
			}
			else
			{
				TxSpecR[j * FFTSize + i] = 0;
				TxSpecI[j * FFTSize + i] = 0;
			}
		}
	}

	/* Change to time waveform using IFFT */
	for (i = 0; i < SymPerFrame; i++)
	{
		FFT(&TxSpecR[i * FFTSize], &TxSpecI[i * FFTSize], -1);
	}

	/* Add cycle prefixion (guardtime) */
	k = 0;
	for (i = 0; i < SymPerFrame; i++)
	{
		for (j = GuardTimeSize; j > 0; j--)
		{
			BaseSignal[k] = TxSpecR[i * FFTSize + FFTSize - j];
			k++;
		}
		for (j = 0; j < FFTSize; j++)
		{
			BaseSignal[k] = TxSpecR[i * FFTSize + j];
			k++;
		}
	}
}

/* This script demodulates a COFDM waveform from an input data:
 * DataStr:    Received real signal, SymbolSize * SymPerFrame in length;
 * SpecSignal: Demodulated RS data in CodewordBit size per symbol,
 *             UsedCarrier * SymPerFrame in length;
 * RefPhase:   DPSK reference phase, UsedCarrier * 1 in length;
 */
void OFDMDemo(short BaseSignal[], DType SpecSignal[], int RefPhase[])
{
	WordType i, j, h1;
	int RxSpecR[FFTSize], RxSpecI[FFTSize];
	int CarrPh[SymPerFrame * UsedCarrier];
	int index;
	short * pBase;
	int *pRxSpecR, *pRxSpecI,*pCarrPh,xR,xI;
	DType *pSpec;

	/* Reshape the linear time waveform into FFT segments & remove guard time */
	for (i = 0; i < SymPerFrame; i++)
	{
		pRxSpecR = RxSpecR;
		pRxSpecI = RxSpecI;
		pBase = &BaseSignal[i * SymbolSize + GuardTimeSize];
		for (j = 0; j < FFTSize; j++)
		{
			*pRxSpecR = (int)(*pBase);
            *pRxSpecI = 0;
			pRxSpecR++;
			pRxSpecI++;
			pBase++;
		}
	
    	/* Apply FFT on the rest of received data */
    	FFT(RxSpecR, RxSpecI, 1);

	    /* Convert to QPSK phase */
		pCarrPh = &CarrPh[i * UsedCarrier];
		pRxSpecR = RxSpecR + CarrierJumpPos - 1;
		pRxSpecI = RxSpecI + CarrierJumpPos - 1;
	  	for (j = CarrierJumpPos - 1;j < CarrierJumpPos + UsedCarrier - 1; j++)
		{
			xR = *pRxSpecR;
			xI = *pRxSpecI;
			index = (float)90 * xI*xI/(xI*xI+xR*xR);
			*pCarrPh = asin_tab[index];                                               //Q15
			 if(xR>=0 && xI<0)
			 {
				*pCarrPh = -(*pCarrPh) + PI2Q15;
			 }
			 else if(xR<0 && xI<=0)
			 {
				 *pCarrPh = *pCarrPh + PIQ15;
			 }
			 else if(xR<0 && xI>0)
			 {
				*pCarrPh = -(*pCarrPh) + PIQ15;
			 }
			 pRxSpecR++;
			 pRxSpecI++;
			 pCarrPh++;	
		}
	}   

	/* Apply DQPSK on the received data */
	for (j = 0; j < UsedCarrier; j++)
	{
		SpecSignal[j] = (DType) ((CarrPh[j] - RefPhase[j] + PI2Q15 + PIQ15 / (1 << CodewordBit)) / 
			(PIQ15 / (1 << CodewordBit) * 2)) & (DType) ((1 << CodewordBit) - 1);
	}

	pSpec = SpecSignal + UsedCarrier;
	pCarrPh = CarrPh + UsedCarrier;
	for (i = 1; i < SymPerFrame; i++)
	{		 
		for (j = 0; j < UsedCarrier; j++)
		{
			xR = *pCarrPh;
			xI = *(pCarrPh - UsedCarrier);
			*pSpec = (DType) ((xR - xI + PI2Q15 + PIQ15 / (1 << CodewordBit)) / (PIQ15 / (1 << CodewordBit) * 2)) & 
					 (DType) ((1 << CodewordBit) - 1);
			pSpec++;
			pCarrPh++;
		}
	}

	/* Save the RefPhase for next subframe */
	pCarrPh = CarrPh + (SymPerFrame - 1) * UsedCarrier;
	for (j = 0; j < UsedCarrier; j++)
	{
		RefPhase[j] = *pCarrPh;
		pCarrPh++;
	}
}

⌨️ 快捷键说明

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