Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion Groups
Mathematics
General TopicsResearchOperations ResearchStatisticsMathematical LogicNumerical AnalysisUndergraduate MathAlgebra HelpRecreational Math
Math Software
MapleMathematicaMATLABScilabSASSPSS

Math Forum / Math Software / MATLAB / July 2008



Tip: Looking for answers? Try searching our database.

Fixed-Point Toolbox : ProductWordLength

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Michael Hui - 18 Jul 2008 00:36 GMT
I am starting to use Fixed-Point Toolbox, within an Embedded
MATLAB Function. I noticed that, since M code does not
support type definitions for variables, the
ProductWordLength attribute in the terms of an equation (its
RHS) determine the ProductWordLength of the product (its LHS).

This is quite a departure from conventional programming
language where you can separately specify the
ProductWordLength of each LHS of an equation.

So how do I model the case where a variable is stored in one
format only, but is used in multiple equations, each needing
a different ProductWordLength in its LHS?
Tom Bryan - 18 Jul 2008 12:37 GMT
> 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';
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2010 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.