%
fid = fopen('SNparam_bLCGT.dat','w');
lcgt=load('data1/LCGT/bLCGT_v1.dat');

% read a directory containing Supernova signal data
%%for test
%%load signal_data/signal_e15a_ls.dat;
%%sig = signal_e15a_ls;
LIST=dir('data1/signal_data/*.dat');

[fnum tmpval]=size(LIST);
for i = 1:fnum
sig = load(fullfile('data1/signal_data',LIST(i).name));

% sampling period. sampling rate
fs = 16384; % sampling frequency[Hz]
dt = 1/fs;
Vt = 1/fs*[0:sig(end,1)*1e-3*fs]; % 1 second data[s]
Vsig = interp1(sig(:,1)*1e-3,sig(:,2),Vt,'linear');

% calculate root-sum-square of SN signals
hrss = sqrt(sum(Vsig.^2).*dt);

% resampled SN signal and making 1 second data
Vt = 1/fs*[0:fs-1];
Vsig = [zeros(1,ceil(fs/2)) zeros(1,ceil((fs-length(Vsig))/2)) Vsig zeros(1,(fs-length(Vsig))-ceil((fs-length(Vsig))/2)) zeros(1,ceil(fs/2))];


%length of data
len = length(Vsig);
len2 = ceil((len-1)/2);

%frequency vector
Vfre = fs*1/len*[1:len2]; 
df = Vfre(3)-Vfre(2);

% LCGT ASP [1/rHz] vector corresponding to Vfre
Vlcgt = interp1(lcgt(3:end,1),lcgt(3:end,2),Vfre(3:end),'linear');
Vlcgt = [Vlcgt(1) Vlcgt(1) Vlcgt];

% SN signal vector in frequency domain
fftVsig = fft(Vsig);
Vhf = fftVsig(1:ceil(length(fftVsig)/2)+1);
Vhf = Vhf(2:end);

% the (detector dependent) integrated characteristic signal frequency
fc = sum(Vhf*dt.*conj(Vhf*dt)./Vlcgt.^2.*Vfre)*df./(sum(Vhf*dt.*conj(Vhf*dt)./Vlcgt.^2)*df);


% write parameters in the file
fprintf(fid,'%7.2f %e \n',fc,hrss);

end
fclose(fid);


param = load ('SNparam_bLCGT.dat');

% plot figure;
loglog(Vfre,Vlcgt,'b');
hold on;
plot(param(:,1),param(:,2),'r*');
hold off;
xlabel('frequency[Hz]');
ylabel('amplitude spectral density[Hz^{-1/2}]');
grid on;
axis([1 16384 1.0E-24 1.0E-19]);
print ('-depsc','SNparam_bLCGT.eps');





