## Basic

Differential Manchester is somewhat a combination of the RZ and NRZ-I line coding schemes. There is always a transition at the middle of the bit but the bit values are determined at the beginning of the bit. If the bit is 0 then there is a transition, if the bit is 1 then there is no transition. Although the transition can be from 1 to 0 and 0 to 1 it is usually used from 1 to -1 and -1 to 1.

Here is an example of Differential Manchester line coding for bits 10111001.

Conversion from bitstream to signal is called encoding.

Conversion from signal to bitstream is called decoding.

## Coding With MatLab

For encoding we need the bitstream first. In MatLAB that can be defined as array or we can take input from user. Both ways are given below:

```
bits = [1 0 1 1 1 0 0 1];
```

or,

```
bits = input('prompt');
```

With bitstream we need bit rate that means how much bit(s) will appear in each second. This can be also defined as a variable or can be taken as input. Normally bit rate is considered as 1 bit per second. So we will define it to 1.

```
bitrate = 1;
```

We need a sampling for each bit. So that MatLAB needs to know how much sample point will be appeared for each bit. Low sampling can loss data or cannot define bits accurately. Very high sampling can cause slow your program. Here we will define it as 1000.

```
n = 1000;
```

MatLAB needs to know the total time needed for the bitstream. Sometimes total time T may be given or said to take as input. If not then we need to find it by dividing length of bitstream with bitrate.

```
T = length(bits)/bitrate;
```

We have a sampling for each bit we have declared previously. Now we need to find total sample point needed for whole bitstream. We can find it by multiplying sample point of each bit with the number of bits as below.

```
N = n*length(bits);
```

Now, we need to found time for each sample point. For this we divide the whole time (T) with number of total sample points (N).

```
dt = T/N;
```

We are near to the main process. Now we will define the time domain. That is the all values of X-axis. For this we need to declare an array from time 0 to time T by partitioning with dt. That is an array like this.

```
t = 0:dt:T;
```

For better performance we need to set all values of the result (amplitue domain) to 0. We can do this task with a build in function zeros().

```
x = zeros(1,length(t));
```

Here we need an additional variable to save the previous bit. The previous bit at the first point is given in the problem statement. If not by default we will declare it to 1. Here I am declaring it 1 that is the previous bit had positive voltage.

```
lastbit = 1;
```

Now we will run a loop through the bits. The loop will be continued from 1 to the length of bitstream. It will check each bit with an if statement.

If the bit is 0 then first half of the time unit will be filled by the opposite (negated) amplitude of lastbit and second half of the time unit will be filled by the same amplitude of the lastbit. In this case lastbit will remain unchanged.

If the bit is 1 then first half of the time unit will be filled by the same amplitude of lastbit and second half of the time unit will be filled by the opposite (negated) amplitude of the lastbit. In this case lastbit will be changed (negated).

This process will run for each bit. Here is the loop.

```
for i=1:length(bits)
if bits(i)==0
x((i-1)*n+1:(i-1)*n+n/2) = -lastbit;
x((i-1)*n+n/2:i*n) = lastbit;
else
x((i-1)*n+1:(i-1)*n+n/2) = lastbit;
x((i-1)*n+n/2:i*n) = -lastbit;
lastbit = -lastbit;
end
end
```

Everything required for encoding is done. We will now plot the result (amplitude domain) on respect to time domain. Here is the plot. Here ‘Linewidth’, 3 means the signal line will be bold three times than original signal.

```
plot(t, x, 'Linewidth', 3);
```

## Code For Encoding

```
bits = input('prompt');
bitrate = 1;
n = 1000;
T = length(bits)/bitrate;
N = n*length(bits);
dt = T/N;
t = 0:dt:T;
x = zeros(1,length(t));
lastbit = 1;
for i=1:length(bits)
if bits(i)==0
x((i-1)*n+1:(i-1)*n+n/2) = -lastbit;
x((i-1)*n+n/2:i*n) = lastbit;
else
x((i-1)*n+1:(i-1)*n+n/2) = lastbit;
x((i-1)*n+n/2:i*n) = -lastbit;
lastbit = -lastbit;
end
end
plot(t, x, 'Linewidth', 3);
```

For decoding we need a counter that will count how much bit has been decoded. We will define the initial value of the counter to 0.

```
counter = 0;
```

Here we also need a variable to store lastbit. We will use the same variable we used in encoding. But as the value of that has been changed so we will define it’s value again.

```
lastbit = 1;
```

Now we need a loop again to decode the signal. The loop will run from 1 to length of t. That is same times of total sample point. In the loop we will test whether t(i) is greater than the counter. If it is true then we will increase the value of counter.

As in encoding we didn’t make any transition for 1 and make lastbit opposite (negated) so we have to do the reverse task. If the bit is equal to (==) lastbit then we will assign 1 to result bitstream and change (negate) the lastbit.

If the bit is not equal to the lastbit then we will assign 0 to the result bitstream. Because while encoding we saw that 0 has always a transition. Here lastbit will remain unchanged as encoding.

In this way we will assign or test the second sample value of each bit. And for other samples the if condition (external if) will be false. For example, At first the value of t(1)=0 and that is not greater than counter so nothing occurred. Then t(2)=0.001 and that is greater than 0. So counter increased to 1 and the bit is set to the value of 1 or 0 (depending on voltage level and lastbit). Next values of t(i) will be skipped (if condition will be false) as long as it is not 1.001. For each bit same things will be occurred. The loop looks like below.

```
for i = 1:length(t)
if t(i)>counter
counter = counter + 1;
if x(i)==lastbit
result(counter) = 1;
lastbit = -lastbit;
else result(counter) = 0;
end
end
end
```

After this decoding is also done. We need to display it. We can display that by just writing the variable without semi-colon. Or this can shown with disp() function.

```
disp('Differential Manchester Decoding:');
disp(result);
```

## Full Code (Encoding and decoding)

```
bits = input('prompt');
bitrate = 1;
n = 1000;
T = length(bits)/bitrate;
N = n*length(bits);
dt = T/N;
t = 0:dt:T;
x = zeros(1,length(t));
lastbit = 1;
for i=1:length(bits)
if bits(i)==0
x((i-1)*n+1:(i-1)*n+n/2) = -lastbit;
x((i-1)*n+n/2:i*n) = lastbit;
else
x((i-1)*n+1:(i-1)*n+n/2) = lastbit;
x((i-1)*n+n/2:i*n) = -lastbit;
lastbit = -lastbit;
endif
endfor
plot(t, x, 'Linewidth', 3);
counter = 0;
lastbit = 1;
for i = 1:length(t)
if t(i)>counter
counter = counter + 1;
if x(i)==lastbit
result(counter) = 1;
lastbit = -lastbit;
else result(counter) = 0;
endif
endif
endfor
disp('Differential Manchester Decoding:');
disp(result);
```

I think, your experience with this tutorial is quite good. You can notify me anything related to this tutorial via comment or contact form.

Thanks.