%  Array beam pattern calculation using equations from
%  Digital Signal Processing
%  DeFatta, Lucas, Hodgkiss
%  0 and 180 degrees are broadside
%  90 and -90 degrees are endfire
%
clear
des_pat
c = 1500.0;
d2r = pi / 180;
num_phones = 4;
freq = [8000 12931 17240 30000];
wave_length = c ./ freq;
spacing = wave_length(4);
k = (2.0 * pi) ./ wave_length;
weight = hanning(num_phones)';
weight(1) = weight(1) * 1.7;
weight(2) = weight(2) * 1.5;
n = 0:(num_phones - 1);
steer_angle = 0.0;
for arrival_angle = 0:180,
  pattern(arrival_angle + 1,1) = sum(weight .* exp(-1 * j * n * k(1) * ...
          spacing * (sin(steer_angle * d2r) - sin(arrival_angle * d2r))));
  pattern(arrival_angle + 1,2) = sum(weight .* exp(-1 * j * n * k(2) * ...
          spacing * (sin(steer_angle * d2r) - sin(arrival_angle * d2r))));
  pattern(arrival_angle + 1,3) = sum(weight .* exp(-1 * j * n * k(3) * ...
          spacing * (sin(steer_angle * d2r) - sin(arrival_angle * d2r))));
  pattern(arrival_angle + 1,4) = sum(weight .* exp(-1 * j * n * k(4) * ...
          spacing * (sin(steer_angle * d2r) - sin(arrival_angle * d2r))));
end  
temp = 0:180;
theta = temp .* pi / 180.0;
normalizer = max(max(abs(pattern)));
rho = 25 + (20 .* log10(abs(pattern)/normalizer));
for index = 1:181,
  for jndex = 1:4,
    if( rho(index, jndex) < 0 )
      rho(index, jndex) = 0;
    end
  end
end
angle = angle * (pi / 180.0);
mag_8 = 25 + mag_8;
polar(theta', rho(:,1), 'r')
hold
polar(angle, mag_8, 'g')
title('8 kHz PATTERN (solid = actual dashed = desired)')
grid
mag_12 = 25 + mag_12;
figure
polar(theta', rho(:,2), 'r')
hold
polar(angle, mag_12, 'g')
title('12 kHz PATTERN (solid = actual dashed = desired)')
grid
mag_17 = 25 + mag_17;
figure
polar(theta', rho(:,3), 'r')
hold
polar(angle, mag_17, 'g')
title('17 kHz PATTERN (solid = actual dashed = desired)')
grid
mag_30 = 25 + mag_30;
figure
polar(theta', rho(:,4), 'r')
hold
polar(angle, mag_30, 'g')
title('30 kHz PATTERN (solid = actual dashed = desired)')
grid
