0

I have been doing research in google, however I cannot find a solution.

How to convert an unsigned short array to a float array in STM32? I need to perform multiplication between an unsigned short array with a float array. To use FPU and DSP library for STM32F4, the unsigned array is to be converted to floating point first.

There are only libraries converting signed short arrays to float arrays. How to convert an unsigned short array to a float array?

richieqianle
  • 602
  • 2
  • 7
  • 20
  • 1
    I doubt that you have this option. The DSP library provides operations on `q15_t`, which is the "fractional" version of `int16_t`. But as you mentioned, this is a `signed short`. I think that you can find the entire API in file arm_math.h (also on GitHub, at https://github.com/mikeferguson/stm32/blob/master/libraries/CMSIS/Include/arm_math.h). I believe that anything which is **not** in this file is simply not supported. – barak manos Jun 10 '14 at 14:37
  • Thanks for your comment, @barakmanos . But it has to be done... The data in unsigned short are read from sensors therefore unsigned arrays have to be used to store them.. – richieqianle Jun 11 '14 at 03:31
  • So you'll have to create an array of 32-bit `float` elements, and copy each `unsigned short` element into it. – barak manos Jun 11 '14 at 03:57
  • How to do that? One by one using my own function? Or there are built-in DSP instructions? There are DSP functions converting signed short array to float array btw. – richieqianle Jun 11 '14 at 04:02

1 Answers1

1

By just storing them as float, I'd say:

void uint16_to_float(float *out, const unsigned short *in, size_t num)
{
  for(; num > 0; --num)
    *out++ = *in++;
}

It's up to the compiler to figure out what instruction(s) are best suited for this, but it's a pretty standard operation.

unwind
  • 391,730
  • 64
  • 469
  • 606
  • I think that OP is looking for a way to pass this array to a "built-in" function (provided within the DSP library), which can regard this array of `unsigned short` values as an array of "fractional" values. In other words, I think that OP wishes to avoid taking an array of 16-bit integers as input and generating an array of 32-bit floats as output. – barak manos Jun 10 '14 at 14:39
  • In the DSP library, the conversion has been done in this way: *pDst++ = ((float32_t) * pIn++ / 32768.0f); – richieqianle Jun 12 '14 at 09:06
  • One cent comment: Loop-unrolling could be used to optimize the code! http://stackoverflow.com/questions/2349211/when-if-ever-is-loop-unrolling-still-useful – richieqianle Jun 13 '14 at 03:19