Fractional Arithmetic

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Fractional Arithmetic

3,968 Views
DaveB
Contributor I
I'm having the hardest time wraping my brain around Fractional data types.

I need to convert integers, such as the values returned from the ADC,
and floats, such as 0.3835545, to Fract16 values.

In the application note an1772.pdf I found these to be almost useful...
#define _CI(X) *(INT )&(X) /* Convert to int */
#define _CPI(X) (INT *)(X) /* Convert to pointer to int */
#define _CLI(X) *(long int *)&(X) /* Convert to long */
#define _CF(X) *(_fract *)&(X) /* Convert to fractional */
#define _CPF(X) (_fract *)(X) /* Convert to pointer to fract. */
#define _CLF(X) *(long _fract *)&(X) /* Convert to long fract.

except there is no definition of _fract.

Does anyone have working macros to do this?

Thanks for any help
Labels (1)
Tags (1)
0 Kudos
Reply
5 Replies

973 Views
trytohelp
NXP Employee
NXP Employee
Hi,
 
This Application Note is not for CodeWarrior tools.
Extract of the AN:

This application note describes the process of preparing standard 16-bit, bit-exact algorithms so that they compile for the Freescale DSP56300 family of processors using the DSP56300 C compiler from TASKING, Inc.

In CodeWarrior Processor Expert provides some beans for all DSP Function& Math Library:
 Array Function Library,
 Digital Signal Processing Library,
 Fractionnal Math Library,
 Matrix Math Library,
 Trigonometric Function Library,
 Vector Math Library.
 
Some example are delivered on the installation using them.
Please have a look to the folder:
      \Stationery\Processor_Expert_Examples\TestApplications\Signal
 
Hope this will help you.
Regards
Pascal
 
0 Kudos
Reply

973 Views
DaveB
Contributor I
Thanks Pascal.
In \Stationery\Processor_Expert_Examples\DemoApplications\56F8300_DemoBoard\Voice_Recorder_Demo
I found this macro ...
#define FRAC16(x) ((Frac16)((x) < 1 ? ((x) >= -1 ? (x)*0x8000 : 0x8000) : 0x7FFF))
used to convert the float FIR Coefficients to Fract16.
This example also just stores the ADC values as type Frac16.
If you were to us the Library funct mult to multiply an ADC integer of 8192 time the Frac16 representation
of 0.25 (also 8192) would the result be 2048 or 67108864 which would not fit in a Frac16?

What I'm trying to do is multiply a float Hanning window coefficient by the sampled ADC values, before doing
an FFT on the samples.

I'm obviously still not getting something.

Thanks.


Message Edited by DaveB on 2008-02-21 12:17 PM
0 Kudos
Reply

973 Views
trytohelp
NXP Employee
NXP Employee
Hi,
 
The compiler has some intinsic functions.
These function are using Fractional too and can be used for math functions.
Please refer to the Targeting_56800E.pdf manual.
There is a chapter named Inline Assembly Language and Intrinsics - 10.
There a lot of functions for multiplication depending of your want to do.
Can you please have look at them ?
 
For instance:

L_mult
Multiply two 16-bit fractional values generating a signed 32-bit fractional result. Saturates
only for the case of 0x8000 x 0x8000.
Assumptions
OMR’s SA bit was set to 1 at least three cycles before this code, that is, saturation on data
ALU results enabled.
Prototype
Word32 L_mult(Word16 sinp1, Word16 sinp2)
Example
short s1 = 0x2000;/* 0.25 */
short s2 = 0x2000;/* 0.25 */
long result;
result = L_mult(s1,s2);
// Expected value of result: 0.0625 = 0x08000000
 
Pascal
 
0 Kudos
Reply

973 Views
DaveB
Contributor I
Thanks Pascal, I seem to have it working now.
0 Kudos
Reply

973 Views
trytohelp
NXP Employee
NXP Employee
your welcome.
Don't hesitate to contact us.
 
Pascal
0 Kudos
Reply