%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%2D
%Matlab code to generate 8 bit strictly positive sinusoid wave in 2D and see approximate expected FFT results:

%% Parameters
N=16;        %image is N-by-N
omega=2;     %vary this to change frequency
angle=pi/4;  %vary this to change angle 0=> horizontal, pi/2 => vertical
phase=0;     %vary this to change phase 0=> sin, -pi/2 => cos

%% Generate sinusoid image
xx=[0:N-1];
yy=[0:N-1];
[X,Y] = meshgrid(xx,yy);
U=X*cos(angle)+Y*sin(angle);
zz=((sin(2*pi/N*U*omega + phase) + 1)*255/2);

%% Compute 2D FFT
result=fft2(zz);
%same as
%{
resultFFTRows=zeros(N,N);
for row=1:N
    resultFFTRows(row,:)=fft(zz(row,:))
end
for col=1:N
    result(:,col)=fft(resultFFTRows(:,col))
end
%}


%% Plot results:
figure;
subplot(2,2,1)
  imagesc(zz);
  colorbar;title('Image')
subplot(2,2,3)
  imagesc(real(result));
  range=caxis(); maximum=max(abs(range));caxis([-maximum maximum]);
  colorbar;title('real fft');
subplot(2,2,4)
  imagesc(imag(result));
  range=caxis(); maximum=max(abs(range));caxis([-maximum maximum]);
  colorbar;title('imag fft')
colormap default; 


%% Print image data as one vector of 8-bit values, scanning first row left
%%%      to right then second row and so on
temp=zz';
data=temp(:)';
%disply input as 8 bit numbers
for index=1:(N^2)
    fprintf('%8s\n',dec2bin(data(index),8))
end
