function [y] = lfhf2(eta,flower,fupper,dt)
%% lfhf - split surface elevation time series into a high/low frequency signal
% 
% syntax: [eta_lf eta_hf] = lfhf(eta,nsplit)
% 
% input:    - eta = surface elevation time series
%           - fsplit = frequency to split between high and low frequency
%             usually at fp/2
%           - fm = measurement frequency
% 
% processing: this function calculates the fast fourier transform of the 
% detrended surface elevation signal and splits the signal in the frequency
% domain and calculates the inverse fft to obtain the high and low
% frequency time series
%
% output:   low and high frequency time series eta_lf and eta_hf    
%
n = length(eta); % length of timeseries
% dt = 1/fm; % bepaal dt uit tijdserie
T = n*dt; % duur meting
df = 1/T; % stap grootte frequentie as
f = df*[0:1:round(n/2) -1*floor(n/2)+1:1:-1]; % maak frequentie as op basis van gegegvens tijdserie hierboven

% nsplit=find(abs(fsplit-f)<=df);
nflower = find(abs(flower-f)<=df);
nfupper = find(abs(fupper-f)<=df);

etaf = fft(eta)/n; % perform FFT

% Low frequency - long waves
set = zeros(1,length(etaf));    % create filter with zeros and ones
set(nflower+1:nfupper-1) = 1;            %
% set(n-nfupper+3:n) = 1;
set(n-nfupper+3:n-nflower+1) = 1;%
etaf_lf = etaf.*set';           % filter spectrum 
y = real(ifft(etaf_lf))*n; % transform back to time series

% % High frequency - short waves
% set = abs(set-1); set(1)=0;
% etaf_hf = etaf.*set';
% eta_hf = real(ifft(etaf_hf))*n;


end