Streamlining of zeroing out last 2 bits of fixed 16 bit number?

1 view (last 30 days)
Would setting correctly the parameters of quantizer object be the solution when the goal is to perform bit truncation of fixed 16 bit, to get left with 14 bit of significant bits, and 0s on last two bits?
I don't know the fraction length to be used, but we can suppose that the binary point is on the extreme left, since all numbers are <1. Tried something like this, just to get the same bit representation as with bitshifts:
q = quantizer('fixed','floor',[14 20]);
x_trunc= quantize(q, x)
Streamlining is needed to avoid for loops with eg. double bitshift:
bitshift(bitshift(fi(x, 1, 16), -2), 2))
so any solution is welcomed. Thank you

Accepted Answer

Andy Bartlett
Andy Bartlett on 25 Oct 2023
Edited: Andy Bartlett on 25 Oct 2023
There are 3 ways this can be achieved.
As already discussed, Bitwise AND against a constant mask or shift right then shift left.
A third approach is to cast to a type with fewer fraction bits with round toward floor, then cast back to the original type.
For power of two scaling (aka binary point scaling), the pair of shifts and pair of cast are equivalent and will generate identical C code.
I would not recommend directly using quantizer. Using casts is an equivalent concept and the more natural way to perform the operation for fi objects.
Input
format long
nBitsDrop = 2;
wl = 16; % word length
fl = 16; % fraction length
slope = 2^-fl;
uDbl = slope * ( (2^(wl-2) - 1) - 2.^(3:5) ).';
u = fi(uDbl,0,wl,fl)
u =
0.249862670898438 0.249740600585938 0.249496459960938 DataTypeMode: Fixed-point: binary point scaling Signedness: Unsigned WordLength: 16 FractionLength: 16
u.bin
ans = 3×16 char array
'0011111111110111' '0011111111101111' '0011111111011111'
Bitwise AND with constant mask
maskDbl = (2^wl - (2^nBitsDrop)) * slope;
mask = cast( maskDbl, 'like', u)
mask =
0.999938964843750 DataTypeMode: Fixed-point: binary point scaling Signedness: Unsigned WordLength: 16 FractionLength: 16
mask.bin
ans = '1111111111111100'
y1 = bitand(u,mask)
y1 =
0.249816894531250 0.249694824218750 0.249450683593750 DataTypeMode: Fixed-point: binary point scaling Signedness: Unsigned WordLength: 16 FractionLength: 16
y1.bin
ans = 3×16 char array
'0011111111110100' '0011111111101100' '0011111111011100'
Pair of Shifts
temp2 = bitsra( u, nBitsDrop)
temp2 =
0.062454223632812 0.062423706054688 0.062362670898438 DataTypeMode: Fixed-point: binary point scaling Signedness: Unsigned WordLength: 16 FractionLength: 16
temp2.bin
ans = 3×16 char array
'0000111111111101' '0000111111111011' '0000111111110111'
y2 = bitsll( temp2, nBitsDrop)
y2 =
0.249816894531250 0.249694824218750 0.249450683593750 DataTypeMode: Fixed-point: binary point scaling Signedness: Unsigned WordLength: 16 FractionLength: 16
y2.bin
ans = 3×16 char array
'0011111111110100' '0011111111101100' '0011111111011100'
Pair of Casts
Key is that first cast has a fraction length that is nBitsDrop bits shorter, and the cast rounds toward Floor.
temp3 = fi( u, 0, wl, fl-nBitsDrop, 'RoundingMethod','Floor');
temp3 = removefimath( temp3 )
temp3 =
0.249816894531250 0.249694824218750 0.249450683593750 DataTypeMode: Fixed-point: binary point scaling Signedness: Unsigned WordLength: 16 FractionLength: 14
temp3.bin
ans = 3×16 char array
'0000111111111101' '0000111111111011' '0000111111110111'
y3 = cast( temp3, 'like', u )
y3 =
0.249816894531250 0.249694824218750 0.249450683593750 DataTypeMode: Fixed-point: binary point scaling Signedness: Unsigned WordLength: 16 FractionLength: 16
y3.bin
ans = 3×16 char array
'0011111111110100' '0011111111101100' '0011111111011100'

More Answers (1)

dpb
dpb on 24 Oct 2023
I have no knowledge of how Fixed-Point Designer stores stuff, but can you not do something like
x=bitand(x,0xFFFC)
?
  2 Comments
Imola Fodor
Imola Fodor on 24 Oct 2023
hi, bitand and the double bitshift have similar performance, and actually both can take an array as an input, i have overseen this somehow.. so my question is at this point more related to just understanding how can this be done with the quantizer

Sign in to comment.

Products


Release

R2023a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!