% Computational Neuroscience II: Foundations of Neural Coding, SS 2003
% Solutions for Assignment 3 (08.05.03) 
% (c) written by Christian Leibold and Roland Schaette

% The name of this script is 'blatt3_octave.m'.It can be executed by typing
% 'blatt3_octave' (without the suffix .m) at an octave prompt.

clear; % clear all variables from memory
       % type "help clear" for more info

% ------------------Functions
% s1=load('spiketrain1.dat');             % 'matlab' load
load 'spiketrain1.dat'; s1 = spiketrain1; % 'octave' load
rate1 = overallrate(s1);

% s2=load('spiketrain2.dat');             % 'matlab' load
load 'spiketrain2.dat'; s2 = spiketrain2; % 'octave' load
rate2 = overallrate(s2);

[mean1, var1, cv1] = statistics(s1);
[mean2, var2, cv2] = statistics(s2);

printf("Spike train 1:\n   rate=%f/ms\n   mean_isi=%fms\n \
  variance=%f(ms)^2\n   CV=%f\n",rate1,mean1,var1,cv1); 

printf("Spike train 2:\n   rate=%f/ms\n   mean_isi=%fms\n \
  variance=%f(ms)^2\n   CV=%f\n",rate2,mean2,var2,cv2); 

% ------------------ spiketrain autocorrelation

printf("\nSpike train autocorrelations: \n");

[Q,Q_norm,s] = autocorr(s1,1,0,100);
% [Q,Q_norm,s] = autocorr(s1,5, -1000,1000);

figure(1);

title('Autocorrelogram spiketrain 1')
subplot(2,1,1)
bar(s,Q);


title('Autocorrelogram spiketrain 1 (normalized)')
xlabel('Time difference [ms]');
ylabel('Correlation');
subplot(2,1,2)
bar(s,Q_norm)


[Q,Q_norm,s] = autocorr(s2,5,0,2000);
%[Q,Q_norm,s] = autocorr(s2,5,-5000,5000); % will take a while

figure(2);

title('Autocorrelogram spiketrain 2')
subplot(2,1,1)
bar(s,Q);

title('Autocorrelogram spiketrain 2 (normalized)')
xlabel('Time difference [ms]');
ylabel('Correlation');
subplot(2,1,2)
bar(s,Q_norm)






