Wolfram Function Repository
Instant-use add-on functions for the Wolfram Language
Function Repository Resource:
Remove outliers from a signal using a Hampel window
| ResourceFunction["HampelFilter"][data] removes outliers from data. | |
| ResourceFunction["HampelFilter"][data,w] uses a moving window of size w. | |
| ResourceFunction["HampelFilter"][data,w,d] removes outliers that are more than d standard deviations away from the expectation. | 
Signal data with outliers:
| In[1]:= |  | 
| In[2]:= | ![ListLinePlot[data, PlotRange -> {-0.3, 1}]](https://www.wolframcloud.com/obj/resourcesystem/images/7f9/7f9415d8-feee-4320-83be-7a7aa87f5bfb/50f0308cf79a33ca.png) | 
| Out[2]= |  | 
Remove outliers from the data:
| In[3]:= | ![ListLinePlot[ResourceFunction["HampelFilter"][data], PlotRange -> {-0.3, 1}]](https://www.wolframcloud.com/obj/resourcesystem/images/7f9/7f9415d8-feee-4320-83be-7a7aa87f5bfb/30b22113269bedf5.png) | 
| Out[3]= |  | 
Reduce the window size used to decide if a point is an outlier:
| In[4]:= |  | 
| In[5]:= | ![ListLinePlot[ResourceFunction["HampelFilter"][data, 1], PlotRange -> {-0.3, 1}]](https://www.wolframcloud.com/obj/resourcesystem/images/7f9/7f9415d8-feee-4320-83be-7a7aa87f5bfb/107fc5557491127e.png) | 
| Out[5]= |  | 
The window size can be also be increased:
| In[6]:= | ![ListLinePlot[ResourceFunction["HampelFilter"][data, 10], PlotRange -> {-0.3, 1}]](https://www.wolframcloud.com/obj/resourcesystem/images/7f9/7f9415d8-feee-4320-83be-7a7aa87f5bfb/37bb7a6d3a215d92.png) | 
| Out[6]= |  | 
Reduce the outlier threshold to make HampelFilter more sensitive:
| In[7]:= | ![ListLinePlot[ResourceFunction["HampelFilter"][data, 3, 0.5], PlotRange -> {-0.3, 1}]](https://www.wolframcloud.com/obj/resourcesystem/images/7f9/7f9415d8-feee-4320-83be-7a7aa87f5bfb/45deed681348ee9a.png) | 
| Out[7]= |  | 
Increasing the threshold will adjust fewer points:
| In[8]:= | ![ListLinePlot[ResourceFunction["HampelFilter"][data, 3, 2], PlotRange -> {-0.3, 1}]](https://www.wolframcloud.com/obj/resourcesystem/images/7f9/7f9415d8-feee-4320-83be-7a7aa87f5bfb/35c62ec32cca6a04.png) | 
| Out[8]= |  | 
This work is licensed under a Creative Commons Attribution 4.0 International License