0% found this document useful (0 votes)
58 views10 pages

Ec-431 - Digitalcommunication: College of E&Me, Nust, Rawalpindi

This document describes a lab task to build a GUI in MATLAB for digital communication. The GUI allows a user to input a character, which is then converted to its ASCII code and binary representation. The binary value is used to modulate a pulse signal, which is plotted. Noise is added and the received signal with noise is plotted. The received signal is demodulated back to the input character. Code is provided to create the GUI components and callbacks to implement these functions.

Uploaded by

Affra Nazir
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
58 views10 pages

Ec-431 - Digitalcommunication: College of E&Me, Nust, Rawalpindi

This document describes a lab task to build a GUI in MATLAB for digital communication. The GUI allows a user to input a character, which is then converted to its ASCII code and binary representation. The binary value is used to modulate a pulse signal, which is plotted. Noise is added and the received signal with noise is plotted. The received signal is demodulated back to the input character. Code is provided to create the GUI components and callbacks to implement these functions.

Uploaded by

Affra Nazir
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

DEPARTMENT OF COMPUTER &SOFTWARE ENGINEERING

COLLEGE OF E&ME, NUST,RAWALPINDI

EC-431|DigitalCommunication

LAB N0# 06

Submitted By:
Saleha Bibi
CE 41 A
325226
LAB TASKS
Task 1:
a. Build a GUI in MATLAB as given above. Containing a string box for character, a string box
for respective ASCII code, a string box for binary value and a graph showing the modulated
pulse signal. You should write a character in the box and once you press the Transmit
button the binary of the GUI, ASCII value appears in the respective box, and its respective
binary value is displayed in the binary text box. Moreover, the pulse waveform graph plots
the pulse modulated signal. You may use a pulse width of 0.1 seconds.
b. After that the signal is received at the pulse demodulator. In the pulse waveform box
given below you should plot the received signal. After that the pulse is demodulated
(converted back to binary) and displayed in the binary text box. Finally, the received
character is displayed in the character box.
c. Next you should add some noise on the channel e.g., by flipping a signal pulse. After that
the signal is received at the pulse demodulator. In the pulse waveform box given below
you should plot the received signal with noise. After that the pulse is demodulated
(converted back to binary) and displayed in the binary text box. Finally, the received
character is displayed in the character box.

Code:
classdef trans < matlab.apps.AppBase

% Properties that correspond to app componentsproperties


(Access = public)
UIFigure matlab.ui.Figure InsertBinaryforInputWaveformOnlyLabel
matlab.ui.control.Labelbtn_transmit matlab.ui.control.Button
BinaryLabel_2 matlab.ui.control.Label ASCIILabel_2
matlab.ui.control.Label
CharacterLabel_2 matlab.ui.control.Label binary_out
matlab.ui.control.EditField ascii_out
matlab.ui.control.EditField
char_out matlab.ui.control.EditField BinaryLabel
matlab.ui.control.Label ASCIILabel
matlab.ui.control.Label CharacterLabel
matlab.ui.control.Label binary_in
matlab.ui.control.EditField
ascii_in matlab.ui.control.EditField
char_in matlab.ui.control.EditField
axes_out matlab.ui.control.UIAxes
axes_in matlab.ui.control.UIAxes
end

methods (Access = private)


function [ascii_value, character] = binary_ascii(~, binary_str)
character_str = convertStringsToChars(binary_str); binary_str8 =
reshape(character_str, [], 8);
ascii_value = bin2dec(binary_str8); character =
char(ascii_value);
end
end

% Callbacks that handle component eventsmethods (Access =


private)

% Button pushed function: btn_transmit function


btn_transmitButtonPushed(app, event)

binary_input = app.binary_in.Value;
[ascii_input, letter_input] = binary_ascii(app, string(binary_input));
app.ascii_in.Value = string(ascii_input);
app.char_in.Value = letter_input;

binary = char(binary_input); binary_array =


zeros(1, length(binary)); for k = 1:length(binary)
binary_array(k) = str2double(binary(k));
end

stairs(app.axes_in, [binary_array, binary_array(end)], 'LineWidth', 2);


xlim(app.axes_in, [1,9]);
app.axes_in.XTick = 1:1:9; noise_signal =

reverse(binary_input);

[ascii_op, letter_op] = binary_ascii(app, string(noise_signal)); app.binary_out.Value


= string(noise_signal);
app.ascii_out.Value = string(ascii_op);
app.char_out.Value = letter_op;

binary = char(noise_signal); binary_array =


zeros(1, length(binary)); for k = 1:length(binary)
binary_array(k) = str2double(binary(k));
end

stairs(app.axes_out, [binary_array, binary_array(end)], 'LineWidth', 2);


xlim(app.axes_out, [1,9]);
app.axes_out.XTick = 1:1:9;

end
end

% Component initialization methods


(Access = private)
% Create UIFigure and componentsfunction
createComponents(app)

% Create UIFigure and hide until all components are created


