Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function preLab5Cleaning()
- clc;clear;close all;
- % Define mu_law_compress and a_law_compress functions here
- function y = mu_law_compress(x, mu)
- y = sign(x).*log(1 + mu.*abs(x))./log(1 + mu);
- end
- function y = a_law_compress(x, A)
- y = sign(x).*log(1 + A.*abs(x))/log(1 + A);
- end
- % Added expandsion functions for A and u law **new
- function y = uExpand(sig, u)
- y = (sign(sig).*((1+u).^(abs(sig)) - 1)./u);
- end
- function y = aExpand(sig, A)
- y = zeros(size(sig));
- a = 1/(1 + log(A));
- for i = 1:1000;
- if abs(sig(i)) < a
- y(i) = sign(sig(i)).*(abs(sig(i)).*(1 + log(A)))./A;
- elseif (abs(sig(i)) >= a) && (abs(sig(i)) <= 1)
- y(i) = sign(sig(i)).*(exp(-1 + abs(sig(i)).*(1 + log(A))) )./A;
- end
- end
- end
- function y = getMean(sig)
- end
- function y = getVariance(sig1, sig2)
- end
- %% end functions
- % Generate the reference signal
- x = linspace(-1, 1, 1000);
- % Compress the signal for different values of mu and A
- % y1 = mu_law_compress(x, 0);
- % y2 = mu_law_compress(x, 5);
- % y3 = mu_law_compress(x, 100);
- % y4 = a_law_compress(x, 1);
- % y5 = a_law_compress(x, 2);
- % y6 = a_law_compress(x, 100);
- u = [0, 5, 100];
- A = [1, 2, 100];
- uComp1 = mu_law_compress(x, u(1));
- uComp2 = mu_law_compress(x, u(2));
- uComp3 = mu_law_compress(x, u(3));
- aComp1 = a_law_compress(x, A(1));
- aComp2 = a_law_compress(x, A(2));
- aComp3 = a_law_compress(x, A(3));
- % Plot the results
- figure(1);
- plot(x, uComp1, x, uComp2, x, uComp3, x, x);
- legend('\mu=0', '\mu=5', '\mu=100', 'signal');
- xlabel('x');
- ylabel('y');
- title('µ-law Compression');
- figure(2);
- plot(x, aComp1, x, aComp2, x, aComp3, x, x);
- legend('A=1', 'A=2', 'A=100', 'signal');
- xlabel('x');
- ylabel('y');
- title('A-law Compression');
- %% Decompress the signals
- uDecomp1 = uExpand(uComp1, u(1));
- uDecomp2 = uExpand(uComp2, u(2));
- uDecomp3 = uExpand(uComp3, u(3));
- aDecomp1 = aExpand(aComp1, A(1));
- aDecomp2 = aExpand(aComp2, A(2));
- aDecomp3 = aExpand(aComp3, A(3));
- % Plot the results
- figure(3);
- plot(x, uDecomp1, x, uDecomp2, x, uDecomp3, x, x);
- legend('\mu=0', '\mu=5', '\mu=100', 'signal');
- xlabel('x');
- ylabel('y');
- title('µ-law de-compression');
- figure(4);
- plot(x, aDecomp1, x, aDecomp2, x, aDecomp3, x, x);
- legend('A=1', 'A=2', 'A=100', 'signal');
- xlabel('x');
- ylabel('y');
- title('A-law de-compression');
- %differences between original and de-compressed signals
- difu1 = x - uDecomp1;
- difu2 = x - uDecomp2;
- difu3 = x - uDecomp3;
- difa1 = x - aDecomp1;
- difa2 = x - aDecomp2;
- difa3 = x - aDecomp3;
- %means of the magnitude of difference
- meanDifa3 = mean(abs(difa3), 'all')
- meanDifa2 = mean(abs(difa2), 'all')
- meanDifa1 = mean(abs(difa1), 'all')
- meanDifu3 = mean(abs(difu3), 'all')
- meanDifu2 = mean(abs(difu2), 'all')
- meanDifu1 = mean(abs(difu1), 'all')
- %standard deviation of the magnitudes of difference
- sdDifa3 = std(abs(difa3))
- sdDifa2 = std(abs(difa2))
- sdDifa1 = std(abs(difa1))
- sdDifu3 = std(abs(difu3))
- sdDifu2 = std(abs(difu2))
- sdDifu1 = std(abs(difu1))
- %max and min values of magnitude of difference (Format [max, min])
- mmDifa3 = [max(abs(difa3)), min(abs(difa3))]
- mmDifa2 = [max(abs(difa2)), min(abs(difa2))]
- mmDifa1 = [max(abs(difa1)), min(abs(difa1))]
- mmDifu3 = [max(abs(difu3)), min(abs(difu3))]
- mmDifu2 = [max(abs(difu2)), min(abs(difu2))]
- mmDifu1 = [max(abs(difu1)), min(abs(difu1))]
- %quantization section
- print('test')
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement