Remove outliers but be careful with end points

5 ビュー (過去 30 日間)
Jason
Jason 2025 年 5 月 2 日
コメント済み: Star Strider 2025 年 5 月 2 日
Hi, I have some typcial data like this that I want to remove the outliers (red arrows)
I use the rmoutliers function and then fillmissing to handle these.
%For last plotted Data
ax=app.UIAxes3;
[datax,datay] = getDataFromGraph(app,ax,1); % my fucntion that gets the last plot x and y values.
A=[datax datay];
[B,TFrm,TFoutlier] = rmoutliers(A,"movmedian",10);
x=B(:,1); y=B(:,2); hold(ax,"on");
The contraint I have is if I remove an outlier from the 1st or last data point (i.e. green arrow above), then it MUST be replaced with e.g the nearest non outlier.
So I thought this would do it:
F = fillmissing(y,'linear','EndValues','nearest'); %F = fillmissing(y,'movmedian',10);
plot(ax,x,F,'.-');
However, its ignoring the last point (I dont mind other outliers being ignored, I just need the starting x and finishing x to be the same as the original data
Thanks

採用された回答

Star Strider
Star Strider 2025 年 5 月 2 日
編集済み: Star Strider 2025 年 5 月 2 日
The rmoutliers function does not know that the end points are outliers because it has nothing after them to compare with beyond that. (Neither do you, actually. They could be valid data.)
You could supply an additional end value (perhaps the mean of the previous values), or just remove them yourself.
EDIT — Corrected typographical errors.
  8 件のコメント
Jason
Jason 2025 年 5 月 2 日
編集済み: Jason 2025 年 5 月 2 日

Ah thats a shame. This is part of an instrument control system where i need almost instant smoothing and I cant be accessing the web to get results

Star Strider
Star Strider 2025 年 5 月 2 日
If any part of your system is analog, your best option then may be to design a Bessel filter with the appropriate lowpass characteristic and realise it in analog hardware, for example just ahead of the ADC stage. (Bessel filters are phase-neutrral, so there is no phase distortion or phase delay.) ICs for this purpose exist, although I've never used them.
If you have the Control System Toolbox, you might be able to design an appropriate continuous-time filter (using MATLAB Answers) and implement the filter in discrete time with a state-space or transfer-function realisation of it. (Bessel filters lose thier phase-neutral characteristic if implemented as discrete filters. An elliptic filter would be my choice.)
Your best option however is probably to get the Signal Processing Toolbox, although the 'almost real time' constraint may prohibit that working as well as you might need it to work.

サインインしてコメントする。

その他の回答 (1 件)

Steven Lord
Steven Lord 2025 年 5 月 2 日
Do you want to remove the outliers or do you want to fill them in? If the latter, see the filloutliers function.
  3 件のコメント
Steven Lord
Steven Lord 2025 年 5 月 2 日
If you want to smooth your data using Savitzky-Golay, you can use the smoothdata function and specify the "sgolay" smoothing method.
Jason
Jason 2025 年 5 月 2 日
Yes thats I currently do, thanks
degree=app.SGDegreeEditField.Value;
ys = smoothdata(y,'sgolay','Degree',degree); %sgolay, Degree default is 2
hold(ax,'on');
plot(ax,x,ys,'y-','LineWidth',0.5); %grid(ax,"on");

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeDigital Filtering についてさらに検索

製品


リリース

R2024b

Community Treasure Hunt

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

Start Hunting!

Translated by