# Conversion from Floating point representation to Fixed point representation

I have written a matlab function for a constellation mapper and I want to create a Fixed-point version of that code for implementation on FPGA. I know matlab has a specific toolbox for fixed-point conversion but i want to do this manually. My specific question is:

1) which parts of the code would require this conversion? i mean do all inputs, outputs, temporary variables need to be converted to a fixed point format ?

2) What are the steps that I should follow to do this conversion? i read that converting from floating point to fixed is no more, no less than scaling numbers. but I'm not quite sure how I go about doing this. Also, I want this fixed point representation to be of a custom length, not necessarily equal to standard lengths like 8, 16, 32-bits .

PS: I don't want to compare the performance of the fixed point code and floating point code in the time being

I think you need to use the floor() or round() functions in MATLAB, to emulate fixed-point variables and operations. So you have to know the range of the fixed-point value and the precision of it. The ratio of the range to the precision is the dynamic range and you get 6.02 dB and one bit of word width every doubling of that ratio.

If you convert:

x_fixed = precision*round(x_float/precision);
if x_fixed > fixed_max
x_fixed = fixed_max;
elseif x_fixed < fixed_min
x_fixed = fixed_min;
end;


then fixed_max - fixed_min = (2^N - 1)*precision for N being the number of bits in your fixed-point word. If you do this after every mathematical operation, you will be emulating fixed-point arithmetic.

there might be better ways to round x_float than i shown. sometimes the floor() is better than round(). and "fraction saving" is a better way to round an intermediate result with more bits to your N-bit fixed-point word, but requires an error state for each variable.

for every variable and do your arithmetic with their fixed-point values and round the result to a fixed-point value with code like above.

that's how to convert floating-point MATLAB to emulating fixed point math.

You need to calculate the mean and standard deviation of your data. Then you pick say 4 standard deviations from your mean as the useable range of your data. Everything outside of that range is clipped, but if your data is Gaussian, it will contain >99% of all your values. You then use the maximum range represented by your fixed point representation, the mean, and the standard deviation to come up with a scale factor.