function [ RFt, rms, nwl ] = decon_waterlevel_matlab_v3_trueamp( numer, denom, tshift, dt, waterlevel, gaussa )


% calculate frequency
NT = length(numer);
Fs = 1/dt;
% fny = Fs/2;      % nyquist
df = Fs/NT;
Q = ceil((NT+1)/2);
freq = df*(0:1:Q-1);
w = 2*pi*[freq,-fliplr(freq(2:NT-Q+1))];
w = reshape(w,[],1);

numer = reshape(numer,[],1);
denom = reshape(denom,[],1);
numerf = fft( numer );
denomf = fft( denom );

% denominator
Df = denomf.* conj(denomf);
dmax = max( real( Df ) );

% add water level correction
phi1 = waterlevel*dmax; % water level
nwl = length( find(Df<phi1) ); % number corrected
% Df( real(Df)<phi1 ) = phi1;
Df = Df + phi1;

% numerator
Nf = numerf .* conj(denomf);

% compute RF
RFf = Nf ./ Df;
rff_nofilter_hyy = abs(RFf);
% compute predicted numerator
pnumerf = RFf.*denomf;

% add gauss filter
gfilter = exp(-w.*w/(4*gaussa*gaussa))/dt*sqrt(pi)/gaussa;
RFf = RFf .* gfilter;
rff_filter_hyy = abs(RFf);
% add phase shift
RFf = RFf .* exp(-1i*w*tshift);
RFt = (1+waterlevel)*real(ifft(RFf));
% RFt = real(ifft(RFf));
% %% temp plot by hyy
% % figure
% fontsize = 32;
% tickfontsize = 28;
% t = (0:NT-1)*dt;
% figa = plot(t, RFt, 'LineWidth', 2);
% set(gca, 'Fontsize', tickfontsize)
% xlabel('Time (sec)', 'FontSize', tickfontsize)
% ylabel('Amplitude', 'FontSize', tickfontsize)
% title('RFs calculated by different ways', 'FontSize', fontsize)
% grid on 
% box on

% compute the fit
pnumer=real(ifft(pnumerf));
rms = sum( (pnumer - numer).^2 )/sum(numer.^2);

% %% temp plot by hyy
% nf_hyy = abs(Nf);
% df_hyy = abs(Df);
% t = (0:NT-1)*dt;
% fontsize = 22;
% tickfontsize = 18;
% smooth_scale = 5;
% figure
% xi1 = linspace(min(freq), max(freq), numel(freq)*smooth_scale);
% yi1 = interp1(freq, nf_hyy(1:Q), xi1, 'pchip'); 
% semilogx(xi1, yi1, 'k', 'LineWidth', 2)
% hold on
% xi2 = linspace(min(freq), max(freq), numel(freq)*smooth_scale);
% yi2 = interp1(freq, df_hyy(1:Q), xi2, 'pchip'); 
% semilogx(xi2, yi2, 'r', 'LineWidth', 1)
% legend('Nf','Df')
% set(gca, 'Fontsize', tickfontsize)
% title(strcat('Nf and Df (waterlevel = ',num2str(waterlevel),')'), 'FontSize', fontsize);
% ylabel('Amplitude', 'FontSize', tickfontsize);
% xlabel('frequency (Hz)', 'FontSize', tickfontsize)
% xlim([freq(2) freq(end)]);
% grid on
% box on
% 
% figure
% subplot(3,1,1)
% xi3 = linspace(min(freq), max(freq), numel(freq)*smooth_scale);
% yi3 = interp1(freq, rff_nofilter_hyy(1:Q), xi3, 'pchip'); 
% semilogx(xi3, yi3, 'k', 'LineWidth', 2);
% set(gca, 'Fontsize', tickfontsize)
% title(strcat('RFf (waterlevel = ', num2str(waterlevel), ')'), 'FontSize', fontsize);
% ylabel('Amplitude', 'FontSize', tickfontsize);
% xlabel('Frequency (Hz)', 'FontSize', tickfontsize)
% xlim([freq(2) freq(end)]);
% grid on
% box on
% 
% subplot(3,1,2)
% xi4 = linspace(min(freq), max(freq), numel(freq)*smooth_scale);
% yi4 = interp1(freq, rff_filter_hyy(1:Q), xi4, 'pchip'); 
% semilogx(xi4, yi4, 'k', 'LineWidth', 2);
% set(gca, 'Fontsize', tickfontsize)
% title(strcat('RFf (With guass filter(factor = ', num2str(gaussa), '), waterlevel = ', num2str(waterlevel), ')'), 'FontSize', fontsize);
% ylabel('Amplitude', 'FontSize', tickfontsize);
% xlabel('Frequency (Hz)', 'FontSize', tickfontsize)
% xlim([freq(2) freq(end)]);
% grid on
% box on
% 
% subplot(3,1,3)
% % single_wig7(t, RFt, 'k', 'LineWidth', 2);
% single_wig7(t, RFt, 0,'blue',[0.82745 0.82745 0.82745],'yes', 'yes')
% set(gca, 'Fontsize', tickfontsize)
% title(strcat('RFt (waterlevel = ', num2str(waterlevel), ')'), 'FontSize', fontsize);
% ylabel('Amplitude', 'FontSize', tickfontsize);
% xlabel('Time (sec)', 'FontSize', tickfontsize)
% axis tight
% grid on
% box on
% qqq =1;
end
