Question
I have a loop which reads out a sensor, once in a while the data is completely off, and gives a spike. I would like to remove those spikes. The data given moves from -180 to 180 degrees, so when my measurement moves from -180 to 180 this should not be filtered (as this is normal). However, if it moves from -180 to 90 then this counts as a spike. It's not possible to do data processing after getting every readout, ideally there should be a filter of some sort in the loop itself.
Expert Answer
Kshitij Singh
PhD Expert
Answered Aug 24, 2026
Do you have the Signal Processing Toolbox? If so, you could use the median filter. If you want to replace spikes with the median value around them, then compute the median and subtract it from the original and take the absolute value. Then replace those elements with a high value with the median.
medianSignal = medfilt1(signal, 7); diffSignal = abs(signal - medianSignal); spikes = diffSignal > 100; % Whatever... % Replace fixedSignal = signal; % Initialize fixedSignal(spikes) = medianSignal(spikes);
Or you can use the Savitzky-Golay filter, sgolayfilt(). A demo is attached.
If this is not right for you, then post your data - both a picture and the actual data in a txt or .mat file.
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 →