You are an expert in writing code in many different languages. Your goal is to perform code completion for the following code, keeping in mind the rest of the code and the file meta data.
    Metadata details:
    filename: planar_sig_gen.m
    path of file: ./repos/Antenna_Theory_Analysis_and_Design_C.A.Balanis---MATLAB/Chapter 16/Beamforming
    
    the code of the file until where you have to start completion: %************************************************************************
%	[S_SOI, s, m] = planar_sig_gen(p,d,nn,NN,type,sig,noise,E_pattern)
%************************************************************************
%	SIG_GEN is a MATLAB function that generates the SOI and SNOIs
%   signals (with and without Gaussian noise)
%
%	Input Parameters Description
%	----------------------------
%	- p          number of elements in x axis and y axis
%	- d          inter-element spacing (in wavelength)
%                default value is (0.5,0.5) (Nyquist rate)
%   - nn         number of samples
%   - NN         number of samples per cycle or symbol
%   - type       option: 'sinusoid' or 'bpsk'
%   - sig        signals amplitudes and directions of SOI and SNOI
%   - noise      amplitude an
    
function that generates the SOI and SNOIs
%   signals (with and without Gaussian noise)
%
%	Input Parameters Description
%	----------------------------
%	- p          number of elements in x axis and y axis
%	- d          inter-element spacing (in wavelength)
%                default value is (0.5,0.5) (Nyquist rate)
%   - nn         number of samples
%   - NN         number of samples per cycle or symbol
%   - type       option: 'sinusoid' or 'bpsk'
%   - sig        signals amplitudes and directions of SOI and SNOI
%   - noise      amplitude and variance values
%	- E_pattern  samples of element pattern (column vector)
%                if interested in array factor only, enter 1
%                default value is 1
%
%	Output Parameters Description
%	-----------------------------
%	- S_SOI      SOI reference signal (column vector)
%   - s          matrix containing all signals including noise
%   - m          frequency multiplier for sinusoidal signals
%************************************************************************
%	Credits:
%		S. Bellofiore
%       Zhiyong Huang
%
%-----------------------------------------------------------------------------

function [S_SOI, s, m] = planar_sig_gen(p,d,nn,NN,type,sig,noise,E_pattern)

%%%%%%%%%%%%%%% Parameters initialization %%%%%%%%%%%%%%%
k0 = 2*pi;                                                 % k in free space
n_sig = size(sig,1);                                       % Total number of signals
m = [];

x = linspace(0,p(1)-1,p(1));
y = linspace(0,p(2)-1,p(2));

sigr = [sig(:,1) pi/180*sig(:,2:3)];                    % degrees to radians conversion

[X,Y] = meshgrid(x,y);

err_1 = sprintf('\nSignal type not supported...');
%-------------------------------------------------------%

%%%%%%%%%%%%%%%%%%%%% Generating SOI %%%%%%%%%%%%%%%%%%%%
PSI_SOI = -k0*d(1,1)*sin(sigr(1,2))*cos(sigr(1,3))*X - k0*d(1,2)*sin(sigr(1,2))*sin(sigr(1,3))*Y;

PSI_SOI = reshape(PSI_SOI,1,p(1)*p(2));

switch type
   case 'bpsk'
      STATE1 = sum(100*clock);
      rand('state',STATE1);
      s_rand = round(rand(nn,1));
      s_rand_index = find(s_rand == 0);
      s_rand(s_rand_index) = -1;
      s_NN = ones(1,NN);
      s_rand = (s_rand*s_NN)';
      s_rand = reshape(s_rand,size(s_rand,1)*size(s_rand,2),1);
      [s_rand,PSI_SOI] = meshgrid(s_rand,PSI_SOI);
      s = sigr(1,1)*(s_rand.*exp(i*PSI_SOI));
      S_SOI  = s(1,:);
   otherwise
      error(err_1);
end