app.UIFigure = uifigure('Visible', 'off'); app.UIFigure.Color =
[0.6392 0.7882 1];
app.UIFigure.Position = [100 100 752 469];app.UIFigure.Name
= 'MATLAB App';

% Create axes_in
app.axes_in = uiaxes(app.UIFigure); title(app.axes_in,
'Input Waveform') app.axes_in.FontName = 'DejaVu Serif
Condensed';app.axes_in.FontWeight = 'bold';
app.axes_in.XTick = [1 2 3 4 5 6 7 8.9];
app.axes_in.XTickLabel = {'1'; '2'; '3'; '4'; '5'; '6'; '7'; '8'; '9'};
app.axes_in.YTick = [0 0.5 1];
app.axes_in.FontSize = 14;
app.axes_in.Position = [211 221 309 206];

% Create axes_out
app.axes_out = uiaxes(app.UIFigure); title(app.axes_out,
'Transmitted Waveform') app.axes_out.FontName = 'DejaVu
Serif Condensed';app.axes_out.FontWeight = 'bold';
app.axes_out.XTick = [1 2 3 4 5 6 7 8 9];
app.axes_out.XTickLabel = {'1'; '2'; '3'; '4'; '5'; '6'; '7'; '8'; '9'};
app.axes_out.YTick = [0 0.5 1];
app.axes_out.FontSize = 14;
app.axes_out.Position = [211 16 309 206];

% Create char_in
app.char_in = uieditfield(app.UIFigure, 'text');
app.char_in.BackgroundColor = [0.7608 1 0.5686];
app.char_in.Position = [28 304 94 30];

% Create ascii_in
app.ascii_in = uieditfield(app.UIFigure, 'text');
app.ascii_in.BackgroundColor = [0.7608 1 0.5686];
app.ascii_in.Position = [28 241 94 30];

% Create binary_in
app.binary_in = uieditfield(app.UIFigure, 'text');
app.binary_in.BackgroundColor = [0.7569 1 0.5686];
app.binary_in.Position = [29 369 94 30];

% Create CharacterLabel
app.CharacterLabel = uilabel(app.UIFigure);
app.CharacterLabel.HorizontalAlignment = 'center';
app.CharacterLabel.FontName = 'DejaVu Serif Condensed';
app.CharacterLabel.FontSize = 14; app.CharacterLabel.FontWeight =
'bold'; app.CharacterLabel.Position = [38 335 77 22];
app.CharacterLabel.Text = 'Character';
% Create ASCIILabel
app.ASCIILabel = uilabel(app.UIFigure);
app.ASCIILabel.HorizontalAlignment = 'center';
app.ASCIILabel.FontName = 'DejaVu Serif Condensed';
app.ASCIILabel.FontSize = 14; app.ASCIILabel.FontWeight =
'bold'; app.ASCIILabel.Position = [52 272 46 22];
app.ASCIILabel.Text = 'ASCII';

% Create BinaryLabel
app.BinaryLabel = uilabel(app.UIFigure);
app.BinaryLabel.HorizontalAlignment = 'center';
app.BinaryLabel.FontName = 'DejaVu Serif Condensed';
app.BinaryLabel.FontSize = 14; app.BinaryLabel.FontWeight =
'bold'; app.BinaryLabel.Position = [51 400 52 22];
app.BinaryLabel.Text = 'Binary';

% Create char_out
app.char_out = uieditfield(app.UIFigure, 'text'); app.char_out.BackgroundColor =
[0.9882 0.9608 0.5686];
app.char_out.Position = [600 98 94 30];

% Create ascii_out
app.ascii_out = uieditfield(app.UIFigure, 'text'); app.ascii_out.BackgroundColor =
[0.9882 0.9608 0.5686];
app.ascii_out.Position = [600 36 94 32];

% Create binary_out
app.binary_out = uieditfield(app.UIFigure, 'text'); app.binary_out.BackgroundColor =
[0.9882 0.9569 0.5725];
app.binary_out.Position = [600 159 94 30];

% Create CharacterLabel_2 app.CharacterLabel_2 =


uilabel(app.UIFigure);
app.CharacterLabel_2.HorizontalAlignment = 'center';
app.CharacterLabel_2.FontName = 'DejaVu Serif Condensed';
app.CharacterLabel_2.FontSize = 14; app.CharacterLabel_2.FontWeight
= 'bold'; app.CharacterLabel_2.Position = [609 129 77 22];
app.CharacterLabel_2.Text = 'Character';

% Create ASCIILabel_2
app.ASCIILabel_2 = uilabel(app.UIFigure);
app.ASCIILabel_2.HorizontalAlignment = 'center';
app.ASCIILabel_2.FontName = 'DejaVu Serif Condensed';
app.ASCIILabel_2.FontSize = 14; app.ASCIILabel_2.FontWeight =
'bold'; app.ASCIILabel_2.Position = [624 67 46 32];
app.ASCIILabel_2.Text = 'ASCII';

