You are on page 1of 37

AKNOWLEDGEMENT

I would like to thank APU, for the opportunity given to me to work on this assignment. And also
my module lecturer Dr. Satish Kumar for his continuous assistance over the course of the
semester. Thank you sir.

Page 1 of 37
Contents
AKNOWLEDGEMENT ..................................................................................................................................... 1
TABLE OF FIGURES ........................................................................................................................................ 3
INTRODUCTION ............................................................................................................................................. 4
DESIGN FLOW OF ROUTINES......................................................................................................................... 5
TASK 3 ....................................................................................................................................................... 5
TASK 4 ....................................................................................................................................................... 5
FORMAT AND STYLE OF ROUTINES ............................................................................................................... 6
TASK 3 ....................................................................................................................................................... 6
Guide ..................................................................................................................................................... 6
Comment .............................................................................................................................................. 7
Plot Function ......................................................................................................................................... 7
Histogram .............................................................................................................................................. 8
CALLBACKS .......................................................................................................................................... 10
If else statement ................................................................................................................................. 11
Error Dialog ......................................................................................................................................... 11
TASK 4 ..................................................................................................................................................... 12
For loop ............................................................................................................................................... 12
Set Handles ......................................................................................................................................... 12
RESULTS AND ANALYSIS .............................................................................................................................. 13
TASK 3 ..................................................................................................................................................... 13
TASK 4 ..................................................................................................................................................... 14
DISCUSSION................................................................................................................................................. 15
TASK 3. .................................................................................................................................................... 15
TASK 4 ..................................................................................................................................................... 15
PROBLEMS ENCOUNTERED. .................................................................................................................... 16
CONCLUSION............................................................................................................................................... 17
REFERENCES ................................................................................................................................................ 17

Page 2 of 37
TABLE OF FIGURES
Figure 1: Guide .............................................................................................................................................. 6
Figure 2: Comment ....................................................................................................................................... 7
Figure 3: Coding for Frequency Domain Graph ............................................................................................ 7
Figure 4: Frequency Domain Graph .............................................................................................................. 8
Figure 5: coding for histogram ...................................................................................................................... 9
Figure 6: histogram ....................................................................................................................................... 9
Figure 7: Color Specification ....................................................................................................................... 10
Figure 8: Callback ........................................................................................................................................ 10
Figure 9: If- Else Statement ......................................................................................................................... 11
Figure 10: Error Dialog code ....................................................................................................................... 11
Figure 11: Error Dialog in GUI ..................................................................................................................... 11
Figure 12: For Loop ..................................................................................................................................... 12
Figure 13: Set Handles ................................................................................................................................ 12
Figure 14: Voice Recorder GUI .................................................................................................................... 13
Figure 15: Traffic Light GUI ......................................................................................................................... 14

Page 3 of 37
INTRODUCTION
The assignment required students to use the programming software MATLAB which is short for
Matrix Laboratory. MATLAB has an ability to develop algorithms, analyze data and create
application as well as models. MATLAB is currently used by more than a million engineers,
designers and researchers in industry and students due to its capacity to concentrate on
calculation structure, and not on low-level details and also its wide toolbox of numeric/image
library capacities which permit users to come up with model developments from algorithms.

The assignment required students to carry out two tasks, task 3 and task 4. These tasks will be
briefly described below.

For Task 3, a system to recognize the spoken words for a set of numbers from 0 to 9 was
designed. The system was designed to be able to recognize the users spoken voice and also be
able to play the voice back when required. If a number which isnt in the range of the selected
values was chosen, there will be an error message which will require to re-enter a value within
the specified range. Two graphs were also plotted, one showing the graph of time against
amplitude and the other graph, a histogram of the amplitude of the recorded voice.

For task 4, students were required to make a traffic light system also using the GUI. Which can
be used to solve real life traffic problems. The assignment question seemed to be open ended in
the sense that each student was required to solve the Task based on his/her understanding of the
question. The GUI makes writing the codes a whole lot easier because a code for a particular UI
function can easily be written under the UI code via the call backs rather than searching for it
through the main GUI editor because this could take ages depending on the length of the codes.
The traffic light consists of a time interval pop up menu which has three variables for the time it
takes for the green light to switch to red. And also a simulation time in a pop up menu which
determines how many times the full sequence will run.

Page 4 of 37
DESIGN FLOW OF ROUTINES
TASK 3
When the program begins to run, the user was required to select an already preset rage of
numbers (0 - 9) from a pop up menu. When a number is selected, the user is then required to
record the number which has been selected. The software then saves the audio which has been
recorded. The user is then required to write a number in the range of 0-9 (as that was the range
given in the question) and press the push button that says play. This then makes the previously
saved file to play i.e. if a user selects the number nine, he should only be able to type number
nine in the edit box or else an error will show. After all these steps are complete, the user has the
choice to plot either an amplitude-time graph or a histogram by clicking on the respective push
buttons.

