EDA365欢迎您登录!
您需要 登录 才可以下载或查看,没有帐号?注册
x
一、模型的设计原理 SysGen 通过使用MCode模块提供了对Matlab的直接支持。MCode模块支持将输入值应用到M函数,用于对使用Xilinx定点数据类型的评估,并且在每个采样周期进行评估。模块通过使用永久的状态变量来保持内部状态。模块的输入端口是由M函数指定的输入变量所决定的,输出端口由M函数的输出变量决定。这个模块为构建有限状态机、控制逻辑等,提供一个便捷的方法。 下面通过MCode构建两个滤波器,并将两个模块的计算结果进行比较。 二、 simple_fir.m 文件 function y = simple_fir(x,lat,coefs,len,c_nbits,c_binpt,o_nbits,o_binpt)
+ p/ k9 |% Q7 p! W: n+ u" k' z; q. ] coef_prec = {xlSigned,c_nbits,c_binpt,xlRound,xlWrap};" v" V% c, B: `" D, @ j1 A
out_prec = {xlSigned,o_nbits,o_binpt};/ i! Y# A& _9 a% Z8 j
coefs_xfix = xfix(coef_prec,coefs);
3 p. g; r0 a* E) x+ ] persistent coef_vec,coef_vec = xl_state(coefs_xfix,coef_prec);( D0 H+ t' M. F3 B" s
persistent x_line,x_line = xl_state(zeros(1,len-1),x);
) W. q& G6 f( ^7 ~5 v$ u persistent p,p = xl_state(zeros(1,lat),out_prec,lat);- p, s+ \- w' f& h) H; Z" M- `/ K
sum = x * coef_vec(0);2 ^/ T+ S; z3 w0 L6 m" |$ i8 h
for idx = 1:len-1$ Y* t2 T$ T3 n, \- R$ F
sum = sum + x_line(idx-1) * coef_vec(idx);
! u+ R' |3 M+ p, ]- j9 x sum = xfix(out_prec,sum);3 o( H; E; ^7 M
end
5 `8 y8 g1 k: A) G y = p.back;
& P; {" F' I, O; q& Z' U p.push_front_pop_back(sum);
5 m( |( t; U7 ^% L- s! t* S) s x_line.push_front_pop_back(x);
. X# O9 c) q3 j% N% k3 f( A6 w
4 G6 v8 e' ?( i$ u3 \- p: gfir_tranpose.m文件 function y = fir_transpose(x,lat,coefs,len,c_nbits,c_binpt,o_nbits,o_binpt)
: R- O; k8 H, }8 [+ }. w2 D7 j coef_prec = {xlSigned,c_nbits,c_binpt,xlRound,xlWrap};
: p; A. E; y4 N; { {) X: D out_prec = {xlSigned,o_nbits,o_binpt};
' ?4 W. @" o, C; d, u coefs_xfix = xfix(coef_prec,coefs);
7 ?6 v# u( w& U+ g0 W' X& H persistent coef_vec,coef_vec = xl_state(coefs_xfix,coef_prec);
4 z5 P: \! V! |$ R0 u persistent reg_line, reg_line = xl_state(zeros(1,len),out_prec);: h( h( y/ j+ z, m+ R _ |
if lat <= 0
/ A# N/ h, r* d% c4 [! g- j$ h7 Y error('latency must be at least 1');
# I; N1 F" n7 l end* Q; s! ]5 p% |
lat = lat -1;( ?4 N" D4 @1 o8 R- O) L) L
persistent dly,
% K+ u2 ]# w, m: H if lat <= 0
. _8 Y M( N* q# f! u y = reg_line.back;1 e5 ^% K( V: a- m, D5 `, K" h" K
else
( z" J& w- q+ f' x2 U, r- T% H dly = xl_state(zeros(1,lat),out_prec,lat);
$ e& M% {! S% ^ y = dly.back;
3 N9 [& [7 U! v P, f0 H k dly.push_front_pop_back(reg_line.back);
" C/ z( x' [+ V end
" c- \: {: a0 h6 e9 Z9 e
0 X" e! W, B, u1 }0 t: ^ for idx = len-1:-1:1. R) U6 D& P+ [" u6 Z' ]
reg_line(idx) = reg_line(idx-1) + coef_vec(len-idx-1)*x;
$ ^5 x, I7 s7 @ c; u; M9 ~+ c end; c: V( N# u: b( ]0 p( t
: y/ [/ N& `, G1 }5 l; d
reg_line(0) = coef_vec(len-1) * x;
: `2 N \4 B- V0 p- m三、系统模型的建立 打开System Generator,然后打开Simulink,新建一个设计。 在Libraries找到Xilinx Blockset ,展开Math,将2个MCode元件加入到设计中。 在Xinlinx Blockset,展开Basic Elements,将Gateway In 和Gateway Out 元件加入到设计。 展开Simulink,找到Sources,将Band-Limited White Noise 元件加入到设计; 在Simulink 的 Sinks 中找到Scope,加入到设计。 在Xilinx Blockset 的Basic Elements 中将System Generator 符号加入到设计。 ; a2 U+ u# }+ P; j9 x/ T5 j f, q) s' F
5 _( t1 x; u' c) q6 T$ z' H' l) g1 w
双击上面的MCode元件符号,打开参数配置对话框,在Basic标签页,点击Browse,定位到simple_fir.m的路径。单击inte RFace,安下图所示配置。在Advanced标签页,勾选Enable printing with disp。 双击下面的MCode元件,在Basic标签页加载fir_transpose.m文件。在Interface标签下,参数如下配置。在Advanced标签页,勾选Enable printing with disp
最后按下图所示连接 b/ D$ y9 E% z* h2 s
& G8 t* R2 ^. i' [四、仿真 在Simulink的工具栏文本框中输入100,然后点击仿真按钮,最后双击Scope符号,查看仿真图像
0 @4 h0 X8 Y$ e, b+ {2 t0 O, |* u8 M4 J- ]) l1 t+ ?
1 y. g+ ]% |3 u0 s+ H
7 m5 J6 E& T" N( a8 G: @ |