% load data into time, voltage and spiketimes x = load('extdata'); time = x.time; voltage = x.voltage; spiketimes = x.spiketimes; waveformt = x.waveformt; waveform1 = x.waveform1; waveform2 = x.waveform2; % indices into voltage trace of spike times: dt = time(2) - time(1); tinx = round(spiketimes/dt) + 1; % plot voltage trace with detected spikes: figure(1); plot(time, voltage, '-b') hold on scatter(time(tinx), voltage(tinx), 'r', 'filled'); xlabel('time [s]'); ylabel('voltage'); xlim([0.1, 0.4]) % zoom in hold off % spike waveform snippets: snippetwindow = 0.005; % milliseconds w = ceil(snippetwindow/dt); vs = zeros(length(tinx), 2*w); for k=1:length(tinx) vs(k,:) = voltage(tinx(k)-w:tinx(k)+w-1); end % time axis for snippets: ts = time(1:size(vs,2)); ts = ts - ts(floor(length(ts)/2)); % plot all snippets: figure(2); hold on plot(1000.0*ts, vs, '-b'); title('spike snippets') xlabel('time [ms]'); ylabel('voltage'); hold off savefigpdf(gcf, 'spikesorting1.pdf', 12, 6); % pca: cv = cov(vs); [ev , ed] = eig(cv); [d, dinx] = sort(diag(ed), 'descend'); % features: figure(2) subplot(1, 2, 1); plot(1000.0*ts, ev(:,dinx(1)), 'r', 'LineWidth', 2); xlabel('time [ms]'); ylabel('eigenvector 1'); subplot(1, 2, 2); plot(1000.0*ts, ev(:,dinx(2)), 'g', 'LineWidth', 2); xlabel('time [ms]'); ylabel('eigenvector 2'); savefigpdf(gcf, 'spikesorting2.pdf', 12, 6); % plot covariance matrix: figure(3); subplot(1, 2, 1); imagesc(cv); xlabel('time bin'); ylabel('time bin'); title('covariance matrix'); caxis([-0.1 0.1]) % spectrum of eigenvalues: subplot(1, 2, 2); scatter(1:length(d), d, 'b', 'filled'); title('spectrum'); xlabel('index'); ylabel('eigenvalue'); savefigpdf(gcf, 'spikesorting3.pdf', 12, 6); % project onto eigenvectors: nx = vs * ev(:,dinx(1)); ny = vs * ev(:,dinx(2)); % clustering (two clusters): %kx = kmeans([nx, ny], 2); % nx smaller or greater a threshold: kthresh = 1.6; kx = ones(size(nx)); kx(nx