clear all; close all; clc



% INPUT PARAMETERS START HERE %

% Define the path to the base directory of the dataset
%basePath = '/ssip1/GOTCHA/CVdomes';
%basePath = '/home/uttam/Desktop/CVdata/Civilian_Vehicle_SampleSet/';
%basePath = 'D:\matl\WP\Tom_turntable\';
basePath = '/home/uttam/Desktop/CVdata/Civilian_Vehicle_SampleSet/';

% Define input data parameters here
target = 'Jeep93';      % What target to image
elev = 30;              % What elevation to image (deg)
pol = 'HH';             % What polarization to image (HH,HV,VV)

%
% Sub-aperture parameters
% Note: Limit azimuth angle size to about 10 degrees, for example, [175,185] degrees
%
minaz = 0;              % Minimum azimuth angle (degrees)
minaz = input('Enter minimum azimuth angle (deg) ');  % Maximum azimuth angle (degrees)
maxaz = 360;            % Maximum azimuth angle (degrees)
maxaz = input('Enter maximum azimuth angle (deg) ');  % Maximum azimuth angle (degrees)

taper_flag = 0;         % Add a hamming taper for sidelobe control

% Define image parameters here
data.Wx = 10;           % Scene extent x (m)
data.Wy = 10;           % Scene extent y (m)
data.Nfft = 8192;       % Number of samples in FFT
data.Nx = 501;          % Number of samples in x direction
data.Ny = 501;          % Number of samples in y direction
data.x0 = 0;            % Center of image scene in x direction (m)
data.y0 = 0;            % Center of image scene in y direction (m)
dyn_range = 70;         % dB of dynamic range to display

% INPUT PARAMETERS END HERE %

% Determine data path
datadir = sprintf('%s%sDomes%s%s',basePath,filesep,filesep,target);

% Determine filename
fname = sprintf('%s%s%s_el%6.4f.mat',datadir,filesep,target,elev);

% Load data
newdata = load(fname);
elev=newdata.data.elev;


% Determine which pulses are located within specified azimuth angles
I = find(and(newdata.data.azim >= minaz, newdata.data.azim <= maxaz));
m=length(I);
if floor(m/2)*2 < m, m=m+1; I(m)=I(m-1)+1; end

[N,M]=size(newdata.data.hh);
dte=(2*pi)/M; % azimuth angle spacing

% Update the phase history
switch pol
    case{'HH'}
        data.phdata = newdata.data.hh(:,I);
    case{'VV'}
        data.phdata = newdata.data.vv(:,I);
    case{'HV'}
        data.phdata = newdata.data.hv(:,I);
end

% Update other parameters needed for imaging
data.AntAzim = newdata.data.azim(I);
data.AntElev = newdata.data.elev * ones(size(data.AntAzim));
data.freq = newdata.data.FGHz * 1e9;

% Calculate the minimum frequency for each pulse (Hz)
data.minF = min(data.freq)*ones(size(data.AntAzim));

% Calculate the frequency step size (Hz)
data.deltaF = diff(data.freq(1:2));

% Determine the number of pulses and the samples per pulse
[data.K,data.Np] = size(data.phdata);

