File Exchange

image thumbnail

Cosinor Analysis

version (25.3 KB) by cheart
Fits cosine curve to a time series using least squares


Updated 24 Jun 2008

No License

Cosinor analysis uses the least squares method to fit a sine wave to a
time series. Cosinor analysis is often used in the analysis
of biologic time series that demonstrate predictible rhythms. This
method can be used with an unequally spaced time series.

Follows cosinor analysis of a time series as outlined by
Nelson et al. "Methods for Cosinor-Rhythmometry" Chronobiologica.
1979. Please consult reference.

t - time series
y - value of series at time t
w - cycle length, defined by user based on prior knowledge of time
alpha - type I error used for cofidence interval calculations. Usually
set to be 0.05 which corresponds with 95% cofidence intervals

Note: Statistics toolbox is necessary only to caculate t and F distributions for the number of samples at the specified alpha. These values could be obtained from a table and manually inserted in the code, thus negating the use of the statistics toolbox.

Cite As

cheart (2021). Cosinor Analysis (, MATLAB Central File Exchange. Retrieved .

Comments and Ratings (13)


It is not working now?

Yuanjia Zheng

Thank you for your cosinor analysis toolbox. It is useful. But now I have two little question. First, I detected body temperature at 15:00, 19:00, 23:00, 3:00, 7:00, 11:00, and I need to calculate these data. I wonder that my "t="script should be "t=[0,4,8,12,16,20]/360",or"t=[0,60,120,180,240,300]/360",or"t=[45,105,165,225,285,345]/360"?Which one is correct? Because I found that the different script will lead to different result. Second, when I got the message of "!! Confidence region overlaps the pole. Confidence limits for Amplitude and Acrophase cannot be determined !!". Is that means the test is negative there is no rhythm available? Because I found that P<0.05 with the message. So it confuses me. Looking forward to your reply! Thank you!

Yuanjia Zheng

I got "!! Confidence region overlaps the pole. Confidence limits for Amplitude and Acrophase cannot be determined !!".How to solve it?

Kelvin Prado

I'm getting the following message:

"Confidence region overlaps the pole. Confidence limits for Amplitude and Acrophase cannot be determined"

Why the confidence region overlaps the pole? Why cannot the confidence limits for amplitude and acrophase be determined? What can I do to solve this?



It seems that unequal sampling problem can be resolved by addition ~isnan(y) to every value in NE matrix. Thus 2) is not an issue. :)


UPD: 3) added by mistake


I am by no means a specialist in Matlab, but there are several comments:
1) In section "Confidence interval for the mesor" is written 'sigma^2', whereas according to Nelson et al, 1979 and Bingham et al., 1982 it should be 'sigma'.
This leads to inflated estimation of mesor CI.
2) This code is can not be used for data sets with missing values - only equally spaced and full time series thus simplified estimation of M, beta and gamma can be applied as shown in Nelson et al.,1979.
3) According to Nelson et al., 1979 t should be computed for two-tailed distribution, thus 1-alpha/2 should be used.

David Bulger

Wouldn't the p-value for the zero-amplitude test have to come from the F-distribution's CDF, rather than its PDF?


Undefined function or variable "phi".

Error in ==> cosinor at 94
fprintf(1,'Mesor = %g \nAmplitude = %g \nAcrophase = %g \n\n',M,Amp,phi);

c heart

The published example should run to completion. The statistics toolbox is necessary. Please post if there are any additional problems.

Peter Giles

The published example does not run to completion. The error message is:

??? Undefined function or method 'tinv' for input arguments of type 'double'.

Error in ==> cosinor at 121
CI_M = tinv(1-alpha/2,n-3)*sigma^2*sqrt(((sum(x.^2))*(sum(z.^2)) - (sum(x.*z))^2)/(n^3*(X*Z - T^2))); %#ok<NASGU>

Either this m-file depends on a toolbox not identified here, or something has been left out of the submission.

a a

There is a bug in the published M-file!!!

MATLAB Release Compatibility
Created with R2007a
Compatible with any release
Platform Compatibility
Windows macOS Linux

Community Treasure Hunt

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

Start Hunting!