Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

LAB 4: Sampling Objectives

Download as pdf or txt
Download as pdf or txt
You are on page 1of 17

Department of Electrical & Electronic Engineering

Ahsanullah University of Science and Technology (AUST)

Student’s Information:

1. Name:

2. Student ID:

3. Email ID:

LAB 4:
Sampling
Objectives
The main objectives of this lab are:
• To understand the concept of Sampling and aliasing.
• To study the effect of different sample rates and verify Sampling Theorem
• To understand the concept of Upsampling and Downsampling

PART 1
Most signals encountered in real life are analog in nature such as speech, biological sig-
nals (ECG, PPG), communication signals etc. These signals must be processed for various
purposes. In a previous lab, it was mentioned that Digital Signal Processing of an analog
signal offers numerous advantages over analog processing such as, better accuracy, better
storing capability, cost efficiency and so on. Hence, in order to process these analog signals,
the signals must be converted into digital domain. Converting an analog signal into digital
one requires the process of sampling, quantization and encoding. In this lab, we’ll discuss
various concepts and issues involved in sampling of a continuous-time signal.

Figure. 4.1: Analog to Digital Converter

1
Sampling
Sampling is the process of converting a continuous-time signal into a discrete time one. It is
the first step in conversion of an analog signal to digital domain. One of the most popular
sampling methods is periodic sampling. In the periodic sampling operation, exact values of
the continuous-time signal at uniformly spaced discrete intervals (nTs ), where Ts is sampling
period and its reciprocal, fs = T1s is the sampling frequency, are retained.
It is convenient to represent the sampling process mathematically in the two stages. The
stages consist of multiplying (modulating) the continuous time signal by an impulse train (a
series of impulses) followed by conversion of the impulse train to a sequence. The periodic
impulse train is
X∞
s(t) = δ(t − nTs ) (1)
n=−∞

where δ(t) is the Delta function. The product of s(t) and continuous signal, xc (t) is:
xs (t) = xc (t)s(t) (2)
Figure 4.2 shows the process of sampling using impulse train modulation. Note that, xs (t) is
still, in a sense, a continuous signal which is zero, except at integer multiples of Ts . On the
other hand, x[n] is indexed on the integer variable n.

Figure. 4.2: (a) Continuous-time Signal, xc (t) (b) Impulse Train, s(t) (c) Signal, xs (t) =
xc (t)s(t) (d) Discrete-time signal obtained from sampling.

2
In general, the sampling operation is not invertible, i.e. given the output x[n], it is not
possible in general to reconstruct xc (t), the input to the sampler, since many continous-time
signals can produce the same output sequences of samples. However, if a continuous time
signal is to be uniquely represented and recovered from its samples, then the signal must be
band-limited. A band-limited signal is one whose Fourier Transform is non-zero on only a
finite interval of the frequency axis. Figure 4.3 shows a band-limited signal.

Figure. 4.2: Spectrum of a Band-limited Signal. Note that the Fourier Transform is non zero
only for frequency |f|≤B.

Further we have to realize that the samples must be sufficiently close and the Sampling Rate
must bear certain relation with the highest frequency component of the original signal. The
sampling theorem also known as Nyquist-Shannon Sampling Theorem gives us the
minimum sampling frequency for a continuous-time signal so that it can be uniquely recon-
structed from its samples. If the sampling frequency is less than this minimum value, a
phenomenon known as aliasing occurs.

Sampling Theorem:

Let xc (t) be a bandlimited signal with

Xc (f) = 0 for |f| ≥ fN .

Then xc (t) is uniquely determined by its samples x[n] = xc (nTs ), n = 0, ±1, ±2...., if
the sampling frequency

fs = ≥ 2fN .
Ts
The frequency fN is commonly referred to as the Nyquist frequency, and the frequency
2fN as the Nyquist rate.

For example, in case of a continuous time sinusoidal signal, xc (t) = cos 4000πt, signal fre-
quency is 2000Hz. So in order to reconstruct the signal uniquely from its samples, the
sampling frequency should be at least 4000Hz. Note that, fN mentioned in the sampling
theory is the maximum frequency content of the signal. Now let’s see what happens if we
sample a signal below the corresponding Nyquist rate. To discuss this, we need to consider

