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.




No comments:

Post a Comment