Integers can only be raised to positive integral powers in db2mag computation

조회 수: 15 (최근 30일)
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

채택된 답변

Chidvi Modala
Chidvi Modala 2021년 9월 21일
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.

추가 답변 (1개)

Nitin Kapgate
Nitin Kapgate 2021년 1월 13일
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
Steven Lord
Steven Lord 2021년 1월 15일
Raising an integer value to a non-integer power does not necessarily result in an integer value.
y = 2^(1/2)
y = 1.4142
Nor does raising that integer to a non-positive integer power always result in an integer value.
w = 2^(-1)
w = 0.5000
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
Error was: Integers can only be raised to positive integral powers.
try, z = uint32(2)^(-1), catch ME, disp("Error was:" + newline + ME.message), end
Error was: Integers can only be raised to positive integral powers.
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.
Life is Wonderful
Life is Wonderful 2021년 1월 18일
Thanks steven, But stil the issue did not go away, can you please suggest another approach ?

댓글을 달려면 로그인하십시오.

태그

Community Treasure Hunt

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

Start Hunting!

Translated by