3
the signal in frequency domain.

Figure. 4.3: Frequency-domain representation of sampling in the time domain. (a) Spectrum
of the original signal. (b) Fourier Transform of the sampling function. (c) Fourier Transform
of the sampled signal with fs > 2fN . (d) Fourier Transform of the sampled signal with
fs < 2fN

Figure 4.3 shows the frequency-domain representation of the impulse train sampling. Figure
4.3(c) shows the sampling of xc (t) with sampling frequency fs > 2fN . Here the replicas
of Xc (f ) do not overlap and therefore, xc (t) can be recovered from xs (t) with an ideal low
pass filter with cut-off frequency (fc ) such that fN ≤ fc ≤ (fs − fN ). However, from figure
4.3(d) shows the case where the sampling frequency fs < 2fN . Here the copies of Xc (f )

4
overlap, so that when they are added together, Xc (f ) is no longer recoverable by low-pass
filtering. In this case, the reconstructed output is related to the original continuous-time
input through a distortion which is known as aliasing. So aliasing is an unwanted case of
sampling, where minimum condition for accurate sampling is not met. Physically, in aliasing,
signals of different frequencies become indistinguishable from each other. The high-frequency
components of the signal spectrum take the identity of the lower frequencies in the spectrum
of the sampled signal. Aliasing can be avoided by sampling the signal at a higher rate than
the Nyquist rate or by using an anti-aliasing filter which is essentially a low pass filter.

Sample Rate Conversion: Upsampling and Downsampling


In many practical applications of digital signal processing, one is faced with the problem of
changing the sampling rate of a signal, either increasing or decreasing it by some amount. For
example, in telecommunication system that transmit and recieve different types of signals,
there is a requirement to process these various signals at different rates according to the
bandwidth of the signals. The process of converting a signal from a given rate to a different
rate is called sample rate conversion. Increasing the sampling rate of an already sampled
signal is known as upsampling and decreasing the sampling rate is known as downsampling.

Upsampling
The purpose of upsampling is to manipulate a signal in order to artificially increase the
sampling rate to avoid aliasing. Upsampling a signal by an integral factor L means inserting
(L-1) zeros between successive sampled data of original signal. The input-output relation of
an upsampler system is given by:
 n
x L , n = 0, ±L, ±2L, . . .
y[n] = (3)
0, otherwise

Downsampling
The operation of reducing the sampling rate is called downsampling. The sampling rate of a
sequence can be reduced by sampling it i.e. by defining a new sequence:

xd [n] = x[nM ] = xc (nM Ts ).

Downsampling a signal by a factor L means that we’ll take one sample from each group of
‘L’ samples. For example, y[n] = x[2n] represents downsampling x[n] by a factor of 2. Here
we’ll discard the odd-numbered samples in x[n] and retain the even-numbered samples. In
other words, we’ll keep one sample from every successive two samples. Figure 4.4 shows the
representation of an upsampler and downsampler.

5
Figure. 4.4: (a) General System of Upsampling a signal by L (b) General system for down-
sampling a signal by M

PRELAB TASKS
• Given a continuous time signal xc (t) = sin (2000πt) + cos (4000πt) + sin (5000πt),
what is the minimum sampling frequency in Hz that satisfies Nyquist Criteria.

• If a signal xc (t) = cos (8000πt) is sampled at a frequency 2000Hz, draw the


spectrum of the i) original signal and ii) sampled signal. Write down the frequency
clearly in the spectrum.

• Let x[n] = {1,1,3,4,5,6,8,1,1,3,4} be a discrete-time version of some continuous



signal. Sketch the signal x[2n] and x[ n3 ].

6
Part 2
Sampling using MATLAB/OCTAVE

• Sampling and aliasing in MATLAB/OCTAVE

• Upsampling and Downsampling in MATLAB/OCTAVE

• Reconstruction of a sampled signal in MATLAB/OCTAVE

• Functions to use: stem(), plot(), subplot()

Instructions
• Organizing files and folder properly for later use.

• Make a unique named folder for this lab like EEE3218_SP20_A1_180105001


under any drive other than C drive.

