colon expression to use integer operands

3 次查看(过去 30 天)
The following code does not work because the colon operands are not integer values
stepSize = (pi/512);
thRadDbl = (0:stepSize:(2*pi - stepSize));
Question :How to rewrite the colon expression to use integer operands ?
  2 个评论
Life is Wonderful
Life is Wonderful 2021-1-28
编辑:Life is Wonderful 2021-1-28
I get this error while converting into fixed point code
Colon operands must have integer values when interacting with type 'embedded.fi'.
Error in ==>> init_data_fixpt Line: 13 Column: 15
Code generation failed: View Error Report
As per the documentation :
If your code uses non-integer operands, rewrite the colon expression so that the operands are integers.The following code does not work because the colon operands are not integer values.
Fs = fi(100);
n = 1000;
t = (0:1/Fs:(n/Fs - 1/Fs));
Rewrite the colon expression to use integer operands.
Fs = fi(100);
n = 1000;
t = (0:(n-1))/Fs;
Life is Wonderful
ATTENTION!
I received an email - someone deleted answer ! Don't know / can't track what was that.

请先登录,再进行评论。

采纳的回答

Andy Bartlett
Andy Bartlett 2021-1-29
1)
For embedded designs, if at all possible consider changing your design to work with revolutions instead of radians.
1 revolution = 360 degrees = 2*pi radians
Many angle sensors are perfectly matched for recording angles in revolutions. For example, a spec. sheet may say that each angle sensor pulse equals 360 degrees / 4096. This is equivalent to 2^-12 revolutions per pulse. Equivalently, 4096 pulses equals one revolution. For this sensor, great data types for the angle would be
numerictype(0,12,12) % 0 to just under 1 revolution
numerictype(0,16,12) % 0 to just under 16 revolutions
Updating this angle would be lossless and would require a simple increment or add. In contrast, trying to record the angle in degrees or radians would always involve precision loss for representing 360/4096 or 2*pi/4096, and would require more math and bigger types.
To compute things like sine or cosine, it is generally useful to do a modulo first. A huge advantage to angles in revolutions with binary scaled fixed point is that modulo 1 revolution is lossless and trival. Just a simple masking operation to keep all the fraction bits and drop any integer bits. In contrast, attempting to do module 360 or 2*pi requires more costly calculations and can introduce precision losses.
2)
To create your evenspaced vector of n elements, covering the closed open interval [0, valueMax).
2a)
You gave an example of n = 1000.
If possible consider changing to an exact power of two like n = 1024 = 2^10. It will generally make the math nicer, and if using revolutions, then the math will be lossless too.
2b)
You need to pick one fixed-point data type for the entire vector. You cannot use different data types for each element of the vector. First determine, the level of accuracy required. For example, if the angle will be coming from the sensor example above with 4096 ticks per revolution, then a Fraction Length of 12 is a perfect choice. Based on the desired FractionLength, the rest of the data type attributes can be determined
2c)
The attached script
evenAngleVecCalc.m
shows how to set the output data type and compute the vector.
-Andy
  6 个评论
Walter Roberson
Walter Roberson 2021-2-2
sind() would expect input in degrees. sin() would expect input in radians.
Life is Wonderful
编辑:Life is Wonderful 2021-2-2
I am pretty well on my way in resolving the problem, I have two choice ,
  1. To have a short lookup table (32bit ) - In the moment , I have 128 size bit lookup table
I am facing problem in getting the sine function output in Q12.20 > higher than 16bit output Q15.xx
2. To have cordicsin function
I have 10 iteration cordicsin function that add to latency/computation complexity
Can you please suggest which is good option to work upon ?
- Thanks

请先登录,再进行评论。

更多回答(2 个)

Image Analyst
Image Analyst 2021-1-28
Not true. That code runs fine.
Maybe you want to consider linspace though. Or if you're going to use it as indexes, then round the values.

Steven Lord
Steven Lord 2021-1-28
编辑:Steven Lord 2021-1-28
Seems to work fine for me.
stepSize = (pi/512);
thRadDbl = (0:stepSize:(2*pi - stepSize));
thRadDbl(1:5)
ans = 1×5
0 0.0061 0.0123 0.0184 0.0245
There's no way you're going to be able to use thRadDbl as a vector of indices since arrays in MATLAB have neither an element 0 nor an element pi/512.
One thing you could do is to wait to multiply by pi until after you've constructed the vector.
step = (1/512);
v = pi*(0:step:(2-step));
v(1:5)
ans = 1×5
0 0.0061 0.0123 0.0184 0.0245
Or perhaps the sinpi and/or cospi functions would be of interest to you.
  7 个评论
Steven Lord
Steven Lord 2021-1-28
We've about exhausted my knowledge of the fi data type so I don't think I can provide any more assistance.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Logical 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by