Matlab creating variables of type fixdt - matlab

With the fixed-point license it is easy to create arbitrary fixed-point data types in Matlab ie a signed 32 bit number with 16 fractional bits:
custom_sfix = fixdt(1,32,16)
custom_sfix =
Simulink.NumericType
DataTypeMode: 'Fixed-point: binary point scaling'
Signedness: 'Signed'
WordLength: 32
FractionLength: 16
IsAlias: false
DataScope: 'Auto'
HeaderFile: ''
Description: ''
How do I create a value of this type in matlab?
For built in types it is just a = int8(5); or a = uint32(45);
I have also tried types cast, but this only seems to accept the built in datatypes.
>> Y = typecast(12.5, custom_sfix )
Error using typecast
The second input argument must be a character array.
>> Y = typecast(12.5, 'fixdt(1,32,16)' )
Error using typecast
Unsupported class.

I believe fixdt is for creating fixed-point data type signals in Simulink, rather than MATLAB. You would then define your signal to be of that data type as shown in http://www.mathworks.co.uk/help/simulink/ug/working-with-data-types.html#f14-90565.
If you want to create fixed-point objects in MATLAB, you probably want to use fi instead. There are casting examples in the doc too.

Related

Getting max possible value of integer data type in Octave

I use GNU Octave, version 6.4.0. Is there any command for getting size of usual data classes, at least for integral data types? Really I want to get max possible value for data type of a matrix which is related to an image. For example for an uint8 image such I it must return 256 for argument class(I). I am looking for a builtin command and do not want to write a switch myself.
The intmax() function does what you want for integer types. Either intmax("uint8") or intmax(a) where a is of type uint8 (or any other integer type).
https://octave.sourceforge.io/octave/function/intmax.html
A similar function realmax() exists for floating point types.

Work with binary numbers as scalars in Matlab

I am working with a MATLAB function that uses numbers in the binary base. To do so it uses the function dec2bin to transform an integer into a char array containing the binary information. The issue is that I plan to use HDL Coder to generate a HDL version of the function. One step of the process is to convert the variables to fixed point. This can be done automatically when the data is a scalar, so is there any way to manage binary numbers without using vectors?
dec2bin is just for display purposes. Numbers are always stored in the computer using binary representation. You can use the functions
bitand,
bitor,
bitxor,
bitcmp,
bitshift,
bitget, and
bitset
to do bit-wise manipulation of integer numbers:
>> a = uint32(7);
>> b = uint32(12);
>> bitand(a, b)
ans =
uint32
4
(Click on the function names above for the documentation. You can also do help bitand in MATLAB to read a shorter version of the documentation or doc bitand to read the full documentation.)

Using a variable-sized argument in Matlab coder

I want to generate a c++ code for DCT function using Matlab coder. I wrote this simple function and tried to convert it to c++.
function output_signal = my_dct(input_signal)
output_signal = dct(input_signal);
end
When I use a fixed size type for the input argument (such as double 1x64), there is no problem; however, a variable-sized type (such as double 1x:64) for the input argument results in these errors:
The preceding error is caused by: Non-constant expression..
The input to coder.const cannot be reduced to a constant.
Can anyone please help me?
Thanks in advance.
The documentation is a bit vague for DCT in Coder, but it implies that the input size must be a constant power of 2 along the dimension of the transform. From DCT help:
C/C++ Code Generation
Generate C and C++ code using MATLAB® Coder™.
Usage notes and limitations:
C and C++ code generation for dct requires DSP System Toolbox™ software.
The length of the transform dimension must be a power of two. If specified, the pad or truncation value must be constant. Expressions or variables are allowed if their values do not change.
It doesn't directly say that the length of the variable (at least along the dimension being transformed) going into the dct function must be a constant, but given how coder works, it really probably has to be. Since that's the error it's returning, it appears that's a limitation.
You could always modify your calling function to zero pad to a known maximum length, thus making the length constant.
For instance, something like this might work:
function output_signal = my_dct(input_signal)
maxlength = 64;
tinput = zeros(1,maxlength);
tinput(1:min(end, numel(input_signal))) = input_signal(1:min(end, maxlength));
output_signal = dct(tinput);
end
That code will cause tinput to always have a size of 1 by 64 elements. Of course, the output will also always be 64 elements long also, which means it'll be scaled and may have a difference frequency scale than you're expecting.

precision matlab vpa

I'm working with an algorithm, which uses hyperbolic functions and in order to get more accurate results from it I need to increase the precision, so I would like to do it by vpa function means, but I'm not quite sure how to implement it. Here some code to clarify the situation further:
x=18; %the hyperbolic relation is valid until x=18
cosh(x)^2-sinh(x)^2
ans = 1
x=19; %the hyperbolic relation is no longer valid
cosh(x)^2-sinh(x)^2
ans = 0
working with the VPA function:
a=vpa('cosh(40)',30); %the hyperbolic relation is valid beyond x=19
b=vpa('sinh(40)',30);
a^2-b^2
ans = 1.00008392333984375
the problem now is that I don't know how to get the value from VPA with a variable of control 'x'
I tried this but it didn't work:
x=40;
a=vpa('cosh(x)',x,30);
b=vpa('sinh(x)',30);
a^2-b^2
When doing symbolic math or variable precision arithmetic one must be careful with with converting between floating-point. In this case, you need to convert your input, x, to variable precision before passing it in to cosh or sinh (otherwise only the output of these will be converted to variable precision). For your example:
x = vpa(40,30);
a = cosh(x);
b = sinh(x);
a^2-b^2
which returns the expected 1.0. I'm not sure where you found the the use of vpa with string inputs, but that form is no longer used (using strings may even result in different results due to different functions being called). Note also that the default setting for digits in current versions of Matlab is 32.

Type casting a vector in Matlab

version: Matlab 2009a
I am generating a vector of size <1x116286> using randsrc() function. Since I am again adding it to the matrix of same size but of uint8 type, I am doing as follows -
l=typecast(randsrc(1,v(2)),'uint8');
Now, Matlab has changed the returned a vector of elements - [240,63,0] instead of [-1,1], with the size of <1x930288 uint8>. This is expected as double and uint8 has different size, but I want a vector of same size and values after type casting.
PS: I want to subtract or add '1' from all trhe values on a matrix of size <1x116286>. Is there any other neat way to do this?
As I understand the problem, there are a couple of issues with the above:
uint8 is an unsigned type so will not support a negative offset;
the "typecast" function is used to reinterpret existing data, not to convert it: here you are interpreting each byte of the floating-point output of randsrc(...) as an integer.
Unfortunately I don't have Matlab handy to test, but the following should provide something closer to what you are after:
l = int8( randsrc(1,v(2)) );
well instead of forming a vector (-1,1...) and adding it to some vector 'z' , I did something like this.
l =randsrc(1,v(2));
z(l==-1)=z(l==-1)-1;
z(l==1)=z(l==1)+1;
So, I now, don't need to change types.