2
votes

I am trying to convert a number from one fixed point representation to another. I am using the 2's complement representation (Qmf representation) This is to verify the hardware implementation. The hardware implementation works as follows:

The input data is in Qmf format. The input data is 8bits wide and has a fractional length of 6 bits. I want to convert it to 4bit number with 4 fractional bits (No integer bits) and I have a few doubts regarding the same.

I noticed that when I am trying to convert numbers like 1.984375 from Q26 {range [-2,1.984375] } format to Q04 {range [-0.5,0.4375]} format, the hardware implementation runs as follows: I read bits 5-2 since bits 6&7 are used for the integer part and store it in a 4-bit register. So this value gets interpreted as -0.5 when converted to Q04 format. Is -0.5 correct? The amount of error incurred seems to be huge. Am I doing something wrong ? If the above mentioned approach is correct. Is there any standard procedure to convert from one fixed-point representation to another in software that mimics the hardware approach that I described above. I am thinking of converting the number from Q26 representation to floating point and the converting it back to fixed point. Is there a cleaner way of doing this in software?

1
Since the Q26 number is larger than the maximum you can store in the Q04 format, the conversion should either report an error (overflow) or return the maximum value possible.1201ProgramAlarm
Is your Q0.4 format intended to be signed [range -0.5..+0.4375] or unsigned [0..0.9375]?NickJH
I am using the signed representation.a220599
@1201ProgramAlarm Yes it incurs errors, but I want to know if there are any algorithms that let me convert from one fixed point representation to another.a220599

1 Answers

0
votes

I think you can do the conversion by simple bit-masking operations, and a little bit of logic.

If your Q26 number is positive (i.e. bit-7 is zero), then the corresponding Q04 number will be just bits-2 to bits-5 of your original number, provided bit-6 is zero. This means that your original number must be less than one.

If your Q26 number is negative (i.e. bit-7 is one), then the corresponding Q04 number is again bits-2 to bits-5 or your original number, but this time provided that bit-6 is one and there is at least on non-zero bit in bits 2-5. This means that your original number must be greater than minus one.

In each case you'll get a rounding error from ignoring bits 0 & 1, which you might want to account for by adding or subtracting one to the least significant bit of your Q04 number. That would obviously require an extra arithmetic operation, and risk overflowing the allowed range of your Q04 number.

If you want to experiment with this in Python, you could try a library such as spfpm.