TASK 4
When the program is first started, the user is required to select a time interval from the pop up
menu that says time interval. The user has three options for the time interval it could be 5, 10 or
15 seconds. The user is then required to select a simulation time for the program which can also
be selected from a pop up menu the user also has three options for the simulation time. It can be
1, 2 or 3 times. The simulation time is how many times the program will run the traffic light
sequence from red to green. Assuming the simulation time is 3, the program will switch from red
to green three times before stopping the program. The user is then required to enter a value in the
edit box. This value is the probability of a car being at the traffic light. Probability is usually
between zero and one so any character aside form that range will be taken as an incorrect value
and an error dialogue will appear prompting the user to select a value in the correct range. The
text boxes show the number of cars before the program runs and also the number of cars left after
the program has finished running.

Page 5 of 37
FORMAT AND STYLE OF ROUTINES
This assignment required students to work using mainly the GUI (graphical user interface);
below is an in depth explanation of the UI control objects and conditions that were used when
writing the program.

TASK 3
Guide
The guide is where all the design work is done. The GUIDE contained all the UI controls
which were used in writing the program.
a) Static boxes were used for the headings of the UI controls and also for the main heading
of the GUI.
b) Edit box which was used to input numbers for the voice play back
c) Push-buttons which contained the commands that enabled the program to run.
d) Pop-up menu listing which had the numbers from 0-9.
e) Axes components which were used for plotting the histogram and the frequency domain
graph.

Figure 1: Guide

Page 6 of 37
Comment
The comment is the percentage character which is seen at the staring of the sentence. The
comment highlights the sentence in green color. They do not affect the program and therefore
will not show in the command window.

Figure 2: Comment

Plot Function
Two types of graphs were used a frequency domain graph and a histogram
f) Frequency Domain Graph
The plot function is used to plot graph from the data. In this particular assignment, Plot (t,
read zero) was used where t is function for x axis and read zero for y axis. The r
function is to add color to the plot line. The labelling used for the x and y axis was done
by using xlabel (name of the intended axis) and ylabel (name of the intended axis).

Figure 3: Coding for Frequency Domain Graph

Page 7 of 37
Figure 4: Frequency Domain Graph

Histogram
The hist function is used to plot the histogram from the data. Hist(x) was used where x is
function that reads the previously saved waveform. The labelling used for the x and y
axis was done by using xlabel (name of the intended axis) and ylabel (name of the
intended axis).

Page 8 of 37
Figure 5: coding for histogram

Figure 6: histogram

Page 9 of 37
Figure 7: Color Specification

CALLBACKS
Callbacks is a way in which the code for a particular UI control can be gotten directly from
the guide. This reduces the time it takes in scrolling through all the codes to find a particular
UI control. It can be gotten by right clicking on the intended UI and then going to view
callbacks and then selecting callback.

Figure 8: Callback

Page 10 of 37
If else statement
This executes the statement if a condition is met.

Figure 9: If- Else Statement

Error Dialog
An error dialog displays when the input does not meet the requirement.

Figure 10: Error Dialog code

Figure 11: Error Dialog in GUI

Page 11 of 37
TASK 4
Since both task 3 and 4 required the use of graphs, many of the same UI control objects and
conditions were used. However, the ones which were not repeated will be explained below.

For loop
In MATLAB, a for loop does pretty much the same thing as it does in any other software.
The for loop makes a program run a certain number of times. In this case, the for loop is
what was used to determine the simulation time. A for loop is seen in the figure below.

Figure 12: For Loop

In the figure above, the number of times the code loops is determined from the first line
of the coding above where b is the simulation time.

Set Handles
The set handles is what enables an output to show in the guide when the program is
running. In this case, the set handles was use to show the number of cars before the traffic
sequence runs and also the number of cars after it has finished running.

Figure 13: Set Handles

Page 12 of 37
RESULTS AND ANALYSIS
TASK 3
The guide shows all the results gotten including the graph and the histogram. The results tend to
be dependent on the previous gotten results in the sense that you need to select a number from
the first pop up menu before you can record your voice and it is the voice which has been
recorded that will play back when a digit is entered in the edit box. The figure below shows how
the outputs appear when the number 0 is selected.

Figure 14: Voice Recorder GUI

The results for the mean, median, standard deviation and variance were shown after the calculate
statistics button was selected. Below are the statistics shown after the button was pushed for 0.

mean:-0.000001
standard deviation:0.077372
median:0.000000
variance:0.005986

