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

📄 receive.c

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

WordType FrameTx = TotalFrame;
WordType FrameRx = TotalFrame;
int DataBuffer[NullSampleSize / 2 + NullSampleSize + SymbolSize * 3];

main()
{
	FILE *OriSource, *TransmitTxt, *DecodedSource;
	WordType RxFirstFrame = 1;
	int i, j, k = 0, q;
	/* Buffer for received data */
	//float ModuData[SymbolSize * (SymPerFrame + 2) + NullSampleSize + HeaderSize];
	short ModuData[SymbolSize * (SymPerFrame + 2)];
	short DecoData[SrcVector * 3];

	if ((TransmitTxt = fopen("OFDM5_16BIT.pcm", "rb")) == NULL)
//	if ((TransmitTxt = fopen("Data_Transmit.bin", "rb")) == NULL)
	{
		printf("Transmit file cannot be opened !\n\n");
		exit(0);
	}
	rewind(TransmitTxt);	
	if ((DecodedSource = fopen("Data_Decoded.bin", "wb")) == NULL)
	{
		printf("Decoded source target file cannot be opened !\n\n");
		exit(0);
	}
	rewind(DecodedSource);

	printf("Demodulating & channel decoding ...\n\n");

	/* RS initialization */
	Init_RS();
	
	while (!feof(TransmitTxt))
	
	{
		/* Eric's timing */
		if (Timing == 1)
		{
			if (FrameRx == TotalFrame)      /* Meet the head of a frame      */
			{
				if (RxFirstFrame == 1)      /* Received frame is the first one */
				{
					/* For (float) type saving */
					/* Read the data and find the jump-off point */
					q = fread(ModuData, sizeof(short), 
						NullSampleSize / 2 + NullSampleSize + SymbolSize * 3, 
						TransmitTxt);
					if (q < NullSampleSize / 2 + NullSampleSize + SymbolSize * 3)
					{
						break;              /* Exit when not a integrate frame */
					}
					fseek(TransmitTxt, 
						0 - ((NullSampleSize / 2 + NullSampleSize + SymbolSize * 3) * sizeof(short)), 
						SEEK_CUR);
					i = FindFrame(ModuData, RxFirstFrame) - FisrtFinePnt;
					RxFirstFrame = 0;
				}
				else                        /* Received frame isn't the first one */
				{
					/* For (float) type saving */
					/* Read the data and find the jump-off point */
					q = fread(ModuData, sizeof(short), 
						NullSampleSize + SymbolSize * 3, TransmitTxt);
					if (q < NullSampleSize + SymbolSize * 3)
					{
						break;              /* Exit when not a integrate frame */
					}
					fseek(TransmitTxt, 
						0 - ((NullSampleSize + SymbolSize * 3) * sizeof(short)), 
						SEEK_CUR);
					i = FindFrame(ModuData, RxFirstFrame) - NullSampleSize / 2 - SecondFinePnt;
				}
				/* Get the actual used data */
				fseek(TransmitTxt, i * sizeof(short), SEEK_CUR);
				q = fread(ModuData, sizeof(short), 
					(SymPerFrame + 2) * SymbolSize, TransmitTxt);
			}
			else                            /* The following part of a frame */
			{
				/* For (float) type saving */
				q = fread(ModuData, sizeof(short), 
					SymPerFrame * SymbolSize, TransmitTxt);
			}
			if (q < SymPerFrame * SymbolSize)
			{
				break;                      /* Exit when not a integrate frame */
			}

			/* Save the data used in FindFrame for next OFDM frame */
			if (FrameRx == 3)
			{
				for (i = 0; i < NullSampleSize / 2; i++)
				{
					DataBuffer[i] = 
						abs(ModuData[SymPerFrame * SymbolSize - NullSampleSize / 2 + i]);         
				}
			}

			/* Proceed demodulation & channel decode */
			q = Receive(ModuData, DecoData);

			/* Display */
			k += q;
			if (fmod((double)k, 200) == 0)
			{
				printf("Demodulated speech frame number: %d\n", k);
			}

			/* Save the final data */
			for (i = 0; i < q; i++)
			{
				fwrite(&DecoData[i * SrcVector], sizeof(short), SrcVector, DecodedSource);
			}
		}
		/* Artificially accurate timing */
		else
		{
			if (FrameRx == TotalFrame)      /* Meet the head of a frame      */
			{
				/* For (float) type saving */
				fseek(TransmitTxt, sizeof(float) * NullSampleSize, SEEK_CUR);
				q = fread(ModuData, sizeof(float), 
					(SymPerFrame + 2) * SymbolSize, TransmitTxt);
			}
			else                            /* The following part of a frame */
			{
				/* For (float) type saving */
				q = fread(ModuData, sizeof(float), 
					SymPerFrame * SymbolSize, TransmitTxt);
			}
			if (q < SymPerFrame * SymbolSize)
			{
				break;                      /* Exit when not a integrate frame */
			}

			/* Proceed demodulation & channel decode */
			q = Receive(ModuData, DecoData);

			/* Save the final data */
			for (i = 0; i < q; i++)
			{
				fwrite(&DecoData[i * SrcVector], sizeof(short), SrcVector, DecodedSource);
			}
		}
	}
	fclose(TransmitTxt);
	fclose(DecodedSource);

	printf("\nAccomplished !\n\n");

	/* Calculate the bit error rate (BER) */
/*	if ((OriSource = fopen("Data_Original.bin", "rb")) == NULL)
	{
		printf("Original transmitted data file cannot be opened !\n\n");
		exit(0);
	}
	rewind(OriSource);
	if ((DecodedSource = fopen("Data_Decoded.bin", "rb")) == NULL)
	{
		printf("Decoded source target file cannot be opened !\n\n");
		exit(0);
	}
	rewind(DecodedSource);

	i = 0;
	j = 0;
	while (!feof(OriSource) || !feof(DecodedSource))
	{
		i++;
		fread(&DecoData[0], sizeof(short), 1, OriSource);
		fread(&DecoData[1], sizeof(short), 1, DecodedSource);
		if (DecoData[0] != DecoData[1])
		{
			j++;
		}
	}
	printf("Total received speech frame: %d\n", (i-1)/SrcVector);
	printf("Total bits: %d;  Error bits: %d;  BER: %e\n\n", 
		i-1, j, (float)j / (float)(i - 1));*/
}













⌨️ 快捷键说明

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