Effacer les filtres
Effacer les filtres

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

24 vues (au cours des 30 derniers jours)
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))
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

Réponse acceptée

Chidvi Modala
Chidvi Modala le 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.
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';
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.

Plus de réponses (1)

Nitin Kapgate
Nitin Kapgate le 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 commentaires
Steven Lord
Steven Lord le 15 Jan 2021
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))
but I'm not sure if the two casts to double will cause problems in code generation.
Life is Wonderful
Life is Wonderful le 18 Jan 2021
Thanks steven, But stil the issue did not go away, can you please suggest another approach ?

Connectez-vous pour commenter.


Community Treasure Hunt

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

Start Hunting!

Translated by