MATLAB Answers

Median filter for an imported Excel file (removing spikes from a signal)

13 views (last 30 days)
freshprince
freshprince on 28 Mar 2021
Commented: Chad Greene on 28 Mar 2021
My excel file has 2 columns with the two variables winkel (x-axis) and weg (y-axis). I want to remove the spikes from my signal. How do I do this with the medfilt command or is there another solution? I am completely desperate. Thanks in advance
clear all;
xlsread('Stahl_rau.xlsx');
tbl = readtable ('Stahl_rau.xlsx');
weg = tbl.Weg;
winkel = tbl.Winkel;
plot(winkel,weg,'LineWidth',0.2);
set (gca, 'Xtick', 0:30:360);
set (gca, 'Ytick', -1.5:.05:1.5);
ylim([-0.1 1])
xlim([0 360])

Accepted Answer

Chad Greene
Chad Greene on 28 Mar 2021
For a 3-point moving median, you can medfilt1 as you suggest, like this:
weg_f = medfilt1(weg,3);
Or similarly, you could use movmedian like this:
weg_f = movmedian(weg,3);
Depending on what exactly you're trying to do, you may prefer a different solution, which would be to use isoutlier to find the outliers and set them to NaN, like this:
weg(isoutlier(weg)) = nan;

More Answers (1)

freshprince
freshprince on 28 Mar 2021
Thank you very much, you have helped me a bit further!!
But with the medfilt command, there is always an error
weg_f = medfilt1(weg,3);
With this command it worked, but there is interruptions in the siganal
weg(isoutlier(weg)) = nan;
weg_f = movmedian(weg,3);
I have not noticed any change here
  1 Comment
Chad Greene
Chad Greene on 28 Mar 2021
It's hard to troubleshoot what the problem could be if the only information is "there is always an error." If you want to use medfilt1, then describe the error and maybe we can get to the bottom of it.
You say you're not seeing a change with movmedian. I assume you mean the signal looks the same before and after applying a 3 point median window? If the outliers are more than 1 point long, then the median of a 3 point moving window will follow the outliers.
It looks like the spikes are adequately removed by isoutlier, but now you want to fill in the gaps with some reasonable values. It's important to keep in mind that in this situation, any method of filling in the gaps is just making up data. Depending on the application, that might be absolutely fine, or it might be problematic, so that's where your judgment has to come in.
One way to fill in the gaps after using isoutlier is to use fillmissing. To do that, you might use the option to linearly interpolate across the sections of missing data, like this:
weg(isoutlier(weg)) = nan;
weg = fillmissing(weg,'linear');

Sign in to comment.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!