Sunday, October 13, 2019

IIR Filter: Direct Form II with MATLAB GUI

IIR Filter: Direct Form II


Here, you will build a desktop application (UI, user interface) to simulate how is filtered using IIR filter with direct form II. 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 IIR 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. Right click on FREQUENCY RESPONSE OF FILTER button and choose View Callbacks and then choose Callback. Define pbFreqResponse_Callback() function below to calculate FFT of designed IIR filter:


% --- Executes on button press in pbFreqResponse.
function pbFreqResponse_Callback(hObject, eventdata, handles)
% hObject    handle to pbFreqResponse (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 b0 to b4
b0 = str2num(get(handles.b0,'String'));
b1 = str2num(get(handles.b1,'String'));
b2 = str2num(get(handles.b2,'String'));
b3 = str2num(get(handles.b3,'String'));
b4 = str2num(get(handles.b4,'String'));

% Reads a1 to a4
a1 = str2num(get(handles.a1,'String'));
a2 = str2num(get(handles.a2,'String'));
a3 = str2num(get(handles.a3,'String'));
a4 = str2num(get(handles.a4,'String'));

% Filter coeffs
b = [b0 b1 b2 b3 b4];
a = [1 a1 a2 a3 a4];

% Menghitung Tanggapan Frekuensi
[h,w] = freqz(b,a,'whole',2001);

axes(handles.axes2); 
plot(w/pi,20*log10(abs(h)),'linewidth',2,'color','r'); grid on;

ax = gca;
ax.YLim = [-100 20];
ax.XTick = 0:.5:2;
xlabel('Normalized Frequency  (\times\pi rad/sample)')
ylabel('Magnitude (dB)'); title('Frequency Response of Designed Filter')

7. You can run the UI. Choose one of signals and click on DRAW button. Then click on FREQUENCY RESPONSE OF FILTER button to calculate FFT of designed IIR filter:


8. Right click on FILTERING SIGNAL button and choose View Callbacks and then choose Callback. Define pbFilteringSignal_Callback() function below to read input signal and calculates filtered signal in axes3 component:


% --- Executes on button press in pbFrequencyResponse.
function pbFrequencyResponse_Callback(hObject, eventdata, handles)
% hObject    handle to pbFrequencyResponse (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 b0 to b4
b0 = str2num(get(handles.b0,'String'));
b1 = str2num(get(handles.b1,'String'));
b2 = str2num(get(handles.b2,'String'));
b3 = str2num(get(handles.b3,'String'));
b4 = str2num(get(handles.b4,'String'));

% Reads a1 to a4
a1 = str2num(get(handles.a1,'String'));
a2 = str2num(get(handles.a2,'String'));
a3 = str2num(get(handles.a3,'String'));
a4 = str2num(get(handles.a4,'String'));

% Filter coefficients
b = [b0 b1 b2 b3 b4];
a = [1 a1 a2 a3 a4];

% Calculates frequency response
[h,w] = freqz(b,a,'whole',2001);

axes(handles.axes2); 
plot(w/pi,20*log10(abs(h)),'linewidth',3,'color','r'); grid on;

ax = gca;
ax.YLim = [-100 20];
ax.XTick = 0:.5:2;
xlabel('Normalized Frequency  (\times\pi rad/cuplik)')
ylabel('Magnitude (dB)'); title('Frequency Response of Filter')




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


% --- Executes on button press in pbLoadAudio.
function pbLoadAudio_Callback(hObject, eventdata, handles)
% hObject    handle to pbLoadAudio (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);
axis([0 max(time) -1 1]); title('Audio Signal');

axes(handles.axes3);
specgram(handles.x, 1024, handles.Fs);
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);

10. You can run the UI. Choose one of audio file by clicking LOAD AUDIO FILE button. The audio signal will be displayed in axes2 component and signal spectrum will be displayed in axes3 component:


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


% --- Executes on button press in pbFilteringAudio.
function pbFilteringAudio_Callback(hObject, eventdata, handles)
% hObject    handle to pbFilteringAudio (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;

% Reads b0 to b4
b0 = str2num(get(handles.b0,'String'));
b1 = str2num(get(handles.b1,'String'));
b2 = str2num(get(handles.b2,'String'));
b3 = str2num(get(handles.b3,'String'));
b4 = str2num(get(handles.b4,'String'));

% Reads a1 to a4
a1 = str2num(get(handles.a1,'String'));
a2 = str2num(get(handles.a2,'String'));
a3 = str2num(get(handles.a3,'String'));
a4 = str2num(get(handles.a4,'String'));

% Filter coefficients
b = [b0 b1 b2 b3 b4];
a = [1 a1 a2 a3 a4];

% Calculates filtered audio
y = filter(b,a,x_audio);

axes(handles.axes2); 
t = 0:length(y)-1;  
plot(t,y);title('Filtered Audio')

axes(handles.axes3)
specgram(y, 1024, handles.Fs);
title('Filtered Signal Spectrum');
handles.fileDimuat = 1;

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

12. Now, you can run the UI and click FILTERING AUDIO FILE button and see the filtered audio and its spectrum:





IIR Filter: Direct Form I with MATLAB GUI

IIR Filter: Direct Form I


Here, you will build a desktop application (UI, user interface) to simulate how is filtered using IIR filter with direct form 1. 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 IIR 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. Right click on FREQUENCY RESPONSE OF FILTER button and choose View Callbacks and then choose Callback. Define pbFreqResponse_Callback() function below to calculate FFT of designed IIR filter:


% --- Executes on button press in pbFreqResponse.
function pbFreqResponse_Callback(hObject, eventdata, handles)
% hObject    handle to pbFreqResponse (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 b0 to b4
b0 = str2num(get(handles.b0,'String'));
b1 = str2num(get(handles.b1,'String'));
b2 = str2num(get(handles.b2,'String'));
b3 = str2num(get(handles.b3,'String'));
b4 = str2num(get(handles.b4,'String'));

% Reads a1 to a4
a1 = str2num(get(handles.a1,'String'));
a2 = str2num(get(handles.a2,'String'));
a3 = str2num(get(handles.a3,'String'));
a4 = str2num(get(handles.a4,'String'));

% Filter coeffs
b = [b0 b1 b2 b3 b4];
a = [1 a1 a2 a3 a4];

% Menghitung Tanggapan Frekuensi
[h,w] = freqz(b,a,'whole',2001);

axes(handles.axes2); 
plot(w/pi,20*log10(abs(h)),'linewidth',2,'color','r'); grid on;

ax = gca;
ax.YLim = [-100 20];
ax.XTick = 0:.5:2;
xlabel('Normalized Frequency  (\times\pi rad/sample)')
ylabel('Magnitude (dB)'); title('Frequency Response of Designed Filter')

7. You can run the UI. Choose one of signals and click on DRAW button. Then click on FREQUENCY RESPONSE OF FILTER button to calculate FFT of designed IIR filter:


8. Right click on FILTERING SIGNAL button and choose View Callbacks and then choose Callback. Define pbFilteringSignal_Callback() function below to read input signal and calculates filtered signal in axes3 component:


% --- Executes on button press in pbFilteringSignal.
function pbFilteringSignal_Callback(hObject, eventdata, handles)
% hObject    handle to pbFilteringSignal (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 b0 to b4
b0 = str2num(get(handles.b0,'String'));
b1 = str2num(get(handles.b1,'String'));
b2 = str2num(get(handles.b2,'String'));
b3 = str2num(get(handles.b3,'String'));
b4 = str2num(get(handles.b4,'String'));

% Reads a1 to a4
a1 = str2num(get(handles.a1,'String'));
a2 = str2num(get(handles.a2,'String'));
a3 = str2num(get(handles.a3,'String'));
a4 = str2num(get(handles.a4,'String'));

% Filter coefficients
b = [b0 b1 b2 b3 b4];
a = [1 a1 a2 a3 a4];

% Calculates filtered signal
y = filter(b,a,x);

axes(handles.axes3); 
t = 0:length(y)-1;  

stem(t,y,'linewidth',3,'color','g'); title('Filtered Signal')



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


% --- Executes on button press in pbLoadAudio.
function pbLoadAudio_Callback(hObject, eventdata, handles)
% hObject    handle to pbLoadAudio (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);
axis([0 max(time) -1 1]); title('Audio Signal');

axes(handles.axes3);
specgram(handles.x, 1024, handles.Fs);
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);

10. You can run the UI. Choose one of audio file by clicking LOAD AUDIO FILE button. The audio signal will be displayed in axes2 component and signal spectrum will be displayed in axes3 component:


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


% --- Executes on button press in pbFilteringAudio.
function pbFilteringAudio_Callback(hObject, eventdata, handles)
% hObject    handle to pbFilteringAudio (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;

% Reads b0 to b4
b0 = str2num(get(handles.b0,'String'));
b1 = str2num(get(handles.b1,'String'));
b2 = str2num(get(handles.b2,'String'));
b3 = str2num(get(handles.b3,'String'));
b4 = str2num(get(handles.b4,'String'));

% Reads a1 to a4
a1 = str2num(get(handles.a1,'String'));
a2 = str2num(get(handles.a2,'String'));
a3 = str2num(get(handles.a3,'String'));
a4 = str2num(get(handles.a4,'String'));

% Filter coefficients
b = [b0 b1 b2 b3 b4];
a = [1 a1 a2 a3 a4];

% Calculates filtered audio
y = filter(b,a,x_audio);

axes(handles.axes2); 
t = 0:length(y)-1;  
plot(t,y);title('Filtered Audio')

axes(handles.axes3)
specgram(y, 1024, handles.Fs);
title('Filtered Signal Spectrum');
handles.fileDimuat = 1;

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

12. Now, you can run the UI and click FILTERING AUDIO FILE button and see the filtered audio and its spectrum:





Discrete Signals And Systems with MATLAB GUI

Discrete Signals And Systems with MATLAB GUI


Here, you will build a desktop application (UI, user interface) to simulate how signals and discrete systems are built and how fast fourier transform is applied on those discrete signals. 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.


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 four axes, thirteen panels, nine push buttons, twenty edit texts on UI form. All Tag properties that needed in Matlab code are set as shown in the figure below. 



4. Set the String property of popupmenusignal1 component as shown below:


4. Set the String property of popupmenusignal2 component as shown below:



6. Save UI form as discrete_signals.fig. 
7. Right-click on pbDraw1 and choose View Callbacks and then choose Callback. 


8. Define pbDraw1_Callback() function below to reads signals parameters and displays chosen signal in axes1 component:


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

% Reads n01, n11, and n21
n0 = str2num(get(handles.n01,'String'));
n1 = str2num(get(handles.n11,'String'));
n2 = str2num(get(handles.n21,'String'));
eksp = str2num(get(handles.coef1,'String'));

% Reads parameters for sinusoidal
A = str2num(get(handles.A1,'String'));
Frek = str2num(get(handles.Freq1,'String'));
Fase = str2num(get(handles.Phase1,'String'));
Fs = str2num(get(handles.Fs1,'String'));

% Reads parameters for square and triangle signals
width = str2num(get(handles.width1, '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].*[(eksp).^(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*(Frek)*((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.n11,'string',(n1+n0));
       set(handles.n21,'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.n11,'string',(nb1));
       set(handles.n21,'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.n11,'string',(nb1));
       set(handles.n21,'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  

9. Run UI, choose one of signals from popupmenusignal1 component, and click Draw buton. The related discrete signal will be displayed in axes1 component:



10. Right-click on pbDraw2 and choose View Callbacks and then choose Callback. Define pbDraw2_Callback() function below to reads signals parameters and displays chosen signal in axes2 component:


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

% Reads n01, n11, and n21
n0 = str2num(get(handles.n02,'String'));
n1 = str2num(get(handles.n12,'String'));
n2 = str2num(get(handles.n22,'String'));
eksp = str2num(get(handles.coef2,'String'));

% Reads sinoisoidal parameters
A = str2num(get(handles.A2,'String'));
Frek = str2num(get(handles.Freq2,'String'));
Fase = str2num(get(handles.Phase2,'String'));
Fs = str2num(get(handles.Fs2,'String'));

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

% Choose option from popupmenusignal2
switch get(handles.popupmenusignal2,'Value')  
    case 1
       n = [n1:n2]; x1 = [(n-n0) == 0];
       
       % Displays discrete signal
       axes(handles.axes2)
       stem(n,x1,'linewidth',2,'color','r'); title('Discrete Impulse Signal');

    case 2
       n = [n1:n2]; x1 = [(n-n0) >= 0];
       
       % Displays discrete signal
       axes(handles.axes2)
       stem(n,x1,'linewidth',2,'color','r'); title('Discrete Step Signal');
       
    case 3
       n = [n1:n2]; x1 = [(n-n0) >= 0].*[(eksp).^(n-n0)];
       
       % Displays discrete signal
       axes(handles.axes2)
       stem(n,x1,'linewidth',2,'color','r'); 
       title('Discrete Exponential Signal');
       
    case 4
       n = [n1:n2]; x1 = [(n-n0) >= 0].*[A*sin(2*pi*(Frek)*((n-n0)/Fs)+Fase)];
       
       % Displays discrete signal
       axes(handles.axes2)
       stem(n,x1,'linewidth',2,'color','r'); 
       title('Discrete Sinusoidal Signal');
       
    case 5
       n = [n1:n2]; x1 = [(n-n0) >= 0].*(rand(1,(n2-n1+1))-0.5);
       
       % Displays discrete signal
       axes(handles.axes2)
       stem(n,x1,'linewidth',2,'color','r'); 
       title('Discrete Random Signal');

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

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

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

       % Creates Equilateral Triangle Signal
       x1 = (x + 2*x3+x2)/(0.5*width);
       
       % Displays discrete signal
       axes(handles.axes2)
       stem(nb,x1,'linewidth',2,'color','r'); 
       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)];
       x4 = x;
       x4 = [zeros(1,2*width), x4(1:end-2*width)];
       nb1 = n1+n0; nb2 = n2+n0;
       nb = [nb1:nb2];
       
       % Reads signal range
       set(handles.n12,'string',(nb1));
       set(handles.n22,'string',(nb2));

       % Creates trapezoidal signal
       x1 = (x - x2 - x4 + x3)/width;
       
       % Displays discrete signal
       axes(handles.axes2)
       stem(nb,x1,'linewidth',2,'color','r'); 
       title('Discrete Trapezoidal Signal');
end  

11. Run UI, choose one of signals from popupmenusignal2 component, and click Draw buton. The related discrete signal will be displayed in axes2 component:



12. Right-click on pbAdd and choose View Callbacks and then choose Callback. Define pbAdd_Callback() function below which adds two signals and display the result and its FFT in axes3 and axes4 components:


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

% Reads n11 and n21 for discrete signal 1
n1 = str2num(get(handles.n11,'String'));
n2 = str2num(get(handles.n21,'String'));

% Reads n12 and n22 for discrete signal 2
n12 = str2num(get(handles.n12,'String'));
n22 = str2num(get(handles.n22,'String'));

% Duration     
n = min(min(n1),min(n12)):max(max(n2),max(n22)); 

% Initialization
y1 = zeros(1,length(n)); y2 = y1; 

% Adding two signals
y1(find((n>=min(n1))&(n<=max(n2))==1))=x; 
y2(find((n>=min(n12))&(n<=max(n22))==1))=x1;
y = y1+y2; 

% Clears axes3
cla(handles.axes3,'reset');
axes(handles.axes3)
stem(n,y,'linewidth',1.5,'color','c'); title('The result of adding signal 1 and signal 2');

% Reads sampling frequency
Fs = str2num(get(handles.Fs2,'String'));

nfft = 512; % Length of FFT

% Calculates FFT
Y = fft(y,nfft);
Y = Y(1:nfft/2);

% Calculates magnitude of FFT
my = abs(Y);

% Frequency vector
f = (0:nfft/2-1)*Fs/nfft;

axes(handles.axes4); 
plot(f,my,'linewidth',1.5,'color','c'); grid on;
title('FFT on two added signals'); xlabel('Hz');


13. Run UI form and click on Addition button and you will see the result of adding two signals and its FFT:



14. Right-click on pbMul and choose View Callbacks and then choose Callback. Define pbMul_Callback() function below which multiplies two signals and display the result and its FFT in axes3 and axes4 components:


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

global x;
global x1;

% Reads n11 and n21 for discrete signal 1
n1 = str2num(get(handles.n11,'String'));
n2 = str2num(get(handles.n21,'String'));

% Reads n12 and n22 for discrete signal 2
n12 = str2num(get(handles.n12,'String'));
n22 = str2num(get(handles.n22,'String'));

% Duration     
n = min(min(n1),min(n12)):max(max(n2),max(n22)); 

% Initialization
y1 = zeros(1,length(n)); y2 = y1; 

% Multiplying two signals
y1(find((n>=min(n1))&(n<=max(n2))==1))=x; 
y2(find((n>=min(n12))&(n<=max(n22))==1))=x1;
y = y1.*y2; 

% Clears axes3
cla(handles.axes3,'reset');
axes(handles.axes3)
stem(n,y,'linewidth',1.5,'color','c'); 
title('The result of multiplying of signal 1 and signal 2');

% Reads sampling frequency
Fs = str2num(get(handles.Fs2,'String'));

nfft = 512; % Length of FFT

% Calculates FFT
Y = fft(y,nfft);
Y = Y(1:nfft/2);

% Calculates magnitude of FFT
my = abs(Y);

% Frequency vector
f = (0:nfft/2-1)*Fs/nfft;

axes(handles.axes4); 
plot(f,my,'linewidth',1.5,'color','c'); grid on;
title('FFT on the result of multiplication of two signals'); xlabel('Hz');

15. Run UI form and click on Multiplication button and you will see the result of multiplying two signals and its FFT:



16. Right-click on pbSubs and choose View Callbacks and then choose Callback. Define pbSubs_Callback() function below which substract second signal from the first one and display the result and its FFT in axes3 and axes4 components:


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

global x;
global x1;

% Reads n11 and n21 for discrete signal 1
n1 = str2num(get(handles.n11,'String'));
n2 = str2num(get(handles.n21,'String'));

% Reads n12 and n22 for discrete signal 2
n12 = str2num(get(handles.n12,'String'));
n22 = str2num(get(handles.n22,'String'));

% Duration     
n = min(min(n1),min(n12)):max(max(n2),max(n22)); 

% Initialization
y1 = zeros(1,length(n)); y2 = y1; 

% Adding two signals
y1(find((n>=min(n1))&(n<=max(n2))==1))=x; 
y2(find((n>=min(n12))&(n<=max(n22))==1))=x1;
y = y1-y2; 

% Clears axes3
cla(handles.axes3,'reset');
axes(handles.axes3)
stem(n,y,'linewidth',1.5,'color','c'); 
title('The result of substracting signal 2 from signal 1');

% Reads sampling frequency
Fs = str2num(get(handles.Fs2,'String'));

nfft = 512; % Length of FFT

% Calculates FFT
Y = fft(y,nfft);
Y = Y(1:nfft/2);

% Calculates magnitude of FFT
my = abs(Y);

% Frequency vector
f = (0:nfft/2-1)*Fs/nfft;

axes(handles.axes4); 
plot(f,my,'linewidth',1.5,'color','c'); grid on;
title('FFT on substraction operation'); xlabel('Hz');



17. Right-click on pbConv and choose View Callbacks and then choose Callback. Define pbConv_Callback() function below which convolve the two signals and display the result and its FFT in axes3 and axes4 components:


function pbConv_Callback(hObject, eventdata, handles)
% hObject    handle to pbConv (see GCBO)
% eventdata  reserved - to be defined in a2 future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

global x;
global x1;

% Reads n11
n11 = str2num(get(handles.n11,'String'));

% Reads n12
n12 = str2num(get(handles.n12,'String'));

% Borders
n_low = n11+n12;
n_high = length(x) + length(x1);
ny=n_low:n_high-(abs(n_low)+2);
length(ny)

% Convolution
y = conv(double(x),double(x1)); 

length(y)
axes(handles.axes3)
stem(ny,y,'linewidth',1,'color','r'); 
title('Result of convolving two signals');

% Reads sampling frequency
Fs = str2num(get(handles.Fs1,'String'));

nfft = 512; % Length of FFT

% Calculates FFT
Y = fft(y,nfft);
Y = Y(1:nfft/2);

% Calculates magnitude of FFT
my = abs(Y);

% Frequency vector
f = (0:nfft/2-1)*Fs/nfft;

axes(handles.axes4); 
plot(f,my,'linewidth',1.5,'color','c'); grid on;
title('FFT on convolution operation'); xlabel('Hz');