E71 Digital Signal Processing Lab 1, Aron Dobos, 25 Jan 2006

Contents

Part 1: Delayed Sample Function

N = 8;
M = 4;
d = [ 1 zeros(1,N-1) ];
dm = [zeros(1,M) 1 zeros(1,N-M-1)];

subplot(2,1,1);
stem(d);
axis([1 10 0 1.5]);
title('unit sample function, N=8');
xlabel('n');
ylabel('Unit Sample');

subplot(2,1,2);
stem(dm);
axis([1 10 0 1.5]);
title('delayed unit sample function, N=8 M=4');
xlabel('n')
ylabel('Delayed Unit Sample')

Part 2: More Simple Functions

N = 10;
Usample = [ 1 zeros(1,N-1) ];
Ustep = [ 0 ones(1,N-1) ];
Uramp = zeros(1, N);
for i=1:N,
    Uramp(i) = i-1;
end;

subplot(1,1,1);
hold on;
stem(Usample, 'r');
stem(Ustep, 'g');
stem(Uramp, 'b');
hold off;
legend('sample', 'step', 'ramp');
axis([0 10 0 10]);
title('Unit Sample, Unit Step, and Unit Ramp');
xlabel('n');
ylabel('value');

Part 3: Sawtooth and Square

A=8;
L=30;
N=11;
X = 1:1:L;

subplot(2,1,1);
Y=A*sawtooth(2*pi/N*X);
stem(X,Y);
xlabel('Time in 0.0001sec');
ylabel('Amplitude');

subplot(2,1,2);
Y=A*square(2*pi/N*X);
stem(X,Y);
xlabel('Time in 0.0001sec');
ylabel('Amplitude');

Part 4: Sinusoids of 3Hz 7Hz 13Hz

% Explanation of results:
%  The sampling theorem states that the sampling frequency must be
%  at least twice the frequency of the input to be able to recreate
%  the sampled signal.  In this case, only the 3Hz can be recreated
%  from the 10Hz sampling, and the sampled 13Hz signal looks identical to the
%  to the 3Hz signal, which is called aliasing.  The 7 Hz digitized signal
%  still bears some similarities to the original, but by the sampling
%  theorem it cannot be always correctly reproduced from the digitized data.

FS = 1000;
t = 0:1/FS:1/3;
Y1 = sin(2*pi*3*t);
Y2 = sin(2*pi*7*t);
Y3 = sin(2*pi*13*t);
clf;
subplot(2,1,1);
plot(t,Y1,t,Y2,t,Y3, '--r');

FS = 10;
tn = 0:1/FS:1/3;
Y1 = sin(2*pi*3*tn);
Y2 = sin(2*pi*7*tn);
Y3 = sin(2*pi*13*tn);
subplot(2,1,2);
hold on;
stem(tn, Y1, 'b');
stem(tn, Y2, 'g');
stem(tn, Y3, '--r');
hold off;

Part 5: Quantized Sinusoid

% Explanation of Quantization
%  The 'round' function rounds to the nearest integer.  The value of the
%  sinusoid is first multiplied by 2^(n-1) which normalizes it to the
%  maximum value representable by number of bits used. (n-1) is used instead of
%  n so that both positive and negative numbers are represented. Then the result
%  is rounded to the nearest integer, and scaled back down, resulting in an
%  discrete quantization of the signal, as it would be if n bits were used.
%  The result is clear from the graph.  In fact for 3 bits we get 9
%  quantization levels instead of 8, since the two's complement format is
%  not quite fully expressed in this rounding method.  Normally in real
%  hardware, the range of possible values would be -4 to +3, so in fact
%  only 8 quantization levels would exist.

clf;
w = 1;
n = 3;
FS = 10;
t = 0:1/FS:2*2*pi;
y = sin(w*t);
yd = round(y*2^(n-1))/(2^(n-1));
plot(t,y, t, yd);
title('Quantization of a 1rad/sec sine wave');

Part 6: Calculating the power of the sequence.

                        N
P =  lim   ( 1/(2N+1) * sum(x_n^2)
   N->inf               -N

Since we have a discrete sequence, we cannot calculate using the infinite limit, so we just sum from 0 to N. The result is P = 0.49993517713030

Using the other equation for the power A^2/2 where A is the amplitude of the sinusoid, we get P = 0.5

clf;
format long;
w = 1;
FS = 100;
N = 2*pi*10;
t = -N:1/FS:N;
y = sin(w*t);
plot(t,y);
N = length(y)/2;
P = sum(y.^2)/(2*N+1)
P =

   0.49993517713030

Part 7: Calculating the error signal

% We calculate the SQNR using two equations
% SQNR_1 = 1.76 + 6.02*n
% SQNR_2 = 10*log10( sum(y^2)/sum(err^2) )
%           (courtesy of brian park's lab group)
% It is clear that the two equations essentially
% give the same results, observable from the values
% displayed in the graph captions.

% recalculate part 5 values
clf;
hold off;
w = 1;
FS = 10;
t = 0:1/FS:2*2*pi;
y = sin(w*t);
sqnr1 = zeros(1,8);
sqnr2 = zeros(1,8);
sqnrx = zeros(1,8);
format short;
for n=1:8,
   sqnrx(n) = n;
   yd = round(y*2^(n-1))/(2^(n-1));
   subplot(4,2,n);
   plot(t,yd, t, yd-y);
   sqnr1(n) = 1.76+6.02*n;
   sqnr2(n) = 10*log10(sum(y.^2)/sum((yd-y).^2));
   title(sprintf('Using %d Bits, SQNR_1=%.1f SQNR_2=%.1f', n, sqnr1(n),sqnr2(n)))
   xlabel('n');
end;

Part 8: SQNR vs bits

% The SQNR increases with an increase in the number of
% bits used to digitize the signal.  This is expected since
% the more bits used results in a much more precise digitization
% of the actual signal, and hence the noise that arises due to the
% quantization process is reduced relative to the original signal.

subplot(1,1,1);
plot(sqnrx, sqnr1, 'go', sqnrx, sqnr2, 'xr');
legend('SQNR=1.76 + 6.02*n', 'SQNR_2 = 10*log10( sum(y^2)/sum(err^2) )');
xlabel('Bits (n)');
ylabel('SQNR (dB)');
title('SQNR vs Number of Bits Used');