Welcome Guest ( Log In | Register )

Outline · [ Standard ] · Linear+

 converting datatype from array to another

views
     
TSnarf03
post Mar 24 2024, 02:18 AM, updated 2y ago

Look at all my stars!!
*******
Senior Member
4,547 posts

Joined: Dec 2004
From: Metro Prima, Kuala Lumpur, Malaysia, Earth, Sol


Lets say i have 2 arrays source and destination.

source array consists of 1000 items of 32bit float, that their value ranged from 1.0f to -1.0f

destination array consists of 2000 bytes

i want to convert each float to 2 byte(16 bit), value ranged 32767 to -32768(00 00 to FF FF), little-endian

i manage to do byte by byte calculation, but its a bit slow, any efficient way to do it ?

This post has been edited by narf03: Mar 24 2024, 02:25 AM
cheesey
post Mar 24 2024, 06:21 AM

On my way
****
Junior Member
521 posts

Joined: Jan 2003
CODE

#include <cstdint>
#include <iostream>
#include <vector>

// Function to convert float in range [-1.0f, 1.0f] to int16_t
int16_t float_to_int16(float value) {
   // Clamp the value to the range [-1.0f, 1.0f]
   value = value > 1.0f ? 1.0f : value < -1.0f ? -1.0f : value;
   return static_cast<int16_t>(value * 32767.0f);
}

// Function to convert an array of floats to an array of int16_t
std::vector<int16_t> convert_float_array(const std::vector<float>& source) {
   std::vector<int16_t> destination;
   destination.reserve(source.size());

   for (float value : source) {
       destination.push_back(float_to_int16(value));
   }

   return destination;
}

int main() {
   // Example usage
   std::vector<float> source = {/* ... fill with your 1000 float values ... */};
   std::vector<int16_t> destination = convert_float_array(source);

   // Output the converted values
   for (int16_t value : destination) {
       std::cout << value << " ";
   }

   return 0;
}


Not tested
TSnarf03
post Mar 24 2024, 07:44 PM

Look at all my stars!!
*******
Senior Member
4,547 posts

Joined: Dec 2004
From: Metro Prima, Kuala Lumpur, Malaysia, Earth, Sol


QUOTE(cheesey @ Mar 24 2024, 06:21 AM)
CODE

#include <cstdint>
#include <iostream>
#include <vector>

// Function to convert float in range [-1.0f, 1.0f] to int16_t
int16_t float_to_int16(float value) {
   // Clamp the value to the range [-1.0f, 1.0f]
   value = value > 1.0f ? 1.0f : value < -1.0f ? -1.0f : value;
   return static_cast<int16_t>(value * 32767.0f);
}

// Function to convert an array of floats to an array of int16_t
std::vector<int16_t> convert_float_array(const std::vector<float>& source) {
   std::vector<int16_t> destination;
   destination.reserve(source.size());

   for (float value : source) {
       destination.push_back(float_to_int16(value));
   }

   return destination;
}

int main() {
   // Example usage
   std::vector<float> source = {/* ... fill with your 1000 float values ... */};
   std::vector<int16_t> destination = convert_float_array(source);

   // Output the converted values
   for (int16_t value : destination) {
       std::cout << value << " ";
   }

   return 0;
}


Not tested
*
not dont know how to do it, i wonder if there is a more efficient way of doing it not byte by byte, my example is 1000 items, in reality, it will be far more.
silkworm
post Mar 25 2024, 01:00 PM

Enthusiast
Group Icon
Elite
965 posts

Joined: Jan 2003
From: Kajang


QUOTE(narf03 @ Mar 24 2024, 07:44 PM)
not dont know how to do it, i wonder if there is a more efficient way of doing it not byte by byte, my example is 1000 items, in reality, it will be far more.
*
Hi, You're probably looking for Floating point to fixed point conversions.

Since there's a fixed number of possible values (65536), the time tested method of using a lookup table is probably the fastest. Trade memory for computation time. One could probably be clever and halve the table size by playing with the sign bit, but it might not be worth the effort.

Bear in mind, because there are multiple ways to represent a similar value in floats, (e.g 0.9 can be 9 * 0.1 or 90 * 0.01, etc) the input may need to be normalized first.


 

Change to:
| Lo-Fi Version
0.0142sec    1.20    5 queries    GZIP Disabled
Time is now: 23rd December 2025 - 04:56 AM