Advertisement
JKattackk

5prelabv2

Apr 5th, 2023
867
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MatLab 3.83 KB | None | 0 0
  1. function preLab5Cleaning()
  2. clc;clear;close all;
  3. % Define mu_law_compress and a_law_compress functions here
  4.     function y = mu_law_compress(x, mu)
  5.         y = sign(x).*log(1 + mu.*abs(x))./log(1 + mu);
  6.     end
  7.  
  8.     function y = a_law_compress(x, A)
  9.         y = sign(x).*log(1 + A.*abs(x))/log(1 + A);
  10.     end
  11.    
  12.     % Added expandsion functions for A and u law **new
  13.     function y = uExpand(sig, u)
  14.         y = (sign(sig).*((1+u).^(abs(sig)) - 1)./u);
  15.     end
  16.    
  17.     function y = aExpand(sig, A)
  18.         y = zeros(size(sig));
  19.         a = 1/(1 + log(A));
  20.  
  21.     for i = 1:1000;
  22.         if abs(sig(i)) < a
  23.             y(i) = sign(sig(i)).*(abs(sig(i)).*(1 + log(A)))./A;
  24.         elseif (abs(sig(i)) >= a) && (abs(sig(i)) <= 1)
  25.             y(i) = sign(sig(i)).*(exp(-1 + abs(sig(i)).*(1 + log(A))) )./A;
  26.         end
  27.     end
  28.     end
  29.    
  30.     function y = getMean(sig)
  31.        
  32.     end
  33.     function y = getVariance(sig1, sig2)
  34.        
  35.     end
  36.     %% end functions
  37.  
  38.     % Generate the reference signal
  39.     x = linspace(-1, 1, 1000);
  40.  
  41.     % Compress the signal for different values of mu and A
  42. %     y1 = mu_law_compress(x, 0);
  43. %     y2 = mu_law_compress(x, 5);
  44. %     y3 = mu_law_compress(x, 100);
  45. %     y4 = a_law_compress(x, 1);
  46. %     y5 = a_law_compress(x, 2);
  47. %     y6 = a_law_compress(x, 100);
  48.    
  49.     u = [0, 5, 100];
  50.     A = [1, 2, 100];
  51.     uComp1 = mu_law_compress(x, u(1));
  52.     uComp2 = mu_law_compress(x, u(2));
  53.     uComp3 = mu_law_compress(x, u(3));
  54.  
  55.     aComp1 = a_law_compress(x, A(1));    
  56.     aComp2 = a_law_compress(x, A(2));
  57.     aComp3 = a_law_compress(x, A(3));
  58.  
  59.     % Plot the results
  60.     figure(1);
  61.     plot(x, uComp1, x, uComp2, x, uComp3, x, x);
  62.     legend('\mu=0', '\mu=5', '\mu=100', 'signal');
  63.     xlabel('x');
  64.     ylabel('y');
  65.     title('µ-law Compression');
  66.     figure(2);
  67.     plot(x, aComp1, x, aComp2, x, aComp3, x, x);
  68.     legend('A=1', 'A=2', 'A=100', 'signal');
  69.     xlabel('x');
  70.     ylabel('y');
  71.     title('A-law Compression');
  72.  
  73.  
  74.     %% Decompress the signals
  75.     uDecomp1 = uExpand(uComp1, u(1));
  76.     uDecomp2 = uExpand(uComp2, u(2));
  77.     uDecomp3 = uExpand(uComp3, u(3));
  78.  
  79.     aDecomp1 = aExpand(aComp1, A(1));
  80.     aDecomp2 = aExpand(aComp2, A(2));
  81.     aDecomp3 = aExpand(aComp3, A(3));
  82.  
  83.     % Plot the results
  84.     figure(3);
  85.     plot(x, uDecomp1, x, uDecomp2, x, uDecomp3, x, x);
  86.     legend('\mu=0', '\mu=5', '\mu=100', 'signal');
  87.     xlabel('x');
  88.     ylabel('y');
  89.     title('µ-law de-compression');
  90.     figure(4);
  91.     plot(x, aDecomp1, x, aDecomp2, x, aDecomp3, x, x);
  92.     legend('A=1', 'A=2', 'A=100', 'signal');
  93.     xlabel('x');
  94.     ylabel('y');
  95.     title('A-law de-compression');
  96.  
  97.     %differences between original and de-compressed signals
  98.     difu1 = x - uDecomp1;
  99.     difu2 = x - uDecomp2;
  100.     difu3 = x - uDecomp3;
  101.  
  102.     difa1 = x - aDecomp1;
  103.     difa2 = x - aDecomp2;
  104.     difa3 = x - aDecomp3;
  105.    
  106.     %means of the magnitude of difference
  107.     meanDifa3 = mean(abs(difa3), 'all')
  108.     meanDifa2 = mean(abs(difa2), 'all')
  109.     meanDifa1 = mean(abs(difa1), 'all')
  110.     meanDifu3 = mean(abs(difu3), 'all')
  111.     meanDifu2 = mean(abs(difu2), 'all')
  112.     meanDifu1 = mean(abs(difu1), 'all')
  113.  
  114.     %standard deviation of the magnitudes of difference
  115.     sdDifa3 = std(abs(difa3))
  116.     sdDifa2 = std(abs(difa2))
  117.     sdDifa1 = std(abs(difa1))
  118.     sdDifu3 = std(abs(difu3))
  119.     sdDifu2 = std(abs(difu2))
  120.     sdDifu1 = std(abs(difu1))
  121.  
  122.     %max and min values of magnitude of difference (Format [max, min])
  123.     mmDifa3 = [max(abs(difa3)), min(abs(difa3))]
  124.     mmDifa2 = [max(abs(difa2)), min(abs(difa2))]
  125.     mmDifa1 = [max(abs(difa1)), min(abs(difa1))]
  126.     mmDifu3 = [max(abs(difu3)), min(abs(difu3))]
  127.     mmDifu2 = [max(abs(difu2)), min(abs(difu2))]
  128.     mmDifu1 = [max(abs(difu1)), min(abs(difu1))]
  129.  
  130.  
  131.     %quantization section
  132.     print('test')
  133.  
  134.  
  135.  
  136.  
  137.  
  138.  
  139.  
  140.  
  141.  
  142. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement