I want to know how to perform MAC calculations using Q-format numbers. ]]>

Thanks,

Shawn

I’m working on FIR implementation using Distributed Arithmetic. And I have to use Q format numbers for MAC calculations. I understood the concept of Q format number but getting confused about using them in calculations. Can you please help me?

Thanks. ]]>

I’m glad the article is useful for you. When the dividend is greater than the divisor, you need to know how much bigger it will be, or use a lot more bits in your result. For example, if you know that the magnitude of the dividend is no larger than one, and the magnitude of the divisor can be as small as 0.5, then the largest magnitude of the result would be 2. You could shift your result such that a value this large could be represented. If you had no restrictions, then you might need to keep a lot of bits in the result to represent the entire range of possible results. In my last example, the division of two Q15 numbers would require a Q30 number to hold the range of results possible.

The multiplication by reciprocal that I mentioned was to do with constant values. I think most C/C++ compilers these days would automatically change a division by constant to a multiplication automatically when optimization is done. I was probably thinking back to when I used to write things in assembly code. I wouldn’t recommend doing a reciprocal normally, unless the result is going to be multiplied by more than one different value afterwards.

I hope that helps.

Shawn

I am struggling to get the division in case of [dividened>divisor], whose result is greater than one.

Would you have any ideas to solve this?

Also, in the beginning of this blog, you mentioned it should be better to multiply by the reciprocal than to do a division.

I wonder how your solution would be to get the reciprocal. might do it by Newton-method? ]]>

The important thing is to know the range of the coefficients, and how many bits will be needed to represent your range of numbers in fixed point. If you don’t use enough bits, then your smallest coefficients may end up as zeroes. When going from double to fixed point, you can round the coefficients to an integer value before casting to an integer. In many cases, it won’t matter how you round or truncate, because the least significant bits are not that significant compared to the magnitude of the coefficient. What in particular did you want to know about the transformation process?

Thanks,

Shawn

I can’t remember exactly how I generated those particular coefficients, but I believe I used a program called Scilab, which is similar to Matlab/Octave. Take a look at my comment here:

https://sestevenson.wordpress.com/2010/01/04/implementation-of-fir-filtering-in-c-part-3/#comment-10

Here is a link to the Scilab function that I used for the FIR filtering example:

https://help.scilab.org/doc/5.3.3/en_US/eqfir.html

You should be able to get something to work using Octave, using a similar script. The band edges don’t have to be really steep for using with the TKEO, so it shouldn’t be too hard to design an adequate filter. I don’t think I put very much effort into designing the one in the code example.

I hope that helps.

Cheers,

Shawn

Your code works great!

I wanted to try it with other frequencies, can you tell me how you got the filter coefficients? Because I tried a few online calculators and even Python and Octave code but couldn’t get results as good as your. Did you use a rectangular filter with no windowing? That’s how I got the best results but still not as good as yours. ]]>