[x_butter_ann, x_butter_bia, x_butter_mos, x_lp_ann, x_lp_mos, x_prolate_ann, x_prolate_bia, x_prolate_mos, x_rmn_ann, x_rmn_bia, x_rmn_mos] = make_filtered_series (x, xdate, 
%
% assumes that you are passing a column vector of data
%
% also best if there are no NaNs in the data.
%
% Names are assuming the data input is daily values.
%


x = nao(:);
x_date = nao_date(:);

%
% Make a temporary variable that flips on the edges of that series
% so that no points are lost.
%
temp=[flipud(x); x; flipud(x);];


%
% Butterworth filtering
% wn=cycles/user unit
%
[m,n]=size(x);
%
% First a monthly filter
%
[a,b,temp_filt]=butter_filter(temp,100,3,0.5);
x_butter_mos=temp_filt(m+1:m+m);
%
% Then an annual filter
%
[a,b,temp_filt]=butter_filter(temp,350,3,0.08333);
x_butter_ann=temp_filt(m+1:m+m);
%
% Then a biannual filter
%
[a,b,temp_filt]=butter_filter(temp,700,3,0.04166);
x_butter_bia=temp_filt(m+1:m+m);

%
% Check filtered versions of the array
%
check_corr(x_butter_mos, x,['Butter Monthly vs. original'])
check_corr(x_butter_ann, x,['Butter Annual vs. original'])
check_corr(x_butter_bia, x,['Butter Biannual vs. original'])


%
% Plot all the Butterworth Filtered series together
%
figure
  plot(x_date, x);
  hold on;
  datetick
  h=plot(x_date, x_butter_mos,'r');
  set(h,'LineWidth',3);
  h=plot(x_date, x_butter_ann,'g');
  set(h,'LineWidth',3);
  h=plot(x_date, x_butter_bia,'m');
  set(h,'LineWidth',3);
  xlabel('Time, years');
  ylabel('Original Data units')  ;
  title('Butterworth filtering');
  legend('Orig','Mos','Ann','Bi-Ann');


%
% Now make some running means
%
[m,n]=size(x);
temp_filt = rmn(temp,3,1);
x_rmn_mos = temp_filt(m+1:m+m);

temp_filt = rmn(temp,12,1);
x_rmn_ann = temp_filt(m+1:m+m);

temp_filt = rmn(temp,24,1);
x_rmn_bia = temp_filt(m+1:m+m);

%
% Check filtered versions of the array
%
check_corr(x_rmn_mos,x,['Running Mean Monthly vs. original'])
check_corr(x_rmn_ann,x,['Running Mean Annual vs. original'])
check_corr(x_rmn_bia,x,['Running Mean Biannual vs. original'])

%
% Plot all the running mean series together
%
figure
  plot(x_date, x);
  hold on;
  datetick
  h=plot(x_date,x_rmn_mos,'r');
  set(h,'LineWidth',3);
  h=plot(x_date,x_rmn_ann,'g');
  set(h,'LineWidth',3);
  h=plot(x_date,x_rmn_bia,'m');
  set(h,'LineWidth',3);
  xlabel('Time, years');
  ylabel('Original Data units')  ;
  title('Running Mean filtering');
  legend('Orig','Mos','Ann','Bi-Ann');


%
% Now use a prolate filter
%
[m,n]=size(x);
temp_filt = prolate_filt(temp,5,1);
x_prolate_mos = temp_filt(m+1:m+m);

temp_filt = prolate_filt(temp,13,1);
x_prolate_ann = temp_filt(m+1:m+m);

temp_filt = prolate_filt(temp,25,1);
x_prolate_bia = temp_filt(m+1:m+m);

%
% Check filtered versions of the array
%
check_corr(x_prolate_mos,x,['Prolate Monthly vs. original'])
check_corr(x_prolate_ann,x,['Prolate Annual vs. original'])
check_corr(x_prolate_bia,x,['Prolate Biannual vs. original'])

%
% Plot all the prolate series together
%
figure
  plot(x_date,x);
  hold on;
  datetick
  h=plot(x_date,x_prolate_mos,'r');
  set(h,'LineWidth',3);
  h=plot(x_date,x_prolate_ann,'g');
  set(h,'LineWidth',3);
  h=plot(x_date,x_prolate_bia,'m');
  set(h,'LineWidth',3);
  xlabel('Time, years');
  ylabel('Original Data units')  ;
  title('Prolate filtering');
  legend('Orig','Mos','Ann','Bi-Ann');



%
% Try Dennis's Lancho filter
%
[m,n]=size(x);
temp_filt = lpfilt(temp,5,1);

x_lp_mos = temp_filt(m+1:m+m);

temp_filt = lpfilt(temp,18,1);
x_lp_ann = temp_filt(m+1:m+m);

%
% Check filtered versions of the array
%
check_corr(x_lp_mos, x,['Lancho Monthly vs. original'])
check_corr(x_lp_ann, x,['Lancho Annual vs. original'])

%
% Plot all the Lancho series together
%
figure
  plot(x_date, x);
  hold on;
  datetick
  h=plot(x_date, x_lp_mos,'r');
  set(h,'LineWidth',3);
  h=plot(x_date, x_lp_ann,'g');
  set(h,'LineWidth',3);
  xlabel('Time, years');
  ylabel('Original Data units')  ;
  title('Lancho filtering');
  legend('Orig','Mos','Ann');

clear ans
clear h a b
clear m n temp temp_filt


