%==========================================================================
% Intf_Diffn_1D_Circ_Aperture_Thy.m
%
% 1-D interference-diffraction associated with N circular apertures
% - simplest theory - far-field/plane-wave approx!
% Sound waves assumed to be propagating in free air/great wide-open!
%
% N circular apertures distributed symmetrically along horizontal y-axis
% x-axis is vertical. x-y-z right-handed coordinate system. 
%==========================================================================
%
% Written by Prof. Steven Errede  Last Updated: Feb. 8, 2011 10:30 hr
%
%==========================================================================
close all;
clear all;

single     thtr(1800);
single     thtd(1800);
single    Itot1(1800);
single     SIL1(1800);

single     yscr(2000);
single    Itot2(2000);
single     SIL2(2000);

single   Itotxy(2000,2000);
single LgItotxy(2000,2000);
single    SILxy(2000,2000);
 
 
% Specify the # of apertures:
Napr   = 10; % 1; 2; 4; 10;

% Specify the numerical values of parameters:
Io     =    1.0;    % intensity from single slit (Watts/m^2)
Ir     = 1.0*10^-12;% reference sound intensity  (Watts/m^2)
Vair   =  343.0;    % speed of propagation of sound - free air (m/s)
freq   = 1000.0;    % frequency (Hz or cps)
lambda = Vair/freq; % wavelength (m)
Lobs   =   10.0;    % observer distance         (m) n.b. lambda << Lobs
Dsrc   =    1.0;    % distance between apertures(m) n.b.   Dsrc << Lobs
Rapr   =    1.0;    % 0.6; 1.0; aperture radius (m) n.b.   Rapr << Lobs

nu     =      1;    % order of bessel function of 1st kind (see below)

%===================================================================
% Calculate Itot, SIL vs. theta-y along horizontal y-axis @ x = 0:
%===================================================================
Thetad =  -90.0;    % angle theta of observer in degrees
dTheta =    0.1;    % step angle in degrees

for i = 1:1800;
     thtd(i) = Thetad;            % angle theta of observer in degrees
     Thetar  = (pi/180.0)*Thetad; % angle theta of observer in radians
     thtr(i) = Thetar;
     
     delta   = ((2.0*pi*Dsrc)/lambda)*sin(Thetar);  % int'f phase (radians)
     rho     = ((2.0*pi*Rapr)/lambda)*sin(Thetar); % diffn phase (radians)
     
    Itot1(i) = Io*(sin(Napr*delta/2.0)/sin(delta/2.0))^2*((2.0*bessel(nu,rho))/rho)^2; % total intensity (Watts/m^2)
     SIL1(i) = 10.0*log10(Itot1(i)/Ir); % Sound Intensity Level (dB)
     
     Thetad  = Thetad + dTheta;   % increment angle for next calculation
end

%===================================================================
% Calculate Itot, SIL vs. yscreen along horizontal y-axis @ x = 0:
%===================================================================
 y = -50.00; % starting position on screen (m)
dy =   0.05; % step size on screen (m);
for i = 1:2000;
     yscr(i) = y;            % position of observer on perp. screen (m)
     Thetar  = atan(y/Lobs); % angle theta of observer in radians
     
     delta   = ((2.0*pi*Dsrc)/lambda)*sin(Thetar); % int'f phase (radians)
     rho     = ((2.0*pi*Rapr)/lambda)*sin(Thetar); % diffn phase (radians)
     
    Itot2(i) = Io*(sin(Napr*delta/2.0)/sin(delta/2.0))^2*((2.0*bessel(nu,rho))/rho)^2; % total intensity (Watts/m^2)
     SIL2(i) = 10.0*log10(Itot2(i)/Ir); % Sound Intensity Level (dB)
     
     y  = y + dy;   % increment screen position for next calculation
end

figure(01);
plot(thtd,Itot1,'b');
grid on;
xlabel('theta (degrees)');
ylabel('Intensity (Watts/m^{2})');
title('Intensity vs. theta-y at x = 0');

