Вы находитесь на странице: 1из 14

EC 247 COMPUTING FOR ENGINEERS

ASSIGNMENT 1
GRAPHICAL USER INTERFACE DESIGN
LECTURERS IN-CHARGE MS. LAKMINI MALASINGHE / MS. SWAPNA PREMASIRI

Submitted By :

EN 14501876 K.R.M AFSER


16th of September 2015

EC247 ASSIGNMENT 1
1

Table of Contents
Abstract. 3
Introduction3
Graphical User Interface.4
Discussion.......................8
Conclusion..11
References12

EC247 ASSIGNMENT 1
2

Abstract
MATLAB (matrix laboratory) is a multi-paradigm numerical computing environment and fourthgeneration programming language. Developed by Math Works, MATLAB allows matrix manipulations,
plotting of functions and data, implementation of algorithms, creation of user interfaces, and interfacing
with programs written in other languages, including C, C++, Java, and FORTRAN. MATLAB GUI
(graphical user interface) designed to integrate functions. This paper presents a GUI development in
MATLAB for piano using MATLAB 15.0.4420.1017(R2014a).

Introduction
GUI is a program interface that takes advantage of the computer's graphics capabilities to make the
program easier to use. Well-designed graphical user interfaces can free the user from learning
complex command languages. On the other hand, many users find that they work more effectively with
a command-driven interface, especially if they already know the command language. This environment
contains pushbuttons, toggle buttons, lists, menus, text boxes, and so forth, all of which are already
familiar to the user, so that he or she can concentrate on using the application. Working with sound in
MATLAB helps you to understand the mathematics involved in digital audio processing with the use of
these options every single MATLAB function can be created as a user friendly interface.

The following GUI was created for making a piano with 7 keys.
Plays a note on the users command
Records a set of notes and plays it back as per users request.
Ability to erase what was recorded

EC247 ASSIGNMENT 1
3

Graphical User Interface


Creating the GUI

Figure 1: Piano design implemented using Matlab


This piano consists of 7 keys with a play, record and reset buttons. For the piano keys, play reset
buttons,1/4 note push buttons were used, a toggle button was used for the record button. A radio button
was used for sine wave.

EC247 ASSIGNMENT 1
4

Figure 2 : GUIDE Starting interface


open an existing GUI or a new blank GUI. Then can customize the GUI.
Adding components

Figure 3 : Adding Components


EC247 ASSIGNMENT 1
5

Callbacks
What Is a Callback?
A callback is a function that you write and associate with a specific GUI component or with the GUI
figure. It controls GUI or component behavior by performing some action in response to an event for its
component. This kind of programming is often called event-driven programming.
When a GUI is run a m file for that is created.

The m-file already has where the each function occurs. Then need to enter the code of that specific
function.

EC247 ASSIGNMENT 1
6

Ex;
% --- 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)

Designing

Adding push buttons.

Double Click

Figure 4: Editing a button


EC247 ASSIGNMENT 1
7

Put the coding for number input push buttons:

First right click the push button that labeled as any number then choose view callback then click on
call back. There will be a window that contains automatically generated coding that could able to
edit.

Then we put algorithm to make that. The following shows that coding.

Types of other callbacks

CreateFcn - MATLAB executes this callback when creating the object.

DeleteFcn - MATLAB executes this callback just before deleting the object.

KeyPressFcn - MATLAB executes the specified callback when users press a key and the cursor is
within the figure window.
ButtonDownFcn - MATLAB executes this callback when users click the left mouse button and the
cursor is over the object or within a five-pixel border around the object.

Discussion of code
At the beginning of the code the following, the following variables were declared as default using
handles as shown below.
% Choose default command line output for piano
handles.output = hObject;
EC247 ASSIGNMENT 1
8

handles.SampleRate = 1/20000;
handles.SoundVector = 0;
handles.TimeValue = 0.3488;
Using handles allows the variable to be used anywhere in the code.. In here sampling rate defined as
1/20000, and time, T as 0.3488.
The following code describes how when a key was pressed, the frequency was set and sound was
played. For example, when the note A was pressed, the following code will be executed.

Code for A
% --- Executes on button press in A.
function A_Callback(hObject, eventdata, handles)
% hObject handle to C4 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
freq = 261.624;
playSound(handles,freq);

Code for B:
% --- Executes on button press in D4.
function D4_Callback(hObject, eventdata, handles)
% hObject handle to D4 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
freq = 293.66;
playSound(handles,freq);
Likewise for each and every note, the above code was executed with the corresponding frequency
needed.
A function called playSound was created, for playing the sounds and recording, as given below.
strFreq = num2str(freq);
set(handles.STFreqValue, 'String', strFreq);
SampleRate = handles.SampleRate;
TimeValue = handles.TimeValue;
EC247 ASSIGNMENT 1
9

Samples = 0:SampleRate:TimeValue;
SinOn
= get(handles.SinWave, 'Value');

if SinOn == 1
soundVector = sin(2*pi*freq*Samples);
end
sound(soundVector, 1/SampleRate)
RecordOn = get(handles.Record, 'Value');
if RecordOn == 1
SoundVectorLong = handles.SoundVector;
if SoundVectorLong == 0
SoundVectorLong = soundVector;
else
SoundVectorLong = cat(2, SoundVectorLong, soundVector);
end
handles.SoundVector = SoundVectorLong;
guidata(handles.figure1, handles);
end
A sinusoidal function was defined with these parameters and for the given frequency.
y = sin(2*pi*freq*t);
Using sound (y, fs) the sound was played.
For the record function, a toggle button was used.
RecordOn = get(handles.Record, 'Value');
This returns the toggle state of Record. This was assigned to a variable called RecordOn. If the value
was equal to 1, then another variable called SoundVectorLong will be equated to soundVector.

Code for the push button play:


% --- Executes on button press in Play.
function Play_Callback(hObject, eventdata, handles)
% hObject handle to Play (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
EC247 ASSIGNMENT 1
10

% handles structure with handles and user data (see GUIDATA)


SoundVector = handles.SoundVector;
SampleRate = handles.SampleRate;
sound(SoundVector, 1/SampleRate)

Reset button
% --- Executes on button press in Reset.
function Reset_Callback(hObject, eventdata, handles)
% hObject handle to Reset (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
handles.SoundVector = 0;
guidata(handles.figure1, handles);

Code for the Quarter note


The quarter note was created using a CreateFcn and callback fn
Call backs

set(handles.QuarterNote, 'Value', 1);


Value = str2double(get(handles.Time, 'String'));
TimeValue = 60/Value;
handles.TimeValue = TimeValue;
guidata(handles.figure1, handles);
Create fcn-This will repeat the quarter note function once its called in the main file.
function QuarterNote_CreateFcn
set(hObject, 'Value', 1);

EC247 ASSIGNMENT 1
11

The quarter note is the third note; it looks like a filled-in half note, with the same stem attached. You
hold it for one full count, which is a quarter of a whole note. Now the design of the time signature makes
full sense; in 4/4 time, the quarter note gets one full beat, and the measure has four beats before reaching
the bar line.

Code for Sine Wave


The sinewave was created using a CreateFcn and callback.
Callback
set(handles.SinWave, 'Value', 1);
set(hObject, 'Value', 1);

Conclusion
This GUI assignment was building a piano using Matlab different functions and various algorithms and
as engineers it is important to understand the importance of audio processing. Piano consists of many
mathematical concepts which can enhance the knowledge of Signals and Systems used in the day to day
life in particular industries.

References
Website-http://www.dummies.com/how-to/content/how-to-read-quarter-notes-half-notes-and-wholenot.html
Accessed on 15/9/2015

Website-Physics of Music- Notes 2015. Available at: http://www.phy.mtu.edu/~suits/notefreqs.html


Accessed on 15/9/2015
EC247 ASSIGNMENT 1
12

EC247 ASSIGNMENT 1
13

EC247 ASSIGNMENT 1
14

Вам также может понравиться