From: May on
function varargout = Week4(varargin)
% WEEK4 M-file for Week4.fig
% WEEK4, by itself, creates a new WEEK4 or raises the existing
% singleton*.
%
% H = WEEK4 returns the handle to a new WEEK4 or the handle to
% the existing singleton*.
%
% WEEK4('CALLBACK',hObject,eventData,handles,...) calls the local
% function named CALLBACK in WEEK4.M with the given input arguments.
%
% WEEK4('Property','Value',...) creates a new WEEK4 or raises the
% existing singleton*. Starting from the left, property value pairs are
% applied to the GUI before Week4_OpeningFcn gets called. An
% unrecognized property name or invalid value makes property application
% stop. All inputs are passed to Week4_OpeningFcn via varargin.
%
% *See GUI Options on GUIDE's Tools menu. Choose "GUI allows only one
% instance to run (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES

% Edit the above text to modify the response to help Week4

% Last Modified by GUIDE v2.5 19-Jun-2010 16:01:43

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @Week4_OpeningFcn, ...
'gui_OutputFcn', @Week4_OutputFcn, ...
'gui_LayoutFcn', [] , ...
'gui_Callback', []);
if nargin && ischar(varargin{1})
gui_State.gui_Callback = str2func(varargin{1});
end

if nargout
[varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT


% --- Executes just before Week4 is made visible.
function Week4_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% varargin command line arguments to Week4 (see VARARGIN)

% Choose default command line output for Week4
handles.output = hObject;

% UIWAIT makes Week4 wait for user response (see UIRESUME)
% uiwait(handles.figure1);

% Update handles structure
guidata(hObject, handles);


% --- Outputs from this function are returned to the command line.
function varargout = Week4_OutputFcn(hObject, eventdata, handles)
% varargout cell array for returning output args (see VARARGOUT);
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)

% Get default command line output from handles structure
varargout{1} = handles.output;

handles.punishmemory = zeros(1,20);

handles.numauth = 0;
handles.numunauth = 0;
handles.traffic = zeros (1,20);
handles.punishmemorytwo = zeros(1,20);
handles.good = zeros (1,20);
handles.timestep = 0;
handles.bpert = zeros(1,100);

[handles] = trafficgen(handles);

[handles] = totaltraffic(handles);

[handles] = authuser(handles);

[handles] = unauthuser(handles);

[handles] = collide(handles);

[handles] = police(handles);

[handles] = punish(handles);

[handles] = collidemain (handles);

guidata(hObject, handles);


