clear

MgGreenTotal = 250
plot_length = 2001
data_number =  111

    
% Including GENESIS plot files to MATLAB
MgGreen = zeros(plot_length, data_number);

for param_number = 1:96
for ratio_flag    = 1:12
    if ratio_flag == 1
        ratio = '2';
    elseif ratio_flag == 2
        ratio = '5';
    elseif ratio_flag == 3
        ratio = '10';
    elseif ratio_flag == 4
        ratio = '20';
    elseif ratio_flag == 5
        ratio = '50';
    elseif ratio_flag == 6
        ratio = '100';
    elseif ratio_flag == 7
        ratio = '0.5';
    elseif ratio_flag == 8
        ratio = '0.2';
    elseif ratio_flag == 9
        ratio = '0.1';
    elseif ratio_flag == 10
        ratio = '0.05';
    elseif ratio_flag == 11
        ratio = '0.02';
    elseif ratio_flag == 12
        ratio = '0.01';
    end

for data_index = 0:(data_number-1)
file_number = int2str(data_index * 10 - 500);
filefullname = strcat('paramstwF_param', int2str(param_number), '_ratio', ratio, '_CFat' ,file_number, 'msec.plot');
transient = load(filefullname);
transient(:,1) = [];
MgGreen(:, data_index + 1)=transient; % put file into transients
end


% Calc total integrate Ca2+ response
for t = 0:(plot_length-1)    
    % Determine from_time
    if t < 500
        from_time(t+1) = t;
    else from_time(t+1) = 500;
    end
    
    % Determine to_time
    if t < 540
        to_time(t+1) = 540 + 500;
    else to_time(t+1) = t + 500;
    end
end

% Convert [MgGreen] to dF/F0
    % Find F0
    F0 = min(MgGreen(1:501,51))/MgGreenTotal + 1
    F  = MgGreen/MgGreenTotal + 1;
    dF_rate = (F - F0) * 100;

% calc the time window function 'G'
G = zeros(data_number,1);
for data_index = 0:(data_number-1)
    duration = data_index * 10;
    for t=from_time(duration + 1):to_time(duration + 1)
        G(data_index + 1) = G(data_index + 1) + dF_rate(t+1, data_index + 1);
    end
    G(data_index + 1) = G(data_index + 1) / (to_time(duration + 1) - from_time(duration + 1) );
end

savefilename = strcat('paramstw_param', int2str(param_number), '_ratio', ratio, '.txt')
save(savefilename, 'G', '-ASCII')

end
end
