Fixed-Point Version of TYPECAST?

9 views (last 30 days)
Kevin
Kevin on 27 Apr 2016
Commented: Walter Roberson on 3 Nov 2018
I really like the MATLAB function typecast. It can easily convert between 32-bit integer and vector of bytes (and vice versa).
>> typecast(uint32(255), 'uint8')
ans =
255 0 0 0
>> typecast(uint8([255 0 0 0]), 'uint32')
ans =
255
However, typecast can only handle standard data types (i.e. uint8, int8, uint16, int16, uint32, int32, uint64, int64, single or double).
But in audio signal processing, audio samples are usually stored in 24 bits and I cannot use typecast anymore.
I have Fixed-Point Designer installed on my PC but I cannot find an equivalent fixed-point function that works like typecast. I have already looked into bitconcat and bitsliceget. But most likely I need to use a for-loop calling this functions multiple times to do the job.
Any suggestion? Or I am pretty much stuck ....
  4 Comments
Kevin
Kevin on 2 Nov 2018
Wow, I posted this almost 3 years ago and suddenly I got 3 answers. Something happened?
Let me try to answer to James and Bruno.
Imagine I (or someone) have done a memory dump from a processor memory into a text file. Imagine that each line in the text file contains one uint8 and so 3 lines would make up one 24-bit integer.
Or sometimes I need to deal with FPGA. It is even worst. In digital hardware, we can do literally arbitrary bit length (i.e. 19-bit, 25-bit integers. really odd). But that is life, something I need to deal with.
James Tursa
James Tursa on 2 Nov 2018
"... I posted this almost 3 years ago and suddenly I got 3 answers ..."
Someone picks up on an old thread and posts an answer or comment. Then the rest of us see it in the recent list and start replying as well ... often without even looking at the date of the original post.

Sign in to comment.

Answers (2)

Hartmut Schorrig
Hartmut Schorrig on 2 Nov 2018
I think you should think about usage of Simulink S-functions and C-Programming for your interface to Matlab/Simulink. If you have 24 bit data they should be existing in a hardware layer or via hardware-oriented driver. You can input the data and convert it to 32 bit with ordinarry C-routines, it is simple. For all more sophisticated algorithm you should use Matlab and Simulink as well you think too. If you get input data in a file etc. to evaluate with Matlab, it may be numbers as text. Excel can be inputted by Matlab, it should not be a problem.

Walter Roberson
Walter Roberson on 2 Nov 2018
If you were to be using fixed-point objects with 24 bits, then you could directly assign into the bits property of the object.
  6 Comments
Kevin
Kevin on 3 Nov 2018
Hi Walter,
Thanks again. Really appreciate your efforts.
But I am very picky. I also care about speed and memory usage.
Also we can do all these codings that you are doing now. Then why do I want to buy Fixed Point Toolbox?
Kevin
Walter Roberson
Walter Roberson on 3 Nov 2018
You can speed up the operative line by assigning the results of the x.bin to a temporary variable t and using
t(:, all(t==' ')) = '' ;
y.bin = reshape(t.',1,[]);
Which can be simplified if x is know to have only one row.
The premise of the original question was that Fixed Point Designer was being used, and that has fixed point toolbox as a prerequisite. The entire question was about how do these operations in fixed point.
If HDL is being generated but fixed point toolbox is not being used, then you would be doing packing into basic integer data types and avoiding using any floating point. If the question has mutated into how to do efficient bit-level indexing the question has potentially important differences if the largest data type exceeds 64 bits, and even with 33 to 64 bits there might be a preference to avoid more than 32 bits at a time in order to reduce the effective ALU size.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!