DSP Lab 6
DSP Lab 6
Objective:
Sampling of continuous time signals and its reconstruction.
Introduction:
Digital signal processing algorithms are often used to process continuous-time
signals. To this end, it is necessary to convert a continuous-time signal into an equivalent discrete-
time signal, apply the necessary digital signal processing algorithm to it, and then convert back
the processed discrete-time signal into an equivalent continuous-time signal. In the ideal case,
the conversion of a continuous-time signal into a discrete-time form is implemented by periodic
sampling and, to prevent aliasing, an analog anti-aliasing filter is often placed before sampling to
band-limit the continuous-time signal. The conversion of a discrete-time signal into a continuous-
time signal requires an analog reconstruction filter. In this lab you will investigate the effect of
sampling in the time domain and the frequency domain, respectively. In addition, you will learn
the basics of analog-to-digital and digital-to-analog conversions.
Task 1:
% Program P6_1
% Illustration of the Sampling Process in the Time-Domain
clf;
t = 0:0.0005:1;
f = 13;
xa = cos(2*pi*f*t);
subplot(2,1,1)
plot(t,xa);grid
xlabel('Time, msec');
ylabel('Amplitude');
title('Continuous-time signal x_{a}(t)');
axis([0 1 -1.2 1.2])
subplot(2,1,2);
T = 0.1;
n = 0:T:1;
xs = cos(2*pi*f*n);
k = 0:length(n)-1;
stem(k,xs);grid;
xlabel('Time index n');
ylabel('Amplitude');
title('Discrete-time signal x[n]');
axis([0 (length(n)-1) -1.2 1.2])
Result:
Questions:
Q6.1: Running Program P6_1 generates two plots, one for the continuous-time signal and the
other for its sampled version. The continuous-time signal is a cosine wave with a frequency of 13
Hz, and the sampled version is obtained by taking samples of the continuous-time signal at a
sampling period of 0.1 seconds.
Q6.2: The frequency of the sinusoidal signal is 13 Hz, and the sampling period is 0.1 seconds.
Q6.3: Running Program P6_1 for four other values of the sampling period with two lower and
two higher than that listed in the program results in the following observations:
When the sampling period is reduced, the number of samples increases, and the discrete-
time signal more closely approximates the continuous-time signal.
When the sampling period is increased, the number of samples decreases, and the discrete-
time signal becomes less accurate in representing the original continuous-time signal.
Q6.4: Running Program P6_1 with a frequency of 3 KHz and 7 KHz generates equivalent
discrete-time signals that are different from the one generated in Q6.1. The difference is due to the
fact that the Nyquist rate for these higher frequencies is greater than the sampling rate used in the
program, which leads to aliasing. Aliasing causes the higher frequency components of the signal
to be folded back into the frequency range that corresponds to lower frequencies, resulting in an
incorrect representation of the original signal.
Q6.5: Running Program P6_2 generates two plots, one for the discrete-time signal x[n] and the
other for its continuous-time equivalent ya(t). The reconstructed continuous-time signal ya(t) is
obtained by passing the discrete-time signal through an ideal low-pass filter.
Q6.6: Line 10 of Program P6_2 calculates the reconstructed continuous-time signal ya(t) by first
generating a matrix of time values t, and another matrix of sample values n. It then computes the
value of the sinc function for each combination of time and sample values, scaled by the sampling
period T, and multiplies it with the discrete-time signal xs.
Q6.7: Running Program P6_2 with frequencies of 3 KHz and 7 KHz generates equivalent
discrete-time signals that are different from the one generated in Q6.5, due to aliasing. As the
frequency of the sinusoidal signal increases, the Nyquist rate increases, and if the sampling rate is
not increased proportionally, the higher frequency components of the signal will be folded back
into lower frequency components, resulting in aliasing.
Task 2:
% Program P6_2
% Illustration of Aliasing Effect in the Time-Domain
clf;
T = 0.1;
f = 13;
n = (0:T:1)';
xs = cos(2*pi*f*n);
t = linspace(-0.5,1.5,500)';
ya = sinc((1/T)*t(:,ones(size(n))) - (1/T)*n(:,ones(size(t)))')*xs;
plot(n,xs,'o',t,ya);grid;
xlabel('Time, msec');ylabel('Amplitude');
title('Reconstructed continuous-time signal y_{a}(t)');
axis([0 1 -1.2 1.2]);
Result:
Questions:
Q6.6: The continuous-time function xa(t) in Program P6_3 is a decaying exponential function
multiplied by a linear ramp function. The expression for xa(t) is 2*t.exp(-t), where . is the element-
wise multiplication operator in MATLAB.The CTFT (continuous-time Fourier transform) of xa(t)
is computed using the freqs() function in MATLAB. The first argument of freqs() is the numerator
coefficients of the transfer function, which is 2 in this case. The second argument is the
denominator coefficients of the transfer function, which is [1 2 1] in this case. The third argument
is the frequency vector at which the CTFT is to be computed, which is wa in this program.
Q6.9: Running Program P6_3 generates the discrete-time signal x[n] and its continuous-time
equivalent xa(t), along with their respective Fourier transforms. The discrete-time signal is
obtained by sampling the continuous-time signal xa(t) at regular intervals of T=1.
There is no visible effect of aliasing in this case because the sampling rate is sufficiently high. The
continuous-time signal xa(t) is bandlimited with a maximum frequency of 2π rad/sec. The Nyquist
sampling rate is twice this frequency, which is 4π rad/sec. The sampling rate used in this program
is 1/T = 1, which is greater than 4π rad/sec. Therefore, there is no aliasing.
Q6.10: When the sampling period is increased to T = 1.5, there is a visible effect of aliasing. The
Nyquist sampling rate is still 4π rad/sec, but the sampling rate used in this program is 1/T = 2/3 *
4π, which is less than 4π rad/sec. As a result, the high-frequency components of the continuous-
time signal xa(t) are folded back into the low-frequency range and cannot be distinguished from
the low-frequency components. This is evident in the Fourier transform of the discrete-time signal,
where the high-frequency components appear in the low-frequency range.
Q6.11: To modify Program P6_3 for the case of xa(t) = e^(-pi*t^2), we need to replace the
expression for xa(t) with exp(-pi*t.^2), and replace the transfer function coefficients [1 2 1] with
[1 0 0]. This is because the transfer function of the system that generates xa(t) is simply H(jw) =
2/(jw + 2 + 1/jw), which reduces to H(jw) = 2/(jw + 1)^2 when the transfer function coefficients
are rationalized. The CTFT of xa(t) can be computed using the same method as in Program P6_3.
Running the modified program and comparing the Fourier transforms of the continuous-time and
discrete-time signals, we can see that there is significant aliasing when T=1, and increasing the
sampling period to T=1.5 only worsens the aliasing effect. This is because xa(t) is a non-
bandlimited signal, and its Fourier transform extends to infinity. Therefore, no finite sampling rate
can capture the entire signal without aliasing.
Task 3:
% Program P6_3
% Illustration of the Aliasing Effect in the Frequency Domain
clf;
t = 0:0.005:10;
xa = 2*t.*exp(-t);
subplot(2,2,1)
plot(t,xa);grid
xlabel('Time, msec');
ylabel('Amplitude');
title('Continuous-time signal x_{a}(t)');
subplot(2,2,2)
wa = 0:10/511:10;
ha = freqs(2,[1 2 1],wa);
plot(wa/(2*pi),abs(ha));grid;
xlabel('Frequency, kHz');
ylabel('Amplitude');
title('|X_{a}(j\Omega)|');
axis([0 5/pi 0 2]);
subplot(2,2,3)
T = 1;
n = 0:T:10;
xs = 2*n.*exp(-n);
k = 0:length(n)-1;
stem(k,xs);grid;
xlabel('Time index n');
ylabel('Amplitude');
title('Discrete-time signal x[n]');
subplot(2,2,4)
wd = 0:pi/255:pi;
hd = freqz(xs,1,wd);
plot(wd/(T*pi), T*abs(hd));grid;
xlabel('Frequency, kHz');
ylabel('Amplitude');
title('|X(e^{j\omega})|');
axis([0 1/T 0 2])
Result:
Q6.15: Using Program P6_5, the decimal equivalents of the binary fractions developed in
Question Q6.13 can be found as follows:
(a) Binary fraction [0 1 0 1 0 0] in sign-magnitude form:
Type in the binary fraction = [0 1 0 1 0 0]
The decimal equivalent is 0.80078125
Task 4:
% Program P6_4
% Determines the binary equivalent of a decimal number in sign-magnitude form
d = input('Type in the decimal fraction = ');
b = input('Type in the desired wordlength = ');
d1 = abs(d);
beq =[zeros(1,b)];
for k = 1:b
int = fix(2*d1);
beq(k) = int;
d1 = 2*d1 - int;
end
if sign(d) == -1;
bin = [1 beq];
else
bin = [0 beq];
end
disp('The binary equivalent is');
disp(bin)
Result:
Task 5:
% Program P6_5
% Determines the decimal equivalent of a binary number in sign-magnitude form
bin = input('Type in the binary fraction = ');
b = length(bin) - 1;
d = 0;
for k = 1:b
d = d + bin(k+1)*2^(-k);
end
if sign(bin(1)) == 0;
dec = d;
else
dec = - d;
end
disp('The decimal equivalent is');
disp(dec);
Result: