% database_structure.m
%
%modified t.montez@vumc.nl, 050502.
%modified k.linkenkaer@nih.knaw.nl, 041005.
%
%******************************************************************************************************************
% Purpose...
%
% General structure for a script that automatically finds the data you have and computes whatever needed for all subjects!
%
%******************************************************************************************************************

addpath E:\Matlab\fieldtrip-0.9.6

ResultsDir = 'E:\Results\';
G_datapnb_avg_comb_pow_AD  = [];
G_datapnb_avg_comb_pow_C   = [];

ParentDir = 'E:\Data\';		        % Defines the location of your database
DataDir =  'AD_Project\Patients';	% Defines the database directory to be analyzed.

cd([ParentDir DataDir])								
d = dir(pwd);

%******************************************************************************************************************
% Run through the 'd' data directories (i.e., one folder for each subject).

for k=3:19		          % Skip=2, the first 2 positions of the struct array 'd' are '.' and '..' and thus not data.	
  pwd
  cd([ParentDir DataDir])			
  cd([d(k).name])					% go to directory specific to subject d(k).
  load data
  
  % power spectrum of the averaged trials
  
  cfg=[];
  cfg.channel = {'all','-MLT21','-MLT22' , '-MLC42', '-MLO43', '-MZO02' , '-MLT41' , '-MZP01' , '-MRT35'}
  cfg.repair  = 'yes'        
  cfg.realign = 'no'         
  cfg.planar  = 'yes' 
  data_planar_nobad=meginterpolate(cfg,data);
  
  datapnb_avg=timelockanalysis (cfg, data_planar_nobad);
  clear data_planar_nobad 

  datapnb_avg_comb=combineplanar(cfg,datapnb_avg);
  clear datapnb_avg 
  
  cfg.method = 'mtmfft' ;
  
  cfg.foilim         = [0.5; 100];
  cfg.tapsmofrq      = 10;
  
  cfg.keeptrials  = 'no';  % average
  cfg.keeptapers  = 'no'; 
  
  cfg.pad         = 2; 
  
  datapnb_avg_comb_pow_AD=freqanalysis(cfg, datapnb_avg_comb);
  clear datapnb_avg_comb
  
 
 
  G_datapnb_avg_comb_pow_AD  = [G_datapnb_avg_comb_pow_AD  datapnb_avg_comb_pow_AD ];
  
 
    if rem(k,1)==0		    % Saving the results...
      k
      clear cfg
      cd([ResultsDir])
      save datapnb_avg_comb_pow_AD 
      clear datapnb_avg_comb_pow_AD
    end      
  
end


%******************************************************************************************************************

ParentDir = 'E:\Data\';		        % Defines the location of your database
DataDir =  'AD_Project\Controls';	% Defines the database directory to be analyzed.

cd([ParentDir DataDir])								
d = dir(pwd);

%******************************************************************************************************************
% Run through the 'd' data directories (i.e., one folder for each subject).

for k=3:20                 		     % the first 2 positions of the struct array 'd' are '.' and '..' and thus not data.	
  pwd
  cd([ParentDir DataDir])			
  cd([d(k).name])					% go to directory specific to subject d(k).
  load data						
  
  % power spectrum of the averaged trials
    
    cfg=[];
    cfg.channel = {'all','-MLT21','-MLT22' , '-MLC42', '-MLO43', '-MZO02' , '-MLT41' , '-MZP01' , '-MRT35'}
    cfg.repair  = 'yes'        
    cfg.realign = 'no'         
    cfg.planar  = 'yes' 
    data_planar_nobad=meginterpolate(cfg,data);
    
    datapnb_avg=timelockanalysis (cfg, data_planar_nobad);
    clear data_planar_nobad 
  
    datapnb_avg_comb=combineplanar(cfg,datapnb_avg);
    clear datapnb_avg 
    
    cfg.method = 'mtmfft' ;
    
    cfg.foilim         = [0.5; 100];
    cfg.tapsmofrq      = 10;
    
    cfg.keeptrials  = 'no';  % average
    cfg.keeptapers  = 'no'; 
    
    cfg.pad         = 2; 
    
    datapnb_avg_comb_pow_C=freqanalysis(cfg, datapnb_avg_comb);
    clear datapnb_avg_comb
    
    G_datapnb_avg_comb_pow_C  = [G_datapnb_avg_comb_pow_C  datapnb_avg_comb_pow_C ];
  
 
    if rem(k,1)==0		    % Saving the results...
      k
      clear cfg
      cd([ResultsDir])
      save datapnb_avg_comb_pow_C 
      clear datapnb_avg_comb_pow_C
    end      
  
end




