On 4 Dec, 04:14, sanborne <sanbo...@fastmail.fm> wrote:
> On Dec 3, 10:37*pm, David Bishop <dbis...@vhdl.org> wrote:
>
>
>
> > sanborne wrote:
> > > I have another question related to fixed point.
>
> > > I wrote a function in MATLAB that will take an arbitrary yet well
> > > behaved function and generate an if-then-elseif tree that can
> > > approximate that function over a given interval. For example, the
> > > following is the output of my function to approximate the natural
> > > logarithm over an interval of 1e-4 to 1 (I apologize if you are
> > > unfamiliar with MATLAB, as this is a VHDL forum, but the functionality
> > > of this code should be relatively clear):
>
> > Stuff deleted....
>
> > Yipe.
>
> > > I think converting this code to VHDL should be relatively straight
> > > forward, although I do not know very well how to deal with fixed
> > > point. How, for example would I convert the constant coefficients
> > > above into fixed point hardware?
>
> > Yes. *You can compare a fixed point number of any width to a number of
> > any other width. *I would just say:
> > if x < to_sfixed (0.0263665, x'high, x'low);
>
> > Without knowing your "fi" function, I couldn't say more.
>
> > > But my real question is whether this is the right approach. I am
> > > afraid the hardware implementation of the equivalent VHDL would use a
> > > TON of multipliers, and might have other problems too. How would
> > > someone implement a non-linear, yet common function in hardware? Note
> > > that taylor series is definitely not the answer. After a fairly in-
> > > depth study the above solution is the best I could come up with. Are
> > > there any other suggestions? Is there something better than my binary
> > > if-tree approach?
>
> > > Thanks a ton for any responses.
>
> > The wonderful thing about fixed point math is that a Taylor series will
> > give you a good result in a finite number of iterations. *As to
> > multipliers, they work the same fixed point or signed. *Like I said, it
> > depends on what "fi" does.
>
> Sorry, the FI function is confusing. Basically, that is just a
> function in Matlab that allows for fixed point data types. The code
> above sets the fixed point type of all the coefficients of the lookup
> table as 32 bits wide with a fractional length of 22. I did some
> analysis to determine those sizes. If you are interested, there is
> documentation online at:
>
> http://www.mathworks.com/access/help...ixedpoint/inde...
>
> The problem with Taylor series is that even after a large number
> (hundreds) of iterations, I was still not getting the accuracy that I
> was hoping for. At part of the interval I was over accurate and at
> part of the interval I way off.
You have to remember that fixed point is very accurate, but is limited
on range. floating point will give you the range, but with the
sacrifice of accuracy at larger values.
As I just detailed in the other thread, fixed point is no different to
"normal" arithmatic. it is just all done at normal accuracy with an
implied /2^n.
The probelm you havem especially with your Y values, is that you'd
need at least a 10 bit magnitude part and at least 23 bits (as you
said this isnt accurate enough). With x being what it is you probably
also need a large number(say 32 bits), and when you start multipltying
in fixed point, a 32 bit x 32 bit number gives a 64 bit result, before
you start rounding and approximating. Most FPGA multipliers are 18 bit
(giving 36 bit outputs) and so you would have to approximate before
you put it into another multiplier.
The trick is though to identify what kind of system you are working
in. I will give you an example. Radial correction algorithm for video
(ie. warping an image to remove the curvature of a lense) involves
warping u and v co-ordinates. In reality, these U and V co-ordinates
represent a memory lookup, and so being accurate at the sub pixel
level isnt really appropriate (if you want to do bilinear
interpolation, you need accuracy to 1/16th of a pixel, but even this
isnt that accurate compared to the overall algorithm). The function is
a taylor series of even powers. Terms beyond the 4th order have very
little effect on the output at the pixel level, and so are discarded.
You then only have 2 terms to work on, and as you know you're not very
interested in sub pixel accuracy, you just keep it as accurate as
possible as long as you can, before rounding to nearest.
So the best thing you can do is analyse the system you're trying to
apply the algorithm too and see what accuracy really is important.