MATLAB: How to identify signal spikes

data acquisitiondigital signal processingMATLABsignalsignal processing

I'm trying to analyse some electric signal data. I have electrical pulse data signal and the first thing which i need to start the analysis is to extract the repetition rate of the signal (the number of spikes per second). For me the most difficult is: i have two signals together in the same file (One being always smaller than the other, a example in the figure). It's possible to calculate the repetition rate for each signal? In the data are the variables of the signal amplitude (signal), time (timescale) and sample rate (fs). Thanks!

Best Answer

Here is my approach to your problem. You may have to experiment to get the result you want, because I am identifying the spikes by their respective amplitudes only, since that is how you described them. (The negative amplitudes are easier to identify and isolate, so I used them.) If there are other criteria, you will have to include them, and tweak the code accordingly.
The Code
D = load('Vanessa Carvalho signal.mat');
signal = D.signal;
timescale = D.timescale;
Fs = D.fs;
[negpks1,locidx1] = findpeaks(-signal, 'MinPeakHeight',0.8);
[negpks2,locidx2] = findpeaks(-signal, 'MinPeakHeight',0.15);
spike2idx = setdiff(locidx2, locidx1);
figure(1)
plot(timescale, signal)
hold on
plot(timescale(locidx1), -negpks1, 'vr', 'MarkerFaceColor','r')
plot(timescale(spike2idx), signal(spike2idx), 'vg', 'MarkerFaceColor','g')
hold off
grid
axis([0 0.3 ylim])
rep_rate1 = [mean(diff(timescale(locidx1))) std(diff(timescale(locidx1))) median(diff(timescale(locidx1)))]
rep_rate2 = [mean(diff(timescale(spike2idx))) std(diff(timescale(spike2idx))) median(diff(timescale(spike2idx)))]
The code is fairly self-explanatory. The idea is to isolate the large-amplitude spikes, then identify the small and large amplitude spikes, then use the setdiff function to isolate the smaller spikes from all the spikes. I used the indices rather than the times because this made the addressing easier for me. See the documentation for the various functions for the details of how they work.
The ‘rep_rate’ vectors are the mean, standard deviation, and median, respectively of the time differences (not indices) in the units you recorded them in.
rep_rate1 =
20.7596e-003 2.7679e-003 20.2000e-003
rep_rate2 =
15.1114e-003 9.5616e-003 16.9500e-003
I isolated a section of the plot to illustrate the results in detail. Comment-out the axis call to see the entire series and the isolated spikes.
The (Zoomed) Plot