Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Re: cosine curve fit

5 views
Skip to first unread message

John D'Errico

unread,
Apr 26, 2006, 10:58:31 AM4/26/06
to
Rabita wrote:
>
>
> Hi friends
> Could anyone help me perform COSINE curve fitting in MATLAB to fill
> up missing biological data (during sleep).
> I have already tried with interp1() which is not very satisfactory.
> Thanks

What specific model will you use? Just
to call it a cosine fit is not adequate
to specify the model.

What was unsatisfactory about interp1?

John

Rabita

unread,
Apr 26, 2006, 7:33:56 AM4/26/06
to

Rabita

unread,
Apr 27, 2006, 5:47:56 AM4/27/06
to

I found this name "cosine curve fitting" in chronobiology journal
only and don't know much about it.
They suggested to use this technique for fitting data that is
missing. The data was collected at say an interval of 3hours in total
24 hours as we cannot awake a person from sleeping.

interp1 gives 2 peaks on the plotted graph. We expect only one.
Rabita

Dave Robinson

unread,
Apr 27, 2006, 6:06:15 AM4/27/06
to
> interp1 gives 2 peaks on the plotted graph. We expect only one.
> Rabita

If you have found a cosine curve of frequency f that exactly fits
your data, then a cosine curve which has frequency of -f also matches
your data.

Only guessing, as I really don't have a clear picture of what you are
trying to do.

Regards

Dave Robinson

John D'Errico

unread,
Apr 27, 2006, 9:07:23 AM4/27/06
to

I'm not sure what you need here. If you are positive
the period of the function is known to be 24 hours,
then I imagine this reference "cosine curve fitting"
means fitting a regression model of the form

f(t) = a*cos((x-phi)*(2*pi/24))

where a and phi are unknowns.

This makes very strong assumptions about the nature
of the curve you will fit however. It forces the
curve shape into the form of a cosine function, even
though there are many curve shapes that are periodic
over that same period, yet do not take a specific
cosine wave form at all.

Is this what you are asking for? Its rather simple
to do in matlab. You could use my fminspleas code
from the file exchange, or you can even do it with
a purely linear regression, as long as the period is
given. Thus, assume that you have a set of data y,
sampled at times t. Assume that both t and y are
column vectors for simplicity. Then fminspleas would
solve this as

fun = @(phi,t) cos((t-phi)*2*pi/24);
[phi,a] = fminspleas({fun},1,t,y);

If you wish to do it with a purely linear regression,
then we need to be slightly trickier:

M = [cos(t*2*pi/24), sin(t*2*pi/24)];
C = M\y;

phi = atan2(C(2),C(1))*24/2/pi
a = norm(C)

We can test these methods with some random data:

n = 10;
t = rand(n,1)*24;
y = 3*cos((t - 5)*2*pi/24);

% fminspleas estimates
fun = @(phi,t) cos((t-phi)*2*pi/24);
[phi,a] = fminspleas({fun},1,t,y);

phi =
5

a =
3


M = [cos(t*2*pi/24), sin(t*2*pi/24)];
C = M\y;

phi = atan2(C(2),C(1))*24/2/pi
a = norm(C)

phi =
5

a =
3

As expected, we get the correct solution from
either method. Find fminspleas here:

http://www.mathworks.com/matlabcentral/fileexchange/loadFile.do?objectId=10093&objectType=FILE

If the term cosine curve fitting means a more
complex model, then you need to define the model.

HTH,
John D'Errico


--
The best material model of a cat is another, or preferably the same, cat.
A. Rosenblueth, Philosophy of Science, 1945

Those who can't laugh at themselves leave the job to others.
Anonymous

jorge

unread,
May 23, 2013, 1:54:09 PM5/23/13
to
John D'Errico <wood...@rochester.rr.com> wrote in message <woodchips-06141...@syrcnyrdrs-03-ge0.nyroc.rr.com>...
what if the function you are trying to fit has the form of f(t) = a*cos((x-phi)*(2*pi/24)) + c.
how would you find c?
thanks
0 new messages