MATLAB Answers

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

47 views (last 30 days)
Jogger
Jogger on 8 Jan 2021
Commented: Jogger on 29 Sep 2021 at 11:17
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

Accepted Answer

Chidvi Modala
Chidvi Modala on 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.

More Answers (1)

Nitin Kapgate
Nitin Kapgate on 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 Comments
Jogger
Jogger on 18 Jan 2021
Thanks steven, But stil the issue did not go away, can you please suggest another approach ?

Sign in to comment.

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by