%MMI361 Project2 Part2
%beam and displacement of swept steered array

clear all;
close all;
clc;

fs = 8000;
l=1;
c=344;
f=1500;
lambda=c./f;
k=2*pi*f/c;
b=-1/2:(1/4000):(1/2);
bpi=b*pi;
points = [0:1:90,90:-1:-90,-90:1:90,90:-1:-90,-90:1:0];   %the main lobe points to this direction in degrees

for i = 1:length(points);
    point = points(i);
    cs=c/sin(pi*point/180); 
    ks=2*pi*f/cs; 
    lambdas=2*pi./ks; 

    X = 5*cos(pi/6);
    Y = 5*sin(pi/6);
    R=sqrt((X^2)+(Y^2));
    sin_tn=Y./sqrt(X^2+Y^2);
    m=sin_tn/lambda;
    s=1./lambdas;
    u=m-s;
    A(i)=(sin(pi*l*u)/(pi*l*u) + 0.5*sin(pi*l*(u+1))/(pi*l*(u+1)) + 0.5*sin(pi*l*(u-1))/(pi*l*(u-1)))*cos(k*R)/R;
end

figure(1)
plot(A);

f = 1500;
time = 4;
t = 0:1/fs:time - 1/fs;
t = sin(2*pi*f*t);

for i = 1:length(t)
    t(i) = t(i) * A(ceil(i*(length(A)/length(t))));
end;
t = .99 * t./max(abs(t));
wavwrite(t,fs,'sound2.wav');
soundsc(t,fs);
plot(t);
title( 'Envelope of the Received Signal' );
xlabel( 'Time' );
ylabel( 'Amplitude' );
