Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

MATLAB Question.. add noise after (or into) QPSK codes and please test the .m fi

ID: 662686 • Letter: M

Question

MATLAB Question.. add noise after (or into) QPSK codes and please test the .m file it gives error or not before send an answer.

clear all

%% transmitter

fid = fopen('sample.txt','r');

data = fread(fid);

data=reshape(data,1,87);

original_text= char(data);

chars = unique(data);%number of unique characters:

number_of_chars = length(chars);

%% Source Coding ---->Huffman Encoding:

% Probability of characters are calculated with using formula:

% probability= usage of char / number of chars in text

probabilities = zeros(1,number_of_chars);

for k = 1:number_of_chars

probabilities(k) = numel(find(data==chars(k)))/length(data);

end

chars_list=double(chars);

[dict,avglen] = huffmandict(chars_list,probabilities);% huffman dict is ready.

coded_data= huffmanenco(data,dict);% data is coded with huffman coding.

disp(length(coded_data))

%% Channel Coding ----> Hamming Encoding:

[h,g,n,k] = hammgen(4);

add_zeros = horzcat(coded_data, [0 0]);

row = length(add_zeros)/11;

res_code=reshape(add_zeros, [row, 11]);

a = res_code*g;

coding = mod(a,2);

disp(coding)

size(coding)

x = reshape(coding,1,36*15)%with interleave

%% frame

y = [0;1;1;0;1;0;1;0;1;0;0;1]'

if y in x

disp('k')

else

disp('l')

end

z = [y,x];

%synrome matrix

syndrome_matrix = [];

for i = 1:15;

error_matrix = zeros(15,1);

error_matrix(i)=1;

errors = h*error_matrix;

syndrome_matrix = horzcat(syndrome_matrix, errors);

end

%% QPSK Modulation

duration = 0.01;

tb = 0:duration:2;

n = 10;

bits = z;

A = 2^0.5;

Tc=1/2;

fc=1/Tc;

wc = 2*pi*fc;

sym1=A*sin(wc*2*tb+5*pi/4); %00

sym2=A*sin(wc*2*tb+3*pi/4); %01

sym3=A*sin(wc*2*tb+7*pi/4); %10

sym4=A*sin(wc*2*tb+1*pi/4); %11

mod = [];

for i=1:2:n-1;

if (bits(i)==0 && bits(i+1) == 0) mod=[mod sym1];

elseif (bits(i)==0 && bits(i+1) == 1) mod=[mod sym2];

elseif (bits(i)==1 && bits(i+1) == 0) mod=[mod sym3];

elseif (bits(i)==1 && bits(i+1) == 1) mod=[mod sym4];

end

end

figure(1)

plot(mod)

title('Modulated')

%% add noise

%% receiver

%% Channel DeCoding ----> Hamming Decoding:

%% Source DeCoding ----> Huffman Decoding:

decoded_data = huffmandeco(coded_data,dict);

%Received data is reshaped in order to get original data.

received_text= char(decoded_data);

disp('Decoded Text:')

disp(text)

fclose(fid);

Explanation / Answer

I have added white noise in the code and compiled the .m file and it successfully ran with some exceptions. The code is fine. Please find the updated code below:

%% transmitter

fid = fopen('sample.txt','r');

data = fread(fid);

data=reshape(data,1,87);

original_text= char(data);

chars = unique(data);%number of unique characters:

number_of_chars = length(chars);

%% Source Coding ---->Huffman Encoding:

% Probability of characters are calculated with using formula:

% probability= usage of char / number of chars in text

probabilities = zeros(1,number_of_chars);

for k = 1:number_of_chars

probabilities(k) = numel(find(data==chars(k)))/length(data);

end

chars_list=double(chars);

[dict,avglen] = huffmandict(chars_list,probabilities);% huffman dict is ready.

coded_data= huffmanenco(data,dict);% data is coded with huffman coding.

disp(length(coded_data))

%% Channel Coding ----> Hamming Encoding:

[h,g,n,k] = hammgen(4);

add_zeros = horzcat(coded_data, [0 0]);

row = length(add_zeros)/11;

res_code=reshape(add_zeros, [row, 11]);

a = res_code*g;

coding = mod(a,2);

disp(coding)

size(coding)

x = reshape(coding,1,36*15)%with interleave

%% frame

y = [0;1;1;0;1;0;1;0;1;0;0;1]'

if y in x

disp('k')

else

disp('l')

end

z = [y,x];

%synrome matrix

syndrome_matrix = [];

for i = 1:15;

error_matrix = zeros(15,1);

error_matrix(i)=1;

errors = h*error_matrix;

syndrome_matrix = horzcat(syndrome_matrix, errors);

end

%% QPSK Modulation

duration = 0.01;

tb = 0:duration:2;

n = 10;

bits = z;

A = 2^0.5;

Tc=1/2;

fc=1/Tc;

wc = 2*pi*fc;

sym1=A*sin(wc*2*tb+5*pi/4); %00

sym2=A*sin(wc*2*tb+3*pi/4); %01

sym3=A*sin(wc*2*tb+7*pi/4); %10

sym4=A*sin(wc*2*tb+1*pi/4); %11

mod = [];

for i=1:2:n-1;

if (bits(i)==0 && bits(i+1) == 0) mod=[mod sym1];

elseif (bits(i)==0 && bits(i+1) == 1) mod=[mod sym2];

elseif (bits(i)==1 && bits(i+1) == 0) mod=[mod sym3];

elseif (bits(i)==1 && bits(i+1) == 1) mod=[mod sym4];

end

end

figure(1)

plot(mod)

title('Modulated')

noise_sym1= sym1 + noiseAmplitude * rand(1,length(sym1));
noise_sym2= sym2 + noiseAmplitude * rand(1,length(sym2));
noise_sym3= sym3 + noiseAmplitude * rand(1,length(sym3));
noise_sym4= sym4 + noiseAmplitude * rand(1,length(sym4));
noise_total= noise_sym1+ noise_sym2+ noise_sym3+ noise_sym4;

%% receiver

%% Channel DeCoding ----> Hamming Decoding:

%% Source DeCoding ----> Huffman Decoding:

decoded_data = huffmandeco(coded_data,dict);

%Received data is reshaped in order to get original data.

received_text= char(decoded_data);

disp('Decoded Text:')

disp(text)

fclose(fid);

I have added noise in each of the sine function seperately and then added by using a random function:

noise_sym1= sym1 + noiseAmplitude * rand(1,length(sym1));
noise_sym2= sym2 + noiseAmplitude * rand(1,length(sym2));
noise_sym3= sym3 + noiseAmplitude * rand(1,length(sym3));
noise_sym4= sym4 + noiseAmplitude * rand(1,length(sym4));
noise_total= noise_sym1+ noise_sym2+ noise_sym3+ noise_sym4;

Please run this code. It should successfully compile.