Wednesday, October 16, 2019

Various Windows Based FIR Filter And Its Implementation On Audio Signal

Various Windows Based FIR Filter And Its Implementation On Audio Signal


Here, you will build a desktop application (UI, user interface) to simulate how is filtered using FIR filter, Various Windows Based FIR Filter. A number of discrete signals are used as test signals are impulse, step, real exponential, sinusoidal, random, square, angled triangle, equilateral triangle, and trapezoidal signals. In the UI, this FIR filter is used to filter signals and audio file (wav) as well.


Follow these steps below:


1. Type this command on MATLAB prompt:


guide

The GUIDE Quick Start dialog box will show up, as shown in figure below:



2. Select the Blank GUI (Default) template, and then click OK:


3. Drag all components needed, as shown in figure below:


4. Right click on DRAW button and choose View Callbacks and then choose Callback. Define pbDraw_Callback() function below to read signals parameters and displays chosen signal in axes1 component:


% --- Executes on button press in pbDraw.
function pbDraw_Callback(hObject, eventdata, handles)
% hObject    handle to pbDraw (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
global n0; 
global n1;
global x;

% Reads n0, n1, and n2
n0 = str2num(get(handles.n0,'String'));
n1 = str2num(get(handles.n1,'String'));
n2 = str2num(get(handles.n2,'String'));
exp = str2num(get(handles.expCoef,'String'));

% Reads parameters for sinusoidal
A = str2num(get(handles.A,'String'));
Freq = str2num(get(handles.Freq,'String'));
Fase = str2num(get(handles.Phase,'String'));
Fs = str2num(get(handles.Fs,'String'));

% Reads parameters for square and triangle signals
width = str2num(get(handles.width, 'String'));

% Choose option from popup menu popupmenusignal1
switch get(handles.popupmenusignal1,'Value')  
    case 1
       n = [n1:n2]; x = [(n-n0) == 0];
       
       % Display discrete signal
       axes(handles.axes1)
       stem(n,x,'linewidth',2,'color','y'); 
       set(gca,'color',[0.4 0.6 0.5]);
       title('Discrete Impulse Signal');

    case 2
       n = [n1:n2]; x = [(n-n0) >= 0];
       % Display discrete signal
       axes(handles.axes1)
       stem(n,x,'linewidth',2,'color','y'); 
       set(gca,'color',[0.4 0.6 0.5]);
       title('Discrete Step Signal');
       
    case 3
       n = [n1:n2]; x = [(n-n0) >= 0].*[(exp).^(n-n0)];
       % Display discrete signal
       axes(handles.axes1)
       stem(n,x,'linewidth',1,'color','y'); 
       set(gca,'color',[0.4 0.6 0.5]);
       title('Discrete Real Exponential Signal');
       
    case 4
       n = [n1:n2]; x = [(n-n0) >= 0].*[A*sin(2*pi*(Freq)*((n-n0)/Fs)+Fase)];
       % Display discrete signal
       axes(handles.axes1)
       stem(n,x,'linewidth',2,'color','y'); 
       set(gca,'color',[0.4 0.6 0.5]);
       title('Discrete Sinusoidal Signal');
       
    case 5
       n = [n1:n2]; x = [(n-n0) >= 0].*(rand(1,(n2-n1+1))-0.5);
       % Display discrete signal
       axes(handles.axes1)
       stem(n,x,'linewidth',1,'color','y'); 
       set(gca,'color',[0.4 0.6 0.5]);
       title('Discrete Random Signal');

    case 6
       n = [n1:n2]; x = [(n-n0) >= 0];
       ny = [n1:n2]; xy = [(ny-width-n0-1) >= 0];
       x = x - xy;
       
       % Display discrete signal
       axes(handles.axes1)
       stem(n,x,'linewidth',1,'color','y'); 
       set(gca,'color',[0.4 0.6 0.5]);
       title('Discrete Square Signal');

    case 7
       n = [n1:n2]; x = n.*[n >= 0];
       ny = [n1:n2]; xy = ny.*[(ny-width-1) >= 0];
       x = (x - xy)/width;
       nb = [n1+n0:n2+n0];
       
       % Reads signal range
       set(handles.n1,'string',(n1+n0));
       set(handles.n2,'string',(n2+n0));

       % Display discrete signal
       axes(handles.axes1)
       stem(nb,x,'linewidth',1,'color','y'); 
       set(gca,'color',[0.4 0.6 0.5]);
       title('Discrete Angled Triangle Signal');
 
    case 8
       n = [n1:n2]; x = n.*[n >= 0];
       x2 = [zeros(1,width), x(1:end-width)];
       x1 = -x;
       x1 = [zeros(1,0.5*width), x1(1:end-0.5*width)];
       nb1 = n1+n0; nb2 = n2+n0;
       nb = [nb1:nb2];
       
       % Read signal range
       set(handles.n1,'string',(nb1));
       set(handles.n2,'string',(nb2));

       % Returns discrete equilateral triangle signal
       x = (x + 2*x1+x2)/(0.5*width);
       
       % Display discrete signal
       axes(handles.axes1)
       stem(nb,x,'linewidth',1,'color','y'); 
       set(gca,'color',[0.4 0.6 0.5]);
       title('Discrete Equilateral Triangle Signal');
       
    case 9
       n = [n1:n2]; x = n.*[n >= 0];
       x2 = [zeros(1,width), x(1:end-width)];
       x3 = [zeros(1,3*width), x(1:end-3*width)];
       x1 = x;
       x1 = [zeros(1,2*width), x1(1:end-2*width)];
       nb1 = n1+n0; nb2 = n2+n0;
       nb = [nb1:nb2];
       
       % Reads signal range
       set(handles.n1,'string',(nb1));
       set(handles.n2,'string',(nb2));

       % Returns discrete trapezoidal signal
       x = (x - x2 - x1 + x3)/width;
       
       % Display discrete signal
       axes(handles.axes1)
       stem(nb,x,'linewidth',1,'color','y');
       set(gca,'color',[0.4 0.6 0.5]);
       title('Discrete Trapezoidal Signal');
end    

5. You can run the UI. Choose one of signals and then click DRAW button:


6. Set the String property of popupmenuwindow with Rectangular, Bartlett, Hann, Hamming, Blackman, and Kaiser.


7. Define ideal_lp() function (save to m file) to calculate ideal lowpass filter, given the cutoff frequency and filter length:


function hd = ideal_lp(wc,M)
% Calculates ideal lowpass filter
% -------------------------
% [hd]= lp_ideal(wc,M)
% wc = cutoff frequency in radian
% M = ideal filter length
%
alpha = (M-1)/2; n = [0:1:(M-1)];
m = n - alpha; fc = wc/pi; hd = fc*sinc(fc*m);

8. Right click on Lowpass button and choose View Callbacks and then choose Callback. Define pbLowpass_Callback() function below to read signals parameters and displays ideal lowpass impulse response, window function (based on window chosen from popupmenuwindow component), actual lowpass impulse response, lowpass magnitude response, and filtered signal:


% --- Executes on button press in pbLowpass.
function pbLowpass_Callback(hObject, eventdata, handles)
% hObject    handle to pbLowpass (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
global x;

% Reads all filter spesifications
As = str2num(get(handles.As,'String'));
ws = str2num(get(handles.omega_s,'String'));
Rp = str2num(get(handles.Rp,'String'));
wp = str2num(get(handles.omega_p,'String'));

ws=ws*pi; wp=wp*pi;
bandwidth = ws - wp;
M = ceil(6.6*pi/bandwidth) + 1;
n=[0:1:M-1];
wc = (ws+wp)/2; % cutoff frequency of LPF

% Choosing option from popup menu
switch get(handles.popupmenuwindow,'Value')  
    case 1
        j=boxcar(M);
    case 2
        j=bartlett(M);
    case 3
        j=hann(M);
    case 4
        j=hamming(M);
    case 5
        j=blackman(M);
    case 6
        beta = 0.1102*(As-8.7);
        j=kaiser(M,beta);
end

hd = ideal_lp(wc,M); h = hd' .* j;
[db,mag,pha,grd,w] = freqz_m(h,[1]); delta_w = 2*pi/1000;

% Actual ripple passband
Rp = -(min(db(1:1:wp/delta_w+1))); 

% Minimal stopband attenuation
As = -round(max(db(ws/delta_w+1:1:501))); 

axes(handles.axes7);
stem(n,hd, 'color','w'); title('Ideal Lowpass Impulse Response')
set(gca,'color',[0.6,0.2,0.1]);
axis([0 M-1 min(hd) max(hd)]); xlabel('n'); ylabel('hd(n)')

axes(handles.axes8);
stem(n,j,'color','w');title('Window Function')
axis([0 M-1 0 1.1]); xlabel('n'); ylabel('w(n)')
set(gca,'color',[0.6,0.2,0.1]);

axes(handles.axes9);
stem(n,h,'color','w');title('Actual Lowpass Impulse')
axis([0 M-1 min(h) max(h)]); xlabel('n'); ylabel('h(n)')
set(gca,'color',[0.6,0.2,0.1]);

axes(handles.axes3);
plot(w/pi,db, 'linewidth',2,'color','y');
title('Magnitude Response in dB');grid
axis([0 1 min(db) max(db)]); 
xlabel('Frequency in pi unit'); ylabel('dB')
set(gca,'color',[0.6,0.2,0.1]);

% Calculates filtered signal
y = conv(double(x),double(h), 'same');

axes(handles.axes2); 
t = 0:length(y)-1;  
stem(t,y,'linewidth',1,'color','g');
title('Filtered Signal')
set(gca,'color',[0.6,0.2,0.1]);

9. You can run the UI. Choose one of signals and then click DRAW button, choose one of window functions, and click on Lowpass button. The result is shown in figure below.


10. Right click on Highpass button and choose View Callbacks and then choose Callback. Define pbHighpass_Callback() function below to read signals parameters and displays ideal highpass impulse response, window function (based on window chosen from popupmenuwindow component), actual highpass impulse response, highpass magnitude response, and filtered signal:


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

global x;

% Reads all filter spesifications
As = str2num(get(handles.As,'String'));
ws = str2num(get(handles.omega_s,'String'));
Rp = str2num(get(handles.Rp,'String'));
wp = str2num(get(handles.omega_p,'String'));

ws=ws*pi; wp=wp*pi;
bandwidth = ws - wp;
M = ceil(6.6*pi/bandwidth) + 1;
n=[0:1:M-1];
wc = (ws+wp)/2; % cutoff frequency of HPF

% Choosing option from popup menu
switch get(handles.popupmenuwindow,'Value')  
    case 1
        j=boxcar(M);
    case 2
        j=bartlett(M);
    case 3
        j=hann(M);
    case 4
        j=hamming(M);
    case 5
        j=blackman(M);
    case 6
        beta = 0.1102*(As-8.7);
        j=kaiser(M,beta);
end

hd = ideal_lp(pi,M) - ideal_lp(wc,M);
h = hd' .* j;
[db,mag,pha,grd,w] = freqz_m(h,[1]); delta_w = 2*pi/1000;

% Actual ripple passband
Rp = -(min(db(1:1:wp/delta_w+1))); 

% Minimal stopband attenuation
As = -round(max(db(ws/delta_w+1:1:501))); 

axes(handles.axes7);
stem(n,hd, 'color','w'); title('Ideal Highpass Impulse Response')
set(gca,'color',[0.6,0.2,0.1]);
axis([0 M-1 min(hd) max(hd)]); xlabel('n'); ylabel('hd(n)')

axes(handles.axes8);
stem(n,j,'color','w');title('Window Function')
axis([0 M-1 0 1.1]); xlabel('n'); ylabel('w(n)')
set(gca,'color',[0.6,0.2,0.1]);

axes(handles.axes9);
stem(n,h,'color','w');title('Actual Highpass Impulse')
axis([0 M-1 min(h) max(h)]); xlabel('n'); ylabel('h(n)')
set(gca,'color',[0.6,0.2,0.1]);

axes(handles.axes3);
plot(w/pi,db, 'linewidth',2,'color','y');
title('Magnitude Response in dB');grid
axis([0 1 min(db) max(db)]); 
xlabel('Frequency in pi unit'); ylabel('dB')
set(gca,'color',[0.6,0.2,0.1]);

% Calculates filtered signal
y = conv(double(x),double(h), 'same');

axes(handles.axes2); 
t = 0:length(y)-1;  
stem(t,y,'linewidth',1,'color','g');
title('Filtered Signal')
set(gca,'color',[0.6,0.2,0.1]);

11. You can run the UI. Choose one of signals and then click DRAW button, choose one of window functions, and click on Highpass button. The result is shown in figure below.


12. Right click on Bandpass button and choose View Callbacks and then choose Callback. Define pbBandpass_Callback() function below to read signals parameters and displays ideal bandpass impulse response, window function (based on window chosen from popupmenuwindow component), actual bandpass impulse response, bandpass magnitude response, and filtered signal:


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

global x;

% Reads all filter spesifications
As = str2num(get(handles.As,'String'));
ws = str2num(get(handles.omega_s,'String'));
Rp = str2num(get(handles.Rp,'String'));
wp = str2num(get(handles.omega_p,'String'));
wp2 = str2num(get(handles.wp2,'String'));
ws2 = str2num(get(handles.ws2,'String'));

ws=ws*pi; wp=wp*pi; wp2=wp2*pi; ws2=ws2*pi;
bandwidth = min((ws-wp),(ws2-wp2)); M = ceil(11*pi/bandwidth) + 1;
n=[0:1:M-1]; wc1 = (ws+wp)/2; 
wc2 = (wp2+ws2)/2;

% Choosing option from popup menu
switch get(handles.popupmenuwindow,'Value')  
    case 1
        j=boxcar(M);
    case 2
        j=bartlett(M);
    case 3
        j=hann(M);
    case 4
        j=hamming(M);
    case 5
        j=blackman(M);
    case 6
        beta = 0.1102*(As-8.7);
        j=kaiser(M,beta);
end

% Bandpass
hd = ideal_lp(wc2,M) - ideal_lp(wc1,M);
h = hd' .* j;
[db,mag,pha,grd,w] = freqz_m(h,[1]); delta_w = 2*pi/1000;

% Actual ripple passband
Rp = -(min(db(1:1:wp/delta_w+1))); 

% Minimal stopband attenuation
As = -round(max(db(ws/delta_w+1:1:501))); 

axes(handles.axes7);
stem(n,hd, 'color','w'); title('Ideal Bandpass Impulse Response')
set(gca,'color',[0.6,0.2,0.1]);
axis([0 M-1 min(hd) max(hd)]); xlabel('n'); ylabel('hd(n)')

axes(handles.axes8);
stem(n,j,'color','w');title('Window Function')
axis([0 M-1 0 1.1]); xlabel('n'); ylabel('w(n)')
set(gca,'color',[0.6,0.2,0.1]);

axes(handles.axes9);
stem(n,h,'color','w');title('Actual Bandpass Impulse')
axis([0 M-1 min(h) max(h)]); xlabel('n'); ylabel('h(n)')
set(gca,'color',[0.6,0.2,0.1]);

axes(handles.axes3);
plot(w/pi,db, 'linewidth',2,'color','y');
title('Magnitude Response in dB');grid
axis([0 1 min(db) max(db)]); 
xlabel('Frequency in pi unit'); ylabel('dB')
set(gca,'color',[0.6,0.2,0.1]);

% Calculates filtered signal
y = conv(double(x),double(h), 'same');

axes(handles.axes2); 
t = 0:length(y)-1;  
stem(t,y,'linewidth',1,'color','g');
title('Filtered Signal')
set(gca,'color',[0.6,0.2,0.1]);

13. You can run the UI. Choose one of signals and then click DRAW button, choose one of window functions, and click on Bandpass button. The result is shown in figure below.


14. Right click on Bandstop button and choose View Callbacks and then choose Callback. Define pbBandstop_Callback() function below to read signals parameters and displays ideal bandstop impulse response, window function (based on window chosen from popupmenuwindow component), actual bandstop impulse response, bandstop magnitude response, and filtered signal:


% --- Executes on button press in pbBandpass.
function pbBandpass_Callback(hObject, eventdata, handles)
% hObject    handle to pbBandpass (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 pbBandstop.
function pbBandstop_Callback(hObject, eventdata, handles)
% hObject    handle to pbBandstop (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

global x;

% Reads all filter spesifications
As = str2num(get(handles.As,'String'));
ws = str2num(get(handles.omega_s,'String'));
Rp = str2num(get(handles.Rp,'String'));
wp = str2num(get(handles.omega_p,'String'));
wp2 = str2num(get(handles.wp2,'String'));
ws2 = str2num(get(handles.ws2,'String'));

ws=ws*pi; wp=wp*pi; wp2=wp2*pi; ws2=ws2*pi;
As = 60;
tr_width = min((ws-wp),(ws2-wp2)); M = ceil(11*pi/tr_width) + 1;

n=[0:1:M-1]; wc1 = (ws+wp)/2; wc2 = (wp2+ws2)/2;

% Choosing option from popup menu
switch get(handles.popupmenuwindow,'Value')  
    case 1
        j=boxcar(M);
    case 2
        j=bartlett(M);
    case 3
        j=hann(M);
    case 4
        j=hamming(M);
    case 5
        j=blackman(M);
    case 6
        beta = 0.1102*(As-8.7);
        j=kaiser(M,beta);
end

% Bandstop
hd = ideal_lp(wc1,M) + ideal_lp(pi,M) - ideal_lp(wc2,M);
h = hd' .* j;
[db,mag,pha,grd,w] = freqz_m(h,[1]); delta_w = 2*pi/1000;

% Actual ripple passband
Rp = -(min(db(1:1:wp/delta_w+1))); 

% Minimal stopband attenuation
As = -round(max(db(ws/delta_w+1:1:501))); 

axes(handles.axes7);
stem(n,hd, 'color','w'); title('Ideal Bandstop Impulse Response')
set(gca,'color',[0.6,0.2,0.1]);
axis([0 M-1 min(hd) max(hd)]); xlabel('n'); ylabel('hd(n)')

axes(handles.axes8);
stem(n,j,'color','w');title('Window Function')
axis([0 M-1 0 1.1]); xlabel('n'); ylabel('w(n)')
set(gca,'color',[0.6,0.2,0.1]);

axes(handles.axes9);
stem(n,h,'color','w');title('Actual Bandstop Impulse')
axis([0 M-1 min(h) max(h)]); xlabel('n'); ylabel('h(n)')
set(gca,'color',[0.6,0.2,0.1]);

axes(handles.axes3);
plot(w/pi,db, 'linewidth',2,'color','y');
title('Magnitude Response in dB');grid
axis([0 1 min(db) max(db)]); 
xlabel('Frequency in pi unit'); ylabel('dB')
set(gca,'color',[0.6,0.2,0.1]);

% Calculates filtered signal
y = conv(double(x),double(h), 'same');

axes(handles.axes2); 
t = 0:length(y)-1;  
stem(t,y,'linewidth',1,'color','g');
title('Filtered Signal')
set(gca,'color',[0.6,0.2,0.1]);

15. You can run the UI. Choose one of signals and then click DRAW button, choose one of window functions, and click on Bandstop button. The result is shown in figure below.


16. Right click on LOAD AUDIO FILE button and choose View Callbacks and then choose Callback. Define pbLoadAudioFile_Callback() function below to read audio file and display its spectrum:


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

global x_audio;

[filename,path] = uigetfile({'*.wav'},'Muat File Wav');
[x,Fs] = audioread([path '/' filename]);
handles.x = x ./ max(abs(x));
handles.Fs = Fs;

axes(handles.axes2);
waktu = 0:1/Fs:(length(handles.x)-1)/Fs;
plot(waktu, handles.x,'linewidth',1,'color','r');
set(gca,'color',[0.1,0.2,0.8]);
axis([0 max(waktu) -1 1]); title('Audio Signal');

axes(handles.axes3);
specgram(handles.x, 1024, handles.Fs);
set(gca,'color',[0.2,0.2,0.8]);
title('Signal Spectrum');
handles.fileLoad = 1;

handles.fileNoise = 0;
handles.fileFinal = 0;

set(handles.SamplingFreq, 'String', num2str(Fs));
set(handles.NSamples, 'String', num2str(length(handles.x)));

x_audio = handles.x;
guidata(hObject, handles);

17. You can run the UI. Click LOAD AUDIO FILE button. The result is shown in figure below.


18. Right click on LPF AUDIO button and choose View Callbacks and then choose Callback. Define pbLPFAudio_Callback() function below to calculate and display filtered signal and its spectrum:


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

global x;
global x_audio;

% Reads all filter spesification
As = str2num(get(handles.As,'String'));
ws = str2num(get(handles.omega_s,'String'));
Rp = str2num(get(handles.Rp,'String'));
wp = str2num(get(handles.omega_p,'String'));

ws=ws*pi; wp=wp*pi;
tr_width = ws - wp;
M = ceil(6.6*pi/tr_width) + 1;
n=[0:1:M-1];
wc = (ws+wp)/2; 

% Choose window function
switch get(handles.popupmenuwindow,'Value')  
    case 1
        j=boxcar(M);
    case 2
        j=bartlett(M);
    case 3
        j=hann(M);
    case 4
        j=hamming(M);
    case 5
        j=blackman(M);
    case 6
        beta = 0.1102*(As-8.7);
        j=kaiser(M,beta);
end

hd = ideal_lp(wc,M); h = hd' .* j;

% Calculates filtered signal
y = conv(hd,x_audio);

axes(handles.axes2); 
t = 0:length(y)-1;  
plot(t,y,'linewidth',1,'color','r');
title('Filtered Signal')
set(gca,'color',[0.1,0.2,0.8]);

axes(handles.axes3)
specgram(y, 1024, handles.Fs);
title('Signal Spectrum');
handles.fileLoad = 1;
set(gca,'color',[0.1,0.2,0.8]);
handles.x=y;
guidata(hObject, handles);

19. You can run the UI. Click LOAD AUDIO FILE button and then click LPF Audio button. The result is shown in figure below.


20. Right click on HPF AUDIO button and choose View Callbacks and then choose Callback. Define pbHPFAudio_Callback() function below to calculate and display filtered signal and its spectrum:


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

global x;
global x_audio;

% Reads all filter spesification
As = str2num(get(handles.As,'String'));
ws = str2num(get(handles.omega_s,'String'));
Rp = str2num(get(handles.Rp,'String'));
wp = str2num(get(handles.omega_p,'String'));

% Calculates cutoff frequency
bandwidth = ws - wp;
M = ceil(6.6*pi/bandwidth) + 1;
n=[0:1:M-1];
wc = (ws+wp)/2; 

% Choose window function
switch get(handles.popupmenuwindow,'Value')  
    case 1
        j=boxcar(M);
    case 2
        j=bartlett(M);
    case 3
        j=hann(M);
    case 4
        j=hamming(M);
    case 5
        j=blackman(M);
    case 6
        beta = 0.1102*(As-8.7); 
        j=kaiser(M,beta);
end

% HPF
hd = ideal_lp(pi,M) - ideal_lp(wc,M);
h = hd' .* j;

% Calculates filtered signal
y = conv(h,x_audio);

axes(handles.axes2); 
t = 0:length(y)-1; 
plot(t,y,'linewidth',1,'color','r');title('Filtered Signal')
set(gca,'color', [0.1,0.2,0.8]);

axes(handles.axes3)
specgram(y, 1024, handles.Fs);
title('Spektrum Sinyal');
handles.fileLoad = 1;
set(gca,'color', [0.1,0.2,0.8]);
handles.x=y;
guidata(hObject, handles);

21. You can run the UI. Click LOAD AUDIO FILE button and then click HPF Audio button. The result is shown in figure below.
















Monday, October 14, 2019

Hamming-Window-Based FIR Filter And Its Implementation On Audio File

Hamming-Window-Based FIR Filter And Its Implementation On Audio File


Here, you will build a desktop application (UI, user interface) to simulate how is filtered using FIR filter, Hamming-Window-Based FIR Filter. A number of discrete signals are used as test signals are impulse, step, real exponential, sinusoidal, random, square, angled triangle, equilateral triangle, and trapezoidal signals. In the UI, this FIR filter is used to filter signals and audio file (wav) as well.


Follow these steps below:


1. Type this command on MATLAB prompt:


guide

The GUIDE Quick Start dialog box will show up, as shown in figure below:



2. Select the Blank GUI (Default) template, and then click OK:


3. Drag all components needed, as shown in figure below:


4. Right click on DRAW button and choose View Callbacks and then choose Callback. Define pbDraw_Callback() function below to read signals parameters and displays chosen signal in axes1 component:


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

global x;

% Reads n0, n1, and n2
n0 = str2num(get(handles.n0,'String'));
n1 = str2num(get(handles.n1,'String'));
n2 = str2num(get(handles.n2,'String'));
exp = str2num(get(handles.expCoef,'String'));

% Reads parameters for sinusoidal
A = str2num(get(handles.A,'String'));
Freq = str2num(get(handles.Freq,'String'));
Fase = str2num(get(handles.Phase,'String'));
Fs = str2num(get(handles.Fs,'String'));

% Reads parameters for square and triangle signals
width = str2num(get(handles.width, 'String'));

% Choose option from popup menu popupmenusignal1
switch get(handles.popupmenusignal1,'Value')  
    case 1
       n = [n1:n2]; x = [(n-n0) == 0];
       
       % Display discrete signal
       axes(handles.axes1)
       stem(n,x,'linewidth',2,'color','b'); title('Discrete Impulse Signal');

    case 2
       n = [n1:n2]; x = [(n-n0) >= 0];
       % Display discrete signal
       axes(handles.axes1)
       stem(n,x,'linewidth',2,'color','b'); title('Discrete Step Signal');
       
    case 3
       n = [n1:n2]; x = [(n-n0) >= 0].*[(exp).^(n-n0)];
       % Display discrete signal
       axes(handles.axes1)
       stem(n,x,'linewidth',2,'color','b'); 
       title('Discrete Real Exponential Signal');
       
    case 4
       n = [n1:n2]; x = [(n-n0) >= 0].*[A*sin(2*pi*(Freq)*((n-n0)/Fs)+Fase)];
       % Display discrete signal
       axes(handles.axes1)
       stem(n,x,'linewidth',2,'color','b'); 
       title('Discrete Sinusoidal Signal');
       
    case 5
       n = [n1:n2]; x = [(n-n0) >= 0].*(rand(1,(n2-n1+1))-0.5);
       % Display discrete signal
       axes(handles.axes1)
       stem(n,x,'linewidth',2,'color','b'); 
       title('Discrete Random Signal');

    case 6
       n = [n1:n2]; x = [(n-n0) >= 0];
       ny = [n1:n2]; xy = [(ny-width-n0-1) >= 0];
       x = x - xy;
       
       % Display discrete signal
       axes(handles.axes1)
       stem(n,x,'linewidth',2,'color','b'); title('Discrete Square Signal');

    case 7
       n = [n1:n2]; x = n.*[n >= 0];
       ny = [n1:n2]; xy = ny.*[(ny-width-1) >= 0];
       x = (x - xy)/width;
       nb = [n1+n0:n2+n0];
       
       % Reads signal range
       set(handles.n1,'string',(n1+n0));
       set(handles.n2,'string',(n2+n0));

       % Display discrete signal
       axes(handles.axes1)
       stem(nb,x,'linewidth',2,'color','b'); 
       title('Discrete Angled Triangle Signal');
 
    case 8
       n = [n1:n2]; x = n.*[n >= 0];
       x2 = [zeros(1,width), x(1:end-width)];
       x1 = -x;
       x1 = [zeros(1,0.5*width), x1(1:end-0.5*width)];
       nb1 = n1+n0; nb2 = n2+n0;
       nb = [nb1:nb2];
       
       % Read signal range
       set(handles.n1,'string',(nb1));
       set(handles.n2,'string',(nb2));

       % Returns discrete equilateral triangle signal
       x = (x + 2*x1+x2)/(0.5*width);
       
       % Display discrete signal
       axes(handles.axes1)
       stem(nb,x,'linewidth',2,'color','b'); 
       title('Discrete Equilateral Triangle Signal');
       
    case 9
       n = [n1:n2]; x = n.*[n >= 0];
       x2 = [zeros(1,width), x(1:end-width)];
       x3 = [zeros(1,3*width), x(1:end-3*width)];
       x1 = x;
       x1 = [zeros(1,2*width), x1(1:end-2*width)];
       nb1 = n1+n0; nb2 = n2+n0;
       nb = [nb1:nb2];
       
       % Reads signal range
       set(handles.n1,'string',(nb1));
       set(handles.n2,'string',(nb2));

       % Returns discrete trapezoidal signal
       x = (x - x2 - x1 + x3)/width;
       
       % Display discrete signal
       axes(handles.axes1)
       stem(nb,x,'linewidth',2,'color','b'); 
       title('Discrete Trapezoidal Signal');
end  

5. You can run the UI. Choose one of signals and then click DRAW button:


6. Define lp_ideal() function to calculate ideal lowpass filter length and its cutoff frequency in radian:


function hd= lp_ideal(wc,M)
% Calculates ideal lowpass filter
% -------------------------
% [hd]= lp_ideal(wc,M)
% wc = cutoff frequency in radian
% M = ideal filter length
% 
alpha=(M-1)/2;
n=[0:1:(M-1)];
m=n-alpha+eps;
hd=sin(wc*m)./(pi*m);

7. Right click on Lowpass button and choose View Callbacks and then choose Callback. Define pbLowpass_Callback() function below to read filter parameters, calculates and displays ideal lowpass impulse response, hamming window, actual lowpass impulse response, magnitude response, and filtered signal:


% --- Executes on button press in pbLowpass.
function pbLowpass_Callback(hObject, eventdata, handles)
% hObject    handle to pbLowpass (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
global x;

% Reads all filter parameters
omega1s = str2num(get(handles.omega1s,'String'));
omega2s = str2num(get(handles.omega2s,'String'));
omega1p = str2num(get(handles.omega1p,'String'));
omega2p = str2num(get(handles.omega2p,'String'));
A1s = str2num(get(handles.A1s,'String'));
A2s = str2num(get(handles.A2s,'String'));
R1p = str2num(get(handles.R1p,'String'));
R2p = str2num(get(handles.R2p,'String'));

% Multiplied with pi
omega1s = omega1s * pi;
omega1p = omega1p * pi;
omega2s = omega2s * pi;
omega2p = omega2p * pi;

if(omega1s > omega1p)
    % Calculates ideal filter length and cutoff frequency
    bandwith=omega1s-omega1p;
    M=ceil(6.6*pi/bandwith)+1
    n=[0:1:M-1];
    wc=(omega1s+omega1p)/2;     

    % The response of ideal lowpass filter
    hd=lp_ideal(wc,M);

    % Acual response of filter
    w_ham=(hamming(M))';
    h=hd.*w_ham;

    % Frequency response
    [db,mag,pha,grd,w]=freqz_m(h,[1]);
    delta_w=2*pi/1000;

    % Calculates ripples and attenuation
    Rp=(min(db(1:1:omega1p/delta_w+1)))   % actual passband ripples
    As=round(max(db(omega1s/delta_w+1:1:501)))  % minimum stopband attenuation

    % Displays ideal impulse response
    axes(handles.axes7);
    stem(n,hd,'color','r', 'LineWidth' ,2); title('Ideal Impulse Response');
    axis([0 M-1 -0.1 0.3]);xlabel('n'); ylabel('hd(n)'); 
    set(gca,'color',[0.2 0.4 0.5]);

    % Displays Hamming window
    axes(handles.axes8);
    stem(n,w_ham,'color','r', 'LineWidth' ,2); title('Hamming Window');
    axis([0 M-1 0 1.1]);xlabel('n'); ylabel('wn'); 
    set(gca,'color',[0.2 0.4 0.5]);

    % Displays actual impulse response
    axes(handles.axes9);
    stem(n,h,'color','r', 'LineWidth' ,2);title('Actual Impulse Response');
    axis([0 M-1 -0.1 0.3]);xlabel('n'); ylabel('h(n)'); 
    set(gca,'color',[0.2 0.4 0.5]);

    % Displays magnitude response in dB
    axes(handles.axes3);
    plot(w/pi,db,'color','r', 'LineWidth', 3);
    title('Magnitude Response in dB'); grid on;
    axis([0 1 -100 10]);xlabel('frekuensi dalam unit pi'); ylabel('dB'); 
    set(gca,'color',[0.2 0.4 0.5]);

    % Calculates and displays filtered signal
    y = conv(double(x),double(h), 'same');
    axes(handles.axes2); 
    t = 0:length(y)-1;  
    stem(t,y,'linewidth',1,'color','y');title('Filtered Signal')
    set(gca,'color',[0.2 0.4 0.5]);

else
    h = msgbox('Omega1s should be larger than Omega1p');
end

% Saves global variable
global lowpass
lowpass = h;

8. You can run the UI, choose one of signals, click DRAW button, and then click Lowpass button The result is shown in figure below.


9. Right click on Bandpass button and choose View Callbacks and then choose Callback. Define pbBandpass_Callback() function below to read filter parameters, calculates and displays ideal bandpass impulse response, hamming window, actual bandpass impulse response, magnitude response, and filtered signal:


% --- Executes on button press in pbBandpass.
function pbBandpass_Callback(hObject, eventdata, handles)
% hObject    handle to pbBandpass (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
global x;

% Reads all filter parameters
omega1s = str2num(get(handles.omega1s,'String'));
omega2s = str2num(get(handles.omega2s,'String'));
omega1p = str2num(get(handles.omega1p,'String'));
omega2p = str2num(get(handles.omega2p,'String'));
A1s = str2num(get(handles.A1s,'String'));
A2s = str2num(get(handles.A2s,'String'));
R1p = str2num(get(handles.R1p,'String'));
R2p = str2num(get(handles.R2p,'String'));

% Multiplied with pi
omega1s = omega1s * pi;
omega1p = omega1p * pi;
omega2s = omega2s * pi;
omega2p = omega2p * pi;

if((omega1p > omega1s)&&(omega2s > omega2p))
    % Calculates ideal filter length and cutoff frequency
    bandwidth=min((omega1p-omega1s),(omega2s-omega2p));
    M=ceil(6.6*pi/bandwidth)+1
    n=[0:1:M-1];
    wc1=(omega1s+omega1p)/2; wc2=(omega2s+omega2p)/2;

    % The response of ideal bandpass response
    hd=lp_ideal(wc2,M)-lp_ideal(wc1,M);

    % Acual response of filter
    w_ham=(hamming(M))';
    h=hd.*w_ham;

    % Frequency response
    [db,mag,pha,grd,w]=freqz_m(h,[1]);
    delta_w=2*pi/1000;

    % Calculates ripples and attenuation
    Rp=(min(db(1:1:omega1p/delta_w+1)))   % actual passband ripples
    As=round(max(db(omega1s/delta_w+1:1:501)))  % minimum stopband attenuation

    % Displays ideal impulse response
    axes(handles.axes7);
    stem(n,hd,'color','r', 'LineWidth' ,2); title('Ideal Impulse Response');
    axis([0 M-1 -0.5 0.5]);xlabel('n'); ylabel('hd(n)'); 
    set(gca,'color',[0.2 0.4 0.5]);

    % Displays Hamming window
    axes(handles.axes8);
    stem(n,w_ham,'color','r', 'LineWidth' ,2); title('Hamming Window');
    axis([0 M-1 0 1.1]);xlabel('n'); ylabel('wn'); 
    set(gca,'color',[0.2 0.4 0.5]);

    % Displays actual impulse response
    axes(handles.axes9);
    stem(n,h,'color','r', 'LineWidth' ,2);title('Actual Impulse Response');
    axis([0 M-1 -0.5 0.5]);xlabel('n'); ylabel('h(n)'); 
    set(gca,'color',[0.2 0.4 0.5]);

    % Displays magnitude response in dB
    axes(handles.axes3);
    plot(w/pi,db,'color','r', 'LineWidth', 3);
    title('Magnitude Response in dB'); grid on;
    axis([0 1 -100 10]);xlabel('frekuensi dalam unit pi'); ylabel('dB'); 
    set(gca,'color',[0.2 0.4 0.5]);

    % Calculates and displays filtered signal
    y = conv(double(x),double(h), 'same');
    axes(handles.axes2); 
    t = 0:length(y)-1;  
    stem(t,y,'linewidth',1,'color','y');title('Filtered Signal')
    set(gca,'color',[0.2 0.4 0.5]);

else
    h = msgbox('Omega1p should be larger than Omega1s and Omega2s should be larger than Omega2p');
end

% Saves global variable
global bandpass
bandpass = h;

10. You can run the UI, choose one of signals, click DRAW button, and then click Bandpass button The result is shown in figure below.


11. Right click on Bandstop button and choose View Callbacks and then choose Callback. Define pbBandstop_Callback() function below to read filter parameters, calculates and displays ideal bandstop impulse response, hamming window, actual bandstop impulse response, magnitude response, and filtered signal:


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

global x;

% Reads all filter parameters
omega1s = str2num(get(handles.omega1s,'String'));
omega2s = str2num(get(handles.omega2s,'String'));
omega1p = str2num(get(handles.omega1p,'String'));
omega2p = str2num(get(handles.omega2p,'String'));
A1s = str2num(get(handles.A1s,'String'));
A2s = str2num(get(handles.A2s,'String'));
R1p = str2num(get(handles.R1p,'String'));
R2p = str2num(get(handles.R2p,'String'));

% Multiplied with pi
omega1s = omega1s * pi;
omega1p = omega1p * pi;
omega2s = omega2s * pi;
omega2p = omega2p * pi;

if((omega1s > omega1p)&&(omega2p > omega2s))
    % Calculates ideal filter length and cutoff frequency
    bandwidth=min((omega1s-omega1p),(omega2p-omega2s));
    M=ceil(6.6*pi/bandwidth)+1
    n=[0:1:M-1];
    wc1=(omega1s+omega1p)/2; wc2=(omega2s+omega2p)/2;
    
    % The response of ideal bandstop filter
    hd=lp_ideal(wc1,M)+lp_ideal(pi,M)-lp_ideal(wc2,M);

    % Acual response of filter
    w_ham=(hamming(M))';
    h=hd.*w_ham;

    % Frequency response
    [db,mag,pha,grd,w]=freqz_m(h,[1]);
    delta_w=2*pi/1000;

    % Calculates ripples and attenuation
    Rp=(min(db(1:1:omega1p/delta_w+1)))   % actual passband ripples
    As=round(max(db(omega1s/delta_w+1:1:501)))  % minimum stopband attenuation

    % Displays ideal impulse response
    axes(handles.axes7);
    stem(n,hd,'color','r', 'LineWidth' ,2); title('Ideal Impulse Response');
    axis([0 M-1 -0.5 0.5]);xlabel('n'); ylabel('hd(n)'); 
    set(gca,'color',[0.2 0.4 0.5]);

    % Displays Hamming window
    axes(handles.axes8);
    stem(n,w_ham,'color','r', 'LineWidth' ,2); title('Hamming Window');
    axis([0 M-1 0 1.1]);xlabel('n'); ylabel('wn'); 
    set(gca,'color',[0.2 0.4 0.5]);

    % Displays actual impulse response
    axes(handles.axes9);
    stem(n,h,'color','r', 'LineWidth' ,2);title('Actual Impulse Response');
    axis([0 M-1 -0.5 0.5]);xlabel('n'); ylabel('h(n)'); 
    set(gca,'color',[0.2 0.4 0.5]);

    % Displays magnitude response in dB
    axes(handles.axes3);
    plot(w/pi,db,'color','r', 'LineWidth', 3);
    title('Magnitude Response in dB'); grid on;
    axis([0 1 -100 10]);xlabel('frekuensi dalam unit pi'); ylabel('dB'); 
    set(gca,'color',[0.2 0.4 0.5]);

    % Calculates and displays filtered signal
    y = conv(double(x),double(h), 'same');
    axes(handles.axes2); 
    t = 0:length(y)-1;  
    stem(t,y,'linewidth',1,'color','y');title('Filtered Signal')
    set(gca,'color',[0.2 0.4 0.5]);

else
    h = msgbox('Omega1s should be larger than Omega1p dan Omega2p should be larger than Omega2s');
end

12. You can run the UI, choose one of signals, click DRAW button, and then click Bandstop button The result is shown in figure below.


13. Right click on Highpass button and choose View Callbacks and then choose Callback. Define pbHighpass_Callback() function below to read filter parameters, calculates and displays ideal highpass impulse response, hamming window, actual highpass impulse response, magnitude response, and filtered signal:


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

global x;

% Reads all filter parameters
omega1s = str2num(get(handles.omega1s,'String'));
omega2s = str2num(get(handles.omega2s,'String'));
omega1p = str2num(get(handles.omega1p,'String'));
omega2p = str2num(get(handles.omega2p,'String'));
A1s = str2num(get(handles.A1s,'String'));
A2s = str2num(get(handles.A2s,'String'));
R1p = str2num(get(handles.R1p,'String'));
R2p = str2num(get(handles.R2p,'String'));

% Multiplied with pi
omega1s = omega1s * pi;
omega1p = omega1p * pi;
omega2s = omega2s * pi;
omega2p = omega2p * pi;

if((omega1s > omega1p)&&(omega2p > omega2s))
    % Calculates ideal filter length and cutoff frequency
    bandwidth=min((omega1s-omega1p),(omega2p-omega2s));
    M=ceil(6.6*pi/bandwidth)+1
    n=[0:1:M-1];
    wc1=(omega1s+omega1p)/2; wc2=(omega2s+omega2p)/2;
    
    % The response of ideal bandstop filter
    hd=lp_ideal(wc1,M)+lp_ideal(pi,M)-lp_ideal(wc2,M);

% --- Executes on button press in pbHighpass.
function pbHighpass_Callback(hObject, eventdata, handles)
% hObject    handle to pbHighpass (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
global x;

% Reads all filter parameters
omega1s = str2num(get(handles.omega1s,'String'));
omega2s = str2num(get(handles.omega2s,'String'));
omega1p = str2num(get(handles.omega1p,'String'));
omega2p = str2num(get(handles.omega2p,'String'));
A1s = str2num(get(handles.A1s,'String'));
A2s = str2num(get(handles.A2s,'String'));
R1p = str2num(get(handles.R1p,'String'));
R2p = str2num(get(handles.R2p,'String'));

% Multiplied with pi
omega1s = omega1s * pi;
omega1p = omega1p * pi;
omega2s = omega2s * pi;
omega2p = omega2p * pi;

if(omega2s > omega2p)
    % Calculates ideal filter length and cutoff frequency
    bandwidth=omega2s-omega2p;
    M=ceil(6.6*pi/bandwidth)+1
    n=[0:1:M-1];
    wc=(omega2s+omega2p)/2;     

    % The response of ideal highpass filter
    hd=lp_ideal(pi,M)-lp_ideal(wc,M);

    % Acual response of filter
    w_ham=(hamming(M))';
    h=hd.*w_ham;

    % Frequency response
    [db,mag,pha,grd,w]=freqz_m(h,[1]);
    delta_w=2*pi/1000;

    % Calculates ripples and attenuation
    Rp=(min(db(1:1:omega1p/delta_w+1)))   % actual passband ripples
    As=round(max(db(omega1s/delta_w+1:1:501)))  % minimum stopband attenuation

    % Displays ideal impulse response
    axes(handles.axes7);
    stem(n,hd,'color','r', 'LineWidth' ,2); title('Ideal Impulse Response');
    axis([0 M-1 -0.5 0.5]);xlabel('n'); ylabel('hd(n)'); 
    set(gca,'color',[0.2 0.4 0.5]);

    % Displays Hamming window
    axes(handles.axes8);
    stem(n,w_ham,'color','r', 'LineWidth' ,2); title('Hamming Window');
    axis([0 M-1 0 1.1]);xlabel('n'); ylabel('wn'); 
    set(gca,'color',[0.2 0.4 0.5]);

    % Displays actual impulse response
    axes(handles.axes9);
    stem(n,h,'color','r', 'LineWidth' ,2);title('Actual Impulse Response');
    axis([0 M-1 -0.5 0.5]);xlabel('n'); ylabel('h(n)'); 
    set(gca,'color',[0.2 0.4 0.5]);

    % Displays magnitude response in dB
    axes(handles.axes3);
    plot(w/pi,db,'color','r', 'LineWidth', 3);
    title('Magnitude Response in dB'); grid on;
    axis([0 1 -100 10]);xlabel('frekuensi dalam unit pi'); ylabel('dB'); 
    set(gca,'color',[0.2 0.4 0.5]);

    % Calculates and displays filtered signal
    y = conv(double(x),double(h), 'same');
    axes(handles.axes2); 
    t = 0:length(y)-1;  
    stem(t,y,'linewidth',1,'color','y');title('Filtered Signal')
    set(gca,'color',[0.2 0.4 0.5]);

else
    h = msgbox('Omega2s should be larger than Omega2p');
end

% Saves global variable
global highpass
highpass = h;

else
    h = msgbox('Omega1s should be larger than Omega1p dan Omega2p should be larger than Omega2s');
end

14. You can run the UI, choose one of signals, click DRAW button, and then click Highpass button The result is shown in figure below.


15. Right click on LOAD AUDIO FILE button and choose View Callbacks and then choose Callback. Define pbLoadingAudioFile_Callback() function below to read wav file and calculate its spectrum:


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

global x_audio;

[filename,path] = uigetfile({'*.wav'},'Muat File Wav');
[x,Fs] = wavread([path '/' filename]);
handles.x = x ./ max(abs(x));
handles.Fs = Fs;

axes(handles.axes2);
waktu = 0:1/Fs:(length(handles.x)-1)/Fs;
plot(waktu, handles.x,'linewidth',1,'color','r');
set(gca,'color',[0.2 0.4 0.5]);
axis([0 max(waktu) -1 1]); title('Audio Signal');

axes(handles.axes3);
specgram(handles.x, 1024, handles.Fs);
set(gca,'color',[0.2 0.4 0.5]);
title('Signal Spectrum Sinyal');
handles.fileDimuat = 1;

handles.fileBerderau = 0;
handles.fileFinal = 0;

set(handles.SamplingFreq, 'String', num2str(Fs));
set(handles.NSamples, 'String', num2str(length(handles.x)));

x_audio = handles.x;
guidata(hObject, handles);

16. You can run the UI, choose wav file and click LOADING AUDIO FILE button. The result is shown in figure below.


17. Right click on LOWPASS AUDIO button and choose View Callbacks and then choose Callback. Define pbLowpassAudio_Callback() function below to read global variable and calculate filtered signal and its spectrum:


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

% Reads global variable
global lowpass
h = lowpass;

global x_audio;

% Calculates filtered signal
y = conv(h,x_audio);

axes(handles.axes2); 
t = 0:length(y)-1; 
plot(t,y,'linewidth',1,'color','r');title('Filtered Signal')
set(gca,'color', [0.2 0.6 0.5]);

axes(handles.axes3)
specgram(y, 1024, handles.Fs);
title('Spectrum');
handles.fileDimuat = 1;
set(gca,'color',[0,0,0]);
handles.x=y;
guidata(hObject, handles);

18. You can run the UI, choose wav file, click LOADING AUDIO FILE button, and click LOWPASS AUDIO button. The result is shown in figure below.


19. Right click on BANDPASS AUDIO button and choose View Callbacks and then choose Callback. Define pbBandpassAudio_Callback() function below to read global variable and calculate filtered signal and its spectrum:


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

% Reads global variable
global bandpass
h = bandpass;
global x_audio;

global x_audio;

% Calculates filtered signal
y = conv(h,x_audio);

axes(handles.axes2); 
t = 0:length(y)-1; 
plot(t,y,'linewidth',1,'color','r');title('Filtered Signal')
set(gca,'color', [0.2 0.6 0.5]);

axes(handles.axes3)
specgram(y, 1024, handles.Fs);
title('Spectrum');
handles.fileDimuat = 1;
set(gca,'color',[0,0,0]);
handles.x=y;
guidata(hObject, handles);

20. You can run the UI, choose wav file, click LOADING AUDIO FILE button, and click BANDPASS AUDIO button. The result is shown in figure below.


21. Right click on BANDSTOP AUDIO button and choose View Callbacks and then choose Callback. Define pbBandstopAudio_Callback() function below to read global variable and calculate filtered signal and its spectrum:


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

% Reads global variable
global bandstop
h = bandstop;

global x_audio;

% Calculates filtered signal
y = conv(h,x_audio);

axes(handles.axes2); 
t = 0:length(y)-1; 
plot(t,y,'linewidth',1,'color','r');title('Filtered Signal')
set(gca,'color', [0.2 0.6 0.5]);

axes(handles.axes3)
specgram(y, 1024, handles.Fs);
title('Spectrum');
handles.fileDimuat = 1;
set(gca,'color',[0,0,0]);
handles.x=y;
guidata(hObject, handles);

22. You can run the UI, choose wav file, click LOADING AUDIO FILE button, and click BANDSTOP AUDIO button. The result is shown in figure below.


23. Right click on HIGHPASS AUDIO button and choose View Callbacks and then choose Callback. Define pbHighpassAudio_Callback() function below to read global variable and calculate filtered signal and its spectrum:


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

% Reads global variable
global highpass
h = highpass;

global x_audio;

% Calculates filtered signal
y = conv(h,x_audio);

axes(handles.axes2); 
t = 0:length(y)-1; 
plot(t,y,'linewidth',1,'color','r');title('Filtered Signal')
set(gca,'color', [0.2 0.6 0.5]);

axes(handles.axes3)
specgram(y, 1024, handles.Fs);
title('Spectrum');
handles.fileDimuat = 1;
set(gca,'color',[0,0,0]);
handles.x=y;
guidata(hObject, handles);

24. You can run the UI, choose wav file, click LOADING AUDIO FILE button, and click HIGHPASS AUDIO button. The result is shown in figure below.