clear all
close all

video = 0;

f = 337e3; %frequency
f = 50e3; %frequency
fs = 35e3;  %sample rate

c = 1500;  %speed of sound in water

lam = c/f; %wavelength

d = 0.033; %spacing between elements
d = lam/2;

omega = 2*pi*f;  %self explanatory

k = 2*pi/lam;    %wavenumber

M = 45;    %number of elements

N = 1000;  %number of samples

T = N/fs;
t = 0:1/fs:T-1/fs;

arr_ang = -pi/6;

signal1 = exp(1j*omega*t);



if rem(M,2)==0
   m = -M/2+0.5:M/2-0.5;
else
   m = -floor(M/2):floor(M/2);
end

theta = -pi/2:0.001:pi/2;
figure
set(gca,'FontName','Arial','FontSize',14)
set(gcf,'color','w');
set(gcf, 'Position',  [100, 100, 1000, 800])


signal(:,:) = exp(-1j*omega*m'*d*(sin(theta)-sin(arr_ang))/c);
% figure
% plot(real(signal(1,:)'))
% figure
% imagesc(real(signal))
% figure
% imagesc(imag(signal))
% figure
% surf((abs(iradon(real(signal),theta'))))
% figure
% plot(theta*180/pi,10*log10(real(sum(signal/M).^2)))

p = polardb(theta,10*log10(abs(sum(signal/M)).^2),-40);    
set(p, 'LineWidth', 4,'color',[0.5 0 0.5])
if video
    
    look_angles = -pi/3:1/100:pi/3;
loops = length(look_angles);
F(loops) = struct('cdata',[],'colormap',[]);

% Prepare the new file.
vidObj = VideoWriter('look_angles.avi');
open(vidObj);


count = 0;
for k = look_angles
    count = count+1;
    arr_ang = k;
signal(:,:) = exp(-1j*omega*m'*d*(sin(theta)-sin(arr_ang))/c);
% figure
% plot(real(signal(1,:)'))
% figure
% imagesc(real(signal))
% figure
% imagesc(imag(signal))
% figure
% surf((abs(iradon(real(signal),theta'))))
% figure
% plot(theta*180/pi,10*log10(real(sum(signal/M).^2)))

polardb(theta,10*log10(abs(sum(signal/M)).^2),-40)
drawnow
F(count) = getframe;
writeVideo(vidObj,F(count));

end

   % Close the file.
    close(vidObj);
end
