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- ...
随机推荐
- 西南民族大学第十二届程序设计竞赛(同步赛) A.逃出机房 (bfs)
题意:有来两个人A和B,A追B,A和B每次向上下左右移动一个单位,一共有两扇门,问A是否可以追上B(在门口追上也算合法). 题解:当时看题意说在门口也算?就觉得是判断两个人到门口的时间,对他们两个人分 ...
- 牛客小白月赛17 A 小sun的假期
传送门 题意: 第一行两个数n,m,代表总共有n天,m个安排.接下来有m行,每行是一个安排l,r,代表从第l天到第r天,小sun有安排了.安排可能会重复. 小 sun 非常喜欢放假,尤其是那种连在一起 ...
- Codeforces Round #636div3 D. Constant Palindrome Sum (划分区间,差分)
题意:给你一个长度为偶数n的数组,每次可以将一个元素修改为不大于k的值,要求每个a[i]+a[n-i+1]都相等,求最少操作多少次 题解:假设每一对的和都为sum,小的记为mn,大的记为mx; ...
- HDU - 6287 口算训练 二分+质因数分解
小Q非常喜欢数学,但是他的口算能力非常弱.因此他找到了小T,给了小T一个长度为nn的正整数序列a1,a2,...,ana1,a2,...,an,要求小T抛出mm个问题以训练他的口算能力.每个问题给出三 ...
- 缓冲区溢出实验 4 内存管理(类似于malloc free)
实验环境.代码.及准备 https://www.cnblogs.com/lqerio/p/12870834.html vul4 观察foo函数,可见问题在于最后一次tfree(q).由于之前已经tfr ...
- Automatic merge failed; fix conflicts and then commit the result.解决方法
产生原因: git pull 的时候会分为两步,第一步先从远程服务器上拉下代码,第二步进行merge.当你merge时候失败了就会产生Automatic merge failed; fix confl ...
- Bootstrap页头
页头组件能够为 h1 标签增加适当的空间,并且与页面的其他部分形成一定的分隔.它支持 h1 标签内内嵌 small 元素的默认效果,还支持大部分其他组件(需要增加一些额外的样式). <div c ...
- 北京网络赛G BOXES 大模拟+BFS
题目描述 Description There is a strange storehouse in PKU. In this storehouse there are n slots for boxe ...
- 重学c#————struct
前言 简单整理一下struct. 正文 struct 对于struct 而言呢,我们往往会拿class作为对比,但是呢,我们在初学阶段用class来替代struct,struct的存在感越来越低了. ...
- 010. NET5_命令参数读取+配置多种读取
上节课遗留问题:上节脚本启动后,CSS样式丢失问题 解决办法:a.拷贝丢失的wwwroot目录:b. 给UesStaticFiles类指定读取wwwroot目录 静态文件读取 Nuget引入:Micr ...