Why t_tide prediction is offseted?

26 views (last 30 days)
Ghani
Ghani on 22 Dec 2025 at 9:29
Commented: William Rose on 4 Jan 2026 at 21:28
Hi, i'm trying to use t_tide_v1.4beta using matlab R2016a in 2 ways like in t_tide example but the result of the harmonic constants is not similar and the prediction is not similar between prediction also offseted to the observation data. Help me please. Herewith the code and the data that I use.
Code:
clear
clc
%format LONGG
%load t_example
% load data excel
data = readtable('D:\Test\T_Tide\t_tide_v1.4beta\kmna_wnan.xlsx');
time_axis = datetime(data.waktu);
data_time = datenum(time_axis);
obs_value = data.ukuran;
% Analisis Harminik
%---------------------------------------------------------------------------
% Way 1
% analisys
[NAME,FREQ,TIDECON,XOUT] = t_tide(obs_value,'interval',1,'start',data_time(1),'latitude',-3.66295,'synthesis',1);
% prediction
YOUT=t_predic(data_time,NAME,FREQ,TIDECON,'latitude',-3.66295,'synthesis',1);
% Way 2
% analisys
[HarKons,XOUT2]=t_tide(obs_value,'interval',1,'start',data_time(1),'latitude',-3.66295,'rayleigh',1,'synthesis',1);
% prediction
% YOUT=T_PREDIC(TIM,TIDESTRUC,...)
YOUTHarKons1=t_predic(data_time,HarKons,'latitude',-3.66295,'synthesis',1);
YOUTHarKons2=t_predic(data_time,HarKons);
%% visualisasi
figure(1)
plot(time_axis,obs_value,'b','LineWidth',1.5); hold on
plot(time_axis,XOUT,'r--','LineWidth',1.5); hold on
plot(time_axis,YOUT,'c.','LineWidth',1.5); hold on
plot(time_axis,YOUTHarKons1,'kx','LineWidth',1.5); hold on
plot(time_axis,YOUTHarKons2,'ko','LineWidth',1.5); hold on
title('Tidal Elevation','FontSize',14,'fontWeight','b')
legend({'obs_value', 'XOUT', 'YOUT', 'YOUTHarKons1', 'YOUTHarKons2'},'FontSize', 10, 'FontWeight', 'bold', 'Location', 'northeastoutside');
xlabel('Time','FontSize',14,'FontWeight','b')
ylabel('Elevation (m)','FontSize',14,'FontWeight','b')
% Format sumbu X agar menampilkan tanggal/waktu
% datetick('x', 'dd/mm/yyyy HH:MM:ss', 'keepticks', 'keeplimits')
grid on
Tidal graph obs_value and prediction:
Data:

Answers (1)

William Rose
William Rose on 31 Dec 2025 at 18:50
The predictions from t_tide do not include a DC (i.e. zero-frequency) term. Therefore the t_tide predictions have zero mean value, since they are a sum of sinusoids with non-zero frequency. You can see this in the original article by the authors of t_tide, here. Compare panels A and B of Figure 1 in the article. The data to be fitted, in panel A, has non-zero mean value. The prediction in panel B has mean value equal to zero.
You can compute the mean value of your sample data, then add it to the t_tide prediction, so that the t_tide prediction better. If you publish this, explain what you have done. Or you can subtract the mean value from your signal before you plot it, so that it corresponds to the t_tide prediction.
Good luck with your research.
  2 Comments
