% estimate the motion vector of a 1 d ``image''

N = 1000;        % number of ``pixels'' in 1d ``image''
x = (0:(N-1))/N;   % [0,1)

f = 2;  % number of cycles of sinusoid
pixelshift = .1234;    % number of pixels shifted over
%pixelshift = 125;
E1 = sin(2*pi*f*x);
pixelspercycle = length(x)/f;
phase = pixelshift/pixelspercycle;  % not  *2*pi; for now
E2 = sin(2*pi*(f*x-phase));    % shift right by pixelshift pixels

subplot(211)
plot(E1)
plot(E2)

% sampling is between each on a 4 by 4 matrix, so if E1 and E2 both 1 by 8
% (ie if movie is 2 by 8) result is 1 by 7, centered
%
%     1 2 3 4 3 2 1 0
%      @ @ @ @ @ @ @
%     9 8 7 6 5 4 3 2

Ex = (diff(E1) + diff(E2))/2;   % dim.  1 by N
now   = (E1(1:N-1) + E1(2:N))/2;    % interpolate to between pixels
later = (E2(1:N-1) + E2(2:N))/2;
Et    = later - now;

u = - sum(Et.*Ex)/sum(Ex.*Ex)   % velocity in X direction
err = u - pixelshift
