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

📄 demodulation.c

📁 详细的OFDM设计过程
💻 C
字号:
/* function y = demodulation(x,b,e,h)
	* find the symbol in the defined modulation scheme map which has the minimum distance to the received symbol, 
	* output the binary code corresponding to the index
	*
	* x is input symbols, in the form of row vector
	* y is demodulated output, in the form of row vector
	* b is the subcarrier bit allocation, b(i) is the number of bits allocated to ith subcarrier 
	* e is the subcarrier energy allocation, same size as b
	* h is the channel value in frequency for all subcarriers. X_estimate = X / H
	*
	* ------- demodulation.m -----------------------------------
	* Black team, Erik
	* April-11-05
	* ----------------------------------------------------------
	****************************************************************/

#include <math.h>
#include <stdio.h>


void demodulation(const float *input, unsigned int *output, const int *b, float *e, const float *h)
{
	

	double k[2];
	int i, tmp;		// tmp gives the index of the d var.
	int t[LEN];
	int	b2[LEN];		// which int[x] from input should be used.
	int b3[LEN];		// which bit in the int is our first bit to consider.
	int b4[LEN];		// real bit position from start of input
	b2[0]=0;			// '0' indicates the first int.
	b3[0]=0;			// '0' indicates the first bit of an int. 
	b4[0]=0;			// always from zero..
	t[0]=32;				// How many bits remain to integer boarder.
	for(i = 1; i<LEN; i++)
	{
		b4[i] = b[i-1]+b4[i-1];
		b2[i] = (int)floor((b4[i])/32);
		b3[i] = ((b4[i])%32);
		t[i] = 32-(b4[i]%32);
		e[i] = 1/((float)sqrt(e[i])*h[i]);
	
		output[i-1] = 0;
	}

	
	for(i=0; i<LEN; i++)
	{
		switch(b[i]) 
		{
		case 1 : {				
				// The masked value, not korrekt added to output:	
			if(input[i*2]>0)
				output[b2[i]] = output[b2[i]] | (0x01 << b3[i]);
//			else					// Can be canceled if output is initialized to 0
//				output[b2[i]] = output[b2[i]]&(~(0x01 << b3[i]));
			break;	 
			}
		case 2 : {
			if(input[i*2]<0) {
				if(input[i*2+1]<0)
					tmp = 2;
				else
					tmp = 0;
			} else {
				if(input[i*2+1]<0)
					tmp = 3;
				else
					tmp = 1;
			}
			if(!(t[i]-1)) {				// Last bit of integer, goto next.
				output[b2[i]] = output[b2[i]] | (tmp << 31);
				output[b2[i]+1] = (tmp >> 1);
			} else
				output[b2[i]] = output[b2[i]] | (tmp << b3[i]);
			break;
			}
		case 4 : {
			k[0] = input[i*2]*e[i];
			k[1] = input[i*2+1]*e[i];
			if(k[0]<0)
				tmp = 0;		// Init tmp
			else
				tmp = 0x08;
			if(k[0] > -0.6330 && k[0] < 0.6330)
				tmp += 0x04;
			if(k[1]>0)
				tmp += 0x02;
			if(k[1] > -0.6330 && k[1] < 0.6330)
				tmp += 0x01;

			if(t[i]<4) {				// Last bit of integer, goto next.
				output[b2[i]] = output[b2[i]] | (tmp << b3[i]);
				output[b2[i]+1] = (tmp >> t[i]);
			} else
				output[b2[i]] = output[b2[i]] | (tmp << b3[i]);
			break;
			}
		case 6 : {
			k[0] = input[i*2]*e[i];
			k[1] = input[i*2+1]*e[i];
			if(k[1]>0)
				tmp = 0;		// Init tmp
			else
				tmp = 0x20;
			if(k[0]>0)
				tmp += 0x04;
			if(k[0] > -0.3086 && k[0] < 0.3086)		// case +-0.15
				tmp += 0x02;
			if(k[1] > -0.3086 && k[1] < 0.3086)		
				tmp += 0x10;
			if(k[0] > -0.6172 && k[0] <= -0.3086 || k[0] < 0.6172 && k[0] >= 0.3086)		// case +-0.46
				tmp += 0x03;
			if(k[1] > -0.6172 && k[1] <= -0.3086 || k[1] < 0.6172 && k[1] >= 0.3086)		// case +-0.46
				tmp += 0x18;
			if(k[0] > -0.9258 && k[0] <= -0.6172 || k[0] < 0.9258 && k[0] >= 0.6172)		// case +-0.77
				tmp += 0x01;
			if(k[1] > -0.9258 && k[1] <= -0.6172 || k[1] < 0.9258 && k[1] >= 0.6172)		// case +-0.77
				tmp += 0x08;

			if(t[i]<6) {				// Last bit of integer, goto next.
				output[b2[i]] = output[b2[i]] | (tmp << b3[i]);
				output[b2[i]+1] = (tmp >> t[i]);
			} else
				output[b2[i]] = output[b2[i]] | (tmp << b3[i]);
			break;
			}
		case 8 : {
			k[0] = input[i*2]*e[i];
			k[1] = input[i*2+1]*e[i];
			
			// Halvera koden genom att k鰎a en forlop 鰒er index i k[]
			if(k[1]>0)
				tmp = 0;		// Init tmp
			else
				tmp = 0x80;
			if(k[0]>0)
				tmp += 0x08;
			if(k[0] > -0.1534 && k[0] < 0.1534)		// case +-0.0767
				tmp += 0x04;
			if(k[1] > -0.1534 && k[1] < 0.1534)		
				tmp += 0x40;
			if(k[0] > -1.0738 && k[0] <= -0.9204 || k[0] < 1.0738 && k[0] >= 0.9204)		// case +-0.9971
				tmp += 0x01;
			if(k[1] > -1.0738 && k[1] <= -0.9204 || k[1] < 1.0738 && k[1] >= 0.9204)		// case +-0.9971
				tmp += 0x10;
			if(k[0] > -0.7670 && k[0] <= -0.6136 || k[0] < 0.7670 && k[0] >= 0.6136)		// case +-0.6903
				tmp += 0x02;
			if(k[1] > -0.7670 && k[1] <= -0.6136 || k[1] < 0.7670 && k[1] >= 0.6136)		// case +-0.6903
				tmp += 0x20;
			if(k[0] > -0.9204 && k[0] <= -0.7670 || k[0] < 0.9204 && k[0] >= 0.7670)		// case +-0.8437
				tmp += 0x03;
			if(k[1] > -0.9204 && k[1] <= -0.7670 || k[1] < 0.9204 && k[1] >= 0.7670)		// case +-0.8437
				tmp += 0x30;
			if(k[0] > -0.3068 && k[0] <= -0.1534 || k[0] < 0.3068 && k[0] >= 0.1534)		// case +-0.2301
				tmp += 0x05;
			if(k[1] > -0.3068 && k[1] <= -0.1534 || k[1] < 0.3068 && k[1] >= 0.1534)		// case +-0.2301
				tmp += 0x50;
			if(k[0] > -0.6136 && k[0] <= -0.4602 || k[0] < 0.6136 && k[0] >= 0.4602)		// case +-0.5369
				tmp += 0x06;
			if(k[1] > -0.6136 && k[1] <= -0.4602 || k[1] < 0.6136 && k[1] >= 0.4602)		// case +-0.5369
				tmp += 0x60;
			if(k[0] > -0.4602 && k[0] <= -0.3068 || k[0] < 0.4602 && k[0] >= 0.3068)		// case +-0.3835
				tmp += 0x07;
			if(k[1] > -0.4602 && k[1] <= -0.3068 || k[1] < 0.4602 && k[1] >= 0.3068)		// case +-0.3835
				tmp += 0x70;

			if(t[i]<8) {				// Last bit of integer, goto next.
				output[b2[i]] = output[b2[i]] | (tmp << b3[i]);
				output[b2[i]+1] = (tmp >> t[i]);
			} else
				output[b2[i]] =  output[b2[i]] | (tmp << b3[i]);
			break;
			}
		default :
			break;
					
		}
	}
}




⌨️ 快捷键说明

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