Ghani
Ghani on 4 Jan 2026 at 10:27
Thank you for the reply William. After read your answer and some browsing for the video tutorial, I relize that the prediction of the t_tide indeed located in the zero mean value not around the observation data. And I look in the command window there is displayed x0 that represent the mean value. In the t_tide.m file x0 are as a part of the coef matrix and it writen as z0. And what I do it to make this z0 variable and value are usable is modify the function argument from 4 arguments to 5 arguments, so the z0 wiil be appeared in the workspace window and can be used for prediction computation.
from this ==> function [nameu,fu,tidecon,xout]=t_tide(xin,varargin)
to this ==> function [nameu,fu,tidecon,xout,z0]=t_tide(xin,varargin)
The Way 1 script become:
[NAME, FREQ, TIDECON, XOUT_raw, MSL_val] = t_tide(obs_value, 'interval', 1, 'start', data_time(1), 'latitude', -3.66295, 'synthesis', 1);
XOUT = XOUT_raw + MSL_val;
The prediction XOUT now at the same mean value with the obs data. But, I still couldn't found the way for the function that using 2 arguments [TIDESTRUC,XOUT]=T_TIDE(XIN) in Way 2. When I modified it to 3 arguments by added the z0 variable as the 3rd argument the result is the same result of tidecon in the Way 1.
William Rose
William Rose on 4 Jan 2026 at 21:28
It sounds like you have solved your problem with your Way 1 method.
I am not sure why your Way 2 method is not giving you the results you want.
I do not like to modify function t_tide(), because it I did not write t_tide(), so I do not fully understand it, and it interacts with other functions that are part of the t_tide package. Any changes I make could have unexpected effects. Therefore I do not modify t_tide(). I do want the predictions to look like the data, so I compute the mean of the good data points (excluding NaNs) and I add the mean to xout when plotting the results. The example below, which is runnable in thre Matlab Answers window, shows this. Scroll to bottom to see the plot of measured and predicted tide data.
load('data2share');
% Vector elev is from satellite data, and includes NaNs.
% tInterval is the time between satellite passes.
%% Identify missing points
gd=find(~isnan(elev)); % indices of good points
%% Compute mean
meanElev=mean(elev(gd)); % mean of observations
Next: analyze the data.
% Analyze tide components
unzip('tide3') % reference data used by t_tide
[tidestruc,xout]=t_tide(elev,...
'interval',tInterval, ... % sampling interval
'error','wboot'); % CI from white noise model
number of standard constituents used: 68 Points used: 277 of 280. percent of var residual after lsqfit/var original: 44.00 % Phases at central time Using nonlinear bootstrapped error estimates Generating prediction without nodal corrections, SNR is 2.000000 percent of var residual after synthesis/var original: 70.30 % ----------------------------------- date: 04-Jan-2026 nobs = 280, ngood = 277, record length (days) = 2776.38 rayleigh criterion = 1.0 Phases at central time x0= -7.55, x trend= 0 var(x)= 0.18876 var(xp)= 0.068388 var(xres)= 0.1327 percent var predicted/var original= 36.2 % tidal amplitude and phase with 95% CI estimates tide freq amp amp_err pha pha_err snr *SA 0.0001141 0.3660 0.048 271.49 7.53 59 SSA 0.0002282 0.0319 0.045 135.94 87.23 0.51 MSM 0.0013098 0.0634 0.046 22.45 44.41 1.9 *MM 0.0015122 0.0830 0.052 254.43 28.73 2.6 MSF 0.0028219 0.0319 0.037 149.33 88.55 0.72 MF 0.0030501 0.0521 0.046 213.28 56.41 1.3 *ALP1 0.0343966 0.0898 0.047 217.08 32.20 3.6 2Q1 0.0357064 0.0249 0.035 89.68 87.66 0.52 SIG1 0.0359087 0.0586 0.043 322.33 52.24 1.8 *Q1 0.0372185 0.0842 0.049 106.74 33.37 2.9 *RHO1 0.0374209 0.0615 0.043 86.51 45.56 2 O1 0.0387307 0.0402 0.042 263.60 70.89 0.94 *TAU1 0.0389588 0.0758 0.049 1.45 36.87 2.4 *BET1 0.0400404 1.6590 0.050 182.22 1.66 1.1e+03 NO1 0.0402686 0.0362 0.038 238.86 75.31 0.9 *CHI1 0.0404710 0.8147 0.046 26.76 2.82 3.2e+02 PI1 0.0414385 0.0112 0.038 122.76 169.73 0.087 *P1 0.0415526 1.9927 0.045 348.48 1.36 2e+03 S1 0.0416667 0.0483 0.044 102.07 54.76 1.2 K1 0.0417807 0.0646 0.047 355.38 45.78 1.9 *PSI1 0.0418948 0.1604 0.051 89.28 17.41 9.9 PHI1 0.0420089 0.0658 0.051 39.27 44.76 1.7 THE1 0.0430905 0.0437 0.044 136.50 65.97 1 J1 0.0432929 0.0384 0.043 87.30 75.89 0.79 SO1 0.0446027 0.0471 0.044 227.46 56.43 1.2 OO1 0.0448308 0.0480 0.044 347.13 59.22 1.2 *UPS1 0.0463430 0.2755 0.043 107.41 9.79 41 OQ2 0.0759749 0.0071 0.034 75.49 201.44 0.043 EPS2 0.0761773 0.0560 0.047 21.28 48.21 1.4 2N2 0.0774871 0.0442 0.040 222.65 59.76 1.2 MU2 0.0776895 0.0580 0.044 89.16 44.10 1.8 *N2 0.0789992 0.0802 0.042 358.04 35.22 3.6 *NU2 0.0792016 0.1147 0.042 184.14 23.38 7.4 *GAM2 0.0803090 2.0321 0.046 190.40 1.44 1.9e+03 ALP2 0.0803973 0.0353 0.041 175.06 73.31 0.73 *M2 0.0805114 0.1229 0.049 243.11 21.14 6.2 BET2 0.0806255 0.0530 0.042 346.67 44.96 1.6 MKS2 0.0807396 0.0409 0.044 174.98 68.62 0.88 *LDA2 0.0818212 1.6516 0.049 356.29 1.63 1.1e+03 *L2 0.0820236 0.0764 0.043 121.30 39.60 3.2 T2 0.0832193 0.0333 0.040 90.91 85.56 0.7 *S2 0.0833333 0.0834 0.043 300.44 32.90 3.8 *R2 0.0834474 0.1088 0.045 260.93 25.60 5.9 K2 0.0835615 0.0692 0.054 62.98 39.12 1.7 MSN2 0.0848455 0.0263 0.037 124.23 96.56 0.51 ETA2 0.0850736 0.0375 0.044 197.33 81.17 0.74 MO3 0.1192421 0.0328 0.040 159.09 85.07 0.68 M3 0.1207671 0.0437 0.040 37.78 62.98 1.2 SO3 0.1220640 0.0425 0.042 116.12 57.80 1 MK3 0.1222921 0.0477 0.045 198.79 48.40 1.1 SK3 0.1251141 0.0582 0.043 136.45 48.56 1.8 MN4 0.1595106 0.0568 0.053 217.32 42.47 1.1 M4 0.1610228 0.0297 0.030 271.71 81.48 0.95 *SN4 0.1623326 0.8247 0.052 204.25 3.40 2.6e+02 MS4 0.1638447 0.0578 0.052 221.41 44.01 1.2 MK4 0.1640729 0.0524 0.043 318.21 53.00 1.5 S4 0.1666667 0.0288 0.039 120.59 81.82 0.55 SK4 0.1668948 0.0561 0.047 280.52 49.21 1.4 *2MK5 0.2028035 0.0731 0.047 111.51 39.85 2.4 2SK5 0.2084474 0.0341 0.040 334.61 77.63 0.74 2MN6 0.2400221 0.0436 0.044 87.43 57.97 0.99 *M6 0.2415342 0.0683 0.043 104.37 42.06 2.5 *2MS6 0.2443561 0.0830 0.044 16.30 32.01 3.5 2MK6 0.2445843 0.0337 0.037 210.47 84.14 0.82 2SM6 0.2471781 0.0390 0.046 33.38 71.91 0.7 MSK6 0.2474062 0.0300 0.044 166.65 77.45 0.47 3MK7 0.2833149 0.0528 0.043 304.40 49.01 1.5 M8 0.3220456 0.0586 0.046 317.17 47.34 1.6
Next: Plot results. Add meanElev to xout when plotting.
% Plot elevation versus time, observed & fitted & residuals
figure;
plot(obsTime,elev,'-b.',...
obsTime,xout+meanElev,'rx', obsTime,elev-(xout+meanElev),'-g+')
title([filename,': Elevation versus Time'],'Interp','none')
xlabel('Date'); ylabel('Elevation (m)')
grid on; legend('Observed','Fitted','Residual')

Sign in to comment.

Categories

Find more on Get Started with Curve Fitting Toolbox in Help Center and File Exchange

Tags

Products


Release

R2016a

Community Treasure Hunt

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

Start Hunting!