Page 13 of 37
TASK 4
Similarly to task 3, the results gotten are also shown in the GUI.

Figure 15: Traffic Light GUI

In this traffic light system, a picture was added to enhance the looks of the GUI the picture was
gotten by following the three steps in the code below.

pic = imread('aputraffic.png', 'PNG');


axes(handles.axes2)
imshow(pic)
This system also had a function to make the pop-up menus appear to be blinking. This was done
as shown in the steps below

set(handles.red,'enable','off');
set(handles.green,'enable','off');
The red and green shown above are the tags for the red and green push buttons respectively.

Page 14 of 37
DISCUSSION
TASK 3.
The task 3 in the assignment required students to design a system for the spoken words from zero
to ten using a GUI.

Immediately the user selects a number from the pop up menu, he has two record his/her voice
because the time window for recording is set at 2 seconds. The reason behind this is that any
more time than two seconds seems redundant and long. The user then has to select a number
between 0 and 9 from the edit box. If a number that isnt in that range is selected, the error
message pops up. When the number is selected, the user then has to press the play button to hear
the audio of the selected number. The user then has the option to either plot the frequency
domain graph or the histogram by the use of their push buttons respectively. The user can also
calculate the mean, standard deviation, variance or median by clicking the push button which
shows calculate statistics.

TASK 4
Task 4 required students to make a traffic light using a GUI which functions based on the
probability of the availability of a car.

For task 4, the user has the option of selecting a time interval of either 5, 10 or 15 seconds. This
is what determines how long the red light will remain red before it changes to green. The red
light and green light changing was done by using the set handles to enable and disable the push
buttons for the red and green lights respectively. The user was then prompted to select a
simulation time which was from a pop up menu between the range of one and three. The
simulation time determines how long the program runs. After this, the user is then required to
enter the probability of a car being at the traffic light. The probability is what determined the
number of cars at the traffic light. The probability was set at fixed values so as to prevent the
total number of waiting cars from being a decimal. The probability cases are shown below.

(c > 0) && (c <= 0.3), C=5;

(c > 0.3) && (c <= 0.7), C=7

(c > 0.7) && (c <= 1), C=10;


Where C is the number of cars and c is the probability of cars.

Page 15 of 37
After the above steps were completed, the user then had to select the run button. The run button
executed the whole program and showed the output in two text boxes. One showed the number
of cars while the other showed the remaining number of cars after the traffic light had changed.

PROBLEMS ENCOUNTERED.
While carrying out the recommended tasks, there were numerous problems encountered. The list
below shows the problems encountered and how they were solved.
PROBLEMS SOLUTIONS
For loop did not run correctly An end statement was put at the end of the for
loop
The image kept on showing as a background The file was saved as a PNG file.
on the GUI instead of just at a corner
The output of the GUI kept on crashing The programs running on the computer were
reduced.
Error dialogue did not run Inverted commas were added to the dialogue
Tagging got confusing and mixed up Each UI control was given a separate tag of
its own
Callback function failed Proper tag was given to the call back

Page 16 of 37
CONCLUSION
The main learning outcome of this module is to be able to formulate solutions to common
engineering problems. The assignment given was taken as an engineering problem and it was
tackled ruthlessly until a solution was gotten. Despite the tricky nature of the questions, the tasks
were completed in time even though there were numerous problems. The voice recorder and the
traffic light system were completed successfully.

REFERENCES
Below are some of the references which were used while this assignment was being carried out.

Gupta, A. K. 2011. MATLAB By Examples: Multipages GUI forms: combining from


muliple GUI m files : Links approach. [online] Available at:
http://matlabbyexamples.blogspot.com/2011/10/multipages-gui-forms-combining-
from.html [Accessed: 1 Feb 2015].

Mathworks.com. 2014. Input Parser Validation Functions - MATLAB & Simulink.


[online] Available at: http://www.mathworks.com/help/matlab/matlab_prog/input-parser-
validation-functions.html [Accessed: 25 Jan 2015].

Mathworks.com. 2014. Store or retrieve GUI data - MATLAB guidata. [online] Available
at: http://www.mathworks.com/help/matlab/ref/guidata.html [Accessed: 24 Jan 2014].

Duane C. Hanselman, 2011. Mastering MATLAB. 1 Edition. Prentice Hall.


Stormy Attaway, 2013. Matlab, Third Edition: A Practical Introduction to Programming
and Problem Solving. 3 Edition. Butterworth-Heinemann.

