MATLAB中FFT_HDL_Optimized模块定点(IEEE754单精度float格式)二进制与十进制转换实现
早些时间段,做了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
MATLAB中FFT_HDL_Optimized模块定点(IEEE754单精度float格式)二进制与十进制转换实现的更多相关文章
- Matlab常用函数:二进制和十进制转换,均值,方差
文章目录 Size s=size(A) [r,c]=size(A) [r,c,m]=size(A) size(A,n) 二进制和十进制转换 dec2bin mean 均值 mean(a,1) mean ...
- matlab中的reshape快速理解,卷积和乘积之间的转换
reshape: THe convertion between convolution and multiplication:
- python中struct模块
# #********struct模块********# # 1.按照指定格式将Python数据转换为字符串,该字符串为字节流,如网络传输时, # 不能传输int,此时先将int转化为字节流,然后再发 ...
- matlab中num2str 将数字转换为字符数组
参考:https://ww2.mathworks.cn/help/matlab/ref/num2str.html?searchHighlight=num2str&s_tid=doc_srcht ...
- matlab中upper 将字符串转换为大写
参考:https://ww2.mathworks.cn/help/matlab/ref/fprintf.html?searchHighlight=fprintf&s_tid=doc_srcht ...
- matlab中fft快速傅里叶变换
视频来源:https://www.bilibili.com/video/av51932171?t=628. 博文来源:https://ww2.mathworks.cn/help/matlab/ref/ ...
- matlab中patch函数的用法
http://blog.sina.com.cn/s/blog_707b64550100z1nz.html matlab中patch函数的用法——emily (2011-11-18 17:20:33) ...
- Matlab中的一些小技巧
(转于它处,仅供参考) 1.. Ctrl+C 中断正在执行的操作 如果程序不小心进入死循环,或者计算时间太长,可以在命令窗口中使用Ctrl+c来中断.MATLAB这时可能正疲于应付,响应会有些滞后. ...
- matlab 中txt文件(含字符及数值)处理
matlab 中txt文件(含字符及数值)处理 (2008-08-02 09:45:12) 转载▼ 标签: 杂谈 分类: matlab及C学习 Matlab文件操作及读txt文件ZZ 2008-07- ...
随机推荐
- hutool学习总结
1. 为什么要学习Hutool的使用 Hutool官网 中文写的已经很清楚了 Hutool是一款强力的工具类.封装了工作开发中一些常见的功能操作.避免重复造轮子,使用它大大提高的开发效率. 2. Hu ...
- BKDR字符串哈希
BKDR字符串哈希 bkdrhash冲突的可能性非常小,但是由于\(hash\)值非常大不能映射到哈希数组地址上,所以可以通过取余,用余数作为索引地址.但这样做造成了可能的地址冲突. #include ...
- Linux系统CentOS进入单用户模式和救援模式详解
一.概述 目前在运维日常工作中,经常会遇到服务器异常断电.忘记root密码.系统引导文件损坏无法进入系统等等操作系统层面的问题,给运维带来诸多不便,现将上述现象的解决方法和大家分享一下,本次主要以Ce ...
- wordpress 插件安装 Too Many Requests
解决办法是在插件界面不直接点击安装/更新,右键新标签页打开链接,会有下载地址. 下载后上传插件即可.
- codeforces 875B
B. Sorting the Coins time limit per test 1 second memory limit per test 512 megabytes input standard ...
- Dos简单命令及CMD打开方式
打开CMD方式 开始+系统+命令提示符 WIN健+R 输入cmd打开控制台(推荐使用) 在任意文件夹下面,按住shift+鼠标右击,进入powershell 资源管理器的地址栏路径最前面+cmd+空格 ...
- 前端 Web 异常监控系统 All In One
前端 Web 异常监控系统 All In One Sentry https://sentry.io trackjs https://trackjs.com/ rollbar https://rollb ...
- 如何使用 js 实现tooltips 的 hover 时候坐标的自动计算
如何使用 js 实现tooltips 的 hover 时候坐标的自动计算 js 监听 mouseover event https://developer.mozilla.org/zh-CN/docs/ ...
- js swap array
js swap array ES6 swap array 就地交换 no need let , const [ b, a, ] = [ a, b, ]; // ES6 swap const arr = ...
- js class static property & public class fields & private class fields
js class static property class static property (public class fields) const log = console.log; class ...