function cfilt = lowpass_first_order(c,dt,tau)
% Function for low-pass filtering time series c with a transfer function
% describing a linear, first-order sensor
% INPUT:  c = time series
%         dt = temporal resolution of data in time series (s)
%         tau = first-order response time characterising the filter
% OUTPUT: cfilt = filtered time series%
%
% Used in Peltola et al. (2021): "The high frequency response correction 
% of eddy covariance fluxes. Part 1: an experimental approach and its 
% interdependence with the time-lag estimation" submitted to Atmospheric 
% Measurement Techniques.
%
% O. Peltola (09/2020)
% olli.peltola@fmi.fi


    
    fs = 1/dt;
    % creating frequency vector
    freq = fs/2.*linspace(0,1,1+length(c)/2);
    freq = freq(:);
    nf = size(freq,1);
    
    % Fast Fourier transform
    c2 = fft(c-mean(c));
    c3 = zeros(size(c2));
    % filtering the data
    c3(1:nf) = c2(1:nf).*1./(1+complex(0,1)*2*pi*freq.*tau);

    % inverse Fourier transform
    chat = ifft(c3,'symmetric');

    % output
    cfilt = real(chat)+mean(c);
    
end

