Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert a 32 bits to float value

Tags:

c

linux

embedded

I am working on a DSP processor to implement a BFSK frequency hopping mechanism using C on a Linux system. On the receiver part of the program, I am getting an input of a set of samples which I de-modulate using Goertzel algorithm to determine whether the received bit was a 0 or 1.

Right now, I am able to detect the bits individually. But I have to return the data for processing in the form of a float array. So, I need to pack every set of 32 bits received to form a float value. Right I am doing something like :

uint32_t i,j,curBit,curBlk;
unint32_t *outData; //this is intiallized to address of some pre-defined location in DSP memory
float *output;
for(i=0; i<num_os_bits; i++) //Loop for number of data bits
{ 

//Demodulate the data and set curBit=0x0000 or 0x8000

curBlk=curBlk>>1;
curBlk=curBlk|curBit;

bitsCounter+=1;
    if(i!=0 && (bitsCounter%32)==0) //32-bits processed, save the data in array
    {
        *(outData+j)=curBlk;
        j+=1;
        curBlk=0;
    }
}

output=(float *)outData;

Now, the values of the output array are just the values of outData array with 0s after the decimal point. example: if output[i]=12345 the `outData[i]=12345.0000'.

But while testing the program I am generating the sample test data of bits using an array of float float data[] ={123.12456,45.789,297.0956};

So after the demodulation I am expecting the float array output to have a same values as data array. Is there some other method to convert 32-bits of data to a float. Should I store the received bits to a char array and then convert it to float.

like image 778
anshu Avatar asked Jul 23 '12 11:07

anshu


1 Answers

Not sure if I get your point - you sequentialy obtain bits and if you got 32 bit you want to make a float from them?

what about:

union conv32
{
    uint32_t u32; // here_write_bits
    float    f32; // here_read_float
};

Which can be used like this in-line:

float f = ((union conv32){.u32 = my_uint}).f32;

Or with a helper variable:

union conv32 x = {.u32 = my_uint};
float f = x.f32;
like image 119
René Kolařík Avatar answered Sep 20 '22 00:09

René Kolařík