Advertisement
makispaiktis

Telecommunication System v2

Sep 27th, 2021 (edited)
477
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MatLab 4.07 KB | None | 0 0
  1. clc
  2. clear all
  3.  
  4. % Define the data
  5. t0 = 0;
  6. tf = 8*pi;
  7. N = 1000;
  8.  
  9. % 1. Sampled signal of info, named "m(t)"
  10. t = linspace(t0, tf, N);
  11. m = cos(t) + 0.5*t;
  12. figure(1);
  13. subplot(2,2,1);
  14. plot(t, m);
  15. title("1. Sampled signal m(t)");
  16.  
  17. % 2. Quantization
  18. M = 5;
  19. numOfLevels = 2^M;
  20. quantizedAndLevels = quantization(m, numOfLevels);
  21. LEN_ALL = length(quantizedAndLevels);
  22. LEN1 = LEN_ALL - numOfLevels;
  23. quantized = quantizedAndLevels([1:LEN1]);
  24. levels = quantizedAndLevels([LEN1+1 : LEN_ALL]);
  25. subplot(2, 2, 2);
  26. plot(t, quantized);
  27. title(strcat("2. Quantized signal with ", num2str(numOfLevels), " levels"));
  28.  
  29. % 3. Digitization - Info bitstream
  30. LEN = length(quantized);
  31. bitstream = zeros(1, M*LEN);
  32. for i = 1:LEN
  33.     % For every qValue, I will add M digits 0,1 in bitsream list
  34.     qValue = quantized(i);
  35.     for j = 1:length(levels)
  36.         level = levels(j);
  37.         if qValue == level
  38.                 decimal = j-1;
  39.                 binary = decimalToBinary(decimal);
  40.                 binaryPad = pad(binary, M);
  41.                 % Binary variable is the M-digit word I will add in
  42.                 % my bitsream list
  43.                 for offset = 1:M
  44.                     bitstream(M *(i-1) + offset) = binaryPad(offset);
  45.                 end
  46.             break
  47.         end
  48.     end
  49. end
  50. bitstream;
  51.  
  52. % 4. Channel encoding
  53.  
  54. X = 7;  % I will send X 0's or 1's instead of a single 0 or 1
  55. encodedInfo = channelEncoding(bitstream, X)
  56. bitString = prettyPrint(encodedInfo)
  57.  
  58. % ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  59. % ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  60. % ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  61. % ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  62.  
  63. % Function 1 - Quantization
  64. function quantizedAndLevels = quantization(m, numOfLevels)
  65.     MAX = max(m);
  66.     MIN = min(m);
  67.     LEVELS = linspace(MIN, MAX, numOfLevels+2);
  68.     levels = zeros(1, numOfLevels);
  69.     for i = 2:length(LEVELS)-1
  70.         levels(i-1) = LEVELS(i);
  71.     end
  72.     % levels is done, now I have to quantize the values
  73.     quantized = zeros(1, length(m));
  74.     for i = 1:length(m)
  75.         value = m(i);
  76.         difference = 10^8;
  77.         optimumLevel = -1;
  78.         for j = 1:length(levels)
  79.             level = levels(j);
  80.             if abs(value - level) < difference
  81.                 difference = abs(value - level);
  82.                 optimumLevel = level;
  83.             end
  84.         end
  85.         quantized(i) = optimumLevel;
  86.     end
  87.     quantizedAndLevels = [quantized levels];
  88. end
  89.  
  90.  
  91. % Function 2 - Decimal to binary
  92. function binary = decimalToBinary(decimal)
  93.     sum = decimal;
  94.     if decimal == 0
  95.         binary = 0;
  96.     else
  97.         N = floor(log2(decimal) + 1);
  98.         binary = zeros(1, N);
  99.         for i = N-1: -1: 0
  100.             if sum >= 2^i
  101.                 sum = sum - 2^i;
  102.                 binary(N - i) = 1;
  103.             else
  104.                 binary(N - i) = 0;
  105.             end
  106.         end
  107.     end
  108. end
  109.  
  110.  
  111. % Function 3 - Pad with zeros - Digitization
  112. function binaryPad = pad(binary, M)
  113.     binaryPad = zeros(1:M);
  114.     if length(binary) < M
  115.         difference = M - length(binary);
  116.         PAD = 1:difference;
  117.         for i = 1:difference
  118.             PAD(i) = 0;
  119.         end
  120.         binaryPad = [PAD binary];
  121.     elseif length(binary) == M
  122.             binaryPad = binary;
  123.     else
  124.         display('Cannot do that');
  125.     end
  126. end
  127.  
  128. % Function 4 - Channel encoding.
  129. % In this function I send X 0's or 1's instead of a single 0 or 1
  130. function encodedInfo = channelEncoding(bitstream, X)
  131.     encodedInfo = zeros(1, X*length(bitstream));
  132.     for i = 1:length(bitstream)
  133.         BIT = bitstream(i);
  134.         for j = 1:X
  135.             encodedInfo(X*(i-1) + j) = BIT;
  136.         end
  137.     end
  138.     encodedInfo;
  139. end
  140.  
  141. % Function 5 - PrettyPrint of a bitstream (encodedInfo)
  142. function bitString = prettyPrint(bitstream)
  143.     bitString = "";
  144.     for i = 1:length(bitstream)
  145.         BIT = bitstream(i);
  146.         bitString = bitString + num2str(BIT);
  147.     end
  148.     bitString;
  149. end
  150.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement