/* 十进制数据转化成二进制数据,可选择使用补码或源码*/ /* d_to_b.h */#define data_length 79177#define data_bit 16 //转化成二进制后的数据位宽#define shift_length 3 //移位位宽#define com_code 0 //取1则负数转化为补码#define code ~com_codedouble data_in_i[data_length]; // I路输入的十进制数据寄存器double data_in_q[data_length]; // Q路输入的十进制数据寄存器int data_reg_i[data_length];int data_reg_q[data_length];int binary_i[data_bit]; //存放二进制数据的寄存器int binary_q[data_bit]; //存放二进制数据的寄存器int binary_i_com[data_bit]; //存放二进制补码数据的寄存器int binary_q_com[data_bit]; //存放二进制补码数据的寄存器/* d_to_b.c */#include <stdio.h>#include <math.h>#include "d_to_b.h"/****************************************************************** 十进制转化成二进制,且负数可选择用补码还是源码表示~~~~******************************************************************/int main(){int i;int j, k;/* ----------------------读入数据--------------------- */FILE *fp_in_i, *fp_in_q;fp_in_i = fopen("tx_interp_out_i.txt", "r");fp_in_q = fopen("tx_interp_out_q.txt", "r");for(i = 0; i < data_length; i++){fscanf(fp_in_i, "%lf", &data_in_i[i]);fscanf(fp_in_q, "%lf", &data_in_q[i]);}fclose (fp_in_i);fclose (fp_in_q);/* -------------------------------------------------- */FILE *fp_shift_i, *fp_shift_q;fp_shift_i = fopen("tx_shift_out_i.txt", "w+");fp_shift_q = fopen("tx_shift_out_q.txt", "w+");FILE *fp_out_i, *fp_out_q;fp_out_i = fopen("change_out_i.txt", "w+");fp_out_q = fopen("change_out_q.txt", "w+");/* -------------需要移位的数据先进行移位,即转化成整数,不需要转的,则设置shift_length=0------------ */for(i = 0; i < data_length; i++){data_reg_i[i] = data_in_i[i] * pow(2, shift_length);data_reg_q[i] = data_in_q[i] * pow(2, shift_length);fprintf(fp_shift_i, "%d\n", data_reg_i[i]);fprintf(fp_shift_q, "%d\n", data_reg_q[i]);}/* ----------------------------------------------- *//* ---------------------------十进制化为二进制,负数用补码-------------------------------- */#if com_codefor(i = 0; i < data_length; i++){/* --------------I路正数转化------------- */if(data_reg_i[i] >= 0){binary_i[data_bit - 1] = 0; //符号位为0fprintf(fp_out_i, "%d", binary_i[data_bit - 1]);for(j = 0, k = fabs(data_reg_i[i]); j < data_bit - 1; j++, k = k/2){if(k % 2 == 0)binary_i[j] = 0;elsebinary_i[j] = 1;}for(j = data_bit - 2; j >= 0; j--){fprintf(fp_out_i, "%d", binary_i[j]);}fprintf(fp_out_i, "\n");}/* --------------I路负数转化------------- */else{binary_i[data_bit - 1] = 1; //符号位为1fprintf(fp_out_i, "%d", binary_i[data_bit - 1]);/* ---------------------------以下要按位取反---------------------------- */if( -1 * data_reg_i[i] % 2 == 0 ){binary_i[0] = 0;binary_i_com[0] = 0;}else{binary_i[0] = 1;binary_i_com[0] = 1;}for(j = 1, k = fabs(data_reg_i[i]) / 2; j < data_bit - 1; j++, k = k/2){if(k % 2 == 0 && binary_i[j - 1] == 0 && binary_i_com[j - 1] == 0){binary_i[j] = 0;binary_i_com[j] = 0;}else if(k % 2 == 0 && binary_i[j - 1] == 0 && binary_i_com[j - 1] == 1){binary_i[j] = 0;binary_i_com[j] = 1;}else if(k % 2 == 0 && binary_i[j - 1] == 1 && binary_i_com[j - 1] == 0){binary_i[j] = 1;binary_i_com[j] = 1;}else if(k % 2 == 0 && binary_i[j - 1] == 1 && binary_i_com[j - 1] == 1){binary_i[j] = 1;binary_i_com[j] = 1;}else if(k % 2 == 1 && binary_i[j - 1] == 0 && binary_i_com[j - 1] == 0){binary_i[j] = 1;binary_i_com[j] = 1;}else if(k % 2 == 1 && binary_i[j - 1] == 0 && binary_i_com[j - 1] == 1){binary_i[j] = 1;binary_i_com[j] = 0;}else if(k % 2 == 1 && binary_i[j - 1] == 1 && binary_i_com[j - 1] == 0){binary_i[j] = 1;binary_i_com[j] = 0;}else{binary_i[j] = 1;binary_i_com[j] = 0;}}/* ------------------------------------------------------------------- */for(j = data_bit - 2; j >= 0; j--){fprintf(fp_out_i, "%d", binary_i_com[j]);}fprintf(fp_out_i, "\n");}/* ------------------------------------- *//* --------------Q路正数转化------------- */if(data_reg_q[i] >= 0){binary_q[data_bit - 1] = 0; //符号位为0fprintf(fp_out_q, "%d", binary_q[data_bit - 1]);for(j = 0, k = fabs(data_reg_q[i]); j < data_bit - 1; j++, k = k/2){if(k % 2 == 0)binary_q[j] = 0;elsebinary_q[j] = 1;}for(j = data_bit - 2; j >= 0; j--){fprintf(fp_out_q, "%d", binary_q[j]);}fprintf(fp_out_q, "\n");}/* --------------Q路负数转化------------- */else{binary_q[data_bit - 1] = 1; //符号位为1fprintf(fp_out_q, "%d", binary_q[data_bit - 1]);/* ---------------------------以下要按位取反---------------------------- */if( -1 * data_reg_q[i] % 2 == 0 ){binary_q[0] = 0;binary_q_com[0] = 0;}else{binary_q[0] = 1;binary_q_com[0] = 1;}for(j = 1, k = fabs(data_reg_q[i]) / 2; j < data_bit - 1; j++, k = k/2){if(k % 2 == 0 && binary_q[j - 1] == 0 && binary_q_com[j - 1] == 0){binary_q[j] = 0;binary_q_com[j] = 0;}else if(k % 2 == 0 && binary_q[j - 1] == 0 && binary_q_com[j - 1] == 1){binary_q[j] = 0;binary_q_com[j] = 1;}else if(k % 2 == 0 && binary_q[j - 1] == 1 && binary_q_com[j - 1] == 0){binary_q[j] = 1;binary_q_com[j] = 1;}else if(k % 2 == 0 && binary_q[j - 1] == 1 && binary_q_com[j - 1] == 1){binary_q[j] = 1;binary_q_com[j] = 1;}else if(k % 2 == 1 && binary_q[j - 1] == 0 && binary_q_com[j - 1] == 0){binary_q[j] = 1;binary_q_com[j] = 1;}else if(k % 2 == 1 && binary_q[j - 1] == 0 && binary_q_com[j - 1] == 1){binary_q[j] = 1;binary_q_com[j] = 0;}else if(k % 2 == 1 && binary_q[j - 1] == 1 && binary_q_com[j - 1] == 0){binary_q[j] = 1;binary_q_com[j] = 0;}else{binary_q[j] = 1;binary_q_com[j] = 0;}}/* ------------------------------------------------------------------- */for(j = data_bit - 2; j >= 0; j--){fprintf(fp_out_q, "%d", binary_q_com[j]);}fprintf(fp_out_q, "\n");}/* ------------------------------------- */}/* ----------------------------------------------------------------------------------- */#endif#if code/* ------------------------------十进制转二进制,负数不用补码--------------------------- */ for(i = 0; i < data_length; i++){/* --------------I路转化------------- */if(data_reg_i[i] >= 0){binary_i[data_bit - 1] = 0; //符号位为0fprintf(fp_out_i, "%d", binary_i[data_bit - 1]);}else{binary_i[data_bit - 1] = 1; //符号位为1fprintf(fp_out_i, "%d", binary_i[data_bit - 1]);}for(j = 0, k = fabs(data_reg_i[i]); j < data_bit - 1; j++, k = k/2){if(k % 2 == 0)binary_i[j] = 0;elsebinary_i[j] = 1;}for(j = data_bit - 2; j >= 0; j--){fprintf(fp_out_i, "%d", binary_i[j]);}fprintf(fp_out_i, "\n");/* --------------Q路转化------------- */if(data_reg_q[i] >= 0){binary_q[data_bit - 1] = 0; //符号位为0fprintf(fp_out_q, "%d", binary_q[data_bit - 1]);}else{binary_q[data_bit - 1] = 1; //符号位为1fprintf(fp_out_q, "%d", binary_q[data_bit - 1]);}for(j = 0, k = fabs(data_reg_q[i]); j < data_bit - 1; j++, k = k/2){if(k % 2 == 0)binary_q[j] = 0;elsebinary_q[j] = 1;}for(j = data_bit - 2; j >= 0; j--){fprintf(fp_out_q, "%d", binary_q[j]);}fprintf(fp_out_q, "\n");}/* ----------------------------------------------------------------------------------- */ #endifprintf("change has passed!\n");fclose(fp_shift_i);fclose(fp_shift_q);fclose(fp_out_i);fclose(fp_out_q);return 0;}。