function [y,coef]=lanczos(x,fp,dt)
%%Lanczos filter
% input
% fp = filter period in hours (e.g. 24)
% dt = sampling interval from observations in minutes (e.g. 60)
% x = vector with raw data to be filtered
% output
% y = vector with filtered data (filtered x)
% coef = filter coefficients

n=length(x) ;% number of observations

ns=fix(fp*60/dt);
om=2*pi/(2*ns+1); %+2% cut-off frequency
ns2=2*ns+1; % twice cut-off period
h0=om/pi; % cutoff freq over pi
con=2*pi/(ns2); %ns2+1
su=h0;
h=zeros(ns+1);
for i=1:ns+1 
  h(i)=h0*sin(i*om)/(i*om)*sin(i*con)/(i*con) ;
  su=su+2*h(i)  ;
end

hh=zeros(ns2);
nn=round(ns2/2);
hh(nn)=h0/su;
h2=h(:,1);
for i=1:ns-1
    hh(nn+i)=h2(i)/su;
    hh(nn-i)=h2(i)/su;
end

coef=hh(:,1);
y=convn(x,coef,'same');