MATLAB Answers

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

11 次查看(过去 30 天)
评论: Chad Greene ,2021-3-28
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;
tbl = readtable ('Stahl_rau.xlsx');
weg = tbl.Weg;
winkel = tbl.Winkel;
set (gca, 'Xtick', 0:30:360);
set (gca, 'Ytick', -1.5:.05:1.5);
ylim([-0.1 1])
xlim([0 360])


Chad Greene
Chad Greene 2021-3-28
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;

更多回答(1 个)

freshprince 2021-3-28
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 个评论
Chad Greene
Chad Greene 2021-3-28
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');


Community Treasure Hunt

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

Start Hunting!

Translated by