% More general batch processing program, better with functions
% 2022.05.30 This particular modified for NSx_CohPSD

clear all, close all;

%% Input Parameters
para.DataSource=1; % 1:BlackRock

para.ChSig1=1;  
para.ChRef1=9;
para.ChSig2=2;
para.ChRef2=9;
para.timeframe=20; 
para.CohSec=1;   
para.BW=0.00;

para.Timecut=0; % trim file
para.TrimStartTime=0; 
para.TrimStopTime=0; 
para.Timeshift=0; % in sec, shift (ChSig2-ChRef2), can be negative

para.Filter=1; % if =1, apply filter
para.ChannelNotFilter=[137]; % assigned channel those do not apply filter
para.Lowcut=0.3;
para.Highcut=250;
para.targetfile=".ns4";
%save inputpara.mat

%%
directories = readtextbyline("filterbatch_dir.txt");

%filenames = readtextbyline("filterbatch_name4.txt");

%% Main %%%%%%%%%%%%%%%%%%%%%%
for i=1:1:length(directories)
    close all
    %load inputpara.mat
    path=cell2mat(directories(i,1));
    cd(path)
    a=dir;
    b=extractfield(a,'name');
    filename=cell2mat(b(find(~cellfun('isempty',strfind(b,para.targetfile)))));
    F_NSx_CohPSD(path,filename,para)
    clearvars -except para i directories
    
    
end