clean_coordinates.m 659 B

1234567891011121314151617181920212223242526
  1. function xy_clean = clean_coordinates(xy, sigma)
  2. if nargin ==1, sigma = 2.56; end
  3. xy_clean = xy;
  4. nan_list = isnan(xy);
  5. speed = [0, mean(abs(diff(xy(1,:))) + abs(diff(xy(2,:))))];
  6. speed_threshold = mean(speed, 'omitnan')+sigma*std(speed, 'omitnan');
  7. outliers = find(speed>speed_threshold);
  8. % interpolate empty spots
  9. xy_clean(:,outliers-1) = nan;
  10. xy_clean(:,outliers) = nan;
  11. xy_clean(:,outliers+1) = nan;
  12. % interpolate empty spots
  13. xy_clean(1,:) = interp_nan(xy_clean(1,:));
  14. xy_clean(2,:) = interp_nan(xy_clean(2,:));
  15. end
  16. %% subfunction
  17. function y = interp_nan(x)
  18. nanx = isnan(x);
  19. t = 1:numel(x);
  20. y = x;
  21. y(nanx) = interp1(t(~nanx), x(~nanx), t(nanx));
  22. end