ILNumerics Computing Engine supports the most common numeric data types out of the box: double, float, complex, fcomplex, byte, short, int, long, ulong
If you need to convert from, let’s say ushort to float, you will not find any prepared conversion function in ILMath. Luckily, it is very easy to write your own:
Here comes a method which implements the conversion from ushort -> float. A straight forward version first:
/// <summary> /// Convert ushort data to ILArray<float> /// </summary> /// <param name="A">Input Array</param> /// <returns>Array of the same size as A, single precision float elements</returns> public static ILRetArray<float> UShort2Single(ILInArray<ushort> A) { using (ILScope.Enter(A)) { ILArray<float> ret = ILMath.zeros<float>(A.S); var retArr = ret.GetArrayForWrite(); var AArr = A.GetArrayForRead(); int c = 0; foreach (ushort a in A) { retArr[c++] = a; } return ret; } }
This method is used like that:
ushort[,] rawSensorData = new ushort[,] {{0,1,2},{3,4,5}}; ILArray<float> converted = UShort2Single(rawSensorData); /* * <Single> [3,2] * [0]: 0 3 * [1]: 1 4 * [2]: 2 5 */ // continue working with 'converted' here...
The following method does the same but utilizes pointer arithmetic, hence it needs the /unsafe flag. Use this, if performance is critical and your data are sufficiently large:
/// <summary> /// Convert ushort data to ILArray<float> (unsafe version) /// </summary> /// <param name="A">Input Array</param> /// <returns>Array of the same size as A, single precision float elements</returns> public unsafe static ILRetArray<float> UShort2SingleUnsafe(ILInArray<ushort> A) { using (ILScope.Enter(A)) { ILArray<float> ret = ILMath.zeros<float>(A.S); var retArr = ret.GetArrayForWrite(); var AArr = A.GetArrayForRead(); fixed (ushort* pAArr = AArr) fixed (float* pRetArr = retArr) { ushort* pInWalk = pAArr; ushort* pInEnd = pAArr + A.S.NumberOfElements; float* pRetWalk = pRetArr; while (pInWalk < pInEnd) { *(pRetWalk++) = /*implicit: (float)*/ (*(pInWalk++)); } } return ret; } }