Monday, October 14, 2019

Odd Length Symmetric Linear-Phase FIR Filter

Odd Length Symmetric Linear-Phase FIR Filter


Here, you will build a desktop application (UI, user interface) to simulate how is filtered using FIR filter, Odd Length Symmetric Linear-Phase 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 Hr_Type1(), Hr_Type2(), Hr_Type3(), and Hr_Type4()  functions to calculate the magnitude response of each linear-phase filter:


function [Hr,w,a,L] = Hr_Type1(h)
% Calculates amplitude response Hr(w) of FIR LP Type-1 filter
% -----------------------------------------------------------
% [Hr,w,a,L] = Hr_Type1(h)
% Hr = Amplitude Response
% w = 500 frequencies in [0 pi] range
% a = Linear-Phase coefficients 
% L = The degree of Hr
% h = Impulse response of FIR LP Type-1 filter
%

M=length(h);
L=(M-1)/2;
a=[h(L+1) 2*h(L:-1:1)];     % row vector 1x(L+1)
n=[0:1:L];                  % column vector (L+1)x1
w=[0:1:500]'*pi/500;
Hr=cos(w*n)*a';

function [Hr,w,b,L] = Hr_Tipe2(h)
% Calculates amplitude response Hr(w) of FIR LP Type-2 filter
% -----------------------------------------------------------
% [Hr,w,a,L] = Hr_Type2(h)
% Hr = Amplitude Response
% w = 500 frequencies in [0 pi] range
% a = Linear-Phase coefficients 
% L = The degree of Hr
% h = Impulse response of FIR LP Type-2 filter
%
M = length(h); L = M/2;
b = 2*[h(L:-1:1)]; n = [1:1:L]; n = n-0.5;
w = [0:1:500]'*pi/500; Hr = cos(w*n)*b';

function [Hr,w,c,L] = Hr_Tipe3(h)
% Calculates amplitude response Hr(w) of FIR LP Type-3 filter
% -----------------------------------------------------------
% [Hr,w,a,L] = Hr_Type3(h)
% Hr = Amplitude Response
% w = 500 frequencies in [0 pi] range
% a = Linear-Phase coefficients 
% L = The degree of Hr
% h = Impulse response of FIR LP Type-3 filter
%
M = length(h); L = floor((M-1)/2);
c = [2*h(L+1:-1:1)]; n = [0:1:L];
w = [0:1:500]'*pi/500; Hr = sin(w*n)*c';

function [Hr,w,d,L] = Hr_Tipe4(h)
% Calculates amplitude response Hr(w) of FIR LP Type-4 filter
% -----------------------------------------------------------
% [Hr,w,a,L] = Hr_Type4(h)
% Hr = Amplitude Response
% w = 500 frequencies in [0 pi] range
% a = Linear-Phase coefficients 
% L = The degree of Hr
% h = Impulse response of FIR LP Type-4 filter
%
M = length(h); L = M/2;
d = 2*[h(L:-1:1)]; n = [1:1:L]; n = n-0.5;
w = [0:1:500]'*pi/500; Hr = sin(w*n)*d';

7. Right click on LINEAR PHASE TYPE 1 button and choose View Callbacks and then choose Callback. Define pbLPType1_Callback() function below calculate LP coefficients, its responses, and filtered signal:


