function plotBP_arra(N,r)
% Plots the complex frequency response and beampattern for 
% an array  
% H(exp(j*2*pi*r*cos(phi))=exp(-j*(N-1)*pi*r*cos(phi)).*(sin(N*pi*r*cos(phi))./Sin(pi*r*cos(phi)))
% for 10 periods of hz=1/lambda centered around hz = 0
% NOTE: hz=cos(phi)/lambda_0
warning off
figure
cos_phi_limit=1;
cos_phi_step=.001;
cos_phi=-cos_phi_limit:cos_phi_step:cos_phi_limit;    
                                  % cos(phi) vector

% labels
label_x_fr=['{\it{cos(\phi )}}'];
label_title_fr=['Complex Freqency Response of \it{H}(e^{j2{\pi} r cos(\phi )})'];
label_title_bp=['Beampattern \it{BP(\phi )}'];
label_title_pbp=['Polar Beampattern \it{BP(\phi )}'];
label_x_bp=['\it{\phi}'];

% Complex Frequency Response
H_exp_j2pi_r_cos_phi=exp(-j*(N-1)*pi*r*cos_phi).*(sin(N*pi*r*cos_phi)./sin(pi*r*cos_phi));
                                  
% Calculate Magnitude and frequency responses of H(j*2*pi*N))                              
mag_H=abs(H_exp_j2pi_r_cos_phi);
phase_H=angle(H_exp_j2pi_r_cos_phi);


% Calculate Complex Wavelength Response versus phi of H(j*2*pi*x_0/lambda*cos(phi))
phi=-pi:pi/5000:pi;
H_j2pi_r_phi=exp(-j*(N-1)*pi*r*cos(phi)).*(sin(N*pi*r*cos(phi))./sin(pi*r*cos(phi)));  

% Calculate Magnitude and frequency responses of H(j*2*pi*cos(phi)/lambda)                              
mag_Hcos=abs(H_j2pi_r_phi);

% Convert H(j*2*pi*cos(phi)/lambda) to Polar
polar_Hcos=mag_Hcos.*exp(j*phi);

% plot results 
subplot(221)
title(label_title_fr)
hold on
axis square
axis([-cos_phi_limit cos_phi_limit 0 N])
plot(cos_phi,mag_H)
xlabel(label_x_fr), ylabel('Magnitude')

subplot(223)
hold on
axis square
axis([-cos_phi_limit cos_phi_limit -1 1])
plot(cos_phi,phase_H/pi)
xlabel(label_x_fr), ylabel('Phase/\pi')

subplot(222)
title(label_title_bp)
hold on
axis square
plot(phi,mag_Hcos)
xlabel(label_x_bp), ylabel('Magnitude')

subplot(224)
title(label_title_pbp)
hold on
axis equal
plot(polar_Hcos)
xlabel(label_x_bp)