规范不规则采样数据
此示例说明如何对不规则采样的数据进行正则化,以使测量之间的时间间隔恒定。您可以更新从 ThingSpeak™通道读取的数据的时间戳以消除不规则性,然后将数据写入通道。由于网络延迟或硬件重置而引入的测量数据的时间戳变化可能会影响数据预处理和数据分析算法。许多算法需要定期采样数据才能正常工作。
从气象站通道读取数据
ThingSpeak通道12397 包含来自位于马萨诸塞州内蒂克的 MathWorks® 气象站的数据。每分钟收集一次数据。通道的字段 4 包含气温数据。要检查不规则采样的数据,请使用 thingSpeakRead
函数从通道12397 读取气温数据。
data = thingSpeakRead(12397,'NumMin',60,'Fields',4,'outputFormat','timetable');
检查不规则采样数据
从通道12397 读取的最后 60 分钟的数据被存储为时间表。使用isregular
函数检查通道数据是否定期采样。如果数据不规则采样,则为感兴趣的时间段生成规则间隔的时间向量。使用 linspace
以及 startTime
、stopTime
和测量次数生成新的时间向量。
regularFlag = isregular(data,'Time'); if ~regularFlag startTime = data.Timestamps(1); stopTime = data.Timestamps(end); newTimeVector = linspace(startTime,stopTime,height(data)); data.Timestamps = newTimeVector; end
将数据发送到 ThingSpeak
使用 thingSpeakWrite
函数将处理后的数据发送到 ThingSpeak通道。
% Change the channelID and the writeAPIKey to send data to your channel. channelID=17504; writeAPIKey='23ZLGOBBU9TWHG2H'; thingSpeakWrite(channelID,data,'WriteKey',writeAPIKey);
另请参阅
thingSpeakRead
(MATLAB) | thingSpeakWrite
(MATLAB) | linspace
(MATLAB)