Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.
Path: blob/master/Tools/simulink/arducopter/functions/resampleLog.m
Views: 1799
function [logResampled] = resampleLog(log,resampling,method)1% Resamples log if resample is set to true, so that all included messages2% have the same first time stamp and the same amount of elements3% (if they have the same sample time)4% If resample is set to false, the Time vector of all messages is just5% adapted to the first time stamp of the log6%7% Fabian Bredemeier - IAV GmbH8% License: GPL v3910headerProps2Copy = {'fileName', 'filePathName', 'platform', 'version', ...11'commit', 'bootTimeUTC', 'msgsContained', 'totalLogMsgs', 'msgFilter', ...12'numMsgs'};1314% Define messages that are to be excluded from resampling, but still copied15msgs2Exclude = {'FMT', 'FMTU', 'MODE', 'MULT', 'UNIT', 'PARM', 'SIDS'};1617% Messages with multiple instances18msgWithMultInstances = {'ARSP', 'BARO', 'BAT', 'BCL', 'BCL2', 'ESC', ...19'GPA', 'GPS', 'IMU', 'MAG', 'PRX', 'RFND'};2021% Define message header props that need to be copied to new log22msgProps2Copy = {'typeNumID', 'fieldUnits', 'fieldMultipliers', 'name', ...23'MsgInstance'};2425% Define message props that need to be excluded in new log26msgProps2Exclude = {'TimeS', 'LineNo', 'DatenumUTC', 'TimeUS'};2728% Define sample times that are used for logging29TsVec = [0.00025 0.001 0.0025 0.01 0.02 0.04 0.1 0.2 1];3031% Allowed jitter percentage of sample time32TsJitPerc = 0.5;3334% Get first timestamp of log35timeZero = getTimeZero(log, log.msgsContained, msgs2Exclude);3637% Convert LogMsgGroup object into structs38log = log.getStruct();3940for prop = string(fieldnames(log))'41propObj = log.(prop);4243% Just copy property if contained in cell array44if any(strcmp(prop, headerProps2Copy))45logResampled.(prop) = propObj;46continue;47end4849% Skip message if it shall be excluded50if any(strcmp(prop, msgs2Exclude))51logResampled.(prop) = propObj;52continue;53end5455% Skip message if it is a deleted handle56if isempty(propObj)57continue;58end5960% Handle messages with multiple instances (like BAT)61if any(strcmp(msgWithMultInstances, prop))62% Create idx vector for certain instance63if isfield(propObj, 'Instance') % Check message name of instance64idxVec = propObj.Instance == 0;65elseif isfield(propObj, 'I')66idxVec = propObj.I == 0;67elseif isfield(propObj, 'C') % XKF messages68idxVec = propObj.C == 0;69end70else % Message without multiple instances71idxVec = logical(ones(length(propObj.TimeS),1));72end7374% Get plausibilized time vector75timePlaus = plausibilizeTime(log.(prop).TimeS(idxVec));7677% Determine sample time of message78TsRaw = mean(diff(timePlaus));79Ts = 0;80for TsElem = TsVec81if TsRaw > TsElem*(1-TsJitPerc) && TsRaw < TsElem*(1+TsJitPerc)82Ts = TsElem;83break;84end85end86% Throw error if Ts could not be determined87if Ts == 088error(['Sample time of message ' char(prop) ' could not be determined. Mean: ' num2str(TsRaw) 's']);89end9091% Copy properties to new object. Resample signals92msgProps = string(fieldnames(log.(prop)))';93for msgProp = msgProps(1:end)9495% Copy property if included in msgProps2Copy96if any(strcmp(msgProp, msgProps2Copy))97logResampled.(prop).(msgProp) = propObj.(msgProp);98continue;99end100101% Skip property if included in msgProps2Exclude102if any(strcmp(msgProp, msgProps2Exclude))103continue;104end105106% Get signal values for the correct indices107signal = propObj.(msgProp)(idxVec);108109% Define time starting from first time stamp110time = propObj.TimeS(idxVec) - timeZero;111112% Plausibilize time and signal vector113[time, signal] = plausibilizeTime(time, signal);114115% If any time sample is still larger than the final time,116% plausibilize the time vector again117while(any(time > time(end)))118[time, signal] = plausibilizeTime(time, signal);119end120121% Define old time vector. Insert zero at beginning for the signals122% to start at 0s. Insert first signal value in value vector.123if time(1) > 1e-3124timeOld = zeros(length(time)+1,1);125signalOld = zeros(length(signal)+1,1);126timeOld(2:length(time)+1, 1) = time;127timeOld(1) = 0;128signalOld(2:length(signal)+1, 1) = signal;129signalOld(1) = signal(1);130else131timeOld = time;132signalOld = signal;133end134135% Catch nan in signal136if any(isnan(signalOld))137signalOld = zeros(length(signalOld),1);138warning(['Detected nan in ' char(prop) '.' char(msgProp)])139end140141if resampling142% Resample signal143if any(strcmp(method, 'makima')) || any(strcmp(method, 'nearest'))144len = round(timeOld(end) / Ts, 0); % Get amount of time vec elements145timeNew = Ts * (0:len)';146signalNew = interp1(timeOld, signalOld, timeNew, method, 'extrap');147else148[signalNew, timeNew] = resample(signalOld, timeOld, 1/Ts, method);149end150else151signalNew = signal;152timeNew = Ts*(0:length(signal)-1)';153end154155% Insert property156logResampled.(prop).(msgProp) = signalNew;157% Add new property sampling time Ts158logResampled.(prop).Ts = Ts;159end160161% Add Time Property162logResampled.(prop).TimeS = timeNew;163end164end165166167168