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- ...
随机推荐
- AtCoder Beginner Contest 188 E - Peddler (树)
题意:有\(n\)个点,\(m\)条单向边,保证每条边的起点小于终点,每个点都有权值,找到联通的点的两个点的最大差值. 题解:因为题目说了起点小于终点,所以我们可以反向存边,然后维护连通边的前缀最小值 ...
- Codeforces Round #647 (Div. 2) - Thanks, Algo Muse! A、Johnny and Ancient Computer B、Johnny and His Hobbies C、Johnny and Another Rating Drop
题目链接:A.Johnny and Ancient Computer 题意: 给你两个数a,b.问你可不可以通过左移位运算或者右移位运算使得它们两个相等.可以的话输出操作次数,不可以输出-1 一次操作 ...
- AtCoder Beginner Contest 177 D - Friends (并查集)
题意:有\(n\)个人,给你\(m\)对朋友关系,朋友的朋友也是朋友,现在你想要将他们拆散放到不同的集合中,且每个集合中的人没有任何一对朋友关系,问最少需要多少集合. 题解:首先用并查集将朋友关系维护 ...
- 2019-2020 ACM-ICPC Brazil Subregional Programming Contest Problem M Maratona Brasileira de Popcorn (二分)
题意:有\(n\)袋爆米花,某个队伍有\(c\)个队员,每个队员每秒做多可以吃\(t\)粒爆米花,但一袋爆米花只能由一个队员吃完,并且一个队员只能吃连续的一袋或几袋,不能隔着吃某一袋,求将所有爆米花吃 ...
- c#记两个变量进行值交换
今天腊月二十九啦,无心上班,专注划水.然后就在那里翻帖子消磨时光. 看到了这样一个问题,有人提问为什么 a=b+(b=a)*0 ??? 第一眼看上去,我也有点蒙,仔细推敲了一下,嗯~的确是交换了 ...
- woj1016 cherry blossom woj1017 Billiard ball 几何
title: woj1016 cherry blossom date: 2020-03-18 20:00:00 categories: acm tags: [acm,几何,woj] 几何题,判断给出的 ...
- C# 类 (5)
Static 静态成员 创建一个类,实例化它,然后使用实例的各种方法或者属性 然而有时候我们不想实例化这个类,难道我们就没法用他里面的成员了吗?比如计算矩形的面积,就是长*宽,万年不变 当然可以,用s ...
- IPC$入侵
一 唠叨一下: 网上关于ipc$入侵的文章可谓多如牛毛,而且也不乏优秀之作,攻击步骤甚至可以说已经成为经典的模式,因此也没人愿意再把这已经成为定式的东西拿出来摆弄. 二 什么是ipc$ IPC$(In ...
- 微服务架构Day04-SpringBoot之web开发
引入项目 把html页面放在模板引擎文件夹templates下,这样能使用模板引擎的功能. 登录页面国际化 国际化:编写国际化配置文件 1.编写国际化配置文件,抽取页面需要显示的国际化消息 2.Spr ...
- Apple Screen Recorder All In One
Apple Screen Recorder All In One Apple macOS 自带录屏 QuickTime Player https://support.apple.com/zh-cn/g ...