% Add a hamming taper to the data if desired
if taper_flag
    data.phdata = data.phdata .* (hamming(data.K)*hamming(data.Np)');
end

%
% Radar frequency sub-band parameters
% Note: Limit bandwidth to about 2 GHz, for example, [8,11] GHz
%
freq_min=data.freq(1)/1e9
freq_min=input('Enter minimum radar frequnecy (GHz) ');
freq_min=freq_min*1e9;

freq_max=data.freq(end)/1e9
freq_max=input('Enter maximum radar frequnecy (GHz) ');
freq_max=freq_max*1e9;
Ifreq=find(data. freq >= freq_min & data.freq <= freq_max);

%
% Sub-band and sub-aperture phase history data
%
N=length(data.freq);
n=length(Ifreq);
m=data.Np;
temp=zeros(N,m);
%temp(Ifreq,:)=data.phdata(Ifreq,:).*(hamming(n)*hamming(m)');
temp(Ifreq,:)=data.phdata(Ifreq,:).*((hamming(n)')*hamming(m));
data.phdata=temp;
clear temp
%

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% 1. Range-Doppler processing
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
fc=mean(data.freq(Ifreq));  % center/carrier frequency
dfreq=data.deltaF;
c = 299792458;
kc=(2*pi*fc)/c;
dky=2*kc*dte;
dt=1/(N*dfreq);
dR=.5*c*dt;
dy=(2*pi)/(m*dky);
R=dR*(-N/2:N/2-1);
y=dy*(-m/2:m/2-1);
RD=iftx(ifty(data.phdata));

figure(1)
colormap(gray(256))
G=abs(RD)';bzz=1000;imgl;image(R,y,cg*(G-ng));%xyi
title('Range Doppler Image: Slant Plane');
xlabel('Radial Range, m');
ylabel('Doppler Azimuth, m');
pause(1)

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% 2. Backprojection
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% Setup imaging grid
data.x_vec = linspace(data.x0 - data.Wx/2, data.x0 + data.Wx/2, data.Nx);
data.y_vec = linspace(data.y0 - data.Wy/2, data.y0 + data.Wy/2, data.Ny);
[data.x_mat,data.y_mat] = meshgrid(data.x_vec,data.y_vec);
data.z_mat = zeros(size(data.x_mat));

% Call the backprojection function with the appropriate inputs
data = bpBasicFarField(data);

% Display the image
figure(2)
colormap(gray(256))
G=abs(data.im_final);bzz=1000;imgl;image(data.x_vec,data.y_vec,cg*(G-ng));%xyi
title('Backprojection Image: Ground Target Plane');
xlabel('Range, m');
ylabel('Azimuth, m');
pause(1)


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% 3. Range-Doppler Image Transformed into Target's Ground Plane
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
r=R;  
dr=dR;
tec=(maxaz+minaz)/2;
dx_vec=mean(diff(data.x_vec));
dy_vec=mean(diff(data.y_vec));
d=min(dx_vec,dy_vec);
n_up=ceil(dr/d);
m_up=ceil(dy/d);
RD2=upsample_image_2D_fn(RD,n_up,m_up);
dr2=dr/n_up;
dy2=dy/m_up;
n2=N*n_up;
m2=m*m_up;
r2=dr2*(-n2/2:n2/2-1);
r2=r2/cosd(elev); % adjusted for depression/elevation angle
y2=dy2*(-m2/2:m2/2-1);
y2=y2/cosd(elev); % adjusted for depression/elevation angle

X=r2(:)*cosd(tec)*ones(1,m2)-ones(n2,1)*y2*sind(tec);
Y=r2(:)*sind(tec)*ones(1,m2)+ones(n2,1)*y2*cosd(tec);
f=interp_2D_fn(abs(RD2),Y,X,data.y_vec,data.x_vec);

figure(3)
colormap(gray(256))
G=abs(f);bzz=1000;imgl;image(data.x_vec,data.y_vec,cg*(G-ng));%xyi
title('Range Doppler Image: Ground Target Plane');
xlabel('Range, m');
ylabel('Azimuth, m');


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% 4. Polar Format Processing
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
pi2=2*pi;
w=pi2*data.freq;
k=w/c;
kx=2*k(:)*cosd(data.AntAzim)*cosd(elev);
ky=2*k(:)*sind(data.AntAzim)*cosd(elev);

X0=data.Wx/2;           
Y0=data.Wy/2;           
dkx=pi/X0;
dky=pi/Y0;

kxmin=min(min(kx));
kxmax=max(max(kx));
kxc=(kxmax+kxmin)/2;
kx0=(kxmax-kxmin)/2;
kx0=1.2*kx0;
nx=2*ceil(kx0/dkx);
KX=kxc+dkx*(-nx/2:nx/2-1);

kymin=min(min(ky));
kymax=max(max(ky));
kyc=(kymax+kymin)/2;
ky0=(kymax-kymin)/2;
ky0=1.2*ky0;
ny=2*ceil(ky0/dky);
KY=kyc+dky*(-ny/2:ny/2-1);

Fp=interp_2D_fn(data.phdata,kx,ky,KX,KY);
fp=iftx(ifty(Fp));

dx=pi2/(nx*dkx);
x=dx*(-nx/2:nx/2-1);
dy=pi2/(ny*dky);
y=dy*(-ny/2:ny/2-1);

figure(4)
colormap(gray(256))
G=abs(fp)';bzz=1000;imgl;image(x,y,cg*(G-ng));%xyi
title('Polar Format Image: Ground Target Plane');
xlabel('Range, m');
ylabel('Azimuth, m');
axis([-X0 X0 -Y0 Y0])