% --- Executes on button press in scantraffic.
function scantraffic_Callback(hObject, eventdata, handles)
% hObject handle to scantraffic (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
[handles] = primaryuser(handles);

[handles] = totaltraffic(handles);

[handles] = authuser(handles);

[handles] = unauthuser(handles);

[handles] = punish(handles);

[handles] = collide(handles);

[handles] = police(handles);

[handles] = collidemain (handles);

[handles] = badevent (handles);

[handles] = totalusers (handles);

[handles] = graphdata (handles);

[handles] = statistics (handles);

[handles] = time (handles);

guidata(hObject, handles);



function numauth_Callback(hObject, eventdata, handles)
% hObject handle to numauth (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of numauth as text
% str2double(get(hObject,'String')) returns contents of numauth as a
% double

input = str2double(get(hObject,'String'));

if (isempty(input))
set(hObject,'String','0')
end
if isnan(input)
errordlg('You must enter a numeric value','Bad Input','modal')
end

handles.numauth = input;
guidata(hObject, handles);


% --- Executes during object creation, after setting all properties.
function numauth_CreateFcn(hObject, eventdata, handles)
% hObject handle to numauth (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
% See ISPC and COMPUTER.

if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end


function numunauth_Callback(hObject, eventdata, handles)
% hObject handle to numunauth (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of numunauth as text
% str2double(get(hObject,'String')) returns contents of numunauth as a double

input = str2double(get(hObject,'String'));

if (isempty(input))
set(hObject,'String','0')
end
if isnan(input)
errordlg('You must enter a numeric value','Bad Input','modal')
end

handles.numunauth = input;
guidata(hObject, handles);

% --- Executes during object creation, after setting all properties.
function numunauth_CreateFcn(hObject, eventdata, handles)
% hObject handle to numunauth (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
% See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end

function [handles] = trafficgen (handles)

% Generate completely random traffic data

handles.traffic = zeros(1,20);

for n = 1:20

r = rand;

if r < .4
handles.traffic(n) = 0;

else
handles.traffic(n) = 1;

end
end




function [handles] = totaltraffic (handles)

% Save the number of open channels

handles.Ttraffic = 0;

for n = 1:20;

handles.Ttraffic = handles.Ttraffic + handles.traffic(n);

end


function [handles] = primaryuser(handles)

for i = 1:3

low = 1;
high = 20;
change = round (low + (high - low) * rand);

if handles.traffic(change) == 1
handles.traffic(change) = 0;
else
handles.traffic(change) = 1;
end
end





function [handles] = authuser(handles)

% Simulate authorized users

handles.authdata = zeros(1,20);

if handles.Ttraffic < 20

for i = 1:handles.numauth

m = floor(20*rand + 1);

while handles.traffic(m) ~= 0
m = floor(20*rand + 1);
end

handles.authdata(m) = handles.authdata(m) + 1;
end

else
handles.authdata = handles.authdata;

end




function [handles] = unauthuser(handles)

% Simulate unauthorized users

handles.unauthdata = zeros(1,20);

for i =1:20
if handles.punishmemory(i) > 2
handles.punishmemorytwo(i) =1;
end
end

handles.open = handles.punishmemorytwo +handles.traffic;

if handles.Ttraffic < 20

for i = 1:handles.numunauth

m = floor(20*rand + 1);

while handles.open(m) ~= 0
m = floor(20*rand + 1);
end

handles.unauthdata (m) = handles.unauthdata (m) + 1;

end

else
handles.unauthdata = handles.unauthdata;
end




function [handles] = punish(handles)

% Save the punishment data

for i = 1:20

if handles.policedata(i) == 1
handles.punishmemory(i) = handles.punishmemory(i) + 1;
elseif handles.collision(i) == 1
handles.punishmemory(i) = handles.punishmemory(i) + 1;
else
handles.punishmemory(i) = 0;

end
end


function [handles] = police (handles)

% Police data

handles.policedata = zeros(1,20);

for i = 1:10

if handles.unauthdata(i) == 1 && handles.authdata(i) ==0;
handles.policedata(i) = 1;

end
end


function [handles] = collide (handles)

% # of Collisions

handles.collision = zeros(1,20);

for i = 1:20

if (handles.unauthdata(i) > 1 && handles.authdata(i) <1)
handles.collision(i) = 1;

elseif (handles.unauthdata(i) >= 1 && handles.authdata(i) >= 1)
handles.collision(i) = 1;
end
end

function [handles] = collidemain (handles)

handles.collisionmain = zeros(1,20);

for i = 1:20

if (handles.unauthdata(i) > 1 && handles.authdata(i) < 1)
handles.collisionmain(i) = 1;

elseif (handles.unauthdata(i) >= 1 && handles.authdata(i) >= 1)
handles.collisionmain(i) = 1;

elseif (handles.authdata(i) > 1 && handles.unauthdata(i) < 1)
handles.collisionmain(i) = 1;
end

end




function [handles] = badevent (handles)

handles.bad = 0;

for i =1:20

if handles.collisionmain(i) == 1
handles.bad= handles.bad + handles.authdata(i) + handles.unauthdata(i);
elseif handles.policedata(i) ==1
handles.bad= handles.bad + handles.unauthdata(i);
end
end

function [handles] = totalusers (handles)

handles.total = 0;

for i = 1:20

handles.total = handles.total + handles.authdata(i) + handles.unauthdata(i);

end

function [handles] = statistics (handles)


if handles.total ~=0;
handles.bpert(handles.timestep) = handles.bad / handles.total;
end

function [handles] = time (handles)

handles.timestep = handles.timestep +1;



function [handles] = graphdata (handles)


ylim = 1;


handles.alldata = [handles.traffic ; handles.authdata; handles.unauthdata];


bar(handles.axes1, handles.alldata', 2.75);
set(handles.axes1,'XTick',1:20);
legend(handles.axes1, 'Primary User', 'Authorized User', 'Restricted User');

for i = 1:20
if handles.collisionmain(i) == 1;
handles.authdata(i) = 0;
handles.unauthdata(i) =0;
end
if handles.policedata(i) == 1;
handles.unauthdata(i) = 0;
end

end

for i=1:20
if handles.traffic(i) ~=0;
handles.traffic(i) = 1;
end
end

handles.alldata = [handles.traffic ; handles.authdata; handles.unauthdata];


bar(handles.axes2, handles.alldata', 2.75);
set(handles.axes2,'XTick',1:20);

colormap( [ 0 0 0; 0 0 1; 1 0 0;])
legend(handles.axes2, 'Primary User', 'Authorized User', 'Restricted User');

handles.baddata = [handles.collisionmain; handles.policedata];

bar(handles.axes6, handles.baddata', 2);
set(handles.axes6,'XTick',1:20);
legend(handles.axes6, 'User Collision', 'Police Collision');


plot(handles.axes8, handles.bpert);
set(handles.axes8,'YTick', 0:3);