using hampel to remove peaks in graphs or how to get rid of spikes

Heiko Engemann on 4 Oct 2021
Latest activity Reply by Christopher Stapels on 11 Oct 2021

Hi,

my first time to use thingspeak. I have a graph where from time to time there is a peak with 4000, ussualy the data ist arround 50. When I insert the "hampel(my_variable)" I get a message that the toolbox is requierd.

Is there a different way to get rid of these spikes?

greetings

Christopher Stapels
Christopher Stapels on 4 Oct 2021

Have a look at this example . You can easily modify an array or matrix of values when you have them in MATALB. For example:

myVar=[1 3 4 6 7 9 11];
lessVar=myVar(myVar<10);
lessVar =
       1     3     4     6     7     9
Heiko Engemann
Heiko Engemann on 7 Oct 2021

i tried movmean and some other functions, but never got it right. it ploted something that had a graph from 0 to 1. rmoutliers gives me the same vectors error. Im just an IT guy and newer worked with matlab, I inherited that project and just need to get rid of the spikes

Heiko Engemann
Heiko Engemann on 5 Oct 2021 (Edited on 5 Oct 2021)

Hello,

maybe my variable is not an array? this is the whole code..

% Temperature Field ID 
TemperatureFieldID = 1; 
% Wind speed Field ID 
HumidityFieldID = 2; 
% Channel Read API Key   
% If your channel is private, then enter the read API 
% Key between the '' below:   
readAPIKey = ''; 
% Read Data. Learn more about the thingSpeakRead function by 
% going to the Documentation tab on the right side pane of this page. 
[data, timeStamps] = thingSpeakRead(readChannelID, 'Fields',[TemperatureFieldID HumidityFieldID], ...
                                                           'NumPoints', 3000, ...
                                                           'ReadKey', readAPIKey);
% Extract the temperature data from the first column
temperatureData = data(:, 1);
% Extract the humidity data from the second column
humidityData = data(:, 2);
% addded by HE
humidityDataCut = humidityData(humidityData<100);
% Visualize Data
yyaxis left
plot(timeStamps, temperatureData);
ylabel('temperature');
yyaxis right
plot(timeStamps, humidityDataCut);
ylabel('humidity');

i get this error now:

"Error using plot Vectors must be the same length."

Christopher Stapels
Christopher Stapels on 7 Oct 2021 (Edited on 7 Oct 2021)

You should act on both the value and timestamp arrays simultaneously to keep them the same length. I recommend you read the ThingSpeak data into a timetable, I should have said that in the earlier reply. use

myTable=thingSpeakRead(...'OutputFormat', 'timetable'...);
then 
newTable=myTable(myTable.(1),:>threshold);

and so on for each variable you want to reduce. This keeps the table the same length as the data You wont need to extract the data into separate columns. you can also use the field name instead of '.(1)'.

then when you plot, use

plot(newTable.Timestamps,newTable.(1)); 
Heiko Engemann
Heiko Engemann on 11 Oct 2021

is there a way to just show the plot from 0 - 80 and the rest just leaves on the top?

Christopher Stapels
Christopher Stapels on 11 Oct 2021

Can you clarify what you mean? You can use ylim([min max]); to change the y limits on your plot.

Tags

No tags entered yet.