Removing seasonal and non-seasonal oscillations with least-square method I have data on sea temperatures at different depths. With these data I need to remove seasonal and non-seasonal oscillations by fitting a function that consists of two sinusoids with periods of 12 and 6 hours on temperatue data (for every depth). The function needs to be put in seasonal diagram. And this adjustment needs to be done with least- square method.
 A: I think that the solution might depend on how you want to use the data. But I'll make a simple suggestion.
Perhaps the most simple approach would be to use a covariate to remove the seasonality.  Do you have a time series of a strong driver?  E.g., air temperature might correlate very well with sea surface temperature.  Regress water temperature against air temperature, and check to see if the residuals contain the appropriate pattern (or lack thereof). If this approach works for surface temperature, maybe it'll work equally well for subsurface temperatures. Obviously this method will not work for certain applications.
A: Natalija,
Notes:


*

*I am hearing this is a like of constant x and y position.

*Transformation of data: all time feeds into decimal year as a single number


Given:
$ f(time, depth) = g(temp) + A_1sin(time+\Delta t_1)+ A_2sin(0.5*time+\Delta t_2) +\epsilon$
The matlab code would be:
function Prob58708
clc;
% enable this if you have data
%load; %load parsed and formatted data
%here is synthetic data
time=linspace(1,10,200);
Temp=pi*sin(time+pi/2)+exp(1)*sin(0.5*time+0.2)+randn(size(time));
global t; %time
t=time;
global T; %Temperature
T=Temp;
X0=[2.87373680779043
          1.59661233154352
          2.89426152938692
          0.10126060393764]
x = fminsearch(@(x) myfit(x),X0)
myfit(x)
return
function [out]=myfit(x)
global t;
global T;
a1=x(1);
dt1=x(2);
a2=x(3);
dt2=x(4);
T2=a1*sin(t+dt1)+a2*sin(0.5*t+dt2);
out=norm(T2-T)./norm(T);
return
