%  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
close all
c = 1500.0;
d2r = pi / 180;
num_phones = 4;

freq = [100 200 500 1000 2000 5000 10000 20000 50000];
wave_length = c ./ freq;
spacing = .5;
kArray = (2.0 * pi) ./ wave_length;

%weight = hanning(num_phones)';
weight = ones(1,num_phones);

n = 0:(num_phones - 1);
steer_angle = 90.0;

temp = -90:90;
theta = temp .* pi / 180.0;

for i=1:length(freq),
    index = 1;
    for arrival_angle = -90:90,
        pattern(i,index) = sum(weight .* exp(-1 * j * n * kArray(i) * ...
                spacing * (sin(steer_angle * d2r) - sin(arrival_angle * d2r))));
        index = index + 1;
    end
    normalizer = max(max(abs(pattern(i,:))));
    rho(i,:) = 25 + (20 .* log10(abs(pattern(i,:))/normalizer));
    for k = 1:181,
        if( rho(i,k) < 0 )
            rho(i,k) = 0;
        end
    end
    polar(theta, rho(i,:))
    title(['F = ', num2str(freq(1)), 'Hz']);
    grid
    if i!= figure
end

