Advertisement
makispaiktis

Telecommunication System

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