Question
I know this question has been asked a number of times, but a thorough search did not help me find an answer and as I am very new to matlab I need some help. I need to count the number of peaks in a large body of EEG data. The code below allows me to plot what the data looks like (for 1 piece of data). I need to count the really long narrow spikes. I know there are a bunch of peakfinder functions out there but I have no idea how to use them because they are all vector based and I don't know how to reference the vectors that are preprocessed in the script (fieldtrip). A simple threshold function wouldn't work because there is a lot of overall noise that is not related to what I want to count. This is what the data looks like: This is the script I have: % create the trial definition cfg=[]; cfg.filename = ['I:\EEG\',SubNum{subject},'\',FolderName, GroupName, SeqNum{subject}, '_', SubNum{subject}, '_Block', num2str(block), '.bdf']; cfg.dataset = cfg.filename; cfg.continuous = 'yes'; data_org = ft_preprocessing(cfg) channel = 164; plot(data_org.time{1}, data_org.trial{1}(channel, :)) xlabel('time (s)') ylabel('channel amplitude (uV)') legend(data_org.label(channel)) I don't know if it's asking too much, but I would really appreciate some guidance, I've been trying to use GUI's forever and it's no luck...but my coding skills are completely insufficient.
Expert Answer
Prashant Kumar
PhD Expert
Answered Aug 24, 2026
You forgot to attach your data, so we can't try anything with it. Attach it if you want better help. Otherwise, if you have the Signal Processing Toolbox, you can try medfilt(), sgolayfilt(), and findpeaks(). It sounds like you tried findpeaks() and say it doesn't work for some reason, though you did not specify why it failed or what parameters you used for it.
For the Savitzky-Golay filter, use it to smooth the signal and remove spikes. This gives you like a background that you can then subtract from the original signal to give a "spikes-only" signal. Then you can use a global threshold. You can use the same basic process with the median filter, or even conv() for that matter. It looks like spikes that are more than about 20 are legitimate spikes and those less than 20 are just noise, but you can play with that number to set it optimally.
100% Run Guarantee
3-Hour Fast-Track Delivery
Need a Custom Version or Complete Simulation for This Problem?
Our 500+ PhD engineers build, debug, and optimize working MATLAB scripts and Simulink (.slx) models tailored to your exact assignment rubrics with zero plagiarism.
Tested on MATLAB R2024b / R2026a
Turnitin 0% Plagiarism Report
Free 7-Day Revisions Guarantee
Have a different question? Ask here
Related matlab Questions & Solutions
Browse All →
Explore similar technical troubleshooting questions and verified MATLAB solutions:
Ready-to-Run MATLAB & Simulink Projects
Browse All Projects →