Skip to content

Commit

Permalink
uploaded code and data files
Browse files Browse the repository at this point in the history
  • Loading branch information
MahmudulAlam committed Feb 14, 2019
1 parent 49bfd46 commit 98b79d9
Show file tree
Hide file tree
Showing 12 changed files with 601 additions and 0 deletions.
81 changes: 81 additions & 0 deletions Data/received.txt

Large diffs are not rendered by default.

81 changes: 81 additions & 0 deletions Data/source_data.txt

Large diffs are not rendered by default.

9 changes: 9 additions & 0 deletions awgn_channel.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
function received = awgn_channel(modulated, snr)
% INPUT:
% modulated: transmitted modulated signal
% snr: value of the signal to noise ratio
% OUTPUT:
% received: noise added modulated signal that appears at the receiver

received = awgn(modulated, snr, 'measured');
end
12 changes: 12 additions & 0 deletions convolutional_coding.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
function y = convolutional_coding(bit_stream, G)
% INPUT:
% bit_stream = uncoded bit stream vector
% G = generator matrix
% OUTPUT:
% y = channel coded bit stream

y = conv2(bit_stream, G);
y = rem(y, 2);
[row, col] = size(y);
y = reshape (y, 1, row * col);
end
65 changes: 65 additions & 0 deletions demodulation.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
function bit_stream = demodulation(modulation_name, received, Rb, k, amp, freq)
% INPUT:
% modulation_name: can be 'BASK', 'BPSK', 'QPSK'
% received: modulated signal received from channel
% Rb = bit rate
% k = samples per bit
% amp = amplitude of the modulated signal
% scaler for BPSK and QPSK, vector ([max min]) for BASK
% freq = carrier frequency of the modulated signal
% OUTPUT:
% bit_stream = demodulated bit stream

n = length(received) / k; % n = number of bits
Tb = 1/Rb;
Fs = k * Rb;
Ts = 1/Fs;
t = 0 : Ts : n*Tb-Ts;
bit_stream = [];

switch modulation_name
case 'BASK'
a0 = amp(1);
a1 = amp(2);
r = received .* sin(2*pi*freq(1)*t);
r = reshape(r, k, n);
yd = mean(r);
threshold = (a0 + a1) / 4;
bit_stream = (yd >= threshold);

case 'BPSK'
r = received .* sin(2*pi*freq(1)*t);
r = reshape(r, k, n);
yd = mean(r);
threshold = 0;
bit_stream = (yd >= threshold);

case 'QPSK'
M = 4;
bn = log2(M);
t = 0 : Ts : bn*Tb-Ts;
A = amp(1);
f = freq(1);
space = length(t);
for i = 1:space:length(received)
sig = received(i:(i-1)+space);
r1 = sig .* sin(2*pi*f*t);
r2 = sig .* cos(2*pi*f*t);
ai = mean(r1);
aq = mean(r2);
phi = (0:M-1) * 2 * pi ./ M;
distance = (A*cos(phi) - 2*ai).^2 + (A*sin(phi) - 2*aq).^2;
[~, min_ind] = min(distance);
min_ind = min_ind - 1;
sym = de2bi(min_ind, bn);
bit_stream = [bit_stream sym];
end

otherwise
disp('WARNINGS [ :( ] . . . ');
disp(['"', modulation_name, '" : Modulation is not supported! ONLY BASK, BPSK, QPSK are supported.']);
end

% converting logical array to double vector array
bit_stream = double(bit_stream);
end
24 changes: 24 additions & 0 deletions huffman_decoding.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
function decoded_msg = huffman_decoding(unique_symbol, code_word, bit_stream)
% INPUT:
% unique_symbol: string of the unique symbols
% code_word: cell array of code to represent each probability of the symbols
% bit_stream: message bit stream that has to be decoded
% OUTPUT:
% decoded_msg = decoded message

decoded_msg = [];

% minimum code word length
i_min = min(cellfun('length', code_word));

% bit stream pointer
ptr = 1;
for i = i_min:length(bit_stream)
if isempty(find(strcmp(code_word, char(bit_stream(ptr:i) + '0')), 1)) ~= 1
ind = find(strcmp(code_word, char(bit_stream(ptr:i) + '0')), 1);
decoded_msg = [decoded_msg char(unique_symbol(ind))];
ptr = i + 1;
i = i + i_min;
end
end
end
38 changes: 38 additions & 0 deletions huffman_encoding.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
function code_word = huffman_encoding(prob)
% INPUT:
% prob = a vector of probability of the unique symbols
% OUTPUT:
% code_word = cell array of huffman code word for each unique symbol

n = length(prob);
code_word = cell(1, n);

% from descending order to ascending order
prob = fliplr(prob);

% initializing code word for n = 1 case
if n == 1
code_word{1} = '1';
end
x = zeros(n, n);
x(:, 1) = (1:n)';