• For every week make a subfolder to keep the all the tasks in a specific week
separated from other tasks.

• Always try to work in the folder specifically created for the current week.

• Follow MATLAB/OCTAVE naming convention for giving a meaningful name


before saving it in MATLAB/OCTAVE.

• While running a code be judicious in choosing add to path or change directory.


Use add to path when you are using a file from different folder.

In this part of our lab, we’ll dive into the concept and issues of sampling and aliasing using
MATLAB/OCTAVE. Then we’ll show upsampling and downsampling of a signal. Finally
we’ll show how to reconstruct a signal from its samples. Let us consider a continuous-time
signal xc (t) = 10 cos (200πt). Frequency of the signal is F0 = 100 Hz and time period is
T0 = 1001
= 0.01s. So the corresponding nyquist rate is 200Hz. At first, we’ll sample the
signal at a rate of 800Hz frequency which is much higher than nyquist rate. From figure 4.5,
it is seen that the original signal can be uniquely identified from its samples. The code for
this is given below:
Listing 1: Sampling a continuous time signal with sampling frequency 800Hz
1 clc;
2 clear all;

7
3 close all;
4 % We'll sample a signal at a frequency 500 Hz
5 % xc = 10*cos(200*pi*t)
6 % Signal Frequency, Fo = 100 and Signal Period, To = 1/100
7 % Sampling Frequency, Fs = 500 and Sampling Period, Ts = 1/500
8 Fo = 100; % Signal Frequency
9 To = 1/Fo; % Signal Period
10 Fs = 500; % Sampling Frequency which is greater than Nyquist Rate
11 Ts = 1/Fs;
12
13 t = 0:To/100:3*To; % Time axis for continuous signal
14 xc = 10*cos(200*pi*t); % continuous signal
15
16 t1 = 0:Ts:3*To; % Sampled time axis
17 xs = 10*cos(200*pi*t1); % Sampled Signal
18
19 subplot(211)
20 plot(t, xc, 'Linewidth', 2); % plotting continuous−time signal
21 hold on
22 stem(t1, xs, 'Linewidth', 2); % showing the sampled values in the same plot
23 subplot(212)
24 stem(t1, xs, 'Linewidth', 2); % showing the sampled values in a separate plot

10

-5

-10
0 0.005 0.01 0.015 0.02 0.025 0.03

10

-5

-10
0 0.005 0.01 0.015 0.02 0.025 0.03

Figure. 4.5: (a) Continuous-time signal and its samples obtained by sampling at a rate of
800Hz (b) Samples of the continuous-time signal in (a)

8
From the samples of signal xc (t) the discrete time signal x[n] can be obtained by dividing the
time axis of the sampled signal i.e. figure 4.5(b) by the sampling period Ts .

Now let’s sample the signal xc (t) at a sampling frequency equal to the nyquist rate i.e. 200Hz
and the corresponding output is shown in figure 4.6. It is seen from the figure that we can
recognize the signal from its samples although this is more difficult than the previous case
where the sampling rate is higher than this. However, the trend in the signal’s behaviour can
be recognized from its samples. In fact, the samples only capture the extremes of each period
of the cosine oscillation. This is the significance of of twice the highest frequency of
the signal value for sampling frequency. We can say that, the samples are just enough to
capture the signal’s oscillation if it is sampled exactly at the nyquist rate.
Listing 2: Sampling a continuous time signal with sampling frequency 200Hz
1 clc;
2 clear all;
3 close all;
4
5 % We'll sample a signal at a frequency 500 Hz
6 % xc = 10*cos(200*pi*t)
7 % Signal Frequency, Fo = 100 and Signal Period, To = 1/100
8 % Sampling Frequency, Fs = 500 and Sampling Period, Ts = 1/500
9
10 Fo = 100; % Signal Frequency
11 To = 1/Fo; % Signal Period
12 Fs = 200; % Sampling Frequency which is greater than Nyquist Rate
13 Ts = 1/Fs;
14
15 t = 0:To/100:3*To; % Time axis for continuous signal
16 xc = 10*cos(200*pi*t); % continuous signal
17
18 t1 = 0:Ts:3*To; % Sampled time axis
19 xs = 10*cos(200*pi*t1); % Sampled Signal
20
21 subplot(211)
22 plot(t, xc, 'Linewidth', 2); % plotting continuous−time signal
23 hold on
24
25 stem(t1, xs, 'Linewidth', 2); % showing the sampled values in the same plot
26
27 subplot(212)
28 stem(t1, xs, 'Linewidth', 2); % showing the sampled values in a separate plot

9
10

-5

-10
0 0.005 0.01 0.015 0.02 0.025 0.03

10

-5

-10
0 0.005 0.01 0.015 0.02 0.025 0.03

Figure. 4.6: (a) Continuous-time signal and its sample obtained by sampling at a rate of 200
Hz (b) Samples of the continuous-time signal in (a)

Finally we’ll sample the signal at a frequency lower than the nyquist rate and we’ll observe
aliasing. If the sample frequency drops too low, the samples seem to come from a different
signal. For example, if the signal xc (t) = 10 cos (200πt) is sampled at a frequency 150Hz, the
resulting discrete time signal is
x[n] = x(nTs ) = 10 cos(200πnTs )
 1 
= 10 cos 200πn
150
 4πn   2πn   n
= 10 cos = 10 cos = 10 cos 2π
3 3 3
So the sampled signal will be an alias of another signal xac (t) = 10 cos(100πt). Let us verify
the result from MATLAB Figures.
Listing 3: Aliasing occurs when the signal is sampled below the Nyquist Rate
1 clc;
2 clear all;
3 close all;
4
5 % We'll sample a signal at a frequency 500 Hz
6 % xc = 10*cos(200*pi*t)
7 % Signal Frequency, Fo = 100 and Signal Period, To = 1/100
8 % Sampling Frequency, Fs = 500 and Sampling Period, Ts = 1/500
9
10 Fo = 100; % Signal Frequency

10
11 To = 1/Fo; % Signal Period
12 Fo1 = 50; % Aliased Frequency
13 To1 = 1/Fo1; % Aliased signal period
14 Fs = 150; % Sampling Frequency which is greater than Nyquist Rate
15 Ts = 1/Fs;
16
17 %% continuous signal
18 t = 0:To/100:3*To; % Time axis for continuous signal
19 xc = 10*cos(200*pi*t); % continuous signal
20
21 %% sampled signal
22 t1 = 0:Ts:3*To; % Sampled time axis
23 xs = 10*cos(200*pi*t1); % Sampled Signal
24
25 %% aliased continuous signal
26 t2 = 0:To1/100:3*To; % Time axis for aliased signal
27 xc2 = 10*cos(100*pi*t2); % Aliased Signal
28
29 %% plotting all the signals together
30 plot(t, xc, 'Linewidth', 2); % plotting continuous−time signal
31 hold on
32 plot(t2, xc2,'g', 'Linewidth', 2); % plotting the aliased signal
33 stem(t1, xs, 'r','filled', 'Linewidth', 2); % showing the sampled values in
the same plot

10

-2

-4

-6

-8

-10
0 0.005 0.01 0.015 0.02 0.025 0.03

Figure. 4.7: Both the signal xc (t) = 10 cos (200πt) and xc2 (t) = 10 ∗ cos(100πt) go through
the samples when xc (t) is sampled below the Nyquist Rate. So xc (t) and xc2 are alias signals.

11
So we see that when a signal is sampled below the Nyquist rate, it cannot be reconstructed
uniquely as there exists another signal with those samples.
Now we’ll write two functions that will perform upsampling by a factor L and downsampling
by a factor M on an already sampled signal.
Listing 4: Function to perform upsampling
1 function [xup, nup] = upsampling(x, n, L)
2
3 % L is the upsampling factor
4 % x is the original sampled signal
5 % n is the original index vector
6
7 nup = n(1)*L:n(end)*L; % Upsampled time index
8 xup = zeros(1, length(nup)); % initializing the upsampled signal
9 xup(1:L:length(nup)) = x; % Insert L−1 zeros between two successive samples.
10
11 end

Listing 5: Function to perform downsampling


