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
