1234567891011121314151617181920212223242526 |
- function xy_clean = clean_coordinates(xy, sigma)
- if nargin ==1, sigma = 2.56; end
- xy_clean = xy;
- speed = [0, mean([abs(diff(xy(1,:))); abs(diff(xy(2,:)))])];
- speed_threshold = mean(speed, 'omitnan')+sigma*std(speed, 'omitnan');
- % Speed-based outlier elimination
- outliers = find(speed>speed_threshold);
- xy_clean(:,outliers-1) = nan;
- xy_clean(:,outliers) = nan;
- xy_clean(:,outliers+1) = nan;
- bad = find(speed==0); % Proc not moving frames
- bad(1)=[];
- xy_clean(:,bad) = 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
|