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 untuk sistem pengenalan suara manusia, deteksi bunyi kerusakan mesin, deteksi kelainan detak jantung, dan lain-lain.

Berikut ini merupakan contoh aplikasi 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

% Adi Pamungkas, S.Si, M.Si
% Website: https://pemrogramanmatlab.com/
% Email  : pemrogramanmatlab@gmail.com

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)

% menetapkan parameter perekaman suara
Fs = 8000;
nBits = 8;
nChannels = 1;
t = 2;
% proses perekaman suara
recObj = audiorecorder(Fs,nBits,nChannels);
set(handles.edit1,'String','Start Speaking.')
stoprecording(recObj,t);
set(handles.edit1,'String','End of Recording.')
% membaca data suara hasil perekaman
myRecording = getdata(recObj);
% menampilkan plot sinyal suara pada axes
axes(handles.axes1)
plot(myRecording)
grid on
title('Sinyal Suara Masukan')
set(gca,'YLim',[-2 2])
% menyimpan variabel Fs dan myRecording pada lokasi handles agar dapat
% dipanggil oleh pushbutton yang lain
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)

% membaca variabel Fs dan myRecording yang ada di lokasi handles
myRecording = handles.myRecording;
Fs = handles.Fs;
% membunyikan suara hasil perekaman
audiosound(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)

% menampilkan menu save file
[filename, pathname] = uiputfile('*.wav');
% jika ada file yang disimpan maka akan mengeksekusi perintah di bawah ini
if ~isequal(filename,0)
% membaca variabel Fs dan myRecording yang ada di lokasi handles
Fs = handles.Fs;
myRecording = handles.myRecording;
% menyimpan file sinyal suara
audiowrite(fullfile(pathname,filename),myRecording,Fs)
else
% jika tidak ada file yang disimpan maka akan kembali
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)

% menampilkan menu browse file
[filename,pathname] = uigetfile('*.wav');
% jika ada file yang dipilih maka akan mengeksekusi perintah di bawah ini
if ~isequal(filename,0)
% membaca file sinyal suara
[y,Fs] = audioread(fullfile(pathname,filename));
% menampilkan plot sinyal suara pada axes
plot(y)
grid on
title('Sinyal Suara Masukan')
set(gca,'YLim',[-2 2])
set(handles.edit2,'String',filename)
else
% jika tidak ada file yang dipilih maka akan kembali
return
end

% menyimpan variabel y dan Fs pada lokasi handles agar dapat dipanggil oleh
% pushbutton yang lain
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)

% memanggil variabel y dan Fs yang ada di lokasi handles
y = handles.y;
Fs = handles.Fs;
% membunyikan sinyal suara
audiosound(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)

% mereset tampilan gui
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

File source code lengkap untuk melakukan akuisisi sinyal suara pada pemrograman matlab di atas dapat diperoleh melalui halaman berikut ini: Source Code

Posted on November 13, 2017, in Pengolahan Sinyal and tagged , , , , , , , , , , , , , , , , , , , , . Bookmark the permalink. 16 Comments.

  1. Assalamu’alaikum kak…
    Disini kok tidak ada pembahasan tentang pengenalan ucapan/suara ?? atau pembahasan ekstraksi suara dengan metode LPC, MFCC…
    Kalau kakak ada pembahsan tentang pengenalan ucapan dengan metode LPC (Linear Predictive Coding) dan HMM (Hidden Markov Model) tolong di share ya kak, untuk membantu tugas akhir saya… Terima Kasih

    • waalaikumsalam nisfatul afiyah
      semoga ke depan terdapat materi mengenai pengenalan ucapan dengan metode LPC (Linear Predictive Coding) dan HMM (Hidden Markov Model)

  2. hyy mas adi apa kabar yaa bisa mas menbatun saya ya mas
    suma cari source code menghitung mobil itu terusnya

  3. sangat bermanfaat mas
    mas, apakah dua sinyal suara bisa digabungkan menjadi satu , kalau bisa bagaimana caranya mas ?
    matur nuwun

  4. mas ada pemabahas ekstraksi sinyal dengan mfcc nggak? kalau ada tolong di share ya. hatur nuhun

  5. mas ada yang pembahasannya tentang deteksi suara misalnya aiueo gitu. trimakasih

  6. Mas kalo sistem Matlab digabungkan kedalam situs website bisa ga sih? seperti misalnya diprogramkan untuk kontrol akses dengan password sinyal suara dan selanjutnya akan dikonfirmasi dengan masuk kedalam situs web tertentu.

  7. izin tanya min, untuk syntax yang akan di gunakan dalam pembuatan gui ada modulnya gak min?

  8. untuk melihat frequency response ada gk mas?

  9. selamat sore mas adi saya sudah mencoba membuat pemograman sesuai dengan milik mas adi namun terdapat error seperti ini
    Error in matlab.graphics.internal.figfile.FigFile/read>@(hObject,eventdata)untitled1(‘pushbutton2_Callback’,hObject,eventdata,guidata(hObject))

    Error while evaluating UIControl Callback.

    mohon bimbingannya, terima kasih mas

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 )

Facebook photo

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

Connecting to %s

%d bloggers like this: