# colon expression to use integer operands

4 views (last 30 days)
Jogger on 28 Jan 2021
Commented: Jogger on 4 Feb 2021
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 CommentsShowHide 1 older comment
Jogger on 4 Feb 2021
ATTENTION!
I received an email - someone deleted answer ! Don't know / can't track what was that.

Sign in to comment.

### Accepted Answer

Andy Bartlett on 29 Jan 2021
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 CommentsShowHide 5 older comments
Jogger on 2 Feb 2021
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

Sign in to comment.

### More Answers (2)

Image Analyst on 28 Jan 2021
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.
##### 1 CommentShowHide None
Jogger on 28 Jan 2021
Thanks - please see details above

Sign in to comment.

Steven Lord on 28 Jan 2021
Edited: Steven Lord on 28 Jan 2021
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 CommentsShowHide 6 older comments
Image Analyst on 29 Jan 2021
Call tech support.

Sign in to comment.

### Community Treasure Hunt

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

Start Hunting!

Translated by