% --- Executes on button press in pbLPType1.
function pbLPType1_Callback(hObject, eventdata, handles)
% hObject    handle to pbLPType1 (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 LP coefficients
h3 = str2num(get(handles.h3,'String'));
h4 = str2num(get(handles.h4,'String'));
h1 = str2num(get(handles.h1,'String'));
h2 = str2num(get(handles.h2,'String'));
h0 = str2num(get(handles.h0,'String'));
h6 = str2num(get(handles.h6,'String'));
h5 = str2num(get(handles.h5,'String'));
h7 = str2num(get(handles.h7,'String'));
h8 = str2num(get(handles.h8,'String'));
h9 = str2num(get(handles.h9,'String'));
h10 = str2num(get(handles.h10,'String'));

h = [h0 h1 h2 h3 h4 h5 h6 h7 h8 h9 h10];

% Calculates LP type-1
M = length(h); n = 0:M-1;
[Hr,w,a,L] = Hr_Type1(h);

amax = max(a)+1; amin = min(a)-1;
axes(handles.axes7);
stem(n,h,'lineWidth', 2, 'color','r'); axis([-1 2*L+1 amin amax])
set(gca,'color',[0,0,0]);
xlabel('n'); ylabel('h(n)'); title('Impulse Response')

axes(handles.axes8);
stem(0:L,a, 'lineWidth', 2, 'color','r'); axis([-1 2*L+1 amin amax])
xlabel('n'); ylabel('a(n)'); title('Linear-Phase Coefficients a(n)')
set(gca,'color',[0,0,0]);

axes(handles.axes9);
plot(w/pi,Hr,'lineWidth', 2, 'color','r');grid
xlabel('Frequncy in pi Unit'); xlabel('Hr')
title('Amplitude Response Type-1')
set(gca,'color',[0,0,0]);

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

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

axes(handles.axes3)
specgram(y, 8, 8);
title('Signal Spectrum');
set(gca,'color',[0,0,0]);

8. You can run the UI. Choose one of signals and click on DRAW button. Then click on LINEAR PHASE TYPE 1 button to calculate LP coefficients, its responses, and filtered signal:



9. Right click on LINEAR PHASE TYPE 2 button and choose View Callbacks and then choose Callback. Define pbLPType2_Callback() function below to calculate LP coefficients, its responses, and filtered signal:


% --- Executes on button press in pbLPType2.
function pbLPType2_Callback(hObject, eventdata, handles)
% hObject    handle to pbLPType2 (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 LP coefficients
h3 = str2num(get(handles.h3,'String'));
h4 = str2num(get(handles.h4,'String'));
h1 = str2num(get(handles.h1,'String'));
h2 = str2num(get(handles.h2,'String'));
h0 = str2num(get(handles.h0,'String'));
h6 = str2num(get(handles.h6,'String'));
h5 = str2num(get(handles.h5,'String'));
h7 = str2num(get(handles.h7,'String'));
h8 = str2num(get(handles.h8,'String'));
h9 = str2num(get(handles.h9,'String'));
h10 = str2num(get(handles.h10,'String'));

h = [h0 h1 h2 h3 h4 h5 h6 h7 h8 h9 h10];

% Calculates LP type-2
M = length(h); n = 0:M-1;
[Hr,w,a,L] = Hr_Type2(h);

amax = max(a)+1; amin = min(a)-1;
axes(handles.axes7);
stem(n,h,'lineWidth', 2, 'color','r'); axis([-1 2*L+1 amin amax])
set(gca,'color',[0,0,0]);
xlabel('n'); ylabel('h(n)'); title('Impulse Response')

axes(handles.axes8);
stem(1:L,a, 'lineWidth', 2, 'color','r'); axis([-1 2*L+1 amin amax])
xlabel('n'); ylabel('a(n)'); title('Linear-Phase Coefficients a(n)')
set(gca,'color',[0,0,0]);

axes(handles.axes9);
plot(w/pi,Hr,'lineWidth', 2, 'color','r');grid
xlabel('Frequncy in pi Unit'); xlabel('Hr')
title('Amplitude Response Type-2')
set(gca,'color',[0,0,0]);

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

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

axes(handles.axes3)
specgram(y, 8, 8);
title('Signal Spectrum');
set(gca,'color',[0,0,0]);

10. You can run the UI. Choose one of signals and click on DRAW button. Then click on LINEAR PHASE TYPE 2 button to calculate LP coefficients, its responses, and filtered signal:


11. Right click on LINEAR PHASE TYPE 3 button and choose View Callbacks and then choose Callback. Define pbLPType3_Callback() function below to calculate LP coefficients, its responses, and filtered signal:


% --- Executes on button press in pbLPType3.
function pbLPType3_Callback(hObject, eventdata, handles)
% hObject    handle to pbLPType3 (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 LP coefficients
h3 = str2num(get(handles.h3,'String'));
h4 = str2num(get(handles.h4,'String'));
h1 = str2num(get(handles.h1,'String'));
h2 = str2num(get(handles.h2,'String'));
h0 = str2num(get(handles.h0,'String'));
h6 = str2num(get(handles.h6,'String'));
h5 = str2num(get(handles.h5,'String'));
h7 = str2num(get(handles.h7,'String'));
h8 = str2num(get(handles.h8,'String'));
h9 = str2num(get(handles.h9,'String'));
h10 = str2num(get(handles.h10,'String'));

h = [h0 h1 h2 h3 h4 h5 h6 h7 h8 h9 h10];

% Calculates LP type-3
M = length(h); n = 0:M-1;
[Hr,w,a,L] = Hr_Type3(h);

amax = max(a)+1; amin = min(a)-1;
axes(handles.axes7);
stem(n,h,'lineWidth', 2, 'color','r'); axis([-1 2*L+1 amin amax])
set(gca,'color',[0,0,0]);
xlabel('n'); ylabel('h(n)'); title('Impulse Response')

axes(handles.axes8);
stem(0:L,a, 'lineWidth', 2, 'color','r'); axis([-1 2*L+1 amin amax])
xlabel('n'); ylabel('a(n)'); title('Linear-Phase Coefficients a(n)')
set(gca,'color',[0,0,0]);

axes(handles.axes9);
plot(w/pi,Hr,'lineWidth', 2, 'color','r');grid
xlabel('Frequncy in pi Unit'); xlabel('Hr')
title('Amplitude Response Type-3')
set(gca,'color',[0,0,0]);

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

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

axes(handles.axes3)
specgram(y, 8, 8);
title('Signal Spectrum');
set(gca,'color',[0,0,0]);

12. You can run the UI. Choose one of signals and click on DRAW button. Then click on LINEAR PHASE TYPE 3 button to calculate LP coefficients, its responses, and filtered signal:


13. Right click on LINEAR PHASE TYPE 4 button and choose View Callbacks and then choose Callback. Define pbLPType4_Callback() function below to calculate LP coefficients, its responses, and filtered signal:


% --- Executes on button press in pbLPType4.
function pbLPType4_Callback(hObject, eventdata, handles)
% hObject    handle to pbLPType4 (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 LP coefficients
h3 = str2num(get(handles.h3,'String'));
h4 = str2num(get(handles.h4,'String'));
h1 = str2num(get(handles.h1,'String'));
h2 = str2num(get(handles.h2,'String'));
h0 = str2num(get(handles.h0,'String'));
h6 = str2num(get(handles.h6,'String'));
h5 = str2num(get(handles.h5,'String'));
h7 = str2num(get(handles.h7,'String'));
h8 = str2num(get(handles.h8,'String'));
h9 = str2num(get(handles.h9,'String'));
h10 = str2num(get(handles.h10,'String'));

h = [h0 h1 h2 h3 h4 h5 h6 h7 h8 h9 h10];

% Calculates LP type-1
M = length(h); n = 0:M-1;
[Hr,w,a,L] = Hr_Type1(h);

amax = max(a)+1; amin = min(a)-1;
axes(handles.axes7);
stem(n,h,'lineWidth', 2, 'color','r'); axis([-1 2*L+1 amin amax])
set(gca,'color',[0,0,0]);
xlabel('n'); ylabel('h(n)'); title('Impulse Response')

axes(handles.axes8);
stem(0:L,a, 'lineWidth', 2, 'color','r'); axis([-1 2*L+1 amin amax])
xlabel('n'); ylabel('a(n)'); title('Linear-Phase Coefficients a(n)')
set(gca,'color',[0,0,0]);

axes(handles.axes9);
plot(w/pi,Hr,'lineWidth', 2, 'color','r');grid
xlabel('Frequncy in pi Unit'); xlabel('Hr')
title('Amplitude Response Type-4')
set(gca,'color',[0,0,0]);

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

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

axes(handles.axes3)
specgram(y, 8, 8);
title('Signal Spectrum');
set(gca,'color',[0,0,0]);

14. You can run the UI. Choose one of signals and click on DRAW button. Then click on LINEAR PHASE TYPE 4 button to calculate LP coefficients, its responses, and filtered signal:


15. Right click on LOADING AUDIO FILE button and choose View Callbacks and then choose Callback. Define pbLoadingAudioFile_Callback() function below to read audio signal and display it in axes2 component:


% --- 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'},'load audio file');
[x,Fs] = audioread([path '/' filename]);
handles.x = x ./ max(abs(x));
handles.Fs = Fs;
axes(handles.axes2);
time = 0:1/Fs:(length(handles.x)-1)/Fs;
plot(time, handles.x,'linewidth',1,'color','r');
set(gca,'color',[0,0,0]);
axis([0 max(time) -1 1]); title('Audio Signal');

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

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

set(handles.freqSamplingAudio, '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 one of audio file by clicking LOADING AUDIO FILE button. The audio signal will be displayed in axes2 component and signal spectrum will be displayed in axes3 component:


17. Right click on FILTERING AUDIO TYPE 1 button and choose View Callbacks and then choose Callback. Define pbFilteringAudioType1_Callback() function below to read audio signal and display it in axes2 component. The filtered signal will be displayed in axes3 component.


% --- Executes on button press in pbFilteringAudioType1.
function pbFilteringAudioType1_Callback(hObject, eventdata, handles)
% hObject    handle to pbFilteringAudioType1 (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 LP coefficients
h3 = str2num(get(handles.h3,'String'));
h4 = str2num(get(handles.h4,'String'));
h1 = str2num(get(handles.h1,'String'));
h2 = str2num(get(handles.h2,'String'));
h0 = str2num(get(handles.h0,'String'));
h6 = str2num(get(handles.h6,'String'));
h5 = str2num(get(handles.h5,'String'));
h7 = str2num(get(handles.h7,'String'));
h8 = str2num(get(handles.h8,'String'));
h9 = str2num(get(handles.h9,'String'));
h10 = str2num(get(handles.h10,'String'));

h = [h0 h1 h2 h3 h4 h5 h6 h7 h8 h9 h10];

% Calculates LP type-1
M = length(h); n = 0:M-1;
[Hr,w,a,L] = Hr_Type1(h);

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

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

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

18. Now, you can run the UI and click FILTERING AUDIO TYPE 1 button and see the filtered audio and its spectrum:


19. Right click on FILTERING AUDIO TYPE 2 button and choose View Callbacks and then choose Callback. Define pbFilteringAudioType2_Callback() function below to read audio signal and display it in axes2 component. The filtered signal will be displayed in axes3 component.


% --- Executes on button press in pbFilteringAudioType1.
function pbFilteringAudioType2_Callback(hObject, eventdata, handles)
% hObject    handle to pbFilteringAudioType1 (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 LP coefficients
h3 = str2num(get(handles.h3,'String'));
h4 = str2num(get(handles.h4,'String'));
h1 = str2num(get(handles.h1,'String'));
h2 = str2num(get(handles.h2,'String'));
h0 = str2num(get(handles.h0,'String'));
h6 = str2num(get(handles.h6,'String'));
h5 = str2num(get(handles.h5,'String'));
h7 = str2num(get(handles.h7,'String'));
h8 = str2num(get(handles.h8,'String'));
h9 = str2num(get(handles.h9,'String'));
h10 = str2num(get(handles.h10,'String'));

h = [h0 h1 h2 h3 h4 h5 h6 h7 h8 h9 h10];

% Calculates LP type-2
M = length(h); n = 0:M-1;
[Hr,w,a,L] = Hr_Type2(h);

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

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

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

20. Now, you can run the UI and click FILTERING AUDIO TYPE 2 button and see the filtered audio and its spectrum:


21. Right click on FILTERING AUDIO TYPE 3 button and choose View Callbacks and then choose Callback. Define pbFilteringAudioType3_Callback() function below to read audio signal and display it in axes2 component. The filtered signal will be displayed in axes3 component.


% --- Executes on button press in pbFilteringAudioType1.
function pbFilteringAudioType3_Callback(hObject, eventdata, handles)
% hObject    handle to pbFilteringAudioType1 (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 LP coefficients
h3 = str2num(get(handles.h3,'String'));
h4 = str2num(get(handles.h4,'String'));
h1 = str2num(get(handles.h1,'String'));
h2 = str2num(get(handles.h2,'String'));
h0 = str2num(get(handles.h0,'String'));
h6 = str2num(get(handles.h6,'String'));
h5 = str2num(get(handles.h5,'String'));
h7 = str2num(get(handles.h7,'String'));
h8 = str2num(get(handles.h8,'String'));
h9 = str2num(get(handles.h9,'String'));
h10 = str2num(get(handles.h10,'String'));

h = [h0 h1 h2 h3 h4 h5 h6 h7 h8 h9 h10];

% Calculates LP type-3
M = length(h); n = 0:M-1;
[Hr,w,a,L] = Hr_Type3(h);

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

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

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

22. Now, you can run the UI and click FILTERING AUDIO TYPE 3 button and see the filtered audio and its spectrum:


23. Right click on FILTERING AUDIO TYPE 4 button and choose View Callbacks and then choose Callback. Define pbFilteringAudioType4_Callback() function below to read audio signal and display it in axes2 component. The filtered signal will be displayed in axes3 component.


% --- Executes on button press in pbFilteringAudioType1.
function pbFilteringAudioType4_Callback(hObject, eventdata, handles)
% hObject    handle to pbFilteringAudioType1 (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 LP coefficients
h3 = str2num(get(handles.h3,'String'));
h4 = str2num(get(handles.h4,'String'));
h1 = str2num(get(handles.h1,'String'));
h2 = str2num(get(handles.h2,'String'));
h0 = str2num(get(handles.h0,'String'));
h6 = str2num(get(handles.h6,'String'));
h5 = str2num(get(handles.h5,'String'));
h7 = str2num(get(handles.h7,'String'));
h8 = str2num(get(handles.h8,'String'));
h9 = str2num(get(handles.h9,'String'));
h10 = str2num(get(handles.h10,'String'));

h = [h0 h1 h2 h3 h4 h5 h6 h7 h8 h9 h10];

% Calculates LP type-4
M = length(h); n = 0:M-1;
[Hr,w,a,L] = Hr_Type4(h);

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

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

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

24. Now, you can run the UI and click FILTERING AUDIO TYPE 4 button and see the filtered audio and its spectrum:




No comments:

Post a Comment