将变量转换为不同数据类型 -凯发官方首页
将变量转换为不同数据类型
说明
示例
将 int8 值转换为定点
定义一个标量 8 位整数。
a = int8(5);
创建一个有符号 fi
对象,其字长为 24
,小数长度为 12
。
p = fi([],1,24,12);
将 a
转换为定点,其 numerictype
、复/实性(实数或复数)和 fimath
与指定的 fi
对象 p
相同。
b = cast(a, 'like', p)
b = 5 datatypemode: fixed-point: binary point scaling signedness: signed wordlength: 24 fractionlength: 12
将数组转换为定点
定义一个由 1 组成的 2×3 矩阵。
a = ones(2,3);
创建一个有符号 fi
对象,其字长为 16
,小数长度为 8
。
p = fi([],1,16,8);
将 a
转换为与 p
具有相同的数据类型和复/实性(实数或复数)的数值。
b = cast(a,'like',p)
b = 1 1 1 1 1 1 datatypemode: fixed-point: binary point scaling signedness: signed wordlength: 16 fractionlength: 8
编写独立于 matlab 数据类型的代码
编写一个 matlab® 算法,该算法支持使用不同数据类型运行,而无需更改算法本身。要重用算法,请将数据类型与该算法分开定义。
通过这种方法,您可以在使用浮点数据类型的情况下运行算法来定义基线。然后,您可以在使用不同定点数据类型的情况下测试算法,并将定点行为与基线进行比较,而无需对原始 matlab 代码进行任何修改。
编写一个 matlab 函数 my_filter
,其输入参数为 t
,它是用来定义系数和输入和输出数据的数据类型的一个结构体。
function [y,z] = my_filter(b,a,x,z,t) % cast the coefficients to the coefficient type b = cast(b,'like',t.coeffs); a = cast(a,'like',t.coeffs); % create the output using zeros with the data type y = zeros(size(x),'like',t.data); for i = 1:length(x) y(i) = b(1)*x(i) z(1); z(1) = b(2)*x(i) z(2) - a(2) * y(i); z(2) = b(3)*x(i) - a(3) * y(i); end end
编写一个 matlab 函数 zeros_ones_cast_example
,用浮点步长输入和定点阶跃输入调用 my_filter
,然后比较结果。
function zeros_ones_cast_example % define coefficients for a filter with specification % [b,a] = butter(2,0.25) b = [0.097631072937818 0.195262145875635 0.097631072937818]; a = [1.000000000000000 -0.942809041582063 0.333333333333333]; % define floating-point types t_float.coeffs = double([]); t_float.data = double([]); % create a step input using ones with the % floating-point data type t = 0:20; x_float = ones(size(t),'like',t_float.data); % initialize the states using zeros with the % floating-point data type z_float = zeros(1,2,'like',t_float.data); % run the floating-point algorithm y_float = my_filter(b,a,x_float,z_float,t_float); % define fixed-point types t_fixed.coeffs = fi([],true,8,6); t_fixed.data = fi([],true,8,6); % create a step input using ones with the % fixed-point data type x_fixed = ones(size(t),'like',t_fixed.data); % initialize the states using zeros with the % fixed-point data type z_fixed = zeros(1,2,'like',t_fixed.data); % run the fixed-point algorithm y_fixed = my_filter(b,a,x_fixed,z_fixed,t_fixed); % compare the results coder.extrinsic('clf','subplot','plot','legend') clf subplot(211) plot(t,y_float,'co-',t,y_fixed,'kx-') legend('floating-point output','fixed-point output') title('step response') subplot(212) plot(t,y_float - double(y_fixed),'rs-') legend('error') figure(gcf) end
输入参数
a
— 要转换为不同数据类型的变量
fi
对象 | 数值变量
变量,指定为 fi
对象或数值变量。
复数支持:是
p
— 原型
fi
对象 | 数值变量
原型,指定为 fi
对象或数值变量。要使用原型指定复数对象,必须为原型指定值。否则,您不需要指定值。
复数支持:是
提示
通过使用 b = cast(a,'like',p)
语法指定独立于算法代码的数据类型,您可以:
重用具有不同数据类型的算法代码。
使用数据类型设定和针对不同数据类型的 switch 语句来保持算法的简洁性。
提高算法代码的可读性。
在定点和浮点数据类型之间切换以比较基线。
在不更改算法代码的情况下,在不同定点设置之间切换。
版本历史记录
在 r2013a 中推出
打开示例
您曾对此示例进行过修改。是否要打开带有您的编辑的示例?
matlab 命令
您点击的链接对应于以下 matlab 命令:
请在 matlab 命令行窗口中直接输入以执行命令。web 浏览器不支持 matlab 命令。
you can also select a web site from the following list:
how to get best site performance
select the china site (in chinese or english) for best site performance. other mathworks country sites are not optimized for visits from your location.