Page 17 of 37
APPENDIX
TASK 3
function varargout = test1(varargin)
% TEST1 MATLAB code for test1.fig
% TEST1, by itself, creates a new TEST1 or raises the existing
% singleton*.
%
% H = TEST1 returns the handle to a new TEST1 or the handle to
% the existing singleton*.
%
% TEST1('CALLBACK',hObject,eventData,handles,...) calls the local
% function named CALLBACK in TEST1.M with the given input arguments.
%
% TEST1('Property','Value',...) creates a new TEST1 or raises the
% existing singleton*. Starting from the left, property value pairs are
% applied to the GUI before test1_OpeningFcn gets called. An
% unrecognized property name or invalid value makes property application
% stop. All inputs are passed to test1_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
global a;

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

% Last Modified by GUIDE v2.5 31-Jan-2015 18:13:43

% Begin initialization code - DO NOT EDIT


gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @test1_OpeningFcn, ...
'gui_OutputFcn', @test1_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 test1 is made visible.


function test1_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

Page 18 of 37
% handles structure with handles and user data (see GUIDATA)
% varargin command line arguments to test1 (see VARARGIN)

% Choose default command line output for test1


handles.output = hObject;

% Update handles structure


guidata(hObject, handles);
pic = imread('iubscj.png', 'PNG');
axes(handles.axes3)
imshow(pic)

% UIWAIT makes test1 wait for user response (see UIRESUME)


% uiwait(handles.figure1);

% --- Outputs from this function are returned to the command line.
function varargout = test1_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;

% --- Executes on selection change in popupmenu1.


function popupmenu1_Callback(hObject, eventdata, handles)
% hObject handle to popupmenu1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
a = get(handles.popupmenu1,'Value');
guidata(hObject,handles)
if (a==1)
fs=8000;
zero=wavrecord(2*fs,fs,'int16');
wavwrite(zero,'zero.wav');

elseif(a==2)
fs=8000;
one=wavrecord(2*fs,fs,'int16');
wavwrite(one,'one.wav');

elseif(a==3)
fs=8000;
two=wavrecord(2*fs,fs,'int16');
wavwrite(two,'two.wav');

elseif(a==4)
fs=8000;
three=wavrecord(2*fs,fs,'int16');
wavwrite(three,'three.wav');

Page 19 of 37
elseif(a==5)
fs=8000;
four=wavrecord(2*fs,fs,'int16');
wavwrite(four,'four.wav');

elseif(a==6)
fs=8000;
five=wavrecord(2*fs,fs,'int16');
wavwrite(five,'five.wav');

elseif(a==7)
fs=8000;
six=wavrecord(2*fs,fs,'int16');
wavwrite(six,'six.wav');

elseif(a==8)
fs=8000;
seven=wavrecord(2*fs,fs,'int16');
wavwrite(seven,'seven.wav');

elseif(a==9)
fs=8000;
eight=wavrecord(2*fs,fs,'int16');
wavwrite(eight,'eight.wav');

elseif (a==10)
fs=8000;
nine=wavrecord(2*fs,fs,'int16');
wavwrite(nine,'nine.wav');

end
% Hints: contents = cellstr(get(hObject,'String')) returns popupmenu1
contents as cell array
% contents{get(hObject,'Value')} returns selected item from popupmenu1

% --- Executes during object creation, after setting all properties.


function popupmenu1_CreateFcn(hObject, eventdata, handles)
% hObject handle to popupmenu1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called

% Hint: popupmenu 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 edit1_Callback(hObject, eventdata, handles)


% hObject handle to edit1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB

Page 20 of 37
% handles structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of edit1 as text


% str2double(get(hObject,'String')) returns contents of edit1 as a
double

% --- Executes during object creation, after setting all properties.


function edit1_CreateFcn(hObject, eventdata, handles)
% hObject handle to edit1 (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

% --- Executes on button press in pushbutton1.


function pushbutton1_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
text = str2num(get(handles.edit1,'string'));

if (text==0)
[y,Fs]=wavread('zero.wav');
a=audioplayer(y,Fs);
handles.a=a;
play(a)
guidata(hObject,handles)

elseif (text==1)
[y,Fs]=wavread('one.wav');
a=audioplayer(y,Fs);
handles.a=a;
play(a)
guidata(hObject,handles)

elseif (text==2)
[y,Fs]=wavread('two.wav');
a=audioplayer(y,Fs);
handles.a=a;
play(a)
guidata(hObject,handles)

elseif (text==3)
[y,Fs]=wavread('three.wav');
a=audioplayer(y,Fs);
handles.a=a;
play(a)
guidata(hObject,handles)

Page 21 of 37
elseif (text==4)
[y,Fs]=wavread('four.wav');
a=audioplayer(y,Fs);
handles.a=a;
play(a)
guidata(hObject,handles)

elseif (text==5)
[y,Fs]=wavread('five.wav');
a=audioplayer(y,Fs);
handles.a=a;
play(a)
guidata(hObject,handles)

elseif (text==6)
[y,Fs]=wavread('six.wav');
a=audioplayer(y,Fs);
handles.a=a;
play(a)
guidata(hObject,handles)

elseif (text==7)
[y,Fs]=wavread('seven.wav');
a=audioplayer(y,Fs);
handles.a=a;
play(a)
guidata(hObject,handles)

elseif (text==8)
[y,Fs]=wavread('eight.wav');
a=audioplayer(y,Fs);
handles.a=a;
play(a)
guidata(hObject,handles)

elseif (text==9)
[y,Fs]=wavread('nine.wav');
a=audioplayer(y,Fs);
handles.a=a;
play(a)
guidata(hObject,handles)
else
errordlg('values not in range')
end

% --- Executes on button press in pushbutton2.


function pushbutton2_Callback(hObject, eventdata, handles)
readtext = str2num(get(handles.edit1,'string'));
cla(handles.axes1, 'reset')
if (readtext==0)
readzero=wavread('zero.wav');
x = size(readzero);

Page 22 of 37
t = 0:x-1;
axes(handles.axes1)
plot(t, readzero','r')
hold on
grid on
xlabel('Time (Second)')
ylabel('Amplitude')
hold off

elseif (readtext==1)
readone=wavread('one.wav');
x = size(readone);
t = 0:x-1;
axes(handles.axes1)
plot(t, readone','y')
hold on
grid on
xlabel('Time (Second)')
ylabel('Amplitude')
hold off

elseif (readtext==2)
readtwo=wavread('two.wav');
x = size(readtwo);
t = 0:x-1;
axes(handles.axes1)
plot(t, readtwo','m')
hold on
grid on
xlabel('Time (Second)')
ylabel('Amplitude')
hold off

elseif (readtext==3)
readthree=wavread('three.wav');
x = size(readthree);
t = 0:x-1;
axes(handles.axes1)
plot(t, readthree','c')
hold on
grid on
xlabel('Time (Second)')
ylabel('Amplitude')
hold off

elseif (readtext==4)
readfour=wavread('four.wav');
x = size(readfour);
t = 0:x-1;
axes(handles.axes1)
plot(t, readfour','g')
hold on
grid on
xlabel('Time (Second)')
ylabel('Amplitude')

Page 23 of 37
hold off

elseif (readtext==5)
readfive=wavread('five.wav');
x = size(readfive);
t = 0:x-1;
axes(handles.axes1)
plot(t, readfive','b')
hold on
grid on
xlabel('Time (Second)')
ylabel('Amplitude')
hold off

elseif (readtext==6)
readsix=wavread('six.wav');
x = size(readsix);
t = 0:x-1;
axes(handles.axes1)
plot(t, readsix','k')
hold on
grid on
xlabel('Time (Second)')
ylabel('Amplitude')
hold off

elseif (readtext==7)
readseven=wavread('seven.wav');
x = size(readseven);
t = 0:x-1;
axes(handles.axes1)
plot(t, readseven','g')
hold on
grid on
xlabel('Time (Second)')
ylabel('Amplitude')
hold off

elseif (readtext==8)
readeight=wavread('eight.wav');
x = size(readeight);
t = 0:x-1;
axes(handles.axes1)
plot(t, readeight','y')
hold on
grid on
xlabel('Time (Second)')
ylabel('Amplitude')
hold off

elseif (readtext==9)
readnine=wavread('nine.wav');
x = size(readnine);
t = 0:x-1;
axes(handles.axes1)
plot(t, readnine','m')

Page 24 of 37
hold on
grid on
xlabel('Time (Second)')
ylabel('Amplitude')
hold off

else
errordlg('select value between 1 and 9')
end
% end

% hObject handle to pushbutton2 (see GCBO)


% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)

% % --- Executes on button press in pushbutton3.


% function pushbutton2_Callback(hObject, eventdata, handles)

% hObject handle to pushbutton3 (see GCBO)


% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)

% --- Executes during object creation, after setting all properties.


function axes1_CreateFcn(hObject, eventdata, handles)
% hObject handle to axes1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called

% Hint: place code in OpeningFcn to populate axes1

% --- Executes on button press in pushbutton3.


function pushbutton3_Callback(hObject, eventdata, handles)
readtext = str2num(get(handles.edit1,'string'));
cla(handles.axes2, 'reset')
if (readtext==0)
axes(handles.axes2)
hold on
x = wavread('zero.wav');
hist (x)
grid on
xlabel('Time (Second)')
ylabel('Amplitude')
hold off

elseif (readtext==1)
axes(handles.axes2)
hold on
x = wavread('one.wav');
hist (x)

Page 25 of 37
grid on
xlabel('Time (Second)')
ylabel('Amplitude')
hold off

elseif (readtext==2)
axes(handles.axes2)
hold on
x = wavread('two.wav');
hist (x)
grid on
xlabel('Time (Second)')
ylabel('Amplitude')
hold off

elseif (readtext==3)
axes(handles.axes2)
hold on
x = wavread('three.wav');
hist (x)
grid on
xlabel('Time (Second)')
ylabel('Amplitude')
hold off

elseif (readtext==4)
axes(handles.axes2)
hold on
x = wavread('four.wav');
hist (x)
grid on
xlabel('Time (Second)')
ylabel('Amplitude')
hold off

elseif (readtext==5)
axes(handles.axes2)
hold on
x = wavread('five.wav');
hist (x)
grid on
xlabel('Time (Second)')
ylabel('Amplitude')
hold off

elseif (readtext==6)
axes(handles.axes2)
hold on
x = wavread('six.wav');
hist (x)
grid on
xlabel('Time (Second)')
ylabel('Amplitude')
hold off

elseif (readtext==7)

Page 26 of 37
axes(handles.axes2)
hold on
x = wavread('seven.wav');
hist (x)
grid on
xlabel('Time (Second)')
ylabel('Amplitude')
hold off

elseif (readtext==8)
axes(handles.axes2)
hold on
x = wavread('eight.wav');
hist (x)
grid on
xlabel('Time (Second)')
ylabel('Amplitude')
hold off

elseif (readtext==9)
axes(handles.axes2)
hold on
x = wavread('nine.wav');
hist (x)
grid on
xlabel('Time (Second)')
ylabel('Amplitude')
hold off
else
errordlg('select value between 1 and 9')
end

% hObject handle to pushbutton3 (see GCBO)


% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)

% --- Executes on button press in statistics.


function statistics_Callback(hObject, eventdata, handles)
n = str2num(get(handles.edit1,'string'));
if (n==0);
[zero,fs]=wavread('zero.wav');
n=length(zero);
fprintf('\n');
fprintf('Digit statistics\n\n');
fprintf('mean:%3f\n',mean(zero));
fprintf('standard deviation:%3f\n',std(zero));
fprintf('median:%3f\n',median(zero));
fprintf('variance:%3f\n',var(zero));
prod=zero(1:n-1).*zero(2:n);

elseif (n==1);
[one,fs]=wavread('one.wav');
n=length(one);

Page 27 of 37
fprintf('\n');
fprintf('Digit statistics\n\n');
fprintf('mean:%3f\n',mean(one));
fprintf('standard deviation:%3f\n',std(one));
fprintf('median:%3f\n',median(one));
fprintf('variance:%3f\n',var(one));
prod=one(1:n-1).*one(2:n);

elseif (n==2);
[two,fs]=wavread('two.wav');
n=length(two);
fprintf('\n');
fprintf('Digit statistics\n\n');
fprintf('mean:%3f\n',mean(two));
fprintf('standard deviation:%3f\n',std(two));
fprintf('median:%3f\n',median(two));
fprintf('variance:%3f\n',var(two));
prod=two(1:n-1).*two(2:n);

elseif (n==3);
[three,fs]=wavread('three.wav');
n=length(three);
fprintf('\n');
fprintf('Digit statistics\n\n');
fprintf('mean:%3f\n',mean(three));
fprintf('standard deviation:%3f\n',std(three));
fprintf('median:%3f\n',median(three));
fprintf('variance:%3f\n',var(three));
prod=three(1:n-1).*three(2:n);

elseif (n==4);
[four,fs]=wavread('four.wav');
n=length(four);
fprintf('\n');
fprintf('Digit statistics\n\n');
fprintf('mean:%3f\n',mean(four));
fprintf('standard deviation:%3f\n',std(four));
fprintf('median:%3f\n',median(four));
fprintf('variance:%3f\n',var(four));
prod=four(1:n-1).*four(2:n);

elseif (n==5);
[five,fs]=wavread('five.wav');
n=length(five);
fprintf('\n');
fprintf('Digit statistics\n\n');
fprintf('mean:%3f\n',mean(five));
fprintf('standard deviation:%3f\n',std(five));
fprintf('median:%3f\n',median(five));
fprintf('variance:%3f\n',var(five));
prod=five(1:n-1).*five(2:n);

elseif (n==6);
[six,fs]=wavread('six.wav');
n=length(six);
fprintf('\n');

Page 28 of 37
fprintf('Digit statistics\n\n');
fprintf('mean:%3f\n',mean(six));
fprintf('standard deviation:%3f\n',std(six));
fprintf('median:%3f\n',median(six));
fprintf('variance:%3f\n',var(six));
prod=six(1:n-1).*six(2:n);

elseif (n==7);
[seven,fs]=wavread('seven.wav');
n=length(seven);
fprintf('\n');
fprintf('Digit statistics\n\n');
fprintf('mean:%3f\n',mean(seven));
fprintf('standard deviation:%3f\n',std(seven));
fprintf('median:%3f\n',median(seven));
fprintf('variance:%3f\n',var(seven));
prod=seven(1:n-1).*seven(2:n);

elseif (n==8);
[eight,fs]=wavread('eight.wav');
n=length(eight);
fprintf('\n');
fprintf('Digit statistics\n\n');
fprintf('mean:%3f\n',mean(eight));
fprintf('standard deviation:%3f\n',std(eight));
fprintf('median:%3f\n',median(eight));
fprintf('variance:%3f\n',var(eight));
prod=eight(1:n-1).*eight(2:n);

elseif (n==9);
[nine,fs]=wavread('nine.wav');
n=length(nine);
fprintf('\n');
fprintf('Digit statistics\n\n');
fprintf('mean:%3f\n',mean(nine));
fprintf('standard deviation:%3f\n',std(nine));
fprintf('median:%3f\n',median(nine));
fprintf('variance:%3f\n',var(nine));
prod=nine(1:n-1).*nine(2:n);
end
% hObject handle to statistics (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)

TASK 4
function varargout = traffictest1(varargin)
% TRAFFICTEST1 MATLAB code for traffictest1.fig
% TRAFFICTEST1, by itself, creates a new TRAFFICTEST1 or raises the
existing
% singleton*.
%
% H = TRAFFICTEST1 returns the handle to a new TRAFFICTEST1 or the
handle to
% the existing singleton*.

Page 29 of 37
%
% TRAFFICTEST1('CALLBACK',hObject,eventData,handles,...) calls the local
% function named CALLBACK in TRAFFICTEST1.M with the given input
arguments.
%
% TRAFFICTEST1('Property','Value',...) creates a new TRAFFICTEST1 or
raises the
% existing singleton*. Starting from the left, property value pairs are
% applied to the GUI before traffictest1_OpeningFcn gets called. An
% unrecognized property name or invalid value makes property application
% stop. All inputs are passed to traffictest1_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 traffictest1

% Last Modified by GUIDE v2.5 03-Feb-2015 01:59:51

% Begin initialization code - DO NOT EDIT


gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @traffictest1_OpeningFcn, ...
'gui_OutputFcn', @traffictest1_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
% guidata(hObject, handles);

% --- Executes just before traffictest1 is made visible.


function traffictest1_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 traffictest1 (see VARARGIN)

% Choose default command line output for traffictest1


handles.output = hObject;

% Update handles structure

Page 30 of 37
guidata(hObject, handles);
set(handles.red,'enable','off');
set(handles.green,'enable','off');
% Choose default command line output for TASK4MATLAB
pic = imread('aputraffic.png', 'PNG');
axes(handles.axes2)
imshow(pic)

% UIWAIT makes traffictest1 wait for user response (see UIRESUME)


% uiwait(handles.figure1);

% --- Outputs from this function are returned to the command line.
function varargout = traffictest1_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;

% --- Executes on selection change in popupmenu1.


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

% Hints: contents = cellstr(get(hObject,'String')) returns popupmenu1


contents as cell array
% contents{get(hObject,'Value')} returns selected item from popupmenu1

% --- Executes during object creation, after setting all properties.


function popupmenu1_CreateFcn(hObject, eventdata, handles)
% hObject handle to popupmenu1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called

% Hint: popupmenu 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

% --- Executes on selection change in popupmenu2.


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

Page 31 of 37
% Hints: contents = cellstr(get(hObject,'String')) returns popupmenu2
contents as cell array
% contents{get(hObject,'Value')} returns selected item from popupmenu2

% --- Executes during object creation, after setting all properties.


function popupmenu2_CreateFcn(hObject, eventdata, handles)
% hObject handle to popupmenu2 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called

% Hint: popupmenu 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

% --- Executes on selection change in popupmenu3.


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

% Hints: contents = cellstr(get(hObject,'String')) returns popupmenu3


contents as cell array
% contents{get(hObject,'Value')} returns selected item from popupmenu3

% --- Executes during object creation, after setting all properties.


function popupmenu3_CreateFcn(hObject, eventdata, handles)
% hObject handle to popupmenu3 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called

% Hint: popupmenu 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

% --- Executes on selection change in popupmenu4.


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

% Hints: contents = cellstr(get(hObject,'String')) returns popupmenu4


contents as cell array
% contents{get(hObject,'Value')} returns selected item from popupmenu4

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

% Hint: popupmenu 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 edit1_Callback(hObject, eventdata, handles)


% hObject handle to edit1 (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 edit1 as text


% str2double(get(hObject,'String')) returns contents of edit1 as a
double

% --- Executes during object creation, after setting all properties.


function edit1_CreateFcn(hObject, eventdata, handles)
% hObject handle to edit1 (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 edit2_Callback(hObject, eventdata, handles)


% hObject handle to edit2 (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 edit2 as text


% str2double(get(hObject,'String')) returns contents of edit2 as a
double

% --- Executes during object creation, after setting all properties.


function edit2_CreateFcn(hObject, eventdata, handles)
% hObject handle to edit2 (see GCBO)

Page 33 of 37
% 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

% --- Executes on button press in pushbutton1.


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

% --- Executes on button press in pushbutton2.


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

% --- Executes on button press in red.


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

% --- Executes on selection change in timeinterval.


function timeinterval_Callback(hObject, eventdata, handles)

% --- Executes on button press in green.


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

% --- Executes on selection change in simulationtime.


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

% Hints: contents = cellstr(get(hObject,'String')) returns simulationtime


contents as cell array
% contents{get(hObject,'Value')} returns selected item from
simulationtime

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

% Hint: popupmenu 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

% --- Executes on button press in run.


function run_Callback(hObject, eventdata, handles)
set(handles.red,'enable','off');
set(handles.green,'enable','off');
a = get(handles.timeinterval,'Value');
b = get (handles.simulationtime,'value');
c = str2num(get(handles.probability,'string'));
if (c > 0) && (c <= 0.3)
C=5;
elseif (c > 0.3) && (c <= 0.7)
C=7;
elseif (c > 0.7) && (c <= 1)
C=10;
else
errordlg('values not in range')
end

if (a==1)
Cars = num2str(C);
set(handles.text14, 'string', ['Number of cars = ' Cars]);
for i = 1:b;
set(handles.red,'enable','on');
set(handles.green,'enable','off');
pause(1)
set(handles.red,'enable','off');
set(handles.green,'enable','on');
pause(1)
set(handles.green,'enable','off');
end
Cars = str2num(Cars)-b
set(handles.text15, 'string', ['Remaining No of cars = ' num2str(Cars)]);
elseif (a==2)
Cars = num2str(C);
set(handles.text14, 'string', ['Number of cars = ' Cars]);
for i = 1:b;
set(handles.red,'enable','on');
set(handles.green,'enable','off');
pause(10)
set(handles.red,'enable','off');
set(handles.green,'enable','on');
pause(10)

Page 35 of 37
set(handles.green,'enable','off');
Cars = str2num(Cars)-(2*b)
set(handles.text15, 'string', ['Remaining No of cars = ' num2str(Cars)]);
end
elseif (a==3)
Cars = num2str(C);
set(handles.text14, 'string', ['Number of cars = ' Cars]);
for i = 1:b;
set(handles.red,'enable','on');
set(handles.green,'enable','off');
pause(15)
set(handles.red,'enable','off');
set(handles.green,'enable','on');
pause(15)
set(handles.green,'enable','off');
Cars = str2num(Cars)-(3*b)
set(handles.text15, 'string', ['Remaining No of cars = ' num2str(Cars)]);
end
end
% hObject handle to timeinterval (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)

% Hints: contents = cellstr(get(hObject,'String')) returns timeinterval


contents as cell array
% contents{get(hObject,'Value')} returns selected item from
timeinterval

% --- Executes during object creation, after setting all properties.


function timeinterval_CreateFcn(hObject, eventdata, handles)
% hObject handle to timeinterval (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called

% Hint: popupmenu 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

% hObject handle to run (see GCBO)


% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)

function probability_Callback(hObject, eventdata, handles)


% hObject handle to probability (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 probability as text

Page 36 of 37
% str2double(get(hObject,'String')) returns contents of probability as
a double

% --- Executes during object creation, after setting all properties.


function probability_CreateFcn(hObject, eventdata, handles)
% hObject handle to probability (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

% --- Executes on button press in pushbutton11.


function pushbutton11_Callback(hObject, eventdata, handles)
c = str2num(get(handles.probability,'string'));
a = get(handles.timeinterval,'Value');
b = get (handles.simulationtime,'value');
if ((c > 0) && (c <= 0.3))
axes(handles.axes1)
plot(b, a,'r')
xlabel('Time (Second)')
ylabel('Amplitude')
end
% hObject handle to pushbutton11 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)

Page 37 of 37

You might also like