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/plausibilizeTime.m
Views: 1799
function [timePlaus, signalPlaus] = plausibilizeTime(time,signal)1% Checks if time vector has a major tracking error and delete respective2% element from time and signal vector3%4% Fabian Bredemeier - IAV GmbH5% License: GPL v367% Get indices for time values that are too high8validIdx = ~(time > 1e6);9if time(1) > time(2) % First time stamp is invalid if greater than second10validIdx(1) = 0;11end1213% Calculate average sample time14Ts_avg = sum(diff(time(validIdx)))/length(time);1516% Only plausibilize time vector17if nargin == 118% Set first element19if time(1) > 1e6 && time(2) < 1e6 || time(1) > time(2)20timePlaus = time(2)-Ts_avg;21else22timePlaus = time(1);23end24idxDiff = 0; % Tracking of removed elements25skipNext = 0; % Flag to skip next element26for i = 2:length(time)-127if skipNext28skipNext = 0;29continue;30end31if(time(i+1)<time(i))32% Check if current or next element is incorrect33if abs((time(i)-time(i-1))-Ts_avg) > abs((time(i+1)-time(i-1))-Ts_avg) && time(i+1)-time(i-1) > 034% Current element is wrong since deviation to average35% sample time is larger - skip current and do nothing3637else38% Next element is wrong, so add current to corrected vector39timePlaus(i-idxDiff,1) = time(i);4041% Make sure next one is skipped42skipNext = 1;43end44idxDiff = idxDiff + 1;45else46timePlaus(i-idxDiff,1) = time(i);47end48end49% Add last element50len = length(time);51timePlaus(len-idxDiff,1) = time(end);52return;5354% Also plausibilize signal vector55elseif nargin == 256% Set first element57if time(1) > 1e6 && time(2) < 1e6 || time(1) > time(2)58timePlaus = time(2)-Ts_avg;59signalPlaus = signal(2);60else61timePlaus = time(1);62signalPlaus = signal(1);63end64idxDiff = 0; % Tracking of removed elements65skipNext = 0; % Flag to skip next element66for i = 2:length(time)-167if skipNext68skipNext = 0;69continue;70end71if(time(i+1)<time(i))72% Check if current or next element is incorrect73if abs((time(i)-time(i-1))-Ts_avg) > abs((time(i+1)-time(i-1))-Ts_avg) && time(i+1)-time(i-1) > 074% Current element is wrong since deviation to average75% sample time is larger - skip current and do nothing7677else78% Next element is wrong, so add current to corrected vector79timePlaus(i-idxDiff,1) = time(i);80signalPlaus(i-idxDiff,1) = signal(i);8182% Make sure next one is skipped83skipNext = 1;84end85idxDiff = idxDiff + 1;86else87timePlaus(i-idxDiff,1) = time(i);88signalPlaus(i-idxDiff,1) = signal(i);89end90end91% Add last element92len = length(time);93timePlaus(len-idxDiff,1) = time(end);94signalPlaus(len-idxDiff,1) = signal(end);95end96end979899100