How to convert real to signed. The range of real will be from -1 to 1, -5 to 5, -10 to 10 and so on. I would like to convert this range to a signed vector of bit width bw(generic). The data has to be scaled but I have no idea on how to do it. I have searched on the internet and did not find any valuable information.
real to signed
Started by ●February 29, 2008
Reply by ●February 29, 20082008-02-29
FPGA wrote:> How to convert real to signed. The range of real will be from -1 to 1, > -5 to 5, -10 to 10 and so on. I would like to convert this range to a > signed vector of bit width bw(generic). The data has to be scaled but > I have no idea on how to do it. I have searched on the internet and > did not find any valuable information. >Start with modulo arithmetic. http://deadsmall.com/3AE
Reply by ●February 29, 20082008-02-29
On Feb 29, 2:46 pm, FPGA <FPGA.unkn...@gmail.com> wrote:> How to convert real to signed. The range of real will be from -1 to 1, > -5 to 5, -10 to 10 and so on. I would like to convert this range to a > signed vector of bit width bw(generic). The data has to be scaled but > I have no idea on how to do it. I have searched on the internet and > did not find any valuable information.You will need to know magnitude width and fraction width as you will be generating a fixed point decimal. Magnitude width (MW) can be done by taking log2(limit) and adding 1 (to account for the sign bit). Fraction width (FW) is then bw-MW. Then you scale the result by 2**FW and convert it to an integer (which then gives you your signed number). Remember Integer(my_real) always rounds to nearest. If you dont want to round to nearest, you have to write a function that rounds to zero, otherwise removing the LSBs will always round down. (towards 0 for +ve, away from 0 for -ve).
Reply by ●February 29, 20082008-02-29
FPGA wrote:> How to convert real to signed. The range of real will be from -1 to 1, > -5 to 5, -10 to 10 and so on.I don't understand this.> I would like to convert this range to a > signed vector of bit width bw(generic). The data has to be scaled but > I have no idea on how to do it.What do you mean by scaling?> I have searched on the internet and did not find any valuable information.If it is only about converting from real to signed, then first convert the real to integer, and then to signed with the to_signed function from ieee.numeric_std. Something like this: LIBRARY ieee; USE ieee.numeric_std.ALL; ... FUNCTION real2signed ( r: real; return_width: positive ) RETURN signed IS BEGIN RETURN to_signed(integer(r), return_width); END FUNCTION real2signed; -- Paul Uiterlinden www.aimvalley.nl e-mail addres: remove the not.
Reply by ●February 29, 20082008-02-29
On Feb 29, 4:29 pm, Tricky <Trickyh...@gmail.com> wrote:> On Feb 29, 2:46 pm, FPGA <FPGA.unkn...@gmail.com> wrote: > > > How to convert real to signed. The range of real will be from -1 to 1, > > -5 to 5, -10 to 10 and so on. I would like to convert this range to a > > signed vector of bit width bw(generic). The data has to be scaled but > > I have no idea on how to do it. I have searched on the internet and > > did not find any valuable information. > > You will need to know magnitude width and fraction width as you will > be generating a fixed point decimal. > Magnitude width (MW) can be done by taking log2(limit) and adding 1 > (to account for the sign bit). > Fraction width (FW) is then bw-MW. > > Then you scale the result by 2**FW and convert it to an integer (which > then gives you your signed number). > Remember Integer(my_real) always rounds to nearest. If you dont want > to round to nearest, you have to write a function that rounds to zero, > otherwise removing the LSBs will always round down. (towards 0 for > +ve, away from 0 for -ve).PS. None of this is synthesisable, as it bases all working on reals, which you cannot synthesise in any way. Reals are only allowed to create constants (which then have to be of a synthesizable type). If you are trying now to synthesize your sine wave generator, you are going about it the wrong way.
Reply by ●February 29, 20082008-02-29
On Feb 29, 11:50=A0am, Tricky <Trickyh...@gmail.com> wrote:> On Feb 29, 4:29 pm, Tricky <Trickyh...@gmail.com> wrote: > > > > > > > On Feb 29, 2:46 pm, FPGA <FPGA.unkn...@gmail.com> wrote: > > > > How to convert real to signed. The range of real will be from -1 to 1,=> > > -5 to 5, -10 to 10 and so on. I would like to convert this range to a > > > signed vector of bit width bw(generic). The data has to be scaled but > > > I have no idea on how to do it. I have searched on the internet and > > > did not find any valuable information. > > > You will need to know magnitude width and fraction width as you will > > be generating a fixed point decimal. > > Magnitude width (MW) can be done by taking log2(limit) and adding 1 > > (to account for the sign bit). > > Fraction width (FW) is then bw-MW. > > > Then you scale the result by 2**FW and convert it to an integer (which > > then gives you your signed number). > > Remember Integer(my_real) always rounds to nearest. If you dont want > > to round to nearest, you have to write a function that rounds to zero, > > otherwise removing the LSBs will always round down. (towards 0 for > > +ve, away from 0 for -ve). > > PS. None of this is synthesisable, as it bases all working on reals, > which you cannot synthesise in any way. Reals are only allowed to > create constants (which then have to be of a synthesizable type). > > If you are trying now to synthesize your sine wave generator, you are > going about it the wrong way.- Hide quoted text - > > - Show quoted text -I dont want to synthesize this.
Reply by ●February 29, 20082008-02-29
On Feb 29, 11:29=A0am, Tricky <Trickyh...@gmail.com> wrote:> On Feb 29, 2:46 pm, FPGA <FPGA.unkn...@gmail.com> wrote: > > > How to convert real to signed. The range of real will be from -1 to 1, > > -5 to 5, -10 to 10 and so on. I would like to convert this range to a > > signed vector of bit width bw(generic). The data has to be scaled but > > I have no idea on how to do it. I have searched on the internet and > > did not find any valuable information. > > You will need to know magnitude width and fraction width as you will > be generating a fixed point decimal. > Magnitude width (MW) can be done by taking log2(limit) and adding 1 > (to account for the sign bit).MW and FW of output real changes with change in amplitude. What is 'limit'?> Fraction width (FW) is then bw-MW. > > Then you scale the result by 2**FW and convert it to an integer (which > then gives you your signed number). > Remember Integer(my_real) always rounds to nearest. If you dont want > to round to nearest, you have to write a function that rounds to zero, > otherwise removing the LSBs will always round down. (towards 0 for > +ve, away from 0 for -ve).
Reply by ●March 3, 20082008-03-03
On 29 Feb., 15:46, FPGA <FPGA.unkn...@gmail.com> wrote:> How to convert real to signed. The range of real will be from -1 to 1, > -5 to 5, -10 to 10 and so on. I would like to convert this range to a > signed vector of bit width bw(generic). The data has to be scaled but > I have no idea on how to do it. I have searched on the internet and > did not find any valuable information.Start with understanding what a real is: http://en.wikipedia.org/wiki/IEEE_754-1985 Than it is easy to write your conversion function. bye Thomas
Reply by ●March 3, 20082008-03-03
On Feb 29, 6:33 pm, FPGA <FPGA.unkn...@gmail.com> wrote:> On Feb 29, 11:29 am, Tricky <Trickyh...@gmail.com> wrote: > > > On Feb 29, 2:46 pm, FPGA <FPGA.unkn...@gmail.com> wrote: > > > > How to convert real to signed. The range of real will be from -1 to 1, > > > -5 to 5, -10 to 10 and so on. I would like to convert this range to a > > > signed vector of bit width bw(generic). The data has to be scaled but > > > I have no idea on how to do it. I have searched on the internet and > > > did not find any valuable information. > > > You will need to know magnitude width and fraction width as you will > > be generating a fixed point decimal. > > Magnitude width (MW) can be done by taking log2(limit) and adding 1 > > (to account for the sign bit). > > MW and FW of output real changes with change in amplitude. What is > 'limit'? > > > Fraction width (FW) is then bw-MW. > > > Then you scale the result by 2**FW and convert it to an integer (which > > then gives you your signed number). > > Remember Integer(my_real) always rounds to nearest. If you dont want > > to round to nearest, you have to write a function that rounds to zero, > > otherwise removing the LSBs will always round down. (towards 0 for > > +ve, away from 0 for -ve).You are ignoring what the MW and FW lengths of the real are, because it uses neither. For a real, which is floating point, its not magnitude and fraction widths, its mantissa and exponent. You are specified what YOU want the real to fit in to. You are making a FIXED POINT decimal value, so MW and FW never change. for example: from 3 to -3 you need MW = 3 (1 sign bit an 1 other bit) FW = how ever many you want. each bit represets 2^-n (with n=0 to the left of the imaginary point) so 0.75 is represended by: 000.1100000 = 2^-1 (0.5) + 2^-2 (0.25) 1.75 = 001.11000000 = 2^0 (1) + 2^-1 (0.5) + 2^-2 (0.25) -1.75 = 110.01111111 (invert all bits and add one to number above) etc etc All values are 2s compliment, and can then be used in any standard adder, multiplier etc on firmware. Just make sure you use the correct bits of the result: a 2.6 number x 6.2 number = 8.8 result a a . a a a a a a b b b b b b . b b = r r r r r r r r . r r r r r r r r
Reply by ●March 4, 20082008-03-04
On Mar 3, 4:08=A0am, Tricky <Trickyh...@gmail.com> wrote:> On Feb 29, 6:33 pm, FPGA <FPGA.unkn...@gmail.com> wrote: > > > > > > > On Feb 29, 11:29 am, Tricky <Trickyh...@gmail.com> wrote: > > > > On Feb 29, 2:46 pm, FPGA <FPGA.unkn...@gmail.com> wrote: > > > > > How to convert real to signed. The range of real will be from -1 to =1,> > > > -5 to 5, -10 to 10 and so on. I would like to convert this range to =a> > > > signed vector of bit width bw(generic). The data has to be scaled bu=t> > > > I have no idea on how to do it. I have searched on the internet and > > > > did not find any valuable information. > > > > You will need to know magnitude width and fraction width as you will > > > be generating a fixed point decimal. > > > Magnitude width (MW) can be done by taking log2(limit) and adding 1 > > > (to account for the sign bit). > > > MW and FW of =A0output real changes with change in amplitude. What is > > 'limit'? > > > > Fraction width (FW) is then bw-MW. > > > > Then you scale the result by 2**FW and convert it to an integer (which=> > > then gives you your signed number). > > > Remember Integer(my_real) always rounds to nearest. If you dont want > > > to round to nearest, you have to write a function that rounds to zero,=> > > otherwise removing the LSBs will always round down. (towards 0 for > > > +ve, away from 0 for -ve). > > You are ignoring what the MW and FW lengths of the real are, because > it uses neither. For a real, which is floating point, its not > magnitude and fraction widths, its mantissa and exponent. You are > specified what YOU want the real to fit in to. You are making a FIXED > POINT decimal value, so MW and FW never change. for example: > > from 3 to -3 > > you need MW =3D 3 =A0(1 sign bit an 1 other bit) > FW =3D how ever many you want. each bit represets 2^-n (with n=3D0 to the > left of the imaginary point) > > so 0.75 is represended by: =A0000.1100000 =3D 2^-1 (0.5) + 2^-2 (0.25) > 1.75 =3D 001.11000000 =A0=3D 2^0 (1) + 2^-1 (0.5) + 2^-2 (0.25) > -1.75 =3D 110.01111111 (invert all bits and add one to number above) > etc > etc > All values are 2s compliment, and can then be used in any standard > adder, multiplier etc on firmware. Just make sure you use the correct > bits of the result: > > a 2.6 number x 6.2 number =3D 8.8 result > > =A0 =A0 =A0 =A0 =A0 =A0 =A0 a a . a a a a a a > =A0 =A0 =A0 b b b b b b . b b > =3D r r r r r r r r . r r r r r r r r- Hide quoted text - > > - Show quoted text -Thanks a bunch. Your explaination really helped.





