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- ...
随机推荐
- Codeforces Round #657 (Div. 2) B. Dubious Cyrpto(数论)
题目链接:https://codeforces.com/contest/1379/problem/B 题意 给出三个正整数 $l,r,m$,判断在区间 $[l,r]$ 内是否有 $a,b,c$ 满足存 ...
- 吉哥系列故事――恨7不成妻 HDU - 4507
题目: 单身! 依然单身! 吉哥依然单身! DS级码农吉哥依然单身! 所以,他生平最恨情人节,不管是214还是77,他都讨厌! 吉哥观察了214和77这两个数,发现: 2+1+4=7 7+7=7*2 ...
- WSL ubuntu重置密码
1. 在powershell中切换到root: 2. 进入ubuntu: 3. 修改制定用户的密码: 4. 切换回默认的用户:
- .NET并发编程-函数闭包
本系列学习在.NET中的并发并行编程模式,实战技巧 内容目录 函数式编程闭包的应用记忆化函数缓存 函数式编程 一个函数输出当做另一个函数输入.有时候一个复杂问题,我们拆分成很多个步骤函数,这些函数组合 ...
- Linux POSIX共享内存方法&ipcs &struct shmid_ds
内容是主进程创建子进程计算斐波那契数列. 其中计算到第几项是有主进程命令行输入. 共享内存段,并且查看了一些信息. 参考操作系统概念第七版 3.10,3.11 关于LINUX C库函数 中的 fpri ...
- Spring(三) Spring IOC 初体验
Web IOC 容器初体验 我们还是从大家最熟悉的 DispatcherServlet 开始,我们最先想到的还是 DispatcherServlet 的 init() 方法.我们发现在 Dispath ...
- 如何快速定位 Redis 热 key?
背景 在 Redis 中,热 key 指的是那些在一段时间内访问频次比较高的键值,具体到业务上,商品的限时抢购.瞬时的新闻热点或某个全局性的资源,都极有可能产生热点 key. 热点 key 的出现可能 ...
- 使用 js 实现一个简易版的模版引擎
使用 js 实现一个简易版的模版引擎 regex (function test() { this.str = str; })( window.Test = ...; format() { let ar ...
- Flutter & release an iOS app
Flutter & release an iOS app https://flutter.dev/docs/deployment/ios .ipa https://en.wikipedia.o ...
- navigator.geolocation.getCurrentPosition
navigator.geolocation.getCurrentPosition Geolocation API Specification 2nd Edition W3C Recommendatio ...