ece210/hw4/feedback/switchsign.m

18 lines
892 B
Matlab

% cursed
% lmao
% basically - I take pairs of adjacent values on the vector `x` and add them
% together. These sums are then checked against both members of the pair,
% and if either member is *larger* than the sum, that indicates a sign change
% since one of the values acted as a subtractor.
% "Glue" is added in the comparison stage, to skew ONLY on cases where
% we are comparing zero to zero (origin case). Its really a rounding error otherwise. lol
% The front is padded w/ a zero.
function ret = switchsign(x)
sum_check = x(1:end-1) + x(2:end);
ret = [0, ( abs(sum_check) < abs(x(1:end-1)+1e-7) ) | ( abs(sum_check) < abs(x(2:end)+1e-7) ) ];
end
% <-.01> works here (this is insane), but technically has an
% error: calling `switchsign([-1 2 1 0 1 -3])` (the example i
% gave) returns `[0 1 0 1 1 1]` rather than `[0 1 0 0 0 1]`.
% not that that really matters. sorry...