1 function [xdown, ndown] = downsampling(x, n, M)
2
3 % M downsampling factor
4 % x input sampled signal
5 % n is the index vector of original sampled signal
6
7 pos = (find(mod(n,M) == 0)); % Finding the position of the index which is
8 ... the multiple of M
9 xdown = x(pos); % taking samples at the indices which is a multiple of M
10 ... i.e. take one sample from every M samples.
11 ndown = n(pos)/M; % downsampled time index vector
12 end

] Let’s consider an already sampled discrete signal x[n] = sin (0.7n) defined for −10 ≤ n ≤ 10.
We’ll upsample x[n] by a factor of 3 and downsample it by a factor of 2. Let’s see what
happens.
Listing 6: Upsample a signal by a factor of 3 and downsample it by a factor of 2
1 close all;
2 clear all;
3 clc;
4
5 n=−10:10; % index vector of original signal
6 x=sin(0.7*n); % original samples
7 subplot(311),
8 stem(n,x, 'Linewidth', 2);

12
9 title('Original Signal')
10
11 %% Up−sampling
12 L=3; %Scaling factor
13 [xup, nup] = upsampling(x, n, L);
14 subplot(312)
15 stem(nup,xup, 'Linewidth',2);
16 title(['Up−sampled Signal by ',num2str(L)]);
17
18 %% Down−sampling
19 M = 2;
20 [xdown, ndown] = downsampling(x,n,M);
21 subplot(313)
22 stem(ndown, xdown, 'Linewidth', 2);
23 title(['Down−sampled Signal by ',num2str(M)]);

Figure 4.8 shows the output of this code.

Original Signal
1

-1
-10 -8 -6 -4 -2 0 2 4 6 8 10
Up-sampled Signal by 3
1

-1
-30 -20 -10 0 10 20 30
Down-sampled Signal by 2
1

-1
-5 -4 -3 -2 -1 0 1 2 3 4 5

Figure. 4.8: (a) Original Sample (b) Upsampled signal by a factor of 3 (c) Downsampled
signal by a factor of 2.

From figure 4.8 we see that when the signal is upsampled by a factor of 3, two zeros are
inserted between successive samples and the total number of samples gets increased. On the

13
contrary, when the signal is downsampled by a factor of 2, one sample from two successive
samples are taken and shown in 4.8(c) i.e. the original sampling frequency is halved. In other
words, here we’ve taken only the even numbered samples and discarded the odd-numbered
ones. This is how the rate of sampling can be converted by upsampling or downsampling.
For the last topic of our lab week 4, we’ll see how to reconstruct a signal from its samples.
For this, we need a bit of theoretical background. In order to reconstruct a signal from its
samples, we need to pass the samples through a low pass filter whose cut-off frequency(fc )
satisfies the condition: fN ≤ fc ≤ (fs − fN ). In time domain this implies multiplying the
samples by an interpolation function or sinc function. In other word, if we consider low pass
filter (LPF) a system, the output of the system will be the convolution sum of the samples
and the impulse response (hr (t)) of the LPF. hr (t) for an LPF with cutoff frequency Tπs is
given by:  
sin Tπts
hr (t) = πt (4)
Ts

This impulse response is shown in figure (4.9).

0.8

0.6
Amplitude

0.4

0.2

-0.2

-0.4
0 0.02 0.04 0.06 0.08 0.1 0.12 0.14 0.16 0.18
Time [sec]

Figure. 4.9: Impulse Response of an ideal Low Pass Filter.

The final reconstructed signal xr (t) which is the convolution sum of x[n] and hr (t) is given

14
by the formula:  
∞ π(t−nTs )
X sin Ts
xr (t) = x[n] π(t−nTs )
(5)
n=−∞ Ts

