Reputation: 18956
I am trying to convert below matlab/octave function to C(Conventional way - understand the matlab function and code it in C from scratch). It is fitting a data to a gaussian curve using polynomial fitting.
function y=func(data)
N=128;
y1=gausswin(N,4);
x1=[0:1/N:1-1/N]';
P=polyfit(x1,y1,12);
y=polyval(P,data);
But when I checked the functions polyfit, that seemed a lot of work as it involves lot of calls to further octave library functions. It computes a Vandermonde matrix first, then performs some QR decomposition of it, and computes norm of the vector etc...
Any pointers would be useful.
Upvotes: 0
Views: 1295
Reputation: 4787
Besides the point of the practical value of fitting such a polynomial to a gaussian, you can just analyze the behavior of your code:
N=128;
y1=gausswin(N,4);
x1=[0:1/N:1-1/N]';
P=polyfit(x1,y1,12);
The output of this section will always be the same, so you can execute this in MATLAB or Octave and just extract the polynomial P
for usage in your C code where you include it as a constant. It's less flexible than rewriting everything in C, but it's also faster.
Otherwise, you might want to take a look at BLAS: BLAS defines an API for libraries used for linear algebra such as LAPACK (which is used by MATLAB). I suspect a lot of these libraries will implement the basic operations you need.
Addition: If you have no little experience with numerical computing or just want a lot of work taken out of your hands, you might want to consider Matlab Coder.
Upvotes: 1