function xy_clean = clean_coordinates(xy, sigma) if nargin ==1, sigma = 2.56; end xy_clean = xy; nan_list = isnan(xy); speed = [0, mean(abs(diff(xy(1,:))) + abs(diff(xy(2,:))))]; speed_threshold = mean(speed, 'omitnan')+sigma*std(speed, 'omitnan'); outliers = find(speed>speed_threshold); % interpolate empty spots xy_clean(:,outliers-1) = nan; xy_clean(:,outliers) = nan; xy_clean(:,outliers+1) = nan; % interpolate empty spots xy_clean(1,:) = interp_nan(xy_clean(1,:)); xy_clean(2,:) = interp_nan(xy_clean(2,:)); end %% subfunction function y = interp_nan(x) nanx = isnan(x); t = 1:numel(x); y = x; y(nanx) = interp1(t(~nanx), x(~nanx), t(nanx)); end