clean_coordinates.m 725 B

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