figure(02);
semilogy(thtd,Itot1,'b');
grid on;
xlabel('theta (degrees)');
ylabel('Intensity (Watts/m^{2})');
title('Log10 Intensity vs. theta-y at x = 0');

figure(03);
plot(thtd,SIL1,'b');
grid on;
xlabel('theta (degrees)');
ylabel('SIL (dB)');
title('SIL vs. theta-y at x = 0');

figure(04);
polar(thtr,SIL1,'b');
grid on;
xlabel('theta (degrees)');
ylabel('SIL (dB)');
title('Polar Plot of SIL vs. theta-y at x = 0');


figure(11);
plot(yscr,Itot2,'b');
grid on;
xlabel('Yscreen (m)');
ylabel('Intensity (Watts/m^{2})');
title('Intensity vs. Yscreen at x = 0');

figure(12);
semilogy(yscr,Itot2,'b');
grid on;
xlabel('Yscreen (m)');
ylabel('Intensity (Watts/m^{2})');
title('Log10 Intensity vs. Yscreen at x = 0');

figure(13);
plot(yscr,SIL2,'b');
grid on;
xlabel('Yscreen (m)');
ylabel('SIL (dB)');
title('SIL vs. Yscreen at x = 0');

%==========================================================================
fprintf('\n Very CPU-intensive I(x,y) vs. x,y calcs - please be patient!! \n')
%==========================================================================

%========================================
% Calculate 2D Itot, SIL vs. x,y-screen:
%========================================
 x = -50.00; % x-starting position on screen (m)
dx =   0.05; % x-step size on screen (m);
for j = 1:2000;
     xscr(j) = x; % x-position of observer on perp. screen (m)

     y = -50.00; % y-starting position on screen (m)
    dy =   0.05; % y-step size on screen (m);
    for i = 1:2000;
         yscr(i) = y; % y-position of observer on perp. screen (m)
         rscr    = sqrt((xscr(j))^2 + (yscr(i))^2); % radial pos'n on perp. screen (m)
         Thetar  = atan(rscr/Lobs); % angle theta of observer in radians
         Thetay  = atan(y/Lobs);    % y-projected angle theta-y in radians
         delta   = ((2.0*pi*Dsrc)/lambda)*sin(Thetay); % int'f phase (radians)
         rho     = ((2.0*pi*Rapr)/lambda)*sin(Thetar); % diffn phase (radians)
     
     Itotxy(j,i) = Io*(sin(Napr*delta/2.0)/sin(delta/2.0))^2*((2.0*bessel(nu,rho))/rho)^2; % total intensity (Watts/m^2)
   LgItotxy(j,i) = log10(Itotxy(j,i));     % log10 of total intensity (Watts/m^2)
      SILxy(j,i) = 10.0*log10(Itotxy(j,i)/Ir);      % Sound Intensity Level (dB)
   
         y  = y + dy;   % increment y-screen position for next calculation
    end
     x  = x + dx;   % increment x-screen position for next calculation
end

figure(21);
surf(xscr,yscr,Itotxy);
shading interp;
xlabel('Xscreen (m)');
ylabel('Yscreen (m)');
zlabel('Intensity (Watts/m^{2})');
title ('Intensity vs. Xscreen-Yscreen');

figure(22);
surf(xscr,yscr,LgItotxy);
shading interp;
xlabel('Xscreen (m)');
ylabel('Yscreen (m)');
zlabel('Log10(Intensity) (Watts/m^{2})');
title ('Log10(Intensity) vs. Xscreen-Yscreen');

figure(23);
surf(xscr,yscr,SILxy);
shading interp;
xlabel('Xscreen (m)');
ylabel('Yscreen (m)');
zlabel('SIL (dB)');
title ('SIL vs. Xscreen-Yscreen');

%==========================================================================
beep;
fprintf('\n Calculation of interference-diffraction through 1-D linear array of N circular apertures completed !!! \n')
%==========================================================================