% Create BinaryLabel_2
app.BinaryLabel_2 = uilabel(app.UIFigure); app.BinaryLabel_2.HorizontalAlignment =
'center'; app.BinaryLabel_2.FontName = 'DejaVu Serif Condensed';
app.BinaryLabel_2.FontSize = 14; app.BinaryLabel_2.FontWeight =
'bold'; app.BinaryLabel_2.Position = [622 190 52 22];
app.BinaryLabel_2.Text = 'Binary';

% Create btn_transmit
app.btn_transmit = uibutton(app.UIFigure, 'push'); app.btn_transmit.ButtonPushedFcn
= createCallbackFcn(app,
@btn_transmitButtonPushed, true);
app.btn_transmit.BackgroundColor = [0.9686 0.6941 0.9804];
app.btn_transmit.FontName = 'DejaVu Serif Condensed';
app.btn_transmit.FontSize = 14; app.btn_transmit.FontWeight =
'bold'; app.btn_transmit.Position = [600 335 94 29];
app.btn_transmit.Text = 'Enter';

% Create InsertBinaryforInputWaveformOnlyLabel
app.InsertBinaryforInputWaveformOnlyLabel = uilabel(app.UIFigure);
app.InsertBinaryforInputWaveformOnlyLabel.FontName = 'DejaVu Serif
Condensed';
app.InsertBinaryforInputWaveformOnlyLabel.FontSize = 18;
app.InsertBinaryforInputWaveformOnlyLabel.Position = [211 439 369 23];
app.InsertBinaryforInputWaveformOnlyLabel.Text = 'Insert Binary for Input
Waveform Only ';

% Show the figure after all components are createdapp.UIFigure.Visible =


'on';
end
end

% App creation and deletionmethods


(Access = public)

% Construct app function app =


trans

% Create UIFigure and components


createComponents(app)

% Register the app with App DesignerregisterApp(app,


app.UIFigure)

if nargout == 0clear app


end
end

% Code that executes before app deletionfunction


delete(app)

% Delete UIFigure when app is deleted delete(app.UIFigure)

end
end
end
Output:

Task-02:

In the previous task, you have transmitted and received a single character using MATLAB
with additive Gaussian noise. Now, you would transmit numbers from 0000 to 9999 as
characters in the format “zzzz” where z represents a single character for a number. At the end
you will calculate the bit error rate for the received bit.

a. First perform the following steps without considering noise. Transmit the
binary stream for the number 0000. Here you would represent each 0 with its respective
binary representation. Receive the transmitted bits and recover the 0000 being
transmitted. Is here 0000 being correctly received if not find the number of wrong bits?
Save the number of erroneous bits in error count. Now write a loop to from 0000 to
9999 and repeat the procedure. Update the error count in each iteration. Moreover, the
pulse waveform graph plots the pulse modulated signal. You may use a pulse width of 0.1
seconds.

b. Now perform the above steps for noise with variances 0, 0.1, 0.2, 0.3, 0.4, …, 3.Calculate
the Bit error rate (BER) for each variance value by averaging the error _count over all the
numbers from 0000 to 9999. Finally plot the BER with variance on the x-axis and the BER
Code:

% hObject handle to pushbutton1 (see GCBO)


% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% hObject handle to pushbutton1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)Error = []; %For Bit
Errors
Variance = []; for var =
0:0.1:3
Num = [];
for n = 0:9999 error_count =
0;num = n;
string = n;
num = num2str(num); num_l =
length(num);if num_l < 4
ending = 4-num_l;for c =
1:ending Num = [Num 0]; end
if num_l == 1
Num =[Num string];else
for c = 1:num_lxx = num(c);
xx = str2double(xx);Num = [Num
xx];
end end else
for l = 1:num_lxx = num(l);
xx = str2double(xx);Num = [Num
xx];
endend

Bin = [];
%Converting to 16 bitsfor i=1:4
x = Num(i);
Binary = dec2bin(x);
z = str2double(Binary); len_of_Binary =
length(Binary);Out = [];
if len_of_Binary<4
ending = 4-len_of_Binary;for c =
1:ending
Out = [Out 0];end
if len_of_Binary==1Out = [Out
z];
else
for l = 1:len_of_Binaryo =
Binary(l);
o1 = str2double(o);
Out = [Out o1];
end

end
for l = 1:len_of_Binaryo =
Binary(l);
o1 = str2double(o);
Out = [Out o1];
end
end
Bin = [Bin Out];

end
Num = [];
Bin_new = Bin;
Bin_new = Bin_new + randn(size(Bin_new))*sqrt(var);Bin_new =
round(Bin_new);
for i=1:16
if Bin_new(i)~=Bin(i) error_count =
error_count + 1;end

end
end
Error = [Error error_count];Variance =
[Variance var];
end
disp('Error:')disp(Error)
disp('Varianc)disp(Variance)
BER = Error/10000;
axes(handles.axes1);
plot(Variance,BER); xlabel('Variance
--->');
ylabel('BER --->'); :
Output

You might also like