matlab subsasgn,Subscripted assignment

This example defines a variable acc to emulate a 40-bit accumulator of a DSP. The products and sums in this example are assigned into the accumulator using the syntax acc(1)=... Assigning values into the accumulator is like storing a value in a register. To begin, turn the logging mode on and define the variables. In this example, n is the number of points in the input data x and output data y, and t represents time. The remaining variables are all defined as fi objects. The input data x is a high-frequency sinusoid added to a low-frequency sinusoid.

fipref('LoggingMode', 'on');

n = 100;

t = (0:n-1)/n;

x = fi(sin(2*pi*t) + 0.2*cos(2*pi*50*t));

b = fi([.5 .5]);

y = fi(zeros(size(x)), numerictype(x));

acc = fi(0.0, true, 40, 30);

The following loop takes a running average of the input x using the coefficients in b . Notice that acc is assigned into acc(1)=... versus using acc=..., which would overwrite and change the data type of acc .

for k = 2:n

acc(1) = b(1)*x(k);

acc(1) = acc + b(2)*x(k-1);

y(k) = acc;

end

By averaging every other sample, the loop shown above passes the low-frequency sinusoid through and attenuates the high-frequency sinusoid.

plot(t,x,'x-',t,y,'o-')

legend('input data x','output data y')

f4a581575f7d67a1b348f55f725d4945.png

The log report shows the minimum and maximum logged values and ranges of the variables used. Because acc is assigned into, rather than overwritten, these logs reflect the accumulated minimum and maximum values.

logreport(x, y, b, acc)

minlog maxlog lowerbound upperbound noverflows nunderflows

x -1.200012 1.197998 -2 1.999939 0 0

y -0.9990234 0.9990234 -2 1.999939 0 0

b 0.5 0.5 -1 0.9999695 0 0

acc -0.9990234 0.9989929 -512 512 0 0

Display acc to verify that its data type did not change.

acc

acc =

-0.0941

DataTypeMode: Fixed-point: binary point scaling

Signedness: Signed

WordLength: 40

FractionLength: 30

Reset the fipref object to restore its default values.

reset(fipref)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值