clear

G0max = 7.7327
G0mean = 113.6853
G0halfwidth = 170.4372
G = load('twparams_all.txt');

interval = -500:10:990;

for parami = 1:1152
G_min(parami) = min(G(1:111,parami));
G_floor(1:111,parami) = G(1:111,parami) - G_min(parami);
G_area(parami) = sum(G_floor(1:111,parami));
G_norm(1:111,parami) = G_floor(1:111,parami) ./ G_area(parami);
sum(G_norm(1:111,parami))
end

for parami = 1:1152
    G_mean(parami) = 0;
    for timing = 1:111
        G_mean(parami) = interval(timing) * G_norm(timing,parami) + G_mean(parami);
    end

    G_var(parami) = 0;
    for timing = 1:111
        G_var(parami) = (interval(timing) - G_mean(parami))*(interval(timing) - G_mean(parami)) * G_norm(timing,parami) + G_var(parami);
    end    
    G_halfwidth(parami) = sqrt(2 * log(2)) * sqrt(G_var(parami));
    G_max(parami) = G_area(parami) * 10 ./ sqrt(2 * pi * G_var(parami)) + G_min(parami);
end


Gmax = zeros(96, 12);
Gmean = zeros(96, 12);
Ghalfwidth = zeros(96, 12);
for param = 1:96
    for parami = 1:12
       Gmean(param, parami) = G_mean((param-1)*12 + parami); 
       Ghalfwidth(param, parami) = G_halfwidth((param-1)*12 + parami); 
       Gmax(param, parami) = G_max((param-1)*12 + parami); 
   end
end

save('peakime.txt', 'Gmean', '-ASCII')
save('halfwidth.txt', 'Ghalfwidth', '-ASCII')
save('peakamp.txt', 'Gmax', '-ASCII')