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

📄 demodulation.c

📁 802.16e物理层下行解调算法实现。严格按照802.16e实现。
💻 C
字号:

#include "..\include\wib_phy_def.h"



/*
 * *************************************************************************************
 * function: dl_demodulation_sum module
 * parameters:
 *             COMPLEX16       *p_dstrc_dat,       // de-modulation data input
 *             INT16           *p_demod_dat,       // de-modulation result output
 *             INT16           *chan_factor,       // channel factor input
 *             UINT16          block_len,          // de-modulation data length
 *             UINT8           rate_id,            // FEC code type
 *             UINT8           dat_width,          // data width
 * return: int
 * description: constellation de-MAP (SUMSUNG Arithmetic)module
 * Author : CAOLIN/LIUGUANGYU
 * -------------------------------------------------------------------------------------
 */

int dl_demodulation_sum
        (
            COMPLEX32       *p_dstrc_dat,       // de-modulation data input
            INT16           *p_demod_dat,       // de-modulation result output
            INT32           *chan_factor,       // channel factor input
            UINT32          block_len,          // de-modulation data length
            UINT8           rate_id,            // FEC code type
			UINT16           data_width           // data width
        )
{

    UINT32 i;
	UINT32 demod_len = 0;

    UINT8  map_bit = 0x00;

    INT32  chanfact_tmp1 = 0;
    INT32  chanfact_tmp2 = 0;
    INT32  chanfact_tmp3 = 0;
    INT32  chanfact_tmp4 = 0;
    INT32  chanfact_tmp5 = 0;

    INT32 *demod_32b = NULL;
    
//-------------------------------------------------
//parameter Generate

	switch(rate_id) {
		case 0 :
		case 1 :
		case 13 :
		case 15 :
			map_bit = 0x02;
			break;
		case 2 :
		case 3 :
		case 16 :
		case 17 :
			map_bit = 0x04;
			break;
		case 4 :
		case 5 :
		case 6 :
		case 18 :
		case 19 :
		case 20 :
		case 21 :
			map_bit = 0x06;
			break;
		default :
            printf("<<<< Error: Not a valid rate_id for dl_modulation! >>>>\n");
            break;
	}

    demod_32b = malloc(map_bit*block_len*sizeof(INT32));
    if(demod_32b == NULL) {
        printf("ERROR: demod_32b malloc fail in dl_dmodulation!!\n");
        return(-1);
    }

//-------------------------------------------------
//de-modulation

    switch(map_bit) {
    case 2:
        for(i=0; i < block_len; i++) {
            demod_32b[2*i] = p_dstrc_dat[i].real;
            demod_32b[2*i+1] = p_dstrc_dat[i].imag;
        }
        break;
    case 4:
        for(i=0; i < block_len; i++) {
            //calculate channel factor
            //chanfact_tmp1 = (INT32)(chan_factor[i]*0.6325684);
			chanfact_tmp1 = (INT32)(chan_factor[i]*648 >> 10);
 


            //The first bit decide
            if(p_dstrc_dat[i].real >= chanfact_tmp1){
	            demod_32b[4*i] = 2*p_dstrc_dat[i].real - chanfact_tmp1;
            } else if(p_dstrc_dat[i].real >= -chanfact_tmp1) {
	            demod_32b[4*i] = 2*p_dstrc_dat[i].real;
            } else {
	            demod_32b[4*i] = 2*p_dstrc_dat[i].real + chanfact_tmp1;
            }

            //The second bit decide
            if(p_dstrc_dat[i].real >= 0) {
	            demod_32b[4*i+1] = -p_dstrc_dat[i].real + chanfact_tmp1;
            } else {
	            demod_32b[4*i+1] = p_dstrc_dat[i].real + chanfact_tmp1;
            }

            //The third bit decide
            if(p_dstrc_dat[i].imag >= chanfact_tmp1){
	            demod_32b[4*i+2] = 2*p_dstrc_dat[i].imag - chanfact_tmp1;
            } else if(p_dstrc_dat[i].imag >= -chanfact_tmp1){
	            demod_32b[4*i+2] = 2*p_dstrc_dat[i].imag;
            } else{
	            demod_32b[4*i+2] = 2*p_dstrc_dat[i].imag + chanfact_tmp1;
            }

            //The fourth bit decide
            if(p_dstrc_dat[i].imag >= 0){
	            demod_32b[4*i+3] = -p_dstrc_dat[i].imag + chanfact_tmp1;
            } else{
	            demod_32b[4*i+3] = p_dstrc_dat[i].imag + chanfact_tmp1;
            }        

        }
        break;
    case 6:
        for(i=0; i < block_len; i++) {
            //calculate temp channel factor
			/*
            chanfact_tmp1 = (INT32)(chan_factor[i]*1264 >> 12);
            chanfact_tmp2 = (INT32)(chan_factor[i]*2528 >> 12);
            chanfact_tmp3 = (INT32)(chan_factor[i]*3792 >> 12);
            chanfact_tmp4 = (INT32)(chan_factor[i]*6320 >> 12);
            chanfact_tmp5 = (INT32)(chan_factor[i]*7584 >> 12);

            chanfact_tmp1 = (INT32)(chan_factor[i]*0.3086067);
            chanfact_tmp2 = (INT32)(chan_factor[i]*0.3086067*2);
            chanfact_tmp3 = (INT32)(chan_factor[i]*0.3086067*3);
            chanfact_tmp4 = (INT32)(chan_factor[i]*0.3086067*5);
            chanfact_tmp5 = (INT32)(chan_factor[i]*0.3086067*6);
*/
			chanfact_tmp1 = (INT32)(chan_factor[i]*316 >> 10);
            chanfact_tmp2 = (INT32)(chan_factor[i]*632 >> 10);
            chanfact_tmp3 = (INT32)(chan_factor[i]*948 >> 10);
            chanfact_tmp4 = (INT32)(chan_factor[i]*1580 >> 10);
            chanfact_tmp5 = (INT32)(chan_factor[i]*1896 >> 10);

            //The first bit decide
            if(p_dstrc_dat[i].real >= chanfact_tmp3) {
	            demod_32b[6*i] = 4*p_dstrc_dat[i].real - chanfact_tmp5;
            } else if(p_dstrc_dat[i].real >= chanfact_tmp2) {
	            demod_32b[6*i] = 3*p_dstrc_dat[i].real - chanfact_tmp3;
            } else if(p_dstrc_dat[i].real >= chanfact_tmp1) {
	            demod_32b[6*i] = 2*p_dstrc_dat[i].real - chanfact_tmp1;
            } else if(p_dstrc_dat[i].real >= -chanfact_tmp1) {
	            demod_32b[6*i] = p_dstrc_dat[i].real;
            } else if(p_dstrc_dat[i].real >= -chanfact_tmp2){
	            demod_32b[6*i] = 2*p_dstrc_dat[i].real + chanfact_tmp1;
            } else if(p_dstrc_dat[i].real >= -chanfact_tmp3){
	            demod_32b[6*i] = 3*p_dstrc_dat[i].real + chanfact_tmp3;
            } else{
	            demod_32b[6*i] = 4*p_dstrc_dat[i].real + chanfact_tmp5;
            }

            //The second bit decide
            if(p_dstrc_dat[i].real >= chanfact_tmp3) {
	            demod_32b[6*i+1] = -2*p_dstrc_dat[i].real + chanfact_tmp4;
            } else if(p_dstrc_dat[i].real >= chanfact_tmp1) {
	            demod_32b[6*i+1] = -p_dstrc_dat[i].real + chanfact_tmp2;
            } else if(p_dstrc_dat[i].real >= 0) {
	            demod_32b[6*i+1] = -2*p_dstrc_dat[i].real + chanfact_tmp3;
            } else if(p_dstrc_dat[i].real >= -chanfact_tmp1) {
	            demod_32b[6*i+1] = 2*p_dstrc_dat[i].real + chanfact_tmp3;
            } else if(p_dstrc_dat[i].real >= -chanfact_tmp3) {
	            demod_32b[6*i+1] = p_dstrc_dat[i].real + chanfact_tmp2;
            } else {
	            demod_32b[6*i+1] = 2*p_dstrc_dat[i].real + chanfact_tmp4;
            }

            //The third bit decide
            if(p_dstrc_dat[i].real >= chanfact_tmp2) {
	            demod_32b[6*i+2] = -p_dstrc_dat[i].real + chanfact_tmp3;
            } else if(p_dstrc_dat[i].real >= 0) {
	            demod_32b[6*i+2] = p_dstrc_dat[i].real - chanfact_tmp1;
            } else if(p_dstrc_dat[i].real >= -chanfact_tmp2) {
	            demod_32b[6*i+2] = -p_dstrc_dat[i].real - chanfact_tmp1;
            } else {
	            demod_32b[6*i+2] = p_dstrc_dat[i].real + chanfact_tmp3;
            }

            //The fourth bit decide
            if(p_dstrc_dat[i].imag >= chanfact_tmp3) {
	            demod_32b[6*i+3] = 4*p_dstrc_dat[i].imag - chanfact_tmp5;
            } else if(p_dstrc_dat[i].imag >= chanfact_tmp2) {
	            demod_32b[6*i+3] = 3*p_dstrc_dat[i].imag - chanfact_tmp3;
            } else if(p_dstrc_dat[i].imag >= chanfact_tmp1) {
	            demod_32b[6*i+3] = 2*p_dstrc_dat[i].imag - chanfact_tmp1;
            } else if(p_dstrc_dat[i].imag >= -chanfact_tmp1) {
	            demod_32b[6*i+3] = p_dstrc_dat[i].imag;
            } else if(p_dstrc_dat[i].imag >= -chanfact_tmp2) {
	            demod_32b[6*i+3] = 2*p_dstrc_dat[i].imag + chanfact_tmp1;
            } else if(p_dstrc_dat[i].imag >= -chanfact_tmp3) {
	            demod_32b[6*i+3] = 3*p_dstrc_dat[i].imag + chanfact_tmp3;
            } else {
	            demod_32b[6*i+3] = 4*p_dstrc_dat[i].imag + chanfact_tmp5;
            }

            //The fifth bit decide
            if(p_dstrc_dat[i].imag >= chanfact_tmp3) {
	            demod_32b[6*i+4] = -2*p_dstrc_dat[i].imag + chanfact_tmp4;
            } else if(p_dstrc_dat[i].imag >= chanfact_tmp1) {
	            demod_32b[6*i+4] = -p_dstrc_dat[i].imag + chanfact_tmp2;
            } else if(p_dstrc_dat[i].imag >= 0) {
	            demod_32b[6*i+4] = -2*p_dstrc_dat[i].imag + chanfact_tmp3;
            } else if(p_dstrc_dat[i].imag >= -chanfact_tmp1) {
	            demod_32b[6*i+4] = 2*p_dstrc_dat[i].imag + chanfact_tmp3;
            } else if(p_dstrc_dat[i].imag >= -chanfact_tmp3) {
	            demod_32b[6*i+4] = p_dstrc_dat[i].imag + chanfact_tmp2;
            } else {
	            demod_32b[6*i+4] = 2*p_dstrc_dat[i].imag + chanfact_tmp4;
            }

            //The sixth bit decide
            if(p_dstrc_dat[i].imag >= chanfact_tmp2) {
	            demod_32b[6*i+5] = -p_dstrc_dat[i].imag + chanfact_tmp3;
            } else if(p_dstrc_dat[i].imag >= 0){
	            demod_32b[6*i+5] = p_dstrc_dat[i].imag - chanfact_tmp1;
            } else if(p_dstrc_dat[i].imag >= -chanfact_tmp2){
	            demod_32b[6*i+5] = -p_dstrc_dat[i].imag - chanfact_tmp1;
            } else{
	            demod_32b[6*i+5] = p_dstrc_dat[i].imag + chanfact_tmp3;
            }

        }
        break;
    default:
        printf("ERROR: No such demodulation type (dl_dmodulation)!!\n");
        break;
    }

    demod_len = (UINT32)(block_len*map_bit);

    // INT16 output
	
    switch(map_bit) {
        case 2:
			for(i=0; i < demod_len; i++) {
			   p_demod_dat[i] = (INT16)(-demod_32b[i] >> data_width);	// VA
//			   p_demod_dat[i] = (INT16)(-demod_32b[i] >> (data_width-1));	// SUI
			}
            break;
        case 4:
			if(rate_id == 16) {
				for(i=0; i < demod_len; i++) {
					p_demod_dat[i] = (INT16)(-demod_32b[i] >> (data_width-1));
				}
			} else {
				for(i=0; i < demod_len; i++) {
					p_demod_dat[i] = (INT16)(-demod_32b[i] >> (data_width-2));
				}
			}
            break;
        case 6:

			if(rate_id == 18) {
				for(i=0; i < demod_len; i++) {
					p_demod_dat[i] = (INT16)(-demod_32b[i] >> (data_width - 2));
				}
			} else if(rate_id == 19){
				for(i=0; i < demod_len; i++) {
					p_demod_dat[i] = (INT16)(-demod_32b[i] >> (data_width - 2));
				}
			} else {
				for(i=0; i < demod_len; i++) {
//					p_demod_dat[i] = (INT16)(-demod_32b[i] >> (data_width - 4));//VA
					p_demod_dat[i] = (INT16)(-demod_32b[i] >> (data_width - 2));//SUI
				}
			}

            break;
    }
/*
	for(i=0; i < demod_len; i++) {
		if(p_demod_dat[i] > 500) {
			p_demod_dat[i] = 500;
		} else if(p_demod_dat[i] < -500){
			p_demod_dat[i] = -500;
		} 
	}
*/
/*	
	for(i=0; i < demod_len; i++) {
		if(p_demod_dat[i] > 1024) {
			p_demod_dat[i] = 127;
		} else if(p_demod_dat[i] > 512) {
			p_demod_dat[i] = 94;
		} else if(p_demod_dat[i] > 256) {
			p_demod_dat[i] = 78;
		} else if(p_demod_dat[i] > 128) {
			p_demod_dat[i] = 69;
		} else if(p_demod_dat[i] > 64) {
			p_demod_dat[i] = 64;
		} else if(p_demod_dat[i] > -64){
			p_demod_dat[i] = p_demod_dat[i];
		} else if(p_demod_dat[i] > -128){
			p_demod_dat[i] = -64;
		} else if(p_demod_dat[i] > -256){
			p_demod_dat[i] = -69;
		} else if(p_demod_dat[i] > -512){
			p_demod_dat[i] = -78;
		} else if(p_demod_dat[i] > -1024){
			p_demod_dat[i] = -94;
		} else {
			p_demod_dat[i] = -128;
		}
	}
*/
	for(i=0; i < demod_len; i++) {
		if(p_demod_dat[i] > 1023) {
			p_demod_dat[i] = 127;
		} else if(p_demod_dat[i] > 895) {
			p_demod_dat[i] = 123;
		} else if(p_demod_dat[i] > 767) {
			p_demod_dat[i] = 119;
		} else if(p_demod_dat[i] > 639) {
			p_demod_dat[i] = 115;
		} else if(p_demod_dat[i] > 511) {
			p_demod_dat[i] = 111;
		} else if(p_demod_dat[i] > 447) {
			p_demod_dat[i] = 107;
		} else if(p_demod_dat[i] > 383) {
			p_demod_dat[i] = 103;
		} else if(p_demod_dat[i] > 319) {
			p_demod_dat[i] = 99;
		} else if(p_demod_dat[i] > 255) {
			p_demod_dat[i] = 95;
		} else if(p_demod_dat[i] > 223) {
			p_demod_dat[i] = 91;
		} else if(p_demod_dat[i] > 191) {
			p_demod_dat[i] = 87;
		} else if(p_demod_dat[i] > 159) {
			p_demod_dat[i] = 83;
		} else if(p_demod_dat[i] > 127) {
			p_demod_dat[i] = 79;
		} else if(p_demod_dat[i] > 111) {
			p_demod_dat[i] = 75;
		} else if(p_demod_dat[i] > 95) {
			p_demod_dat[i] = 71;
		} else if(p_demod_dat[i] > 79) {
			p_demod_dat[i] = 67;
		} else if(p_demod_dat[i] > 63) {
			p_demod_dat[i] = 63;
		} else if(p_demod_dat[i] > -64) {
			p_demod_dat[i] = p_demod_dat[i];
		} else if(p_demod_dat[i] > -80){
			p_demod_dat[i] = -64;
		} else if(p_demod_dat[i] > -96){
			p_demod_dat[i] = -68;
		} else if(p_demod_dat[i] > -112){
			p_demod_dat[i] = -72;
		} else if(p_demod_dat[i] > -128){
			p_demod_dat[i] = -76;
		} else if(p_demod_dat[i] > -160){
			p_demod_dat[i] = -80;
		} else if(p_demod_dat[i] > -192){
			p_demod_dat[i] = -84;
		} else if(p_demod_dat[i] > -224){
			p_demod_dat[i] = -88;
		} else if(p_demod_dat[i] > -256){
			p_demod_dat[i] = -92;
		} else if(p_demod_dat[i] > -320){
			p_demod_dat[i] = -96;
		} else if(p_demod_dat[i] > -384){
			p_demod_dat[i] = -100;
		} else if(p_demod_dat[i] > -448){
			p_demod_dat[i] = -104;
		} else if(p_demod_dat[i] > -512){
			p_demod_dat[i] = -108;
		} else if(p_demod_dat[i] > -640){
			p_demod_dat[i] = -112;
		} else if(p_demod_dat[i] > -768){
			p_demod_dat[i] = -116;
		} else if(p_demod_dat[i] > -896){
			p_demod_dat[i] = -120;
		} else if(p_demod_dat[i] > -1024){
			p_demod_dat[i] = -124;
		} else {
			p_demod_dat[i] = -128;
		}
	}

	

    if(demod_32b != NULL) {
        free(demod_32b);
    }

    return(0);
}



//*******************************************************************************
void main(){

	
 
	UINT16 i,j,k;


	COMPLEX32  p_dstrc_dat[1];
	INT16 p_demod_dat[1];
	INT32 chan_factor[1] = {1555976};
	UINT32 block_len = 1;
	UINT8 rate_id = 18;
	UINT16 data_width = 12;


	FILE *fp_demap_data;
	fp_demap_data = fopen("demap_data.txt","w");


	p_dstrc_dat[0].real = -1724888;
	p_dstrc_dat[0].imag = 1506812;





	dl_demodulation_sum
        (
            p_dstrc_dat,       
            p_demod_dat,       
            chan_factor,       
            block_len,          
            rate_id,           
			data_width 
		);


	for(i = 0; i < 6; i++){
		fprintf(fp_demap_data,"demap_data[%d]: %d\n",i, p_demod_dat[i]);
	}

	fclose(fp_demap_data);
}



	



⌨️ 快捷键说明

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