for i = 1:n-1
temp = prob;
[~, min1] = min(temp);
temp(min1) = 1;
[~, min2] = min(temp);
prob(min1) = prob(min1) + prob(min2);
prob(min2) = 1;
x(:, i+1) = x(:, i);
for j = 1:n
if x(j, i+1) == min1
code_word(j) = strcat('0', code_word(j));
elseif x(j, i+1) == min2
x(j, i+1) = min1;
code_word(j) = strcat('1', code_word(j));
end
end
end
code_word = fliplr(code_word);
end
131 changes: 131 additions & 0 deletions main_code.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
clc, clear, close all;
start = tic;


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%% "System Properties" %%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
modulation_name = 'BASK';
samples_per_bit = 40;
Rb = 1000;
amp = [1 0];
freq = 1000;
snr = 10;
Generator = [1 1 1; 1 0 1];
shift = 1;


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%% "Reading Text Data File" %%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
tic
fprintf('Reading data: ');
file = fopen('Data/source_data.txt');
text = fread(file, '*char')';
fclose(file);
toc


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%% "Source Statistics" %%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
fprintf('Source statistics: ');
tic
[unique_symbol, probability] = source_statistics(text);
toc


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%% "Huffman Encoding" %%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
fprintf('Huffman encoding: ');
tic
code_word = huffman_encoding(probability);
toc


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%% "Stream Generator" %%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
tic
fprintf('Stream generator: ');
bit_stream = stream_generator(unique_symbol, code_word, text);
input = bit_stream;
toc


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%% "Channel Coding" %%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
tic
fprintf('Channel coding: ');

channel_coded = convolutional_coding(bit_stream, Generator);
toc


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%% "Modulation" %%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
tic
fprintf('Modulation: ');
modulated = modulation(modulation_name, channel_coded, Rb, samples_per_bit, amp, freq);
toc


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%% "Channel" %%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
tic
fprintf('Channel: ');
received = awgn_channel(modulated, snr);
toc


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%% "Demodulation" %%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
tic
fprintf('Demodulation: ');
bit_stream = demodulation(modulation_name, received, Rb, samples_per_bit, amp, freq);
toc


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%% "Channel Decoding" %%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
tic
fprintf('Channel decoding: ');
bit_stream = viterbi_decoder(bit_stream, Generator, shift);
output = bit_stream;
toc


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%% "Huffman Decoding" %%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
tic
fprintf('Huffman decoding: ');
decoded_msg = huffman_decoding(unique_symbol, code_word, bit_stream);
toc


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%% "Writting the Received Data" %%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
tic
fprintf('Writing data: ');
f = fopen('Data/received.txt','w+');
fprintf(f, decoded_msg);
fclose(f);
toc


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%% "Time & Error Calculation" %%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
fprintf('Total execution time: ');
toc(start);

Error = sum(abs(input - output));
disp(['Total Bit Error: ' num2str(Error)]);
63 changes: 63 additions & 0 deletions modulation.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
function modulated = modulation(modulation_name, bit_stream, Rb, k, amp, freq)
% INPUT:
% modulation_name = can be 'BASK', 'BPSK', 'QPSK'
% bit_stream = string of bits
% Rb = bit rate
% k = samples per bit
% amp = amplitude of the modulated signal
% scaler for BPSK and QPSK, vector ([max min]) for BASK
% freq = carrier frequency of the modulated signal
% OUTPUT:
% modulated = modulated signal of the bit stream

modulated = [];

% converting string to vector
N = length(bit_stream);

% line coding
line_code = repelem(bit_stream, k);

Tb = 1/Rb; % bit duration
Fs = k * Rb; % sampling frequency
Ts = 1 / Fs;
time = 0 : Ts : N*Tb-Ts;

switch modulation_name
case 'BASK'
carrier = sin(2*pi*freq*time);
a1 = amp(1);
a0 = amp(2);
line_code = a1 * line_code + a0 .* (line_code==0);
modulated = line_code .* carrier;

case 'BPSK'
a = amp(1);
carrier = sin(2*pi*freq*time);
line_code = a .* (line_code == 1) + (-a) .* (line_code == 0);
modulated = line_code .* carrier;

case 'QPSK'
M = 4;
bn = log2(M);
t = 0 : Ts : bn*Tb-Ts;

% Gray code generation
x = 0 : M-1;
code = bin2gray(x, 'psk', M);
gray_code = de2bi(code, bn, 'left-msb');

for i = 1:bn:length(bit_stream)
sym = bit_stream(i:i+bn-1);
index = find(ismember(gray_code, sym, 'rows'), 1);
val = bi2de(gray_code(index, :));
phi = 2 * pi * val / M;
modulated = [modulated amp(1)*sin(2*pi*freq*t + phi)];
end

otherwise
fprintf('\n');
warning(['"', modulation_name, '" Modulation is not supported! ONLY BASK, BPSK, QPSK are supported.']);
return;
end
end
17 changes: 17 additions & 0 deletions source_statistics.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
function [unique_symbol, probability]=source_statistics(text)
% INPUT:
% text = input text data string
% OUTPUT:
% unique_symbol = string of unique symbols
% probability = probability of each unique symbols

unique_symbol = unique(text);
count_symbol = histc(text, unique_symbol);

% calculating each symbol probability
probability = count_symbol / length(text);

% sorting probability in descending order
[probability, index] = sort(probability, 'descend');
unique_symbol = unique_symbol(index);
end
17 changes: 17 additions & 0 deletions stream_generator.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
function bit_stream = stream_generator(unique_symbol, code_word, msg)
% INPUT:
% unique_symbol = a string of unique symbols
% code_word = cell array of code to represent each probability of the symbols
% OUTPUT:
% msg = message that has to be converted into bit stream

bit_stream = '';
increment = length(char(unique_symbol(1)));
for i = 1:increment:length(msg)
index = strfind(unique_symbol, msg(i:i+increment-1));
bit_stream = [bit_stream char(code_word(index))];
end

% converting string bit stream to double vector array
bit_stream = double(bit_stream - double('0'));
end
Loading

0 comments on commit 98b79d9

Please sign in to comment.