Showing posts with label CSCI466 Coding for Secure Communication. Show all posts
Showing posts with label CSCI466 Coding for Secure Communication. Show all posts

CSCI466 Assignment 3

This assignment is more towards mathematical than computer security. Let's study! Question 1: For a [5,3] code over GF(4), the generator matrix is given by



Q1(i). Find the parity check matrix.
A: Based on the given generator G, we can easily find G = I | P.
Therefore,


From P, we can find P^T.


Since P^T - P^T = 0, P^T = -P^T.

The parity check matrix, H = -PT | I




Q1(ii) How many errors can this code detect?
A: According to Singleton bound, d* <= (n-k + 1).

Apply n=5, k=3, then the minimum distance, d* is
d* <= 5-3+1
d* <= 3

Therefore, the minimum distance is equal or less than 3.

According to algorithm for nearest neighbor decoding, d* <= t+ 1.

Apply d*=3, then the number of detection, t is
3 <= t+1
2 <= t

The number of detection is equal or less than 2.

Q1(iii) How many errors can this code correct?
A: d* <= 2t + 1

Apply d*=3, then the number is correction, t is
3 <= 2t + 1
1 <= t

The number of correction is equal or less than 1.

Q1(iv) How many erasures can this code correct?
A: d* = e + 1

Apply d*=3, then the number of erasures, e is
3 = e + 1
2 = e

Q1(v) Is this a perfect code?
A: In order to be a perfect code, it must be able produce the same result for Sphere Packing Bound or Hamming Bound.

Sphere Packing Bound or Hamming Bound


Apply n=5, q=4 for GF(4), d*=3, k=3.
M = q^k = 4^3 = 64

r = (d* - 1) / 2 = (3-2) /2 = 1

Left Hand Side:


Right Hand Side:


Since Left Hand Side = Right Hand Side, it is a perfect code.


2. Consider the channel model shown in figure 1, which accepts three symbols. The first symbol, A is not affected by noise, while the second (B) and third (C) symbols have a probability p of not being corrupted and a probability q of being changed into the other of the pair. Let a = -p log p - q log q, and let P be the probability that the first symbol is chosen. Furthermore, let Q be the probability that either of the other two is chosen, so that P+2Q = 1.

(a) Show that H(X) = -P log P - 2Q log Q.
(b) Show that H(X|Y) = 2Qa.
(c) State how you would determine the capacity of this channel.


X is the set of input symbol while Y is the set of output symbols.

A:


(a) Probability of Input X=A, P(X=A) =P
Entropy, H(X=A) = -P log P

Probability of Input X=B, P(X=B) = Q
Entropy, H(X=B) = -Q log Q

Probability of Input X=C, P(X=C) = Q
Entropy, H(X=C) = - Q log Q

Input Entropy, H(X) = H(X=A) + H(X=B) + H(X=C) = -P log P -2Q log Q

(b) Probability of Input X=A, P(X=A) = P
Probability of Input X=B, P(X=B) = Q
Probability of Input X=C, P(X=C) = Q

Probability of Output Y=A, P(Y=A) = P
Probability of Output Y=B, P(Y=B) = Q(p+q)
Probability of Output Y=C, P(Y=C) = Q(p+q)

Probability of Input X=A & Output Y=A,
P(X=A|Y=A) = P(X=A,Y=A) / P(Y=A) = P/P =1

Probability of Input X=B & Output Y=B,
P(X=B|Y=B) = P(X=B,Y=B) / P(Y=B) = Qp/Q(p+q) =p/(p+q)

Probability of Input X=B & Output Y=C,
P(X=B|Y=C) = P(X=B,Y=C) / P(Y=C) = Qq/Q(p+q) =q/(p+q)

Probability of Input X=C & Output Y=B,
P(X=C|Y=B) = P(X=C,Y=B) / P(Y=B) = Qq/Q(p+q) =q/(p+q)

Probability of Input X=C & Output Y=C,
P(X=C|Y=C) = P(X=C,Y=C) / P(Y=C) = Qp/Q(p+q) =p/(p+q)

