早些時間段,做了Matlab中FFT_HDL_Optimzed模塊FFT HDL代碼仿真,并與Xilinx Vivado自帶的xfft IP進行單精度浮點比較(后面隨筆敘述),因為FFT_HDL_Optimized只有在設定輸入為定點格式fixdt()的時候,生成的HDL代碼才能進行綜合,否則只能用于仿真,因此做了相應的定點生成,
對于系統的輸入為定點格式fixdt(1,32,23),而在不改變FFT輸出幅值時,FFT_HDL_Optimzed輸出定點模型fixdt(1,45,23),為了進行FFT_HDL_Optimzed模塊輸出結果與xfft IP進行比較,特撰寫了相應的C代碼,僅供參考(有符號45位二進制--------->定點fixdt(1,45,23)):
1 #include<stdio.h> 2 #include<string.h> 3 #include<math.h> 4 #include<stdlib.h> 5 6 #define MAX_LINE 47 //MAX_LINE 從文本中讀取的字符長度 7 8 #define LENGTH_OF_BIN 45 //LENGTH_OF_BIN指定了定點二進制數的個數 9 10 #define LENGTH_OF_INTEGER 21 //LENGTH_OF_INTEGER指定了定點二進制數的整數個數 11 12 float fun2_10(char str[], int length_of_integer, int length_of_bin, int now_position)//帶小數的二進制轉十進制 此處j = 21 ; len = 45 13 { 14 int k = length_of_integer+1;//將k指向小數部分第一位 15 int cetz=0,cetx=-1; 16 long Sumz=0; 17 double Sumx=0; 18 19 for( ; length_of_integer > 0; length_of_integer--)//整數部分逆向累加 20 { 21 Sumz+=(str[length_of_integer]-'0')*pow(2,cetz); 22 cetz++; 23 } 24 for( ; k < length_of_bin; k++)//小數部分正向累加 25 { 26 Sumx+=(str[k]-'0')*pow(2,cetx); 27 cetx--; 28 } 29 //printf("%d = %f\n\t",now_position,Sumz+Sumx);//整數部分加小數部分 30 31 return (Sumz+Sumx); 32 } 33 34 35 float fun2_10_negative(char str[], int length_of_integer, int length_of_bin, int now_position)//帶小數的二進制轉十進制 此處j = 21 ; len = 45 36 { 37 int k = length_of_integer+1;//將k指向小數部分第一位 38 int cetz=0,cetx=-1; 39 long Sumz=0; 40 double Sumx=0; 41 42 for( ; length_of_integer > 0; length_of_integer--)//整數部分逆向累加 43 { 44 Sumz+=(str[length_of_integer]-'0')*pow(2,cetz); 45 cetz++; 46 } 47 for( ; k < length_of_bin; k++)//小數部分正向累加 48 { 49 Sumx+=(str[k]-'0')*pow(2,cetx); 50 cetx--; 51 } 52 //printf("%d = -%f\n\t",now_position,Sumz+Sumx);//整數部分加小數部分,負數取反后還需要加1 53 54 return (-(Sumz+Sumx)); 55 } 56 57 int main() 58 { 59 int count_buf = 0; 60 int negative_count = 1; 61 char buf[MAX_LINE]; 62 float result_2to10; 63 64 FILE *outfile; //定義所寫檔案的FILE 65 66 outfile = fopen("FFT_HDL_re_out.txt","w"); 67 if(outfile == NULL) 68 { 69 printf("can't open the FFT_HDL_re_out.txt\n"); 70 } 71 72 FILE *fp; //定義所讀檔案的FILE 73 int len; 74 if((fp = fopen("FFT_out_re_data.txt","r")) == NULL) 75 { 76 perror("fail to read"); 77 exit(1); 78 } 79 80 while(fgets(buf,MAX_LINE,fp) != NULL) 81 { 82 len = strlen(buf); 83 buf[len-1] = '\0'; //去掉換行符 84 count_buf++; 85 86 if(count_buf == 8192) 87 { 88 fclose(outfile); 89 break; 90 } 91 92 if(buf[0] == '0') //判斷二進制數為正數 93 { 94 result_2to10 = fun2_10(buf, LENGTH_OF_INTEGER, LENGTH_OF_BIN, count_buf); 95 } 96 else if(buf[0] == '1') //判斷二進制數為負數 97 { 98 for(negative_count = 1; negative_count < LENGTH_OF_BIN; negative_count++) 99 { 100 if(buf[negative_count]=='0') 101 { 102 buf[negative_count] = '1'; 103 } 104 else if(buf[negative_count] == '1') 105 { 106 buf[negative_count] = '0'; 107 } 108 } 109 110 result_2to10 = fun2_10_negative(buf, LENGTH_OF_INTEGER, LENGTH_OF_BIN, count_buf); 111 //printf("%d = -%f\n\t",count_buf,result_2to10); 112 } 113 114 115 //write the data to the FFT_HDL_im_out.txt 116 fprintf(outfile,"%f\n",result_2to10); 117 118 // printf("%d = %f\n\t",count_buf,result_2to10); 119 120 121 // printf("%d %s %d\n",count_buf,buf,len-1); 122 123 124 } 125 126 return 0; 127 }
下面再附上Matlab中十進制浮點數---------->轉IEEE754單精度浮點二進制格式的代碼實作
1 function fixed_bin=my_fix_flr_bin(a,numint,numdec) 2 % a為被定點化的矩陣或標量,為實數 3 % numint位整數,numdec位小數 4 % 選取的總位數為1+numint+numdec,其中1為符號位所占用 5 fixed_a=floor(a*2^numdec); % 模擬計算機中直接截位的結果 6 % 限幅 7 if ((fixed_a>=2^(numint+numdec))||(fixed_a<-2^(numint+numdec))) 8 fixed_a=sign(a)*(2^(numint+numdec)-1)+0.5*(sign(a)-1); 9 % 正數最大是2^(numint+numdec)-1,負數最大是-2^(numint+numdec) 10 end 11 % 轉化為補碼 12 if (a<0) 13 % 需要寫成補碼的形式 14 fixed_a=fixed_a+2^(numint+numdec); 15 fixed_bin=dec2bin(fixed_a,numint+numdec); 16 fixed_bin=strcat('1',fixed_bin); 17 else 18 fixed_bin=dec2bin(fixed_a,numint+numdec); 19 fixed_bin=strcat('0',fixed_bin); 20 end
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/256224.html
標籤:其他