We’ll use equation (5) to reconstruct a signal from its samples. Here we’re considering
sampling frequency higher than the nyquist rate so that we can properly reconstruct the
original signal.
Listing 7: Reconstruction of a signal from its samples
1 clc;
2 close all;
3 clear all;
4
5
6 % First we'll sample the signal at a higher rate than
7 % Nyquist Rate
8 % xc(t) = 5*cos(200*pi*t)
9 % Signal Frequency, Fo = 100 and period, To = 1/100
10 % Sampling Frequency, Fs = 800 and period, Ts = 1/800
11
12 Fo = 100; % signal frequency
13 To = 1/Fo; % signal period
14 Fs = 800; % sampling frequency
15 Ts = 1/Fs; % sampling period
16
17 tc = 0:To/100:3*To; % defining time axis for xc upto 3 cycles
18 xc = 5*cos(200*pi*tc); % constructing the continuous−time signal
19
20 ts = 0:Ts:3*To; % sampled the time axis a rate Fs
21 xs = 5*cos(200*pi*ts); % sampled signal at a frequency Fs
22 N = length(ts); % number of samples
23
24 %% Reconstruction by using the formula:
25 % xr(t) = sum over n = 0...., N−1 : x(nT)*sin(pi*(t−nT)/T)/(pi*(t−nT)/T)
26 % Here sin(pi*(t−nT)/T)/(pi*(t−nT)/T) = sinc((pi*(t−nT))/T)
27
28 xr = zeros(1, length(tc)); % initialize the reconstructed signal which is the
29 ... same length as
continuous−time
axis
30 sinc fun = zeros(N, length(tc)); % initializing the interpolation function or
_
sinc function
31
32 for t = 1:length(tc)
33 for n = 0:N−1

15
34 % sinc function. This line is required if you want to visualize
the sinc function.
35 %sinc_fun(n+1,:) = sin(pi*(tc−n*Ts)/Ts)./(pi*(tc−n*Ts)/Ts);
36 %this line computes the convolution of xr and sinc_fun
37 xr(t) = xr(t) + xs(n+1)*sin(pi*(tc(t)−n*Ts)/Ts)/(pi*(tc(t)−n*Ts)/
Ts);
38
39 end

40 end
41
42
43 %% plot the results
44
45 plot(tc, xc);
46 hold on
47 stem(ts, xs);
48 hold on
49 plot(tc, xr);
50
51 %% Reconstruction Process Visualization
52
53 % This portion is not mandatory for reconstructing a signal
54 % This is just for visualization purpose
55 % To run this segment, you must uncomment the sinc_fun line
56
57 % figure(2)
58 % hold on
59 % grid on
60 % plot(tc, xs.'.*sinc_fun);
61 % stem(ts, xs);
62 % title('Reconstruction Process');
63 % xlabel('Time(s)');
64 % ylabel('Amplitude');

If you run the above code, you can see the original signal, its samples and reconstructed
signal in a single plot.
In summary,

• A signal is to be sampled at a frequency higher than the corresponding Nyquist Rate


for successful reconstruction.

• Upsampling and Downsampling change the sampling rate of an already sampled signal.

• A Low Pass Filter is used to reconstruct the original signal from its samples.

16
POST LAB TASKS
1. Consider x(t) = 10 cos (120πt) + 5 sin (100πt + 30°) + 4 sin (150πt + 45°).
Sample the signal for i) Fs = 4 ∗ Fo , ii) Fs = 2 ∗ Fo and iii) Fs = F0 where
Fo is the maximum frequency content of x(t). Show the original signal and its
samples in one figure for 3 cycles.

2. Consider a signal x(t) = 10 cos (250πt + 60°) + 5 sin (200πt + 75°). Convert
the signal into a sequence x[n] for −10 ≤ n ≤ 10 by choosing an appropriate
sampling frequency. Upsample x[n] by a factor of 3 and downsample the signal
by a factor of 4. Show i) original signal, ii) sampled signal, iii) discrete signal,
iv) upsampled signal and v) final output.

3. Consider x(t) = 21 sin(14πt) + 13 sin(18πt) + 51 sin(24πt) + 71 sin(30πt). Sample


the signal on the interval 0 ≤ t ≤ 2 at i) Fs = 5Fo and ii) Fs = 1.5Fo . Where Fo
is the maximum frequency content of x(t). Reconstruct the signal in each case
from its samples. What is the alias signal’s time domain expression in case (ii).

4. There is another way to reconstruct the original signal by using interpolation


method. MATLAB has a built-in function interp1() for this purpose. Write a
MATLAB Code using this function to reconstruct the signal of problem 3.

17

You might also like