Fitting Cosine function to many different data sets.

9 views (last 30 days)
Hi,
I am calibrating a camera and need to fit a cosine curve to each pixel. There 2048x2448 pixels. There's a total of 19 images of different intensities . I am trying to fit each pixel to a cosine function across the different images.
I used a fit function
fit = @(A,B,C,x) A + B.*(cos((x) + C)) ;
and put the paramters i wanted into a matrix whose location correspondeded to which pixel ir refers to.
The problem arises when i have a for loop to do this for all the pixels. It would take 5 years for the code to run .
Is there any tips for optimization? I can easly predict close to what the fit will look like; is there a way to implement this?
Any help/tips are greatly appreciated

Accepted Answer

Star Strider
Star Strider on 17 Apr 2019
Thi idea I deleted is to use the Angle sum and difference identities (link) to linearise your regression, since linear regressions are significantly faster than nonlinear regressions, especially in MATLAB.
The derivation and detais:
In the context of your model, this becomes:
or:
where:
so after the linear regression is finished, calculating the B and C parameter involves initially
squaring them:
restated:
and if I did my maths correctly:
then knowing B, it is possible to calculate C, ideally as:
The reason I initially deleted it has to do with my not being able to specifically constrain and as functions of each other or of another parameter so that B and C would be unambiguous, so that B and C calculated from either or would yield approximately the same result. I cannot be certain that would hold, and as I mentioned, I cannot devise a way of constraining them as functions of each other or of another parameter so that it would. (You may also be able to eliminate A by subtracting the mean of your signal.)
I am confident that the linear regression will be faster. I cannot claim that the parameter estimates will be as reliable.

More Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!