> I am starting to use Fixed-Point Toolbox, within an Embedded
> MATLAB Function. I noticed that, since M code does not
[quoted text clipped - 9 lines]
> format only, but is used in multiple equations, each needing
> a different ProductWordLength in its LHS?
Hi Michael,
One way to do it is to leave the product-mode in full-precision, then
assign into a left-hand-side argument to cast to a specific type.
For example, leave the ProductMode in the FullPrecision default, define
the types of the left-hand-side variables, and assign into them:
A = fi(2, true, 8, 0);
B = fi(3, true, 8, 0);
C = fi(5, true, 16, 0);
D = fi(0, true, 8, 0); % To hold the first product
E = fi(0, true, 16, 0); % To hold the second product
% The full-precision product is s16,0, then assign into s8,0
D(:) = A*B % Assign into D
% D = 6 s8,0
% The full-precision product is s24,0, then assign into s16,0
E(:) = A*C % Assign into E
% E = 10 s16,0
If this doesn't answer the question, I would be happy to follow up with
you if you could send a specific example.
Best wishes,
Tom Bryan
tbryan@mathworks.com
p.s.
Here is a section from the demos, and there is more in the
documentation. From the MATLAB command line, type demos. Then select
Fixed-Point Toolbox, Fixed-Point Basics, and the section
A(:) = B vs. A = B
There is a difference between
A = B
and
A(:) = B
In the first case, A = B replaces A with B, and A assumes B's numeric type.
In the second case, A(:) = B assigns the value of B into A, while
keeping A's numeric type. This is very handy for casting one numeric
type into another.
Michael Hui - 18 Jul 2008 19:14 GMT
OK thanks for the help Tom.
I will standardize on that coding style then, since we
traditionally (and want to continue to) define LHS word
lengths independently from RHS word lengths.
We of course will take advantage of Fixed-Point Toolbox's
ability to warn of overflows, and this coding style does
allow that.
Michael Hui - 18 Jul 2008 20:04 GMT
But wait ...
I just tried your demo, and FPT didn't give any overflow
indication at all. So that's no good.
I suppose another solution is to make local copies of all
variables that will be used in more than one equation's RHS,
and set their ProductWordLength to different values.
Or should I use Fixed-Point Block Sets?
The doc gives a good reason for using Embedded MATLAB
Functions, and that reason is exactly why I am using it:
"This capability is useful for coding algorithms that are
better stated in the textual language of the MATLAB software
than in the graphical language of the Simulink product."
Another reason is that a lot of our algorithms are already
in floating point M code, so we benefit from not having to
rewrite that code extensively to convert them to fixed-point.
Michael Hui - 18 Jul 2008 20:19 GMT
Ooops ... the overflow detection does work.
>> D = fi(0, true, 3, 0);
>> D(:)=A*B
Warning: 1 overflow occurred in the fi assignment operation.
I forgot to set
P.LoggingMode = 'on';