% Bok 2004.  Kap. 2
%------------------------------------------
% Avtakende analog sinus  Fig.2.1 
alfa=0.01;beta=0.5;
t=0:0.1:40;
f=exp(-alfa*t).*sin(beta*t);
plot(t,f,'-')
xlabel('t   [s]')
legend('x(t)')
grid
pause % Fig2.1
%-----------------------------------------
% Kvantisert signal  Fig. 2.3 
k=f;
for i=1:1:401
   f(i)=exp(-alfa*t(i)).*sin(beta*t(i));
   if f(i)<-0.8  k(i)=-0.995;
   elseif f(i)<-0.6  k(i)=-0.8;
   elseif f(i)<-0.4 k(i)=-0.6;
   elseif f(i)<-0.2 k(i)=-0.4;
   elseif f(i)<0.0 k(i)=-0.2;
   elseif f(i)<0.2  k(i)=0.0;
   elseif f(i)<0.4  k(i)=0.2;
   elseif f(i)<0.6  k(i)=0.4;
   elseif f(i)<0.8  k(i)=0.6;
   elseif f(i)<1    k(i)=0.8;
   end
end   
plot(t,k,'-',t,f,':')
legend('xq(t)')
grid
xlabel('t   [s]')
pause  % Fig. 2.3
%----------------------------------------
% Trappetrinn  Ikke i bruk
u=0:2:40;
g=exp(-alfa*u).*sin(beta*u);
stairs(u,g)        % Bruker stairs()
hold on
plot(t,f,':')
grid
xlabel('t   [s]')
legend('xq(t)')
hold off
pause % Ikke i bruk
%------------------------------------------
% Diskret uten kvantisering  Fig. 2.5
u=0:2:40;
g=exp(-alfa*u).*sin(beta*u);
bar(u,g,0.1)                 % Bar-plotting
hold
plot(t,f,':')
grid
axis([0 40 -1 1])
xlabel('t   [s]')
legend('xs(t)')
hold off
pause % Fig 2.5
%-------------------------------------------
% Diskret uten kvantisering  Fig. 2.6
u=0:2:40;
g=exp(-alfa*u).*sin(beta*u);
stem(u,g,'fill')                 % Stem-plotting
hold
plot(t,f,':')
grid
xlabel('t   [s]')
legend('x(nTs)')
axis([0 40 -1 1])
hold off
pause % Fig 2.6
%-----------------------
% Digitalt Fig. 2.7
u=0:2:40;
g=exp(-alfa*u).*sin(beta*u);
d=u;
for i=1:1:21
   %d(i)=exp(-alfa*u(i)).*sin(beta*u(i));
   if g(i)<-0.8  d(i)=-0.8;
   elseif g(i)<-0.6 d(i)=-0.6;
   elseif g(i)<-0.4 d(i)=-0.4;
   elseif g(i)<-0.2 d(i)=-0.2;
   elseif g(i)<0.2  d(i)=0.0;
   elseif g(i)<0.4  d(i)=0.2;
   elseif g(i)<0.6  d(i)=0.4;
   elseif g(i)<0.8  d(i)=0.6;
   elseif g(i)<1    d(i)=0.8;
   end
end   
%g=exp(-alfa*u).*sin(beta*u);
stem(u,d,'fill')        % Stem-plotting
hold on
plot(t,f,':')
grid
xlabel('t   [s]')
legend('xq(nTs)')
hold off
pause % Fig. 2.7
%-----------------------
% Digitalt Fig. 2.8
n=0:1:20;
stem(n,d,'fill')        % Stem-plotting
grid
xlabel('n')
legend('x(n)')
pause % Fig. 2.8
%-------------------------------
% Eksponentiell demping	a^n  Fig. 2.17a
a=0.9;
n=0:40;
q=0:4:40;
for p=1:41
    eksdemp(p)=a^n(p);		% Tildeler verdier til hvert element i vektoren
end
for p=1:11
   eks(p)=a^q(p);
end   
plot(n,eksdemp,':')
hold on
stem(q,eks,'fill')
xlabel('n'),ylabel('0.9^n')
hold off
pause
% Eksponentiell demping	a^n Fig. 2.17b
a=1.1;
n=0:40;
q=0:4:40;
for p=1:41
    eksdemp(p)=a^n(p);		% Tildeler verdier til hvert element i vektoren
end
for p=1:11
   eks(p)=a^q(p);
end   
plot(n,eksdemp,':')
hold on
stem(q,eks,'fill')
xlabel('n'),ylabel('1.1^n')
hold off
pause
% -----------------------------------
% Sinussignal ----------   Fig. 2.19
N=32;		   	    	% Antall samplinger  
fs=8;	T=1/fs;			% Samplingsfrekvens og -periode
n=0:(N-1); 	          % Linjevektor n får verdier 0 1 2 ..31
f=0.5;				% Signalfrekvens i KHz ( eller Hz )
sinus=sin(2*pi*n*f/fs);   	% N*f0/fs er antall svingninger
stem(n,sinus,'fill')			% Sampleplotter 
xlabel('n')
grid
pause
