echo on
clc

 %-----------------------------------------------------------%
 %                  name: xxxxxxxx                           %
 %        student number: xxxxxxxx                           %
 %                  data: 1994.10.1                          %
 %                                                           %
 %-----------------------------------------------------------%


 %-----------%
 %  EX.1-1   %
 %-----------%
load s5
s=s5/32768;
t=1:24570;
pause %strike any key for plot.........

plot(t,s),title('EX.1-1 plot(s5/32768)'),
ylabel('amplitude level with max=1'),xlabel('samples'),pause

 %Now we will plot the striplot.
 %strike any key for striplot(s,8000,2000)........
striplot(s,8000,2000),title('EX.1-1 striplot(s,8000,2000)'),pause


%----------%
% EX.1-2   %
%----------%

min(s)


max(s)
pause %please strike any key for coutine...



mean(s)
%mean-squared(s)=m

m=sum(s.^2)/size(s,1)
pause
%Now we want to plot a histogram of s.
 %Strike any key for plot hist(s,20).
hist(s,20),title('EX.1-2 hist(s,20)'),pause


%--------%
% Ex.1-3 %
%--------%

 %Now we plot the spectrum of 's' in dB by use welch(s,1000,100,'r',1000)
 %strike any key for plot...
welch(s,1000,300,'r',1000);pause


%--------%
% EX.2-1 %
%--------%

 %Now we will plot some input-output characteristics of quantizer.
x=-2:0.01:2;
 %strike any key for plot...
plot(x,fxquant(x,3,'round','sat')),title('EX.2-1 plot(x,fxquant(x,3,round,sat))'),pause

 %strike any key for plot...
plot(x,fxquant(x,3,'round','sat')-x),title('EX.2-1 plot(x,fxquant(x,3,round,sat)-x)'),pause


%--------%
% EX.2-2 %
%--------%

 %Now we want to see the quantization error of 's'
 %first is 10-bit quantization
 %strike any key for plot....
g1=fxquant(s,10,'round','sat');
e1=g1-s;
striplot(e1,8000,2000),title('EX.2-2 striplot(s,8000,2000) for 10-bit'),pause

hist(e1,30),pause


 %second is 8-bit quantization
 %strike any key for plot....
g2=fxquant(s,8,'round','sat');
e2=g2-s;
striplot(e2,8000,2000),title('EX.2-2 striplot(s,8000,2000) for 8-bit'),pause

hist(e2,30),pause


 %third is 4-bit quantization
 %strike any key for plot....
g3=fxquant(s,4,'round','sat');
e3=g3-s;
striplot(e3,8000,2000),title('EX.2-2 striplot(s,8000,2000) for 4-bit'),pause

hist(e3,30),pause


%---------%
% Ex.2-3  %
%---------%
 %Now we computer the power spectrum of quantization error
 %strike any for plot
[p1,c1]=welch(e1,400,400,'r');
[p2,c2]=welch(e2,400,400,'r');
[p3,c3]=welch(e3,400,400,'r');
plot((0:200)/200,10*log10(p1),(0:200)/200,10*log10(p2),(0:200)/200,10*log10(p3)),pause

%--------%
% EX.2-4 %
%--------%

 %repeat EX.2-2 for 'trunc'
 %--------------
 %first is 10-bit quantization
 %strike any key for plot....
g1=fxquant(s,10,'trunc','sat');
e1=g1-s;
striplot(e1,8000,2000),title('EX.2-2 striplot(s,8000,2000) for 10-bit'),pause

hist(e1,30),pause


 %second is 8-bit quantization
 %strike any key for plot....
g2=fxquant(s,8,'trunc','sat');
e2=g2-s;
striplot(e2,8000,2000),title('EX.2-2 striplot(s,8000,2000) for 8-bit'),pause

hist(e2,30),pause


 %third is 4-bit quantization
 %strike any key for plot....
g3=fxquant(s,4,'trunc','sat');
e3=g3-s;
striplot(e3,8000,2000),title('EX.2-2 striplot(s,8000,2000) for 4-bit'),pause

hist(e3,30),pause


 %repeat EX.2-3 % for 'trunc'
 %----------------

%[p1,c1]=welch(e1,400,400,'r');
%[p2,c2]=welch(e2,400,400,'r');
%[p3,c3]=welch(e3,400,400,'r');
%strike any for plot
[p1,c1]=welch(e1,400,400,'r');
[p2,c2]=welch(e2,400,400,'r');
[p3,c3]=welch(e3,400,400,'r');
plot((0:200)/200,10*log10(p1),(0:200)/200,10*log10(p2),(0:200)/200,10*log10(p3)),pause

 %----------%
 %  EX.3-1  %
 %----------%

%EX.3-1-a.
 %creat a linear input vector [0:0.0005:1]
x=0:0.0005:1;
t=0:2000;
y1=mulaw1(x,100);
y2=mulaw1(x,255);
y3=mulaw1(x,500);
plot(t,y1,t,y2,t,y3),text(600,0.7,'mu=100'),text(600,0.82,'mu=500'),text(600,0.78,'----------mu=255'),pause

%EX.3-1-b.
y=mulaw1(s,255);
t=1:24570;
plot(t,s),title('s=s5/32768'),pause
plot(t,y),title('mulaw(s)'),pause





%EX.3-1-c.
%function x=muinv1(y,mu)
%x=((1+mu).^(abs(y))-1)/mu.*sign(y);
y=mulaw1(s,255);
x=muinv1(y,255);
e=s-x;
subplot(2,1,1),plot(t,s),title('s')
subplot(2,1,2),plot(t,x),title('mulawinv s'),pause

sound(s,8192);pause
sound(y,8192);pause
sound(x,8192);
  %%%%%%%%%% the end  %%%%%%%%%%%
  %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

