
% testing file for lambda computation...
% from Readme file
% 
% input parameters:
%   x: input data
%   m: embedding dimension 
%   L: delay time
%   k: number of iterations
%   w: minimum interval, related to the decorrelation time. Please refer to pages 245, 249-250 of 
%      the book "Multiscale analysis of complex time series --- Integration of Chaos and Random 
%      Fractal Theory, and Beyond" for more details 
%   max_shell: maximum shell 2^(-max_shell)       
%   num_of_shell: number of shells
% 
% output results stored in the variale named "output_result", it has the following format:
%   the first row stores the number of pairs of vectors within each shell
%   the rest rows store the information of Lambda(k) curves
%   each column corresponds to one shell used for computing Time Depedent Exponent (TDE) curves.
% 

% from lambda.m:
%function output_result = lambda(x, m, L, k, w, max_shell, num_of_shell);
%
% input parameters:
%x: input data
%m: embedding dimension 
%L: delay time
%k: number of iterations
%w: minimum interval, related to the decorrelation time. Please refer to

%x=dlmread('cadusd_close.txt');
x=dlmread('dax.txt');
%x=x(1:250);
m=3; %embedding dimension 
L=1; %delay time
k=100; %number of iterations
w=(m-1)*L; 
% w - minimum interval, related to the decorrelation time. Please refer topages 245, 249-250 of 
%      the book "Multiscale analysis of complex time series --- Integration of Chaos and Random 
%      Fractal Theory, and Beyond" for more details 
max_shell=13; %max_shell: maximum shell 2^(-max_shell)      
num_of_shell=125; %number of shells
max_orig_scale=max_shell;
n=length(x);
window=1000;
masminlambda=[];
masmaxlambda=[];
masmine=[];
masmaxe=[];
beginScale=1;
endScale=256;
Scalesmesh_lambda_width=[];
Scalesmesh_e_width=[];
for i=1:1:10%n-window
    i
    n-window
    xfragment=x(i:i+window);
    %output_result = lambda(xfragment, m, L, k, w, max_shell, num_of_shell);
    %figure;
    %plot_psic(output_result);
    %max_orig_scale=10;%???
    %figure;
    %[minlambda,maxlambda,mine,maxe]=plot_sdle(output_result,max_orig_scale,0);
    [lambda_scales,e_scales]=SLDE_coarse_graining(xfragment, m, L, k, w, max_shell, num_of_shell,beginScale,endScale);
    Scalesmesh_lambda_width=[Scalesmesh_lambda_width;lambda_scales(2,:)];
    Scalesmesh_e_width=[Scalesmesh_e_width;e_scales(2,:)];
    %masminlambda=[masminlambda,minlambda];
    %masmaxlambda=[masmaxlambda,maxlambda];
    %masmine=[masmine,mine];
    %masmaxe=[masmaxe,maxe];
end

figure;
mesh(Scalesmesh_lambda_width);

figure;
mesh(Scalesmesh_e_width);
% figure;
% plot(masminlambda);
% hold on;
% plot(masmaxlambda,'r');
% legend('min(\lambda)','max(\lambda)');
% figure;
% plot(masmaxlambda-masminlambda);
% legend('max(\lambda)-min(\lambda)');
% 
% figure;
% plot(masmine);
% hold on;
% plot(masmaxe,'r');
% legend('min(\epsilon)','max(\epsilon)');
% figure;
% plot(masmaxe-masmine);
% legend('max(\epsilon)-min(\epsilon)');


save('dax_w1000_s1_doublescale.mat');