added all my stuff

This commit is contained in:
2014-11-12 18:39:02 +01:00
parent 0fdcf2f82e
commit 350ee7ca2b
160 changed files with 5869 additions and 253 deletions

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@@ -0,0 +1,52 @@
% some vectors:
x = [ -1:0.02:1 ];
y = x*0.5 + 0.1*randn( size(x) );
plot( x, y, '.b' );
hold on
plot( x(10), y(10), '.r' );
% new coordinate system:
%e1 = [ 3/5 4/5 ];
%e2 = [ -4/5 3/5 ];
e1 = [ 3 4 ];
e2 = [ -4 3 ];
me1 = sqrt( e1*e1' );
e1 = e1/me1;
me2 = sqrt( e2*e2' );
e2 = e2/me2;
quiver( 0.0, 0.0 , e1(1), e1(2), 1.0, 'r' )
quiver( 0.0, 0.0 , e2(1), e2(2), 1.0, 'g' )
axis( 'equal' )
% project [x y] onto e1 and e2:
% % the long way:
% nx = zeros( size( x ) ); % new x coordinate
% ny = zeros( size( y ) ); % new y coordinates
% for k=1:length(x)
% xvec = [ x(k) y(k) ];
% nx(k) = xvec * e1';
% ny(k) = xvec * e2';
% end
% plot( nx, ny, '.g' );
% the short way:
%nx = [ x' y' ] * e1';
%nx = [x; y]' * e1';
% nx = e1 * [ x; y ];
% ny = e2 * [ x; y ];
% plot( nx, ny, '.g' );
% even shorter:
n = [e1; e2 ] * [ x; y ];
plot( n(1,:), n(2,:), '.g' );
hold off

View File

@@ -0,0 +1,37 @@
% some vectors:
x=[-1:0.02:1]'; % column vector!
y=4*x/3 + 0.1*randn( size( x ) );
plot( x, y, '.b' );
axis( 'equal' );
hold on;
% new coordinate system:
% e1 = [ 3 4 ]
% e2 = [ -4 3 ]
% and normalized to unit length (factor 1/5):
e = [ 3/5 -4/5; 4/5 3/5 ];
quiver( [0 0], [0 0], e(1,:), e(2,:), 1.0, 'r' );
% project [x y] onto e1 and e2:
% % the long way:
% nx = x;
% ny = y;
% for k=1:length(x)
% nx(k) = [x(k) y(k)]*e(:,1);
% ny(k) = [x(k) y(k)]*e(:,2);
% end
% plot( nx, ny, '.g' );
% the short way
% nx = [x y]*e(:,1);
% ny = [x y]*e(:,2);
% plot( nx, ny, '.g' );
% even shorter:
n = [x y]*e;
plot( n(:,1), n(:,2), '.g' );
xlabel( 'x' );
ylabel( 'y' );
hold off;

View File

@@ -0,0 +1,29 @@
set( 0, 'DefaultTextFontSize', 22.0 );
set( 0, 'DefaultAxesFontSize', 22.0 );
n = 10000;
x = 1.0*randn( n, 1 );
z = 1.0*randn( n, 1 );
for r=-1:0.2:1
y = r*x + sqrt(1.0-r^2.0)*z;
cv = cov( x, y );
cr = corrcoef( x, y );
scatter( x, y, 'filled', 'MarkerEdgeColor', 'white' );
xlabel( 'x' );
ylabel( 'y' );
text( 0.05, 0.9, sprintf( 'r = %.2f', r ), 'Units', 'normalized' )
text( 0.05, 0.8, sprintf( 'cor = %.2f %.2f', cr(1,1), cr(1,2) ), 'Units', 'normalized' )
text( 0.05, 0.7, sprintf( 'cov = %.2f %.2f', cv(1,1), cv(1,2) ), 'Units', 'normalized' )
waitforbuttonpress;
end
y = x.^2.0 + 0.5*z;
cv = cov( x, y );
cr = corrcoef( x, y );
scatter( x, y, 'filled', 'MarkerEdgeColor', 'white' );
xlabel( 'x' );
ylabel( 'y' );
text( 0.05, 0.9, sprintf( 'x^2', r ), 'Units', 'normalized' )
text( 0.05, 0.8, sprintf( 'cor = %.2f %.2f', cr(1,1), cr(1,2) ), 'Units', 'normalized' )
text( 0.05, 0.7, sprintf( 'cov = %.2f %.2f', cv(1,1), cv(1,2) ), 'Units', 'normalized' )

View File

@@ -0,0 +1,91 @@
function covareigen( x, y, pm, w, h )
% computes covariance matrix from the pairs x, y
% diagonalizes covariance matrix
% x and y are column vectors
% pm: 0 - scatter of data, 1 histogram, 2 multivariate gauus from
% covariance matrix, 1 and 2 require w and h
% w and h are the width and the height (in data units) for plotting
% histograms instead of scatter
% covariance matrix:
cv = cov( [ x y ] );
% eigen values:
[ v , d] = eig( cv )
s = sign( v );
s(1,:) = s(2,:);
v = v .* s;
% plots:
subplot( 2, 2, 1 );
hold on;
if (pm > 0) & (nargin == 5)
if pm == 1
% histogram of data:
xp = -w:0.5:w;
yp = -h:0.5:h;
[n,c] = hist3([x y], { xp, yp } );
contourf( c{1}, c{2}, n' );
else
xp = -w:0.1:w;
yp = -h:0.1:h;
[xg,yg] = meshgrid( xp, yp );
xy = [ xg(:) yg(:) ];
gauss = reshape( exp(-0.5*diag(xy*inv(cv)*xy'))/sqrt((2.0*pi)^2.0*det(cv)), size( xg ) );
contourf( xp, yp, gauss )
end
colormap( 'gray' );
else
% scatter plot:
scatter( x, y, 'b', 'filled', 'MarkerEdgeColor', 'white' );
end
% plot eigenvectors:
quiver( ones( 1, 2 ).*mean( x ), ones( 1, 2 )*mean(y), v(1,:).*sqrt(diag(d))', v(2,:).*sqrt(diag(d))', 'r', 'LineWidth', 3, 'AutoScale', 'off', 'AutoScaleFactor', 1.0, 'MaxHeadSize', 0.7 )
xlabel( 'x' );
ylabel( 'y' );
axis( 'equal' );
hold off;
% histogram of x values:
subplot( 2, 2, 3 );
hist( x, 50, 'b' );
xlabel( 'x' );
ylabel( 'n_x' );
% sort the eigenvalues:
[d,inx] = sort( diag(d), 'descend' );
subplot( 2, 2, 2 );
hold on;
% subtract means:
x = x - mean( x );
y = y - mean( y );
% project onto eigenvectors:
nc = [ x y ] * v(:,inx);
cv = cov( nc )
[ v , d] = eig( cv )
if (pm > 0) & (nargin == 5)
if pm == 1
[n,c] = hist3( nc, { xp, yp } );
contourf( c{1}, c{2}, n' );
else
gauss = reshape( exp(-0.5*diag(xy*inv(cv)*xy'))/sqrt((2.0*pi)^2.0*det(cv)), size( xg ) );
contourf( xp, yp, gauss )
end
else
scatter( nc(:,1), nc(:,2), 'b', 'filled', 'MarkerEdgeColor', 'white' );
end
xlabel( 'x' );
ylabel( 'y' );
axis( 'equal' );
hold off;
% histogram of new x values:
subplot( 2, 2, 4 );
hist( nc(:,1), 50, 'b' );
xlabel( 'new x' );
ylabel( 'n_newx' );
end

View File

@@ -0,0 +1,60 @@
function covareigen3( x, y, z )
% computes covariance matrix from the triples x, y, z
% diagonalizes covariance matrix
% x, y and z are column vectors
% covariance matrix:
cv = cov( [ x y z ] );
% eigen values:
[ v , d] = eig( cv )
% plots:
subplot( 1, 2, 1 );
hold on;
% scatter plot:
view( 3 );
scatter3( x, y, z, 0.1, 'b', 'filled', 'MarkerEdgeColor', 'blue' );
xlabel( 'x' );
ylabel( 'y' );
zlabel( 'z' );
grid on;
% plot eigenvectors:
quiver3( ones( 1, 3 ).*mean( x ), ones( 1, 3 )*mean(y), ones( 1, 3 )*mean(z), v(1,:).*sqrt(diag(d))', v(2,:).*sqrt(diag(d))', v(3,:).*sqrt(diag(d))', 'r', 'LineWidth', 3, 'AutoScale', 'off', 'AutoScaleFactor', 1.0, 'MaxHeadSize', 0.7 )
%axis( 'equal' );
hold off;
% 2D scatter plots:
subplot( 2, 6, 4 );
scatter( x, y, 'b', 'filled', 'MarkerEdgeColor', 'white' );
xlabel( 'x' )
ylabel( 'y' )
subplot( 2, 6, 5 );
scatter( x, z, 'b', 'filled', 'MarkerEdgeColor', 'white' );
xlabel( 'x' )
ylabel( 'z' )
subplot( 2, 6, 6 );
scatter( y, z, 'b', 'filled', 'MarkerEdgeColor', 'white' );
xlabel( 'y' )
ylabel( 'z' )
% sort the eigenvalues:
[d,inx] = sort( diag(d), 'descend' );
subplot( 2, 2, 4 );
hold on;
% subtract means:
x = x - mean( x );
y = y - mean( y );
% project onto eigenvectors:
nx = [ x y z ] * v(:,inx);
scatter( nx(:,1), nx(:,2), 'b', 'filled', 'MarkerEdgeColor', 'white' );
xlabel( 'ex' )
ylabel( 'ey' )
axis( 'equal' );
hold off;
end

View File

@@ -0,0 +1,19 @@
scrsz = get( 0, 'ScreenSize' );
set( 0, 'DefaultFigurePosition', [ scrsz(3)/2 scrsz(4)/2 scrsz(3)/2 scrsz(4)/2 ] );
n = 10000;
% three distributions:
x = randn( n, 1 );
y = randn( n, 1 );
z = randn( n, 1 );
dx = [ 0 8 0 0 ];
dy = [ 0 0 8 0 ];
dz = [ 0 0 0 8 ];
for k = 1:4
x((k-1)*n/4+1:k*n/4) = x((k-1)*n/4+1:k*n/4) + dx(k);
y((k-1)*n/4+1:k*n/4) = y((k-1)*n/4+1:k*n/4) + dy(k);
z((k-1)*n/4+1:k*n/4) = z((k-1)*n/4+1:k*n/4) + dz(k);
end
f = figure( 1 );
covareigen3( x, y, z );

View File

@@ -0,0 +1,35 @@
scrsz = get( 0, 'ScreenSize' );
set( 0, 'DefaultFigurePosition', [ scrsz(3)/2 scrsz(4)/2 scrsz(3)/2 scrsz(4)/2 ] );
% correlation coefficients:
n = 10000;
x = randn( n, 1 );
f = figure( 1 );
for r = 0.01:0.19:1
fprintf( 'Correlation = %g\n', r );
clf( f );
y = r*x + sqrt(1-r^2)*randn( n, 1 );
covareigen( x, y, 0, 5.0, 3.0 );
key = waitforbuttonpress;
end
return
% two distributions:
n = 10000;
x1 = randn( n/2, 1 );
y1 = randn( n/2, 1 );
x2 = randn( n/2, 1 );
y2 = randn( n/2, 1 );
f = figure( 1 );
pause( 'on' );
for d = 0:1:5
fprintf( 'Distance = %g\n', d );
clf( f );
d2 = d / sqrt( 2.0 );
x = [ x1; x2 ];
y = [ y1+d2; y2-d2 ];
scrsz = get(0,'ScreenSize');
covareigen( x, y, 0, 10.0, 7.0 );
%key = waitforbuttonpress;
pause( 1.0 );
end

Binary file not shown.

View File

@@ -0,0 +1,54 @@
% this script simulates extracellularly recorded
% spike waveforms and saves them in extdata.mat.
% generate spikes:
n = 1000;
misi = 0.01;
isis = exprnd( misi, n, 1 );
isis = isis + 0.01;
spikes = cumsum( isis );
p = rand( size( spikes ) );
% spike waveforms:
dt = 0.0001;
waveformt = -0.01:dt:0.01;
waveform1 = 2.0*exp( -(waveformt-0.0003).^2/2.0/0.0005^2 ) - 1.4*exp( -(waveformt-0.0005).^2/2.0/0.0005^2 );
waveform2 = exp( -waveformt.^2/2.0/0.002^2 ).*cos(2.0*pi*waveformt/0.005);
p12 = 0.5;
% voltage trace:
noise = 0.2;
time = 0:dt:spikes(end)+3.0*waveformt(end);
voltage = noise*randn( 1, length( time ) );
for k = 1:length( spikes )
inx = ceil( spikes(k)/dt );
if p(k) < p12
voltage(inx:inx+length(waveformt)-1) = voltage(inx:inx+length(waveformt)-1) + waveform1;
else
voltage(inx:inx+length(waveformt)-1) = voltage(inx:inx+length(waveformt)-1) + waveform2;
end
end
figure( 1 );
clf;
plot( time(time<1.0), voltage(time<1.0) );
hold on;
% find peaks:
thresh = 0.7;
inx = find( voltage(2:end-1) > thresh & voltage(1:end-2)<voltage(2:end-1) & voltage(2:end-1) > voltage(3:end) ) + 1;
spiketimes = time(inx);
spikevoltage = voltage(inx);
tinx = inx;
for k=1:2
inx = find( ( spiketimes(2:end-1)-spiketimes(1:end-2)>0.005 | spikevoltage(2:end-1) > spikevoltage(1:end-2) ) & ( spiketimes(3:end)-spiketimes(2:end-1)>0.005 | spikevoltage(2:end-1) > spikevoltage(3:end) ) )+1;
spiketimes = spiketimes(inx);
spikevoltage = spikevoltage(inx);
tinx = tinx(inx);
end
scatter( spiketimes(spiketimes<1.0), spikevoltage(spiketimes<1.0), 100.0, 'r', 'filled' );
%scatter( t(tinx), v(tinx), 100.0, 'r', 'filled' );
hold off;
% save data to file:
save( 'extdata', 'time', 'voltage', 'spiketimes', 'waveformt', 'waveform1', 'waveform2' );

View File

@@ -0,0 +1,25 @@
% Gaussian density from histogram:
x = randn( 1000000, 1 );
[ n, c ] = hist( x, 100 );
n = n/sum(n)/(c(2)-c(1));
bar( c, n );
hold on;
% equation p(x):
xx = -5:0.01:5;
p=exp(-xx.^2/2.0)/sqrt(2.0*pi);
plot( xx, p, 'r', 'LineWidth', 3 )
% with mean=2 and sigma=0.5:
mu = 2.0;
sig = 0.5;
x = sig*x + mu;
[ n, c ] = hist( x, 100 );
n = n/sum(n)/(c(2)-c(1));
bar( c, n );
% equation:
p=exp(-(xx-mu).^2/2.0/sig^2)/sqrt(2.0*pi*sig^2);
plot( xx, p, 'r', 'LineWidth', 3 )
hold off;
xlabel( 'x' );
ylabel( 'p(x)' );
title( 'Gaussian distribution' );

View File

@@ -0,0 +1,21 @@
% Visualize some common matrix transformations
% in a 2D coordinate system
disp( 'Click into the plot to advance to the next matrix' )
matrixbox( [ 1 0; 0 1], 'Identity' );
matrixbox( [ 2 0; 0 1], 'Scale x' );
matrixbox( [ 1 0; 0 2], 'Scale y' );
matrixbox( [ 2 0; 0 2], 'Scale x and y' );
matrixbox( [ 0.5 0; 0 0.5], 'Scale x and y' );
matrixbox( [ -1 0; 0 1], 'Flip x' );
matrixbox( [ 1 0; 0 -1], 'Flip y' );
matrixbox( [ -1 0; 0 -1], 'Flip both' );
matrixbox( [ 1 2; 0 1], 'Shear x' );
matrixbox( [ 1 0; 2 1], 'Shear y' );
matrixbox( [ 0.5 1; 1 0.5], 'Something A' );
matrixbox( [ 0.5 1; 1.2 0.6], 'Something B' );
% rotation matrices:
for deg = 0:10:360
phi = deg*pi/180.0;
matrixbox( [ cos(phi) -sin(phi); sin(phi) cos(phi)], sprintf( 'Rotate %.0f', deg ) );
end

View File

@@ -0,0 +1,45 @@
function matrixbox( m, s )
% visualizes the effect of a matrix m on a set of vectors forming a box
% m: a 2x2 matrix
% s: a string plotted as the title of the plot
% this function is called by matrix2dtrafos.m
% the 5 vectors that point into the cornes of a unit square:
v = [ 0 1 1 0 0;
0 0 1 1 0 ];
% transform v by means of m into new vector w:
w = m*v;
clf;
hold on;
% axis:
plot( [-2 2], [0 0], 'k', 'LineWidth', 1 );
plot( [0 0], [-2 2], 'k', 'LineWidth', 1 );
% old vectors:
plot( v(1,:), v(2,:), 'k', 'LineWidth', 1.0 )
quiver( v(1,1), v(2,1), v(1,3), v(2,3), 1.0, 'k', 'LineWidth', 2.0 );
scatter( v(1,2), v(2,2), 60.0, 'filled', 'k' );
% transformed vectors:
plot( w(1,:), w(2,:), 'b', 'LineWidth', 2.0 )
quiver( w(1,1), w(2,1), w(1,3), w(2,3), 1.0, 'b', 'LineWidth', 3.0 );
scatter( w(1,2), w(2,2), 100.0, 'filled', 'b' );
% eigenvectors:
[ev ed] = eig(m);
n = ev*ed;
if isreal( n )
quiver( [0 0], [0 0], n(1,:), n(2,:), 1.0, 'r', 'LineWidth', 2.0 );
text( 0.1, 0.2, sprintf( '\\lambda_1 = %.3g', ed(1,1) ), 'Units', 'normalized' )
text( 0.1, 0.1, sprintf( '\\lambda_2 = %.3g', ed(2,2) ), 'Units', 'normalized' )
end
hold off;
xlim( [ -2 2 ] );
ylim( [ -2 2 ] );
axis( 'equal' );
m( abs(m) < 1e-3 ) = 0.0; % make zeros really a zero
text( 0.7, 0.15, 'A =', 'Units', 'normalized' )
text( 0.8, 0.2, sprintf( '%.3g', m(1,1) ), 'Units', 'normalized' )
text( 0.9, 0.2, sprintf( '%.3g', m(1,2) ), 'Units', 'normalized' )
text( 0.8, 0.1, sprintf( '%.3g', m(2,1) ), 'Units', 'normalized' )
text( 0.9, 0.1, sprintf( '%.3g', m(2,2) ), 'Units', 'normalized' )
title( s );
waitforbuttonpress;
end

View File

@@ -0,0 +1,85 @@
% open a file to write in the LaTeX commands:
f = fopen( 'matrices.tex','w');
fprintf( f, '\\documentclass{exam}\n' );
fprintf( f, '\\usepackage{amsmath}\n' );
fprintf( f, '\\begin{document}\n' );
for k = 1:40
% compute row and column numbers:
pd = rand( 1, 1 );
if pd < 0.05
% row vector multiplication that might not be possible:
am = 1;
an = randi( [ 2 4 ]);
bm = randi( [ 2 4 ]);
bn = 1;
elseif pd < 0.1
% column vector multiplication that might not be possible:
am = randi( [ 2 4 ]);
an = 1;
bm = 1;
bn = randi( [ 2 4 ]);
elseif pd < 0.4
% row vector multiplication that is possible:
am = 1;
an = randi( [ 2 4 ]);
bm = an;
bn = 1;
elseif pd < 0.5
% column vector multiplication that is possible:
am = randi( [ 2 4 ]);
an = 1;
bm = 1;
bn = am;
elseif pd < 0.6
% matrix multiplication that might not be possible:
am = randi( [ 2 4 ]);
an = randi( [ 2 4 ]);
bm = randi( [ 2 4 ]);
bn = randi( [ 2 4 ]);
else
% matrix multiplication that is possible:
am = randi( [ 2 4 ]);
an = randi( [ 2 4 ]);
bm = an;
bn = randi( [ 2 4 ]);
end
% generate the matrices:
a = randi( [-4 4], am, an );
b = randi( [-4 4], bm, bn );
% write them out as LaTeX code:
% matrix a:
fprintf( f, ' \\[ \\begin{pmatrix}' );
for r = 1:size( a, 1 )
for c = 1:size( a, 2 )
if c > 1
fprintf( f, ' &' );
end
fprintf( f, ' %d', a(r,c) );
end
if r < size( a, 1 )
fprintf( f, ' \\\\' );
end
end
fprintf( f, ' \\end{pmatrix} \\cdot\n' );
% matrix b:
fprintf( f, ' \\begin{pmatrix}' );
for r = 1:size( b, 1 )
for c = 1:size( b, 2 )
if c > 1
fprintf( f, ' &' );
end
fprintf( f, ' %d', b(r,c) );
end
if r < size( b, 1 )
fprintf( f, ' \\\\' );
end
end
fprintf( f, ' \\end{pmatrix} = \\]\n\n' );
end
% close the document and the file:
fprintf( f, '\\end{document}\n' );
fclose( f );

Binary file not shown.

Binary file not shown.

View File

@@ -0,0 +1,95 @@
function pca2d( x, y, pm, w, h )
% computes covariance matrix from the pairs x, y
% diagonalizes covariance matrix, i.e. performs a PCA on [ x, y ]
% x and y are column vectors
% pm: 0 - scatter of data, 1 - histogram, 2 - multivariate gauss from
% covariance matrix, 1 and 2 require w and h
% w and h are the width and the height (in data units) for plotting
% histograms instead of scatter
% covariance matrix:
cv = cov( [ x y ] );
% eigen values:
[ v , d] = eig( cv )
s = sign( v );
s(1,:) = s(2,:);
v = v .* s;
% plots:
subplot( 2, 2, 1 );
hold on;
if (pm > 0) & (nargin == 5)
if pm == 1
% histogram of data:
xp = -w:0.5:w;
yp = -h:0.5:h;
[n,c] = hist3([x y], { xp, yp } );
contourf( c{1}, c{2}, n' );
else
% bivariate Gaussian:
xp = -w:0.1:w;
yp = -h:0.1:h;
[xg,yg] = meshgrid( xp, yp );
xy = [ xg(:) yg(:) ];
gauss = reshape( exp(-0.5*diag(xy*inv(cv)*xy'))/sqrt((2.0*pi)^2.0*det(cv)), size( xg ) );
contourf( xp, yp, gauss )
end
colormap( 'gray' );
else
% scatter plot:
scatter( x, y, 'b', 'filled', 'MarkerEdgeColor', 'white' );
end
% plot eigenvectors:
quiver( ones( 1, 2 ).*mean( x ), ones( 1, 2 )*mean(y), v(1,:).*sqrt(diag(d))', v(2,:).*sqrt(diag(d))', 'r', 'LineWidth', 3, 'AutoScale', 'off', 'AutoScaleFactor', 1.0, 'MaxHeadSize', 0.7 )
xlabel( 'x' );
ylabel( 'y' );
axis( 'equal' );
hold off;
% histogram of x values:
subplot( 2, 2, 3 );
hist( x, 50, 'b' );
xlabel( 'x' );
ylabel( 'count' );
% sort the eigenvalues:
[d,inx] = sort( diag(d), 'descend' );
subplot( 2, 2, 2 );
hold on;
% subtract means:
x = x - mean( x );
y = y - mean( y );
% project onto eigenvectors:
nc = [ x y ] * v(:,inx);
cv = cov( nc )
[ v , d] = eig( cv )
if (pm > 0) & (nargin == 5)
if pm == 1
% histogram of data:
[n,c] = hist3( nc, { xp, yp } );
contourf( c{1}, c{2}, n' );
else
% bivariate Gaussian:
gauss = reshape( exp(-0.5*diag(xy*inv(cv)*xy'))/sqrt((2.0*pi)^2.0*det(cv)), size( xg ) );
contourf( xp, yp, gauss )
end
else
% scatter plot:
scatter( nc(:,1), nc(:,2), 'b', 'filled', 'MarkerEdgeColor', 'white' );
end
xlabel( 'n_x' );
ylabel( 'n_y' );
axis( 'equal' );
hold off;
% histogram of new x values:
subplot( 2, 2, 4 );
hist( nc(:,1), 50, 'b' );
xlabel( 'n_x' );
ylabel( 'count' );
end

View File

@@ -0,0 +1,33 @@
scrsz = get( 0, 'ScreenSize' );
set( 0, 'DefaultFigurePosition', [ scrsz(3)/2 scrsz(4)/2 scrsz(3)/2 scrsz(4)/2 ] );
% correlation coefficients:
n = 10000;
x = randn( n, 1 );
f = figure( 1 );
for r = 0.01:0.19:1
fprintf( 'Correlation = %g\n', r );
clf( f );
y = r*x + sqrt(1-r^2)*randn( n, 1 );
pca2d( x, y, 0, 5.0, 3.0 );
waitforbuttonpress;
end
% two distributions:
n = 10000;
x1 = randn( n/2, 1 );
y1 = randn( n/2, 1 );
x2 = randn( n/2, 1 );
y2 = randn( n/2, 1 );
f = figure( 1 );
pause( 'on' );
for d = 0:1:5
fprintf( 'Distance = %g\n', d );
clf( f );
d2 = d / sqrt( 2.0 );
x = [ x1; x2 ];
y = [ y1+d2; y2-d2 ];
scrsz = get(0,'ScreenSize');
pca2d( x, y, 0, 10.0, 7.0 );
waitforbuttonpress;
end

View File

@@ -0,0 +1,60 @@
function pca3d( x, y, z )
% computes covariance matrix from the triples x, y, z
% diagonalizes covariance matrix, i.e. performs pca on [ x, y, z ]
% x, y and z are column vectors
% covariance matrix:
cv = cov( [ x y z ] );
% eigen values:
[ v , d] = eig( cv )
% plots:
subplot( 1, 2, 1 );
hold on;
% scatter plot:
view( 3 );
scatter3( x, y, z, 0.1, 'b', 'filled', 'MarkerEdgeColor', 'blue' );
xlabel( 'x' );
ylabel( 'y' );
zlabel( 'z' );
grid on;
% plot eigenvectors:
quiver3( ones( 1, 3 ).*mean( x ), ones( 1, 3 )*mean(y), ones( 1, 3 )*mean(z), v(1,:).*sqrt(diag(d))', v(2,:).*sqrt(diag(d))', v(3,:).*sqrt(diag(d))', 'r', 'LineWidth', 3, 'AutoScale', 'off', 'AutoScaleFactor', 1.0, 'MaxHeadSize', 0.7 )
%axis( 'equal' );
hold off;
% 2D scatter plots:
subplot( 2, 6, 4 );
scatter( x, y, 'b', 'filled', 'MarkerEdgeColor', 'white' );
xlabel( 'x' )
ylabel( 'y' )
subplot( 2, 6, 5 );
scatter( x, z, 'b', 'filled', 'MarkerEdgeColor', 'white' );
xlabel( 'x' )
ylabel( 'z' )
subplot( 2, 6, 6 );
scatter( y, z, 'b', 'filled', 'MarkerEdgeColor', 'white' );
xlabel( 'y' )
ylabel( 'z' )
% sort the eigenvalues:
[d,inx] = sort( diag(d), 'descend' );
subplot( 2, 2, 4 );
hold on;
% subtract means:
x = x - mean( x );
y = y - mean( y );
% project onto eigenvectors:
nx = [ x y z ] * v(:,inx);
scatter( nx(:,1), nx(:,2), 'b', 'filled', 'MarkerEdgeColor', 'white' );
xlabel( 'ex' )
ylabel( 'ey' )
axis( 'equal' );
hold off;
end

View File

@@ -0,0 +1,19 @@
scrsz = get( 0, 'ScreenSize' );
set( 0, 'DefaultFigurePosition', [ scrsz(3)/2 scrsz(4)/2 scrsz(3)/2 scrsz(4)/2 ] );
n = 10000;
% three distributions:
x = randn( n, 1 );
y = randn( n, 1 );
z = randn( n, 1 );
dx = [ 0 8 0 0 ];
dy = [ 0 0 8 0 ];
dz = [ 0 0 0 8 ];
for k = 1:4
x((k-1)*n/4+1:k*n/4) = x((k-1)*n/4+1:k*n/4) + dx(k);
y((k-1)*n/4+1:k*n/4) = y((k-1)*n/4+1:k*n/4) + dy(k);
z((k-1)*n/4+1:k*n/4) = z((k-1)*n/4+1:k*n/4) + dz(k);
end
f = figure( 1 );
pca3d( x, y, z );

View File

@@ -0,0 +1,22 @@
function plotvector( v, s, sp )
% plot an arrow for the 2D-vector v
% v: the 2D vector
% s: a string passed to quiver for the color
% sp: if this string is set it defines the color of some helper lines
if nargin < 2
s = 'b';
end
quiver( [0.0], [0.0], [v(1)], [v(2)], 1.0, s );
grid on;
if nargin == 3
hh = ishold;
if ~hh
hold on;
end
plot( [0.0 v(1) v(1)], [0.0 0.0 v(2)], sp );
if ~hh
hold off;
end
end
end

View File

@@ -0,0 +1,24 @@
function plotvector3( v, s, sp )
% plot an arrow for the 3D-vector v
% v: the 3D vector
% s: a string passed to quiver for the color
% sp: if this string is set it defines the color of some helper lines
if nargin < 2
s = 'b';
end
quiver3( [0.0], [0.0], [0.0], [v(1)], [v(2)], [v(3)], 1.0, s );
view( 42, 12 );
grid on
if nargin == 3
hh = ishold;
if ~hh
hold on;
end
plot3( [v(1) v(1)], [v(2) v(2)], [0.0 v(3)], sp );
plot3( [0.0 v(1) v(1)], [v(2) v(2) 0.0], [0.0 0.0 0.0], sp );
if ~hh
hold off;
end
end
end

View File

@@ -0,0 +1,21 @@
% Visualize some common matrix transformations
% in a 2D coordinate system
% this uses the simplematrixbox() function for visualization
% use the matrix2dtrafos script for the the nicer matrixbox() function.
disp( 'Click into the plot to advance to the next matrix' )
simplematrixbox( [ 1 0; 0 1], 'Identity' );
simplematrixbox( [ 2 0; 0 1], 'Scale x' );
simplematrixbox( [ 1 0; 0 2], 'Scale y' );
simplematrixbox( [ 2 0; 0 2], 'Scale x and y' );
simplematrixbox( [ 0.5 0; 0 0.5], 'Scale x and y' );
simplematrixbox( [ -1 0; 0 1], 'Flip x' );
simplematrixbox( [ 1 0; 0 -1], 'Flip y' );
simplematrixbox( [ -1 0; 0 -1], 'Flip both' );
simplematrixbox( [ 1 1; 0 1], 'Shear x' );
simplematrixbox( [ 1 0; 1 1], 'Shear y' );
% rotation matrices:
for deg = 0:10:360
phi = deg*pi/180.0;
simplematrixbox( [ cos(phi) -sin(phi); sin(phi) cos(phi)], sprintf( 'Rotate %.0f', deg ) );
end

View File

@@ -0,0 +1,21 @@
function simplematrixbox( a, s )
% visualizes the effect of a matrix a on a set of vectors forming a box
% a: a 2x2 matrix
% s: a string plotted as the title of the plot
% this function is called by simplematrix2dtrafos.m
% this is a simple version of the more elaborate matrixbox() function.
x = [ 0 1 1 0 0 1; 0 0 1 1 0 1 ];
plot( x(1,:), x(2,:), '-b' );
hold on;
y = a*x;
plot( y(1,:), y(2,:), '-r' );
[ ev ed ] = eig( a );
ev = ev * ed;
quiver( [0 0], [0 0], ev(1,:), ev(2,:), 1.0, 'g' )
hold off;
xlim( [-2 2 ] )
ylim( [-2 2] )
title( s );
waitforbuttonpress;
end

View File

@@ -0,0 +1,72 @@
% read in wav files:
[s1, fs1 ] = audioread( 'PeterUndDerWolf.wav' );
[s2, fs2 ] = audioread( 'Tielli-Kraulis.wav' );
[s3, fs3 ] = audioread( 'Chorthippus_biguttulus.wav' );
% take out left channel and minimum number of samples:
n = min( [ size( s1, 1 ), size( s2, 1 ), size( s3, 1 ) ] );
%n = 300000;
x1 = s1(1:n,1)';
x2 = s2(1:n,1)';
x3 = s3(1:n,1)';
clear s1 s2 s3;
% plot the sound waves:
% dt = 1/fs1;
% time = 0:dt:(length(x1)-1)*dt;
% plot( time, x1 );
% plot( time, x2 );
% plot( time, x3 );
% play original sounds:
% disp( 'Playing x1 (Peter und der Wolf)' )
% a = audioplayer( x1, fs1 );
% playblocking( a );
% disp( 'Playing x2 (Martin Tielli)' )
% a = audioplayer( x2, fs2 );
% playblocking( a );
% disp( 'Playing x3 (Grasshopper song)' )
% a = audioplayer( x3, fs3 );
% playblocking( a );
% mix them:
m = [ 1.0 0.3 0.4; 0.1 0.6 0.4; 0.7 0.1 0.7 ];
y = m*[ x1; x2; x3 ];
% add noise:
%y = y + 0.03*randn(size(y));
% play mixtures:
% disp( 'Playing mixture y1' )
% a = audioplayer( y(1,:), fs1 );
% playblocking( a );
% disp( 'Playing mixture y2' )
% a = audioplayer( y(2,:), fs2 );
% playblocking( a );
% disp( 'Playing mixture y3' )
% a = audioplayer( y(3,:), fs3 );
% playblocking( a );
% demix them:
disp( 'Inverse mixing matrix:' )
dm = inv(m)
dx = dm*y;
% estimate demixing matrix:
size( y )
cv = cov( y' );
[ ev ed ] = eig( cv );
disp( 'Estimated demixing matrix:' )
ev
%play mixtures:
% disp( 'Playing x1 (Peter und der Wolf) recovered from mixtures' )
% a = audioplayer( dx(1,:), fs1 );
% playblocking( a );
% disp( 'Playing x3 (Martin Tielli) recovered from mixtures' )
% a = audioplayer( dx(2,:), fs2 );
% playblocking( a );
% disp( 'Playing x3 (Grasshopper song) recovered from mixtures' )
% a = audioplayer( dx(3,:), fs3 );
% playblocking( a );

View File

@@ -0,0 +1,118 @@
% load data into time, voltage and spiketimes
load( 'extdata' );
% indices into voltage trace of spike times:
dt = time( 2) - time(1);
tinx = round(spiketimes/dt)+1;
% plot voltage trace with dettected spikes:
figure( 1 );
clf;
plot( time, voltage, '-b' )
hold on
scatter( time(tinx), voltage(tinx), 'r', 'filled' );
xlabel( 'time [ms]' );
ylabel( 'voltage' );
hold off
% spike waveform snippets:
w = ceil( 0.005/dt );
vs = [];
for k=1:length(tinx)
vs = [ vs; voltage(tinx(k)-w:tinx(k)+w-1) ];
end
ts = time(1:size(vs,2));
ts = ts - ts(floor(length(ts)/2));
% % plot snippets:
figure( 2 );
clf;
hold on
for k=1:size(vs,1)
plot( ts, vs(k,:), '-b' );
end
xlabel( 'time [ms]' );
ylabel( 'voltage' );
hold off
% pca:
cv = cov( vs );
[ ev , ed ] = eig( cv );
[d,dinx] = sort( diag(ed), 'descend' );
figure( 3 );
clf;
subplot( 4, 2, 1 );
imagesc( cv );
xlabel( 'time bin' );
ylabel( 'time bin' );
title( 'covariance matrix' );
caxis([-0.1 0.1])
% spectrum of eigenvalues:
subplot( 4, 2, 2 );
scatter( 1:length(d), d, 'b', 'filled' );
xlabel( 'index' );
ylabel( 'eigenvalue' );
% features:
subplot( 4, 2, 5 );
plot( 1000.0*ts, ev(:,dinx(1)), 'r', 'LineWidth', 2 );
xlabel( 'time [ms]' );
ylabel( 'eigenvector 1' );
subplot( 4, 2, 6 );
plot( 1000.0*ts, ev(:,dinx(2)), 'g', 'LineWidth', 2 );
xlabel( 'time [ms]' );
ylabel( 'eigenvector 2' );
% project onto eigenvectors:
nx = vs * ev(:,dinx(1));
ny = vs * ev(:,dinx(2));
%scatter( nx, ny, 'b', 'filled', 'MarkerEdgeColor', 'white' );
% clustering (two clusters):
%kx = kmeans( [ nx, ny ], 2 );
% nx smaller or greater a threshold:
kthresh = 1.6;
kx = ones( size( nx ) );
kx(nx<kthresh) = 2;
subplot( 4, 1, 2 );
scatter( nx(kx==1), ny(kx==1), 'r', 'filled', 'MarkerEdgeColor', 'white' );
hold on;
scatter( nx(kx==2), ny(kx==2), 'g', 'filled', 'MarkerEdgeColor', 'white' );
hold off;
xlabel( 'projection onto eigenvector 1' );
ylabel( 'projection onto eigenvector 2' );
% show sorted spike waveforms:
subplot( 4, 2, 7 );
hold on
kinx1 = find(kx==1);
for k=1:length(kinx1)
plot( 1000.0*ts, vs(kinx1(k),:), '-r' );
end
plot( 1000.0*waveformt, waveform2, '-k', 'LineWidth', 2 );
xlim( [ 1000.0*ts(1) 1000.0*ts(end) ] )
xlabel( 'time [ms]' );
ylabel( 'waveform 1' );
hold off
subplot( 4, 2, 8 );
hold on
kinx2 = find(kx==2);
for k=1:length(kinx2)
plot( 1000.0*ts, vs(kinx2(k),:), '-g' );
end
plot( 1000.0*waveformt, waveform1, '-k', 'LineWidth', 2 );
xlim( [ 1000.0*ts(1) 1000.0*ts(end) ] )
xlabel( 'time [ms]' );
ylabel( 'waveform 2' );
hold off
% spike trains:
figure( 1 );
hold on;
scatter( time(tinx(kinx1)), voltage(tinx(kinx1)), 100.0, 'r', 'filled' );
scatter( time(tinx(kinx2)), voltage(tinx(kinx2)), 100.0, 'g', 'filled' );
hold off;

View File

@@ -0,0 +1,11 @@
p = 0.0;
n=0;
for k = 1:length( spikes )
%a= 0.5*(spikes(k+1)+spikes(k));
%fprintf( 'set label %d "$T_{%d}$" at %g, -0.5 center\n', k+2, k, a );
%fprintf( 'set arrow %d from %g, 0.3 to %g, 0.3 heads\n', k+2, spikes(k), spikes(k+1) );
%fprintf( '%g %d\n%g %d\n\n', p, n, spikes(k), n );
fprintf( '%g %d\n', spikes(k), n+1 );
n = n+1;
p = spikes(k);
end

227
linearalgebra/code/sta.m Normal file
View File

@@ -0,0 +1,227 @@
function [ stavg, stavgtime, spikesnippets, stimsnippets, meanrate ] = sta( stimulus, spikes, left, right )
% computes the spike-triggered average
% stimulus: the stimulus as a nx2 matrix with the first column being time
% in seconds and the second column being the actual stimulus
% spikes: a cell array of vectors of spike times
% left: the time to the left of each spike
% right: the time to the right of each spike
% returns
% stavg: the spike-triggered average
% stavgtime: the corresponding time axis
% spikesnippets: the spike-triggered waveforms as a nspikes x stavgtimes
% matrix
% meanrate: the mean firing rate
% time indices:
dt = stimulus(2,1) - stimulus(1,1);
wl = round( left/dt );
wr = round( right/dt );
nw = wl+wr+1;
% total number of spikes:
nspikes = 0;
for k = 1:length( spikes )
nspikes = nspikes + length( spikes{k} );
end
% loop over trials:
spikesnippets = zeros( nspikes, nw );
nspikes = 0;
for k = 1:length( spikes )
times = spikes{k};
for j = 1:length(times)
% index of spike in stimulus:
inx = round(times(j)/dt);
if ( inx-wl > 0 ) & ( inx+wr <= size( stimulus, 1 ) )
nspikes = nspikes + 1;
snip = stimulus( inx-wl:inx+wr, 2 );
spikesnippets( nspikes, : ) = snip; % - mean(snip);
end
end
end
% delete not used snippets:
spikesnippets(nspikes+1:end,:) = [];
stavgtime = [-left:dt:right];
meanrate = nspikes/length(spikes)/(stimulus(end,1)-stimulus(1,1));
% spike-triggered average:
stavg = mean( spikesnippets, 1 );
% loop over stimulus:
nstim = size( stimulus, 1 )-nw+1;
stimsnippets = zeros( nstim, nw );
stimsnippetstime = zeros( nstim, 1 );
for j=1:nstim
snip = stimulus( j:j+wr+wl, 2 );
stimsnippets(j,:) = snip; % - mean(snip);
stimsnippetstime(j) = j*dt;
end
% projection onto sta:
spikeonsta = spikesnippets * stavg';
stimonsta = stimsnippets * stavg';
% projection onto orthogonal to sta:
orthos = null( stavg );
mixcoef = rand(size( orthos, 2 ), 1);
mixcoef(length(mixcoef)/10:end) = 0.0;
staortho = orthos*mixcoef;
% stavg*staortho
staortho = staortho/(staortho'*staortho);
spikeonstaortho = spikesnippets * staortho;
stimonstaortho = stimsnippets * staortho;
% psth binned:
ratetime = 0:dt:stimulus(end,1);
% rate = zeros( size( ratetime ) );
% for k = 1:length( spikes )
% [n, ~] = hist( spikes{k}, ratetime );
% rate = rate + n/dt/length( spikes );
% end
% kernel psth:
kernelsigma = 0.001;
windowtime = -5.0*kernelsigma:dt:5.0*kernelsigma;
window = normpdf( windowtime, 0.0, kernelsigma )/length(spikes);
% plot( 1000.0*windowtime, window );
w2 = floor( length( windowtime )/2 );
kernelrate = zeros( size( ratetime ) );
for k = 1:length( spikes )
times = spikes{k};
for j = 1:length(times)
% index of spike in rate:
inx = round(times(j)/dt);
if ( inx - w2 > 0 ) & ( inx + w2 < length( kernelrate ) )
kernelrate(inx-w2:inx+w2) = kernelrate(inx-w2:inx+w2) + window;
end
end
end
if nargout == 0
% sta plot:
figure( 1 );
subplot( 3, 2, 1 )
plot( 1000.0*stavgtime, staortho, '-r', 'LineWidth', 3 )
hold on;
plot( 1000.0*stavgtime, stavg, '-b', 'LineWidth', 3 )
hold off;
xlabel( 'time [ms]' );
ylabel( 'stimulus' );
title( 'Spike-triggered average' );
legend( 'ortho', 'STA' );
% 2D scatter of projections:
subplot( 3, 2, 2 )
scatter( stimonsta, stimonstaortho, 40.0, 'b', 'filled', 'MarkerEdgeColor', 'white' )
hold on;
scatter( spikeonsta, spikeonstaortho, 20.0, 'r', 'filled', 'MarkerEdgeColor', 'white', 'LineWidth', 1.0 )
hold off;
xlabel( 'projection sta' );
ylabel( 'projection ortho' );
title( 'Projections' );
% histogram of projections onto sta:
subplot( 3, 2, 3 )
[ n, projections ] = hist( stimonsta, 50 );
bw = (projections(2)-projections(1));
pstim = n / sum(n)/bw;
bar( projections, pstim, 'b' );
hold on;
[ n, ~ ] = hist( spikeonsta, projections );
pstimspike = n / sum(n)/bw;
bar( projections, pstimspike, 'r' );
xlabel( 'projection x' );
title( 'Projection onto STA' );
xlim( [projections(1) projections(end)] );
hold off;
legend( 'p(x)', 'p(x|spikes)' );
% nonlinearity for orthogonal projections:
subplot( 3, 2, 5 )
nonlinearity = meanrate*pstimspike./pstim;
plot( projections(pstim>0.01), nonlinearity(pstim>0.01), '-r', 'LineWidth', 3 );
xlim( [projections(1) projections(end)] );
ylim( [0 1000 ])
xlabel( 'projection x' );
ylabel( 'meanrate*p(x|spikes)/p(x) [Hz]' );
title( 'Nonlinearity');
% histogram of projections onto orthogonal sta:
subplot( 3, 2, 4 )
[ n, ~] = hist( stimonstaortho, projections );
pstimortho = n / sum(n)/bw;
bar( projections, pstimortho, 'b' );
hold on;
[ n, ~ ] = hist( spikeonstaortho, projections );
pstimspikeortho = n / sum(n)/bw;
bar( projections, pstimspikeortho, 'r' );
xlim( [projections(1) projections(end)] );
xlabel( 'projection x' );
title( 'Projection onto orthogonal to STA' );
hold off;
legend( 'p(x)', 'p(x|spikes)' );
% nonlinearity for orthogonal projections:
subplot( 3, 2, 6 )
nonlinearityortho = meanrate*pstimspikeortho./pstimortho;
plot( projections(pstimortho>0.01), nonlinearityortho(pstimortho>0.01), '-r', 'LineWidth', 3 );
xlim( [projections(1) projections(end)] );
ylim( [0 1000 ])
xlabel( 'projection x' );
ylabel( 'meanrate*p(x|spikes)/p(x) [Hz]' );
title( 'Nonlinearity');
end
% stimulus reconstruction with STA:
stareconstruction = zeros( size( stimulus, 1 ), 1 );
for k = 1:length( spikes )
times = spikes{k};
for j = 1:length(times)
% index of spike in stimulus:
inx = round(times(j)/dt);
if ( inx-wl > 0 ) & ( inx+wr <= size( stimulus, 1 ) )
stareconstruction( inx-wl:inx+wr ) = stareconstruction( inx-wl:inx+wr ) + stavg';
end
end
end
stareconstruction = stareconstruction/length(spikes);
if nargout == 0
% linear stimulus reconstruction:
figure( 2 )
ax1 = subplot( 3, 1, 1 );
plot( 1000.0*stimulus(:,1), stimulus(:,2), 'g', 'LineWidth', 2 );
hold on;
plot( 1000.0*stimulus(:,1), stareconstruction, 'r', 'LineWidth', 2 );
ylabel( 'stimulus' );
hold off;
legend( 'stimulus', 'reconstruction' );
% stimulus projection onto sta:
ax2 = subplot( 3, 1, 2 );
% plot( 1000.0*ratetime, rate, '-b', 'LineWidth', 2 )
plot( 1000.0*ratetime, kernelrate-mean(kernelrate), '-b', 'LineWidth', 2 )
hold on;
plot( 1000.0*(stimsnippetstime+left), 2.0*stimonsta*meanrate, 'r', 'LineWidth', 2 );
ylabel( 'rate [Hz]' );
hold off;
legend( 'rate', 'stimulus projection on STA' );
% sta with nonlinearity:
ax3 = subplot( 3, 1, 3 );
plot( 1000.0*ratetime, kernelrate, '-b', 'LineWidth', 2 )
hold on;
xmax = max(projections(pstim>0.01));
stimonstax = stimonsta;
stimonstax( stimonstax > xmax ) = xmax;
sinx = round((stimonstax-projections(1))/bw);
sinx( sinx < 1 ) = 1;
stastimulus = nonlinearity( sinx );
plot( 1000.0*(stimsnippetstime+left), stastimulus, 'r', 'LineWidth', 2 );
legend( 'rate', 'LNP' );
xlabel( 'time [ms]' );
ylabel( 'rate [Hz]' );
hold off;
linkaxes( [ax1 ax2 ax3], 'x' );
end
end

View File

@@ -0,0 +1,31 @@
clear;
addpath( '../../pointprocesses/simulations/' );
set( 0, 'DefaultTextFontSize', 22.0 );
set( 0, 'DefaultAxesFontSize', 22.0 );
% load data
load( 'p-unit_stimulus.mat' );
load( 'p-unit_spike_times.mat' );
%load( 'pyramidal_noise_2014-09-02-af_cutoff_50_contr_5.mat' );
% load( 'pyramidal_noise_2014-09-02-ab_cutoff_300_contr_5.mat' );
% for k = 1:length( spike_times )
% spike_times{k} = spike_times{k} * 0.001;
% end
% stimulus = stimulus(1:20000*5,:);
%dt = stimulus(2,1) - stimulus(1,1);
%spike_times = lifadaptspikes( 20, 50+100.0*stimulus(:,2), dt, 0.1, 0.04, 10.0 );
%spike_times = lifspikes( 20, 12+30.0*stimulus(:,2), dt, 0.1 );
%spike_times = lifspikes( 20, 20.0+30.0*stimulus(:,2), dt, 0.0001 );
%figure( 1 );
%clf;
%spikeraster( spike_times );
%isivec = isis( spike_times );
%isihist( isivec );
%isiserialcorr( isivec, 20 );
%fano( spike_times );
sta( stimulus(1:5:length(stimulus),:), spike_times, 0.05, 0.01 );
%stc( stimulus(1:5:length(stimulus),:), spike_times, 0.05, 0.01 );

66
linearalgebra/code/stc.m Normal file
View File

@@ -0,0 +1,66 @@
function stc( stimulus, spikes, left, right )
% computes the spike-triggered covariance matrix
% stimulus: the stimulus as a nx2 matrix with the first column being time
% in seconds and the second column being the actual stimulus
% spikes: a cell array of vectors of spike times
% left: the time to the left of each spike
% right: the time to the right of each spike
% time indices:
dt = stimulus(2,1) - stimulus(1,1);
wl = round( left/dt );
wr = round( right/dt );
nw = wl+wr+1;
% spike-triggered average with snippets:
[ stavg, stavgtime, spikesnippets, stimsnippets, meanrate ] = sta( stimulus, spikes, left, right );
% spike-triggered covariance matrix:
figure( 3 );
subplot( 2, 2, 1 );
spikescv = cov( spikesnippets );
imagesc( spikescv );
caxis([-0.1 0.1])
% stimulus covariance matrix:
subplot( 2, 2, 2 );
stimcv = cov( stimsnippets );
imagesc( stimcv );
caxis([-0.1 0.1])
subplot( 2, 1, 2 );
imagesc( spikescv-stimcv );
caxis([-0.01 0.01])
% eigenvalues:
%[ ev , ed ] = eig( spikescv-stimcv );
[ ev , ed ] = eig( spikescv );
[d,dinx] = sort( diag(ed), 'descend' );
% spectrum of eigenvalues:
figure( 4 );
subplot( 3, 1, 1 );
scatter( 1:length(d), d, 'b', 'filled' );
% scatter( 1:length(d), d/sum(abs(d)), 'b', 'filled' );
xlabel( 'index' );
ylabel( 'eigenvalue [% variance]' );
% features:
subplot( 3, 2, 5 );
plot( 1000.0*stavgtime, ev(:,dinx(1)), 'g', 'LineWidth', 2 );
xlabel( 'time [ms]' );
ylabel( 'eigenvector 1' );
subplot( 3, 2, 6 );
plot( 1000.0*stavgtime, ev(:,dinx(2)), 'r', 'LineWidth', 2 );
xlabel( 'time [ms]' );
ylabel( 'eigenvector 2' );
% project onto eigenvectors:
nx = spikesnippets * ev(:,dinx(1));
ny = spikesnippets * ev(:,dinx(2));
subplot( 3, 1, 2 );
scatter( nx, ny, 'b', 'filled', 'MarkerEdgeColor', 'white' );
xlabel( 'projection onto eigenvector 1' );
ylabel( 'projection onto eigenvector 2' );
end