Akuisisi Sinyal Suara Menggunakan MATLAB


Sinyal suara (audio signal) merupakan sinyal yang dihasilkan oleh gelombang audiosonik (gelombang bunyi yang memiliki frekuensi antara 20 Hz sampai 20000 Hz).

Penerapan pengolahan sinyal suara antara lain adalah sistem pengenalan suara manusia, deteksi bunyi kerusakan mesin, deteksi kelainan detak jantung, dan lain-lain.

Berikut ini merupakan contoh pemrograman GUI matlab untuk melakukan akuisisi sinyal suara. Proses akuisisi dilakukan dengan cara merekam suara menggunakan mikrofon yang ada pada komputer atau laptop.

Langkah-langkah pemrogramannya yaitu:
1. Membuat interface GUI dengan desain tampilan seperti pada gambar di bawah ini

sehingga hasil running programnya adalah

2. Memberikan koding perintah pada masing-masing pushbutton. Koding perintah yang digunakan adalah sebagai berikut

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

% Last Modified by GUIDE v2.5 10-Nov-2017 17:04:07

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @akuisisi_sinyal_suara_OpeningFcn, ...
                   'gui_OutputFcn',  @akuisisi_sinyal_suara_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 akuisisi_sinyal_suara is made visible.
function akuisisi_sinyal_suara_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 akuisisi_sinyal_suara (see VARARGIN)

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

% Update handles structure
guidata(hObject, handles);
movegui(hObject,'center');

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

% --- Outputs from this function are returned to the command line.
function varargout = akuisisi_sinyal_suara_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 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)
Fs = 8000;
nBits = 8;
nChannels = 1;
t = 2;

recObj = audiorecorder(Fs,nBits,nChannels);
set(handles.edit1,'String','Start Speaking.')
recordblocking(recObj,t);
set(handles.edit1,'String','End of Recording.')

myRecording = getaudiodata(recObj);

axes(handles.axes1)
plot(myRecording)
grid on
title('Sinyal Suara Masukan')
set(gca,'YLim',[-2 2])

handles.Fs = Fs;
handles.myRecording = myRecording;
guidata(hObject,handles);


% --- 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)
myRecording = handles.myRecording;
Fs = handles.Fs;

sound(myRecording,Fs);


% --- Executes on button press in pushbutton3.
function pushbutton3_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)
[filename, pathname] = uiputfile('*.wav');

if ~isequal(filename,0)
    Fs = handles.Fs;
    myRecording = handles.myRecording;
    audiowrite(fullfile(pathname,filename),myRecording,Fs)
else
    return
end


% --- Executes on button press in pushbutton4.
function pushbutton4_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton4 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
[filename,pathname] = uigetfile('*.wav');

if ~isequal(filename,0)
    [y,Fs] = audioread(fullfile(pathname,filename));
    plot(y)
    grid on
    title('Sinyal Suara Masukan')
    set(gca,'YLim',[-2 2])
    set(handles.edit2,'String',filename)
else
    return
end

handles.y = y;
handles.Fs = Fs;
guidata(hObject, handles)


% --- Executes on button press in pushbutton5.
function pushbutton5_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton5 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
y = handles.y;
Fs = handles.Fs;

sound(y,Fs);


% --- Executes on button press in pushbutton6.
function pushbutton6_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton6 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
axes(handles.axes1)
cla reset
set(gca,'XTick',[])
set(gca,'YTick',[])

set(handles.edit1,'String',[])
set(handles.edit2,'String',[])


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

3. Mensimulasikan proses akuisisi sinyal suara
a. Merekam sinyal suara selama 2 detik

b. Membunyikan sinyal suara hasil perekaman

c. Menyimpan sinyal suara hasil perekaman dengan format .wav

d. Me-Load sinyal suara yang sebelumnya telah disimpan

e. Membunyikan sinyal suara yang telah di-load

f. Me-reset tampilan GUI

Source code untuk melakukan akuisisi sinyal suara pada pemrograman matlab di atas dapat diunduh pada halaman berikut ini: Source Code

Posted on November 13, 2017, in Pengolahan Sinyal and tagged , , , , , , , , , , , , , , , , , , , , . Bookmark the permalink. Leave a comment.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: