cancel
Showing results for 
Search instead for 
Did you mean: 

q1.15 Format

Sax
Associate III

I have 1024 integer numbers in an array that are between 0 and 4096. Now I'm trying to convert these array values to Q1.15 format. These should then be between -1 and +1. With my attempt, I don't achieve any negative values, all of them are just positive. Thanks for the help.

 

float32_t fir_in_arm [1024],fir_out_arm [1024],fir_state [1089-1];

int16_t fir_in_temp [1024]; // with float32_t also tried and no success

float32_t fir_in_temp_gleit [1024];

 

for (int i = 0; i<= adcBufferSize; i++)

{

adc_buffer_float[i] = (float32_t)adc_buffer [i];

fir_in_temp_gleit[i] = (adc_buffer_float[i])/32767;

fir_in_temp [i] = fir_in_temp_gleit[i]*32768;

}

1 ACCEPTED SOLUTION

Accepted Solutions
TDK
Guru

To map (0, 4096) to q1.15 (expressed as an int16_t):

 

uint16_t input = 0 ... 4095;
float mid = input / 2048.0f - 1.0f;
int16_t output = (int16_t) (mid * 32768);

 

 Or all in one statement:

 

int16_t output = (int16_t) ((input / 2048.0f - 1.0f) * 32768);

 

 Gives the following mapping:

 

0 -> -1.000 -> -32768
256 -> -0.875 -> -28672
512 -> -0.750 -> -24576
768 -> -0.625 -> -20480
1024 -> -0.500 -> -16384
1280 -> -0.375 -> -12288
1536 -> -0.250 -> -8192
1792 -> -0.125 -> -4096
2048 -> 0.000 -> 0
2304 -> 0.125 -> 4096
2560 -> 0.250 -> 8192
2816 -> 0.375 -> 12288
3072 -> 0.500 -> 16384
3328 -> 0.625 -> 20480
3584 -> 0.750 -> 24576
3840 -> 0.875 -> 28672

 

If you feel a post has answered your question, please click "Accept as Solution".

View solution in original post

1 REPLY 1
TDK
Guru

To map (0, 4096) to q1.15 (expressed as an int16_t):

 

uint16_t input = 0 ... 4095;
float mid = input / 2048.0f - 1.0f;
int16_t output = (int16_t) (mid * 32768);

 

 Or all in one statement:

 

int16_t output = (int16_t) ((input / 2048.0f - 1.0f) * 32768);

 

 Gives the following mapping:

 

0 -> -1.000 -> -32768
256 -> -0.875 -> -28672
512 -> -0.750 -> -24576
768 -> -0.625 -> -20480
1024 -> -0.500 -> -16384
1280 -> -0.375 -> -12288
1536 -> -0.250 -> -8192
1792 -> -0.125 -> -4096
2048 -> 0.000 -> 0
2304 -> 0.125 -> 4096
2560 -> 0.250 -> 8192
2816 -> 0.375 -> 12288
3072 -> 0.500 -> 16384
3328 -> 0.625 -> 20480
3584 -> 0.750 -> 24576
3840 -> 0.875 -> 28672

 

If you feel a post has answered your question, please click "Accept as Solution".