MATLAB: How can ı do spike sorting to filtered EEG signal.

eegspikethreshold

I have this sample and ı need to do spike sorting. I start with finding thresholding value and want to see changes with this code. I have trouble with plotting in for loop and also further steps. I am very new in Matlab. If you can help it would be great thank you.
for k = 1:0.1:5
omg = median(abs(samp)/0.6745);
Thr =k * omg;
[pks, locs] = findpeaks(samp);
findpeaks(samp, 'Threshold', Thr);
[r,NumPeaks] = size(pks);
NumPeaks
end
hold on

Best Answer

Try this:
D = load('samp.mat');
samp = D.samp;
data = 1E-3; % What Are ‘data’?
omg = median(abs(data)/0.6745);
% th = 1:0.1:5;
th = 5 : -0.1 : 1; % Easier To Count Down Here
for k = 1:numel(th)
Thr = th(k) * omg;
[pks{k}, locs{k}] = findpeaks(samp, 'Threshold', Thr); % Peaks Above This ‘Thr’ Value
kprv = max(k-1,1); % Previous Index Value
lv = ~ismember(locs{k},locs{kprv}); % Spikes Above Current ‘Thr’ & Below Previous ‘Thr’
nspikes(k) = nnz(lv); % Number Of Spikes Above Current ‘Thr’ & Below Previous ‘Thr’
ThrPks{k} = pks{k}(lv); % Peak Amplitudes Above Current ‘Thr’ & Below Previous ‘Thr’
ThrLcs{k} = locs{k}(lv); % Locations Of Peak Amplitudes Above Current ‘Thr’ & Below Previous ‘Thr’
end
figure
bar(th, nspikes)
xlabel('Threshold')
ylabel('# Spikes')
I am not certain what you want, so I saved everything about your data in my code.