Advertisement
makispaiktis

Telecommunication System v3

Sep 27th, 2021 (edited)
1,689
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MatLab 4.35 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. LEN = length(encodedInfo);
  57. subplot(2,2,3);
  58. plot(encodedInfo);
  59. title('Signal bitstream after encoding');
  60. subplot(2,2,4);
  61. OFFSET = 100;
  62. yAxis = encodedInfo([LEN/2-OFFSET : LEN/2+OFFSET]);
  63. plot(yAxis);
  64. title(strcat("Bistream zoomed (", num2str(2*OFFSET+1), " bits in center)"));
  65. bitString = prettyPrint(encodedInfo)
  66.  
  67.  
  68.  
  69. % ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  70. % ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  71. % ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  72. % ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  73.  
  74. % Function 1 - Quantization
  75. function quantizedAndLevels = quantization(m, numOfLevels)
  76.     MAX = max(m);
  77.     MIN = min(m);
  78.     LEVELS = linspace(MIN, MAX, numOfLevels+2);
  79.     levels = zeros(1, numOfLevels);
  80.     for i = 2:length(LEVELS)-1
  81.         levels(i-1) = LEVELS(i);
  82.     end
  83.     % levels is done, now I have to quantize the values
  84.     quantized = zeros(1, length(m));
  85.     for i = 1:length(m)
  86.         value = m(i);
  87.         difference = 10^8;
  88.         optimumLevel = -1;
  89.         for j = 1:length(levels)
  90.             level = levels(j);
  91.             if abs(value - level) < difference
  92.                 difference = abs(value - level);
  93.                 optimumLevel = level;
  94.             end
  95.         end
  96.         quantized(i) = optimumLevel;
  97.     end
  98.     quantizedAndLevels = [quantized levels];
  99. end
  100.  
  101.  
  102. % Function 2 - Decimal to binary
  103. function binary = decimalToBinary(decimal)
  104.     sum = decimal;
  105.     if decimal == 0
  106.         binary = 0;
  107.     else
  108.         N = floor(log2(decimal) + 1);
  109.         binary = zeros(1, N);
  110.         for i = N-1: -1: 0
  111.             if sum >= 2^i
  112.                 sum = sum - 2^i;
  113.                 binary(N - i) = 1;
  114.             else
  115.                 binary(N - i) = 0;
  116.             end
  117.         end
  118.     end
  119. end
  120.  
  121.  
  122. % Function 3 - Pad with zeros - Digitization
  123. function binaryPad = pad(binary, M)
  124.     binaryPad = zeros(1:M);
  125.     if length(binary) < M
  126.         difference = M - length(binary);
  127.         PAD = 1:difference;
  128.         for i = 1:difference
  129.             PAD(i) = 0;
  130.         end
  131.         binaryPad = [PAD binary];
  132.     elseif length(binary) == M
  133.             binaryPad = binary;
  134.     else
  135.         display('Cannot do that');
  136.     end
  137. end
  138.  
  139. % Function 4 - Channel encoding.
  140. % In this function I send X 0's or 1's instead of a single 0 or 1
  141. function encodedInfo = channelEncoding(bitstream, X)
  142.     encodedInfo = zeros(1, X*length(bitstream));
  143.     for i = 1:length(bitstream)
  144.         BIT = bitstream(i);
  145.         for j = 1:X
  146.             encodedInfo(X*(i-1) + j) = BIT;
  147.         end
  148.     end
  149.     encodedInfo;
  150. end
  151.  
  152. % Function 5 - PrettyPrint of a bitstream (encodedInfo)
  153. function bitString = prettyPrint(bitstream)
  154.     bitString = "";
  155.     for i = 1:length(bitstream)
  156.         BIT = bitstream(i);
  157.         bitString = bitString + num2str(BIT);
  158.     end
  159.     bitString;
  160. end
  161.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement