function [x,y] = square_wave(v)

v = v(:)';
changes = find(v(1:end-1) ~= v(2:end));

if ~isempty(changes)
  x = [changes+1; changes+1];
  y = [v(changes); v(changes+1)];
else
  x = []; y = [];
end

x = [1, x(:)', length(v)];

y = [v(1), y(:)', v(end)];