H(X|Y=A) = -(P(X=A|Y=A) log P(X=A|Y=A)) = - (1 log 1) = 0





H(X|Y) = P(Y=A)H(X|Y=A) + P(Y=B)H(X|Y=B) + P(Y=C)H(X|Y=C)
= P*0 + Q(p+q) * a + Q(p+q) * a
= 2Qa

(c) The mutual information,
I(X;Y) = H(X)- H(X|Y) = -P log P - 2Q (log Q+p log p + q log q)


3. Consider the (23,12,7) binary code. What will be the approximate word error rate if it is used over a binary symmetric channel (BSC) with probability of bit error p=0.001?

A: From given q-ary code (n,k,d*), n=23, k=12, d*=7.

d* >= 2t + 1
7 >= 2t + 1
3 >= t

This q-ary code can correct up to 3 errors. In another word, i = 3 for the formulae below.

CSCI466 Assignment 2

This is a huge programming assignment to explore other forms of lossless compression. In particular, we investigate the Burrows-Wheeler Transform (BWT, in short) compression scheme and compare its efficiency with Huffman Coding.

The Huffman encoded text is to be transmitted over a channel that can be modeled as binary symmetric channel with some given transition probabilities. With this model, we will insert the errors in the encoded text file and attempt to decode the text after reception at the receiver.

We will then design a suitable channel code that upon receiving and decoding, the amount of errors should be reduced. This is for the program introduction.

A few tasks involved and I will give some answers in here.

1. Source Model

1.1 What is the file size in bytes for "Dec of Ind.txt"?
Ans: the file is given by your lecturer. To find the file size, in command line, type "ls -al" command. This will list down the file properties. Alternatively, you can right click on the file and select properties.



1.2 Write the program to compute the frequency of the ASCII characters used in the text.
Ans: Simple. Just write a program. If you want the program, ask me.

1.3 Report the findings as probability distribution table.
Ans:Try to write your program to output like this.


1.4 Compute the entropy of the source generating these characters.
Ans: In Information Entropy, Claude Elwood Shanon said the entropy of the source can be calculated with

Based on formula, the answer for this is 4.30196.

1.5 Comment on the probability distribution of the character used in text file. Is the expectation on the most frequently used letter of the English alphabet confirmed?
Ans: Yes. This is science proof. The most frequent letter in English alphabet is the letter "E". Of course, you will get the same answer.

1.6 On average, how many bits/symbol would you expect to use in encoding this text file?
Ans: It is the same as entropy value. In this case, our entropy is 4.30196 which is the average usage of codeword for this text file.

2. Entropy Coding

2.1 Encoding

Huffman coding is an entropy encoding algorithm used for lossless data compression technique. I will discuss the Huffman Tree Construction. It is based on probability distribution. Huffman tree produces the unique and linear codeword for every source entity.

You got to write your program to do Huffman Tree Construction. The algorithm should look like this.

BEGIN
Arrange the Source Symbols in decreasing order of probabilities
WHILE
Pick and Add the two smallest values and write it to both nodes.
Enable flag so that it will not repeat for next round.
Label the branches with a "1" and "0"
Ensure that the branches are checked and updated accordingly
END
END

After the algorithm, your program will produce something like this.


Output all these codewords into a text file and check the file size. It should be smaller than the original text file.

2.2 Decoding

This process is the reverse process of encoding. In encoding, we convert the letter into codeword and in decoding, we will revert the codeword into the letter. It should not be deviate or change after the decoding process. If it changes, then your encoding tree is not accurate.

2.3 Comparison of origin vs entropy coded file.

To compare, type command "cmp -l ". The cmp utility compares two files of any type and writes the results to the standard output. By default, cmp is silent if the files are the same; if they differ, the byte and line number at which the first difference occured is reported.

3. Burrows- Wheeler Transform

BWT is not actually a compression. In fact, BWT requires that a small amount of additional information to be stored with the transformed data so that the reverse process can take place. BWT is useful because it converts the data into a format that is generally more compressible by statistical encoders (eg. Huffman Coding). With the help of "Move to Front" coding, the data will be in a format wich is generally more compressible by even zero order statistical encoders.

Your lecturer could give you some documents about BWT, but trust me, it is complicated. But I will explain it here, so that it is easier to understand.

Let's say the original string is "nokia". First, we will do the sorting as the table below.

Next, we will arrange this table in lexicographical order.

Okay, notice that F is now in alphabetical order. We will take the column "L". So "L" column is "ikoan". We also can find out the primary index. Primary index is simply to search for S1 and its row value will be the primary index. So the primary index = 4.

Given L and primary index, the one can decode with this program code.
void decode()
{
int index = primary_index;
for (int i=0; i<7; i++) index=T[index];
}


Move to Front Coding

This is one of the recommendation from BWT coding. Move to front will help us to compress fast and better. Let's see how it works.

We know that our L column is "ikoan". And there, the receiver can calculate the entropy list.

Source1= { a i k o n} and we have L of "ikoan". Let's encode.

First, we see "i" in L column, then we search "i" in source symbols. We found it in 1st positiion. So R[I]={1}. We will flip the source by putting the symbol at the front. Therefore, the source become {i a k o n}.

Second, we continue to see L column, now we see "k". Check the "k" position in new source {i a k o n}, it is at 2nd position. So R[I]={1,3}. We will flip the source by putting the symbol at the front. Now, the source become {k i a o n}.

In this way, we continue all the alphabet in L column and occassionally, we will get R[I] = {1,2,3,3,4}.

We will use this {1,2,3,3,4} with huffman coding. We construct the huffman tree based on this. As you notice, the original word "nokia" is now being replaced by {1,2,3,3,4}. When we compress, the file size of reduced dramistically.

To program this topic, you will follow-
Compession: 1. Apply BWT, 2. Apply Move-to-Front, 3. Apply Huffman Coding
Depression: 1. Decode Huffman, 2. Decode Move-to-Front, 3. Apply Reverse BWT.

The result of all the process will be such differences.



Channel Coding

Now, we have data encoded with Huffman coding algorithm. We are going to send this information across some kind of channel.

The question is how reliable is our information across the channel. This is becoming a big problem since our information is depending on the unreliable medium. This is due to the noise over signal that could easily change our information bit with our acknowledgement.

To demo this problem, we will program which could introduce the error bit probability of 0.03% and 0.01%. It means, each time the program read a bit from the file, I call my function to generate a random number. if the value is les than or equal to the value selected for p, it will invert the bit otherwise, the bit remain unchanged.



We are now using Hamming Code (7,4). I will give u some example for that.

Step 1: Decide for the position of parity bit.


Step 2: Put the data bit into the chart. We use the example data bit = 0101.


Step 3: Now we will calculate the bit#1. (check 1 bit, skip 1 bit; start from bit#1)
result = bit#1 + bit#3 + bit#5 + bit#7 = ? + 0 + 1 + 1 = 2 (even)
if the result is even, we assign "0". If it is odd, assign "1". Now the table become-


Step 4: To calculate bit#2, (check 2 bits, skip 2 bits; start from bit#2)
result = bit#2 + bit#3 + bit#6 + bit#7 = ? + 0 + 0 + 1 = 1 (odd)
The result is odd, so assign "1". Now the table become -


Step 5: To calculate bit#4, (check 4 bits, skip 4bits; start from bit#4)
result = bit#4 + bit#5 + bit#6 + bit#7 = ? + 1 +0 +1 = 2 (even)
The result is even, so assign "0". The table is now


The data(4 bits) "0101" is now "0100101". Now, I will show you how it corrects the error bit.
Let's say, the data is transferred and the receiver get "0100111". So, the receiver has this table.

Error pointer = 0;

First, check for parity at bit#1. Check 1 bit, skip 1 bit.
Result = bit#1 + bit#3 + bit#5 + bit#7 = 0 + 0 + 1 + 1 = 0 (even) => no error

Second, check for parity at bit#2, check 2 bits, skip 2 bits.
Result = bit#2 + bit#3 + bit#6 + bit#7 = 1 + 0 + 1 + 1 = 1 (odd) => error
If error at bit#2, add the error pointer by 2. (Error Pointer = 2)

Third, check for parity at bit#4, check 4 bits, skip 4 bits.
Result = bit#4 + bit#5 + bit#6 + bit#7 = 0 + 1 + 1 + 1 = 3 (odd) => error
If error at bit#4, add the error pointer by 4. (Error Pointer = 2 + 4 = 6).

Now, we know the error bit at bit#6. To correct the error, simply toggle it. In this way, the error is automatically corrected.

With Hamming Coding, the reliability is better than transmitting data bits alone. We can reduce the error rate. However, one must bear in mind - to get reliability, you must sacrifice on file size and transmission time.

CSCI466 Assignment 1

This subject has a lot of loves and hates. Some people love it while some people hate it. Some people called this subject as "killing" subject for students. The failure rate is high and most people do not understand the topic. Well, if you are a math lover, you will love this subject. This subject is about "PROBABILITY". Yes.. it's about probability. When you toss a coin, there are 50% of chance that it is head, likewise, 50% for the tail. Question for this subject is "how do you make use of it to apply to data communication? how will you code your program so that it is against the unexpected probable data changes?"

Well, this is the assignment 1 questions. You need a calculator for this subject.

1. (a) There is n people in a room. Compute the probability that at least 2 have the same birth month.
Answer: 12^n - {12! / (12-n)!} / 12^n
1. (b) Evaluate this probability when n=3, 4, 5,6.
Answer: Just replace n with numbers in the above formula.

2. Two different suppliers, A and B, provide manufacturer with the same part. All supplies of this part are kept in a large bin. In the past, 5 percent of parts supplied by A and 9 percent of parts supplied by B have been defective. Supplier A supplies 4 times as many parts as supplier B. Suppose you reach into the bin and select a part and find it defective. What is the probability that it was supplied by A?
Answer: Use Bayes' theorem.

3. You are given two urns each containing two biased coins. The coins in urn I come up heads with probability p1, and the coins in urn II come up heads with probability p2≠p1. You are given a choice of (a) choosing an urn at random and tossing the two coins in this urn or (b) choosing one coin from each urn and tossing these two coins. You win a prize if both coins turn up heads. Show that you are better of selecting choice (a).
Answer: There are two methods.

Method 1 : P(both heads) = 1/2 * p1 * p1 + 1/2 * p2 * p2


Method2: P(both heads) = p1 * p2


Since p2≠p1, there are two possible scenarios.
S1: If p1 is greater than p2, then p1=p2+k. Sub p2=0.4, k=0.1, then p1=0.5. Using
Method1: P(both heads) = 0.205
Method2: P(both heads) = 0.200
S2: If p2 is grater than p1, then p2=p1+k. Sub p1=0.4, k=0.1, then p2=0.4.Using
Method1: P(both heads) = 0.125
Method2: P(both heads) = 0.120
In both scenarios, method 1 has higher probability.

4. A discrete memoryless source with source probabilities {0.23,0.20,0.14,0.16,0.10,0.10,0.02,0.05}. Assume corresponding source symbols are "a, b, c, d, e, f, g, h". We are given the test string & the code that will be generated for the test string "daggechfb".

(a) Determine an efficient fixed length of code for the source.
Answer: log2 (possible sources)

(b) Determine the Huffman code for this source and the average length of the code. Also write out the code for test string "daggechfb".
Answer: Construct the Huffman coding tree.


Average Length of Code = p(a).l(a)+p(b).l(b)+....+p(g).l(g) = 2.81 bits

(c) Determine the arithmetic code for test string given the probability distribution for the source.

Follow the example in your lecture note for the calculation.

Well, this is about assignment 1.