MATLAB: Cosinor analysis of temperature Excel data

analysisanomaliescosinorexceltemperature

Hi there,
I have obtained temperature data over 2 days from participants and it has been saved on an excel spreadsheet with the time and its corresponding temperature at that time. I would like to plot this data using cosinor analysis on matlab but i have never done this. i have attached a sample of this data. there are certain anomalies which i hope wont impact the cosinor analysis. I would really appreciate a code that would help me plot the analysis. it would also be helpful if someone could help/suggest a way to remove the anomalies in the data.
thanks in advance

Best Answer

One approach:
D = readtable('vital_parameters_15540 (1) copy.xlsx');
x = datenum(D.time) - datenum(D.time(1)); % Date Numbers For Curve Fitting
xd = D.time - D.time(1); % Duration Array
y = D.temperature;
y = y-median(y);
yf = medfilt1(y, 750); % Filter
yu = max(yf);
yl = min(yf);
yr = (yu-yl); % Range of ‘y’
yz = y-yu+(yr/2);
zci = @(v) find(v(:).*circshift(v(:), [-1 0]) <= 0); % Returns Approximate Zero-Crossing Indices Of Argument Vector
zx = find(zci(yf)); % Find zero-crossings
per = 2*mean(diff(zx)); % Estimate period
ym = mean(y); % Estimate offset
fit = @(b,x) b(1).*(sin(2*pi*x./b(2) + 2*pi/b(3))) + b(4); % Function to fit
fcn = @(b) sum((fit(b,x) - y).^2); % Least-Squares cost function
s = fminsearch(fcn, [yr; per; -1; ym]) % Minimise Least-Squares
xp = linspace(min(x),max(x));
figure
plot(xd,yf,'b', xd,fit(s,x), 'r')
grid
text(0.2*mean(xd), 0.6, sprintf('T = %.2f sin(2\\times\\pi\\timest\\times%.2f %.2f) + %.2f', s(1), 1/s(2), 1/s(3), median(D.temperature)))
producing:
This code may not be robust. It works here. The estimated frequency is 1.03 cycles/day with a phase of -1.54 cycles. Note that the time begins at 00:00. Add the beginning time to get the actual time.
I have not done cosinor analysis in decades, so I will leave it to you to plot these in polar coordinates (using the polarplot function) if you want them plotted that way.