Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Simulink - Using Data Type Conversion block

904 views
Skip to first unread message

Ivan

unread,
Apr 24, 2012, 9:00:07 AM4/24/12
to
Dear all,

I'm trying to use a Data Type Conversion block in this way: at the input I have 4 bytes of "uint8" data, and at the output I need to have a unique value of type "single". In MatLab I do that with the follow line:

OUT = double(typecast([IN(1) IN(2) IN(3) IN(4)], 'single'));

if IN is, for example, "182 174 133 61", OUT should be "0.0653".

but, I don't know how to do it in Simulink. When I configure the Data Type Conversion block, I only change the Output data type to "Single", but it outputs 4 bytes of single data that are equal to the input values, instead a unique value.

I hope you can help me.

BR.

Phil Goddard

unread,
Apr 24, 2012, 11:55:26 PM4/24/12
to
There are two operations here: firstly buffering the 4 uint8 signals; then doing the typecast.

For the first step, if you have the Signal Processing Blockset then use the buffer block from it, otherwise you'll need to write your own.
For the second part use various of the blocks from the Logical and Bit Operations library.
A good way to see how to do this is to look at the examples in http://www.mathworks.com/matlabcentral/fileexchange/5063-bit-concatenation-block-for-simulink
(The author of those examples is one of the primary authors of the fixed-point functionality within Simulink.)

On the other hand, if you don't need to worry about code generation (i.e. you are not using Simulink Coder) then you could write a quite simple m-code S-Function to call the MATLAB typecast function.

Phil

Ivan

unread,
Apr 25, 2012, 5:28:05 AM4/25/12
to
Phil thank you for your reply,

"Phil Goddard" <ph...@goddardconsulting.ca> wrote in message <jn7sje$ke8$1...@newscl01ah.mathworks.com>...
> There are two operations here: firstly buffering the 4 uint8 signals; then doing the typecast.
>
> For the first step, if you have the Signal Processing Blockset then use the buffer block from it, otherwise you'll need to write your own.

I tried to do it but it doesn't work. I don't know why I should use this buffer block. The INput signal is formed by 4 uint8 values that arrive at the same time and are joined in an vector by a mux block, which is then introduced into the data type conversion block.

In the MatLab line to perform what I need to do, these 4 bytes form a vector.

> For the second part use various of the blocks from the Logical and Bit Operations library.
> A good way to see how to do this is to look at the examples in http://www.mathworks.com/matlabcentral/fileexchange/5063-bit-concatenation-block-for-simulink
> (The author of those examples is one of the primary authors of the fixed-point functionality within Simulink.)
>

I tried this too, but It doesn't return the expected values, I think that I don't need to concatenate the values, the typecast function works with a vector, no with a value formed by the union of the 4.

> On the other hand, if you don't need to worry about code generation (i.e. you are not using Simulink Coder) then you could write a quite simple m-code S-Function to call the MATLAB typecast function.
>

As far as I know, S-Functions' performance is slower than Simulink blocks, doesn't it?

> Phil

BR

Phil Goddard

unread,
Apr 25, 2012, 1:54:23 PM4/25/12
to

> I tried to do it but it doesn't work. I don't know why I should use this buffer block. The INput signal is formed by 4 uint8 values that arrive at the same time and are joined in an vector by a mux block, which is then introduced into the data type conversion block.

I had assumed the data was coming in serially.
If it's in parallel then the buffering isn't required, and a simple concatenation/mux will do.

Then just feed the output of the concat/mux into an Embedded MATLAB Function block that contains
%%%%%%%%%%%%cut here%%%%%%%%%%%%%
function y = fcn(u)
eml.extrinsic('typecast');
y = 0;
y = double(typecast(u,'single'));
%%%%%%%%%%%%%cut here%%%%%%%%%%%%%

> As far as I know, S-Functions' performance is slower than Simulink blocks, doesn't it?

yes, but...
If you can do the same thing in either Simulink blocks, an m-code S-Function or a c-mex S-function then the m-code S-function will be the worst performing.
But if you cant' do it using Simulink blocks then you need to use an S-Function.

Without knowing your application, initially I'd suggest just doing the above.
If speed becomes an issue then spent time looking for alternatives.

Phil.

Ivan

unread,
Apr 26, 2012, 7:43:07 AM4/26/12
to
"Phil Goddard" <ph...@goddardconsulting.ca> wrote in message <jn9dof$bom$1...@newscl01ah.mathworks.com>...

>
> I had assumed the data was coming in serially.
> If it's in parallel then the buffering isn't required, and a simple concatenation/mux will do.
>
> Then just feed the output of the concat/mux into an Embedded MATLAB Function block that contains
> %%%%%%%%%%%%cut here%%%%%%%%%%%%%
> function y = fcn(u)
> eml.extrinsic('typecast');
> y = 0;
> y = double(typecast(u,'single'));
> %%%%%%%%%%%%%cut here%%%%%%%%%%%%%
>


Thank you very much Phil, It seems to work fine! with the Embedded Function, and performance is good.


Best regards,


Iván.
0 new messages