Main Content

Remove Outliers in Your Data

This example shows how to detect and remove outliers in your ThingSpeak™ channel data using a Hampel filter. This filter is closely related to a median filter, and it helps to remove outliers from a signal without overly smoothing the data.

Read Data

Read the wind speed stored in channel 22641 using the thingSpeakRead function.

data = thingSpeakRead(12397,'NumPoints',80,'Fields',2,'outputFormat','table');

Remove Outliers

Remove outliers in the raw data by applying hampel function. Specify the window size as 6, or about three minutes of data on either side of measurement window. This setting allows for sufficient data to decide whether each point is an outlier. Since the data is dynamic, your results may vary depending on the present weather. Specify the number of standard deviations to be 2. If a data point differs from the local median by more than this number, it is replaced with the local median value.

windowSize = 6;
numMedians = 2;
[filteredData,outliers]=hampel(data.WindSpeedmph,windowSize,numMedians);

Write Data to ThingSpeak

Send the number of outliers detected to a ThingSpeak channel using the thingSpeakWrite function. Change the channelID and the writeAPIKey to send data to your channel.

channelID=17504;
writeAPIKey='23ZLGOBBU9TWHG2H';
totalOutliers = sum(outliers)
thingSpeakWrite(channelID,totalOutliers,'WriteKey',writeAPIKey);
totalOutliers =

     8

See Also

Functions