DSPRelated.com
Forums

An Open-Source Fixed-Point (fxp) Class for GNU Octave

Started by ahmedshahein 4 weeks ago7 replieslatest reply 2 weeks ago213 views

Hello all,

I would like to share an open-source fixed-point arithmetic class (fxp) for GNU Octave that I have been developing with a strong focus on DSP-oriented workflows and verification.

Motivation

While floating-point simulation is convenient, many DSP algorithms ultimately target fixed-point hardware. The goal of this class is to make fixed-point behavior explicit, inspectable, and testable early in the algorithm development cycle—without leaving the Octave environment. It has been always a challenge to verify fixed-point using Octave compared to Matlab "fi" class. Hopefully, this class will close the gap between NU Octave and Matlab in that context.

Key Features

  • Signed and unsigned fixed-point support
  • Configurable word length (WL) and fractional length (FL)
  • Explicit quantization, rounding, and overflow behavior
  • Wrap-around and saturation modeling
  • Element-wise and vectorized operation support
  • Designed for bit-true behavioral modeling
  • Regression-tested against Python’s fxpmath package as a golden reference

Typical Use Case

  • Algorithm development in floating-point
  • Gradual migration to fixed-point
  • Validation of numerical effects (quantization error, overflow, wrap-around)
  • Cross-checking Octave results against Python fixed-point models

Example

x = fxp([0:0.1:1], 1, 16, 8);
y = fxp([0:0.2:2], 1, 16, 8);
z = x + y;

Project Status

  • Actively tested and evolving
  • Used for fixed-point verification and regression testing
  • Feedback-driven improvements are welcome

Repository

GitHub:
https://github.com/ahmedshahein/fxp

I would appreciate feedback from the community—especially regarding:

  • Fixed-point edge cases
  • DSP-specific workflows you would like supported
  • Comparison with existing fixed-point modeling approaches

Looking forward to your comments and discussion

Ahmed Shahein.



@26-01-2026
I have started creating a package for the fixed-point class on the official NU Octave GitHub repository.
The new link now is at:
https://github.com/ahmedshahein/pkg-fxp/tree/main

I am not sure what is the best way to proceed with it, shall I close the old one and update the link to the GNU Octave package repo, or keep both just create links between them ...
Any suggestions?!

[ - ]
Reply by ChuckMcMJanuary 21, 2026

This is awesome Shahein, I had to switch to Matlab from Octave because the lack of a good fixed point support. I'll have to plug this into my Octave environment and check it out.

[ - ]
Reply by ahmedshaheinJanuary 21, 2026

Most welcome.

Looking forward for your feedback.

One thing I added which was a big struggle to me always, is to have a 2’s complement binary representation with decimal point. This is now available in this class. I hope you find it helpful 

[ - ]
Reply by Lito844January 24, 2026

Hello Ahmed,

Congratulations on releasing your open-source fixed point class for GNU Octave. I think a lot of folks, myself included, will find it very useful.

I've tinkered a bit with your class and would like to offer my feedback. Take my inputs with a grain of salt, as its just one user's opinion. So, here goes...

1. Improve portability by writing MATLAB compatible code. One of GNU Octave's great attractions is its compatibility with MATLAB. By following just a few simple rules it's very easy to write MATLAB compatible GNU Octave code.

Rule: Use "end" in place of "endif", "endswitch", "endwhile", and "endfunction".

Rule: Use "%" in place of "#" at the start of a comment.

Rule: Use "~" in place of "!" for logical "not" in logical comparisons (for example use "a ~= b" in place of "a != b".

Rule: Avoid "+=", "-=", "*=", "-=" assignments.

Your class is already 99.9% MATLAB compatible and could be 100% by replacing 1 instance of "endswitch" with "end", one "#" comment with "%", and one "!=" with "~=". After doing this I obtained the same results running your regression test in MATLAB as I did with GNU Octave.

2. Improve portability by avoiding dependencies on GNU Octave packages and MATLAB toolboxes where possible. Here again, you're 99.9% there, with your class having only a single package/toolbox dependency, "de2bi", the function that converts a decimal integer into a vector of binary (bits). Fortunately this is an easy function to put together yourself. See below for an example.

3. Overall, your README document is very good. I especially appreciate that you include a Quick Start section. That being said, the meaning of configuration parameters "S", "WL", "IL", and "FL" are not described but are fundamentally critical in this type of class. I think the README document would be improved by adding a succinct description of these parameters.

4. Another improvement to your README is to show the responses the class displays on the console in each of your examples. Even better would be to include a description of what these responses are telling us.

5. It is also worth noting that the word length limitation of your class is 53 bits, due to the double precision floating point natively underlying GNU Octave and MATLAB. You could get to 64 bits by using the supported uint64 types but anything beyond that will take a lot more work.

Finally, here's an example of a DIY de2bi function, I called dec2bit. Note that this will work up to 64 bit integer, so from that perspective it's better than de2bi.

function y = dec2bit(x,n,msbfirst)
% Usage: y = dec2bit(x,n,msbfirst)
%
%  x...........unsigned integer [0,2^64)
%  n...........scalar number of bits
%  msbfirst....if nonzero, left-justify msb
%  y...........row vector of bits
%
    x64 = uint64(x);
    y = zeros(1,n);
    kk = n;
    while x64 ~= 0 && kk
        y(kk) = bitand(x64,1);
        x64 = bitshift(x64,-1);
        kk = kk - 1;
    end

    if all(~msbfirst) || strcmpi(msbfirst,'right-msb')
        y = fliplr(y);
    end

end % function
[ - ]
Reply by ahmedshaheinJanuary 25, 2026

Hi Angelito,

I have a new tag fxp_v5.1.0, which includes the following updates:

- Replace endif by end
- Replace endswitch by end
- Replace != by ~=
- Replace de2bi by dec2bit (Developed by Angelito Hamm)

I am trying to get access to Matlab to check it further. I have you as the author for the dec2bit function.

Regards,

Ahmed.

[ - ]
Reply by Lito844January 25, 2026

Ahmed,

The last remaining item is the comment on line 127 in your current version. Your class will be 100% MATLAB and GNU Octave compatible if you replace the comment "#return" with "%return", or delete that line if it's no longer needed. I verified consistency of results between MATLAB and GNU Octave using your test_regression.m script.

It's completely understandable, if you don't have access to MATLAB then it's not realistic nor practical to guarantee compatibility in your future projects. However, I think it's good practice to follow those 4 simple rules I cited in my previous post to maximize compatibility. This will open up your code to a wider range of users. And if you think about it, that's really the whole point of open-source.

[ - ]
Reply by ahmedshaheinJanuary 29, 2026

Hi,

I have tested it on Matlab 2023, and it worked smoothly. I only had to add it to the path "addpath('<PATH_TP_FXP>');"

Regards,

Ahmed.

[ - ]
Reply by ahmedshaheinJanuary 24, 2026

Dear Angelito,


Thank you for your valuable feedback. I will go through them one by one and make a new release once tested.

Unfortunately, I do not have access to Matlab at this point to test there.

Regards,

Ahmed.