qpsk.c

来自「Hard Viterbi QPSK in AWGN, Rayleight so」· C语言 代码 · 共 122 行

C
122
字号
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <time.h>
#include "random.h"
//make struct complex
typedef struct complex1
{
	double real;
	double imag;
} complex;
// define the number of interation 
#define ITERATION pow(10, 7)

double modulation(int data);
double demodulation(double r_data);
int bercheck(int data, double d_data);

void main()
{
	FILE *fp1;
	double Noisepower;
	complex Noise;
	int data[2];
	
	complex m_data;
	complex r_data;
	complex d_data;
	
	int error;
	int error1;
	double avgerr;
	int EbofN0;
	// open the file 
	fp1=fopen("result_qpsk.txt","w");
	
	for(EbofN0=0; EbofN0<=10; EbofN0+=1)
	{
		double sumerr=0;
		int run;		
	
		for(run=0;run<ITERATION;run++)
		{
			// create the data
			generate_random(&data[0], 2);
			// modulate the real and imagine of the data 
			m_data.real = modulation(data[0]);
			m_data.imag = modulation(data[1]);
			// make the noisepower
		    Noisepower=1/(2*pow(10,(double)EbofN0/10));
			// create the noise 
		    Noise.real = sqrt(Noisepower/2)*GaussRand();
			Noise.imag = sqrt(Noisepower/2)*GaussRand();
			// receive the signal (modulated data + noise)
			r_data.real = sqrt(1.0/2.0)*m_data.real + Noise.real;
			r_data.imag = sqrt(1.0/2.0)*m_data.imag + Noise.imag;
			// demodulate the signal
			d_data.real = demodulation(r_data.real);
			d_data.imag = demodulation(r_data.imag);
			// check the error
			error= bercheck(data[0], d_data.real);
			error1= bercheck(data[1], d_data.imag);

			sumerr = sumerr + error + error1;
		}
		// calculate the BER
		avgerr = (double)sumerr / (ITERATION*2);
		fprintf(fp1,"%.7f\n",avgerr);
		

		printf("%.7f\n", avgerr);
	}
	fclose(fp1);
}
// modulation function
double modulation(int data)
{
	int m_data;
	if (data==1)
	{
		m_data=1;
	}
	else
	{
		m_data=-1;
	}
	return (m_data);
}
// demodulation function
double demodulation(double r_data)
{
	int d_data;
	if (r_data>=0)
	{
		d_data=1;
	}
	else
	{
		d_data=0;
	}
	return (d_data);
}
// error check function
int bercheck(int data, double d_data)
{
	int error=0;
	if ((double)data==d_data)
	{
		error=0;
	}
	else
	{
		error=1;
	}
	return (error);
}





⌨️ 快捷键说明

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