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')
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)