规范不规则采样数据
此示例说明如何规范不规则采样的数据以使测量之间的时间间隔保持恒定。您更新从 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)