%Michael Battaglia
%Acoustics Project 2 Part 2a
%Modified from "tapered_steeredarray.m"

% You are required to set up an electronically-steered one-dimensional 
% loudspeaker array and plot its beam pattern and particle displacement 
% in the far field. Select the piston parameters so that for frequencies 
% between 300 Hz and 10 kHz it exhibits a main lobe with maximum width 
% equal to 30o , that is pointing at 45o to the left of the array’s 
% normal axis. Clearly state the selected array parameters. Provide 
% beam plots for 3, 6, and 10 kHz.
% Beam pattern 
clear all, close all
b=-1/2:(1/4000):(1/2);
bpi=b*pi;
l=1;
c=354;
f=1500;
lambda=c./f;
k=2*pi*f/c;

aviobj = avifile('ac2a.avi', 'compression', 'Cinepak');

for point= 0:12:720;  % the main lobe points to this direction in degrees
    cs=c/sin(pi*point/180);
    ks=2*pi*f/cs;
    lambdas=2*pi./ks; 

    % Beam pattern
    for j=1:length(bpi)
        p0(j)=sin(pi*l*(sin(bpi(j))/lambda-1/lambdas))/(pi*l*(sin(bpi(j))/lambda-1/lambdas));
        p1(j)=sin(pi*l*((sin(bpi(j))/lambda-1/lambdas)+1))/(pi*l*(sin(bpi(j))/lambda-1/lambdas+1));
        p2(j)=sin(pi*l*((sin(bpi(j))/lambda-1/lambdas)-1))/(pi*l*(sin(bpi(j))/lambda-1/lambdas-1));
        p(j)=p0(j)+0.5*(p1(j)+p2(j));
    end
    polar(bpi,abs(p),'m-')
    title(['Beam pattern for piston size of size length/lambda= ', num2str(l/lambda),'. f = ', num2str(f),' Hz.'])
    title(['Displacement pattern for piston size of size length/lambda= ', num2str(l/lambda)])
    figure(gcf)
    frame = getframe;
    aviobj = addframe(aviobj, frame);
    pause(0.067)
end

aviobj = close(aviobj);

