I am looking for a method to fill in data gaps in a time-series that contains periodic data (in this case with a frequency equal to a tidal frequency, so both semi-diurnal and spring/neap frequencies) using MATLAB. The data series also contains noise which I would like to superimpose on top of the artificial data that fills the time gap. The data has a certain trend that I want to preserve. Ideally I would be looking at a method that uses the recorded data on either side of the time gap.
Is there anyway of doing this in Matlab?
Thank you.
Donald John
So what one can do is "guess" a modelfunction and fit the data by that model using some optimization routine. Then take a close look at the residuals and get the statistics which characterize the noise from that residuals. Then apply the model and add the noise. In Matlab Code the Ansatz could look like:
t_full = linspace(0,4*pi,500);
t = t_full([1:200, 400:end]);
f = 2;
A = 3;
D = 5;
periodic_signal = A*sin(t*f) + D;
trend = 0.2*t;
noise = randn(size(t));
y = periodic_signal + trend + noise;
% a model for the data -- haha i know the exact model here!
model = @(par, t) par(1)*sin(t*par(2)) + par(3) + par(4)*t;
par0 = [2, 2, 2, 2]; % and i can make a good guess for the parameters
par_opt = nlinfit(t,y, model, par0); % and optimize them
% now from the residuals (data minus model) one can guess noise
% characteristics
residual = y - model(par_opt, t);
% compare residual with "real noise" (should coincide if optimisation
% doesnt fail)
[mean(noise), mean(residual)] % about [0, 0]
[std(noise), std(residual)] % about [1, 1]
missing_data = 201:399;
new_noise = mean(residual) + std(residual)*randn(size(missing_data));
% show what is going on
figure
plot(t,y,'k.')
hold on
plot(t_full, model(par_opt, t_full), 'r-', 'linewidth', 2);
plot(t_full(missing_data), model(par_opt, t_full(missing_data)) + new_noise, 'r.')
legend('data', sprintf('y(t) = %.2f*sin(%.2f*t) + %.2f + %.2f*t + e(t)', par_opt), 'reconstructed data')
It results in the following graphic: