📄 modulation.c
字号:
/********************************************************
* *
* Modulation and demodulation functions *
* Memory optimized version. *
* Black Team *
* Author: Erik 2005-04-26 *
********************************************************/
#include <math.h>
#define LEN 128 // Assuming framelength always vill be 128, might be adjusted later.
#define LENFFT2 124
// input is integers in which information should be stored in all 32 bits.
// outut is modulated signal, is complex valued == double length, odd index gives the real value and even index gives corresponding imaginary number.
// 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
void modulation(float *output, unsigned int *input, const short *b, float *e)
{
const short re_p2[] = {1, -1}; // OBS!!! THE ORDER OF THESE VALUES ARE SWITCHED!!!
const short im_p4[] = {-1, 1, 1, 1, -1, -1, 1, -1};
const short sign[] = {-1, 1};
const float val_p16[] = {(float)0.9468, (float)0.3162};
const float val_p64[] = {1.0801, 0.7715, 0.1543, 0.4629};
const float val_p256[] = {1.1505, 0.9971, 0.6903, 0.8437, 0.0767, 0.2301, 0.5369, 0.3835};
int i, tmp; // The tmp is the index number which defines the value of the bits defined by b[].
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, temporary variable for the assigning of the other b:s
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;
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);
}
// Start working at first subcarrier:
for(i=0; i<LEN; i++)
{
switch(b[i])
{
case 1 : {
output[i*2] = re_p2[!(input[b2[i]]&(0x01 << b3[i]))]*e[i]; // b2 anger position i bitar fr錸 start, maska ut r鋞t bit..
output[i*2+1] = 0;
break;
}
case 2 : {
if(!(t[i]-1))
{
tmp = (input[b2[i]]&(0x80000000)) >> 31;
tmp = tmp + ((input[b2[i+1]]&(0x1)) << 1);
}
else
tmp = (input[b2[i]]&(0x03 << b3[i])) >> b3[i]; // get the 2 bits and shift to become a correct index
output[i*2] = im_p4[tmp*2]*e[i];
output[i*2+1] = im_p4[tmp*2+1]*e[i];
break;
}
case 4 : {
if(t[i]<4)
{
tmp = getbits((unsigned)input[b2[i]], 31, t[i]);
tmp = tmp + (getbits(input[b2[i+1]], (3-t[i]), (4-t[i])) << t[i]);
}
else
tmp = (input[b2[i]]&(0x0F << b3[i])) >> b3[i]; // get the 4 bits and shift to become a correct index
output[i*2] = val_p16[(tmp&0x04) >> 2]*e[i]*sign[(tmp&0x08) >> 3];
output[i*2+1] = val_p16[(tmp&0x01)]*e[i]*sign[(tmp&0x02) >> 1];
break;
}
case 6 : {
if(t[i]<6)
{
tmp = getbits((unsigned)input[b2[i]], 31, t[i]);
tmp = tmp + (getbits(input[b2[i+1]], (5-t[i]), (6-t[i])) << t[i]);
}
else
tmp = (input[b2[i]]&(0x3F << b3[i])) >> b3[i]; // get the 6 bits and shift to become a correct index
output[i*2] = val_p64[(tmp&0x03)]*e[i]*sign[(tmp&0x04) >> 2];
output[i*2+1] = val_p64[(tmp&0x18) >> 3]*e[i]*sign[!((tmp&0x20) >> 5)];
break;
}
case 8 : {
if(t[i]<8)
{
tmp = getbits((unsigned)input[b2[i]], 31, t[i]);
tmp = tmp + (getbits(input[b2[i+1]], (7-t[i]), (8-t[i])) << t[i]);
}
else
tmp = (input[b2[i]]&(0xFF << b3[i])) >> b3[i]; // get the 6 bits and shift to become a correct index
output[i*2] = val_p256[(tmp&0x07)]*e[i]*sign[(tmp&0x08) >> 3];
output[i*2+1] = val_p256[(tmp&0x70) >> 4]*e[i]*sign[!((tmp&0x80) >> 7)];
break;
}
default : // Default case should never occur..
output[i]=0;
output[i+1]=0;
}
}
}
void modulation2(float *output, unsigned int *input, const short *b, short index)
{
const short sign[2] = {-1, 1};
const float im_p4[8] = {-0.7071, 0.7071, 0.7071, 0.7071, -0.7071, -0.7071, 0.7071, -0.7071};
const float val_p16[2] = {(float)0.9468, (float)0.3162};
const float val_p64[4] = {1.0801, 0.7715, 0.1543, 0.4629};
const float val_p256[] = {1.1505, 0.9971, 0.6903, 0.8437, 0.0767, 0.2301, 0.5369, 0.3835};
short i, tmp; //The tmp is the index number which defines the value of the bits defined by b.
short b2=0; //which int[x] from input should be used.
short b3=index; // which bit in the int is our first bit to consider.
for(i=0; i<LENFFT2; i++){
switch(b[i])
{
case 0:
output[i*2] = 0;
output[i*2+1] = 0;
break;
case 2:
tmp = (input[b2]&(0x03 << b3)) >> b3; // get the 2 bits and shift to become a correct index
output[i*2] = im_p4[tmp*2];
output[i*2+1] = im_p4[tmp*2+1];
b3 +=2;
if(b3==32){
b3=0;
b2++;
}
break;
case 4:
if((32-b3)<4){ //less than 4 bits remain in Uint32
tmp = getbits((unsigned)input[b2], 31, (32-b3));//last bits
tmp = tmp + (getbits(input[b2+1], (3-(32-b3)), (4-(32-b3))) << (32-b3));
b3 = (b3+4)%32;
b2++;
}
else {
tmp = (input[b2]&(0x0F << b3)) >> b3; // get the 4 bits and shift to become a correct index
b3+=4;
if(b3==32){
b3=0;
b2++;
}
}
output[i*2] = val_p16[(tmp&0x04) >> 2]*sign[(tmp&0x08) >> 3];
output[i*2+1] = val_p16[(tmp&0x01)]*sign[(tmp&0x02) >> 1];
break;
case 6 :
if((32-b3)<6)
{
tmp = getbits((unsigned)input[b2], 31, (32-b3));
tmp = tmp + (getbits(input[b2+1], (5-(32-b3)), (6-(32-b3))) << (32-b3));
b3 = (b3+6)%32;
b2++;
}
else {
tmp = (input[b2]&(0x3F << b3)) >> b3; // get the 6 bits and shift to become a correct index
b3+=6;
if(b3==32){
b3=0;
b2++;
}
}
output[i*2] = val_p64[(tmp&0x03)]*sign[(tmp&0x04) >> 2];
output[i*2+1] = val_p64[(tmp&0x18) >> 3]*sign[!((tmp&0x20) >> 5)];
break;
case 8 :
if((32-b3)<8)
{
tmp = getbits((unsigned)input[b2], 31, (32-b3));
tmp = tmp + (getbits(input[b2+1], (7-(32-b3)), (8-(32-b3))) << (32-b3));
b3 = (b3+8)%32;
b2++;
}
else{
tmp = (input[b2]&(0xFF << b3)) >> b3; // get the 8 bits and shift to become a correct index
b3+=8;
if(b3==8){
b3=0;
b2++;
}
}
output[i*2] = val_p256[(tmp&0x07)]*sign[(tmp&0x08) >> 3];
output[i*2+1] = val_p256[(tmp&0x70) >> 4]*sign[!((tmp&0x80) >> 7)];
break;
}
}
}
void demodulation_shart(const short *input, short *output)
{
// float k[2];
short i, tmp = 0; // tmp gives the index of the d var.
// short t[LEN];
short b2; // which int[x] from input should be used.
short b3; // which bit in the int is our first bit to consider.
// short b4[LEN]; // real bit position from start of input
b2=0; // '0' indicates the first int.
b3=0; // '0' indicates the first bit of an int.
for(i=0; i<(LENFFT2*2); i++)
{
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;
}
output[b2] = output[b2] | (tmp << b3);
b3+=2;
if(b3==4)
{
b3=0;
b2++;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -