Integers can only be raised to positive integral powers in db2mag computation
10 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Life is Wonderful
am 8 Jan. 2021
Kommentiert: Life is Wonderful
am 29 Sep. 2021
I am converting matlab code to c using fixed point coder app
I am getting the below error
Input to function db2mag_conv
decibelTomag.u1 = 10
decibelTomag.u2 = [-90:0.1:90]
function [y] = db2mag_conv(decibelTomag)
y = power(decibelTomag.u1,double(decibelTomag.u2/20))
end
Error signature
### Begin Fixed Point Simulation using Scaled Doubles : db2mag_conv
Error using >intpower', 'C:/Program Files/MATLAB/R2020b/toolbox/eml/lib/matlab/ops/power.m', 187)">power>>intpower (line 187)
Integers can only be raised to positive integral powers
I need help in resolving the error
0 Kommentare
Akzeptierte Antwort
Chidvi Modala
am 21 Sep. 2021
The reason for the error is using negative powers as input to test the generated code. In general, power function can be used in MATLAB for negative powers by typecasting the input to "double". But power function for fixed point data ‘fi’ type has the following restriction.
Exponent must be a non-negative, real, and integer-valued scalar.
power(fi(1.5),fi(2.5))
throws error in MATLAB. Because of this restriction, if you generate code for
function [C] = power_fxpt(A,B)
C = power(double(A),double(B));
the arguments are always converted to integers in the generated code as shown below.
C = fi(power(fi_toint(double(A)),fi_toint(double(B))), 0, 16, 5, fm);
I have brought this issue to the notice of the concerned people, and it might be considered in the future releases.
As a workaround, you can keep the original types as double or single and you can use function replacement to achieve this.
Example: Write original code in power_fxpt.m as
function [C] = power_fxpt(A,B)
C = localpower(double(A),double(B));
function C = localpower(A, B)
C = power(A, B);
Write a replacement function mypower.m as
function C=mypower(A,B)
C = power(double(A),double(B));
Convert to fixed-point using:
cfg = coder.config('fixpt');
cfg.TestBenchName = 'mp_tb';
cfg.addFunctionReplacement('localpower','mypower');
codegen -config cfg power_fxpt
Another option, if the input ranges are small is to create lookup table manually for the limited set of input values.
Weitere Antworten (1)
Nitin Kapgate
am 13 Jan. 2021
You will need to cast both "decibelTomag.u1" and "decibelTomag.u2" as single type (Fixed Point Coder App doesn't support doubles) as follows:
y = power(single(decibelTomag.u1),single(decibelTomag.u2/20))
This should help in getting your problem resolved.
4 Kommentare
Steven Lord
am 15 Jan. 2021
Raising an integer value to a non-integer power does not necessarily result in an integer value.
y = 2^(1/2)
Nor does raising that integer to a non-positive integer power always result in an integer value.
w = 2^(-1)
Here's what I receive when I replace the double precision integer value 2 with a 2 stored in an integer type.
try, x = uint32(2)^(1/2), catch ME, disp("Error was:" + newline + ME.message), end
try, z = uint32(2)^(-1), catch ME, disp("Error was:" + newline + ME.message), end
You could try:
%{
function [y] = db2mag_conv(decibelTomag)
y = power(double(decibelTomag.u1),double(decibelTomag.u2/20))
end
%}
but I'm not sure if the two casts to double will cause problems in code generation.
Siehe auch
Kategorien
Mehr zu Manual Fixed-Point Conversion in MATLAB finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!