MATLAB Answers

Extracting a subset of a binary number to form a new binary number

3 views (last 30 days)
scc28x
scc28x on 30 Mar 2021
Commented: Walter Roberson on 5 Apr 2021
Hi, I am new using fixed point designer. I'm trying to pick out a subset of a fixed point number
This is what I need to do verbally:
"a" is '010' and I would like to form a new fixed number, "b", by taking the upper two bits of "a", ie. '01',
a=fi(2,1,3,0); b=fi(0,1,2,0);
b= uppder two bits of a
Just wondering what the best way is to do this. Do I use the quantize command, or do I need to do bitshift? Is there a quick way to pick out the first two bits of "a"?
Thank you very much.

Accepted Answer

Andy Bartlett
Andy Bartlett on 5 Apr 2021
Edited: Andy Bartlett on 5 Apr 2021
Bin fine for interpretted mode, but not code gen or Simulink
If you only need to perform the operation in MATLAB interpretted mode, then using bin as Walter showed is fine.
If you need to have support for code generation, or to use this in a Simulink context such as with MATLAB Function block or MATLAB System block, then bin will not work. You'd see an error like this.
??? Property 'bin' is not supported in code generation.
Cast via fi supports code generation
If you wish to extract bits in code generation or in a Simulink context, then casting via fi is a good approach. A discussion of this approach can be found here.
If you are operating directly on a Simulink signal, then the Extract Bits block is available.
Example function extractMSBits
I have attached an example function, extractMSBits, for extracting the most significant n bits from an input using fi casting.
function y = extractMSBits(u,nBitsKeep) %#codegen
%extractMSBits extract n most significant bits of the input
%
%Usage
% y = extractMSBits(u,nBitsKeep)
%
% u is a fixed-point or integer variable
% nBitsKeep is the number of MSBits to return
%
% The least significant bits are simply discarded. Discarding
% is mathematically equivalent to rounding toward floor.
% Copyright 1994-2020 The MathWorks, Inc.
% Change built-in integer to fi equivalent
%
u2 = castIntToFi(u);
assert(isfi(u2))
assert(isfixed(u2))
ntu = numerictype(u2);
isSigned = ntu.SignednessBool;
wordLength = ntu.WordLength;
assert( nBitsKeep <= wordLength );
assert( isSigned < nBitsKeep );
assert( nBitsKeep == floor(nBitsKeep) );
nBitsDrop = wordLength - nBitsKeep;
fixedExponent = ntu.FixedExponent + nBitsDrop;
if ntu.SlopeAdjustmentFactor ~= 1 || ntu.Bias ~= 0
nty = numerictype( ...
isSigned, ...
nBitsKeep, ...
ntu.SlopeAdjustmentFactor, ...
fixedExponent, ...
ntu.Bias);
else
nty = numerictype( ...
ntu.SignednessBool, ...
nBitsKeep, ...
-fixedExponent);
end
fmWrapFloor = fimath('RoundingMethod', 'Floor', ...
'OverflowAction', 'Wrap');
y2 = fi( u2, nty, fmWrapFloor );
y = removefimath( y2 );
end
The attached script, example_extractMSBits, will exercise the function extractMSBits using random choices of input data type and input value. The following is the random output for one call.
>> example_extractMSBits
Term bin_SI type RealWorldValue
a 1111100100000 numerictype(0,13,11) 3.890625
extractMSBits( a, 13) 1111100100000 numerictype(0,13,11) 3.890625
extractMSBits( a, 12) 111110010000 numerictype(0,12,10) 3.890625
extractMSBits( a, 11) 11111001000 numerictype(0,11,9) 3.890625
extractMSBits( a, 10) 1111100100 numerictype(0,10,8) 3.890625
extractMSBits( a, 9) 111110010 numerictype(0,9,7) 3.890625
extractMSBits( a, 8) 11111001 numerictype(0,8,6) 3.890625
extractMSBits( a, 7) 1111100 numerictype(0,7,5) 3.875
extractMSBits( a, 6) 111110 numerictype(0,6,4) 3.875
extractMSBits( a, 5) 11111 numerictype(0,5,3) 3.875
extractMSBits( a, 4) 1111 numerictype(0,4,2) 3.75
extractMSBits( a, 3) 111 numerictype(0,3,1) 3.5
extractMSBits( a, 2) 11 numerictype(0,2,0) 3
extractMSBits( a, 1) 1 numerictype(0,1,-1) 2
  1 Comment
Walter Roberson
Walter Roberson on 5 Apr 2021
Good point about code generation -- I did not take that into account at all.

Sign in to comment.

More Answers (1)

Walter Roberson
Walter Roberson on 31 Mar 2021
a=fi(2,1,3,0)
a =
2 DataTypeMode: Fixed-point: binary point scaling Signedness: Signed WordLength: 3 FractionLength: 0
b=fi(0,1,2,0)
b =
0 DataTypeMode: Fixed-point: binary point scaling Signedness: Signed WordLength: 2 FractionLength: 0
b.bin = a.bin(1:2)
b =
1 DataTypeMode: Fixed-point: binary point scaling Signedness: Signed WordLength: 2 FractionLength: 0

Community Treasure Hunt

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

Start Hunting!