0% found this document useful (0 votes)
21 views18 pages

DSP Lab 3

This document describes an experiment on sampling, reconstruction and aliasing. It contains tasks to sample a voice signal using MATLAB at different sampling rates, play it back, and illustrate the effect of sampling frequency on signal reconstruction. Reconstructing with a sampling frequency equal to or higher than twice the highest frequency ensures accurate recovery of the original signal without aliasing.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views18 pages

DSP Lab 3

This document describes an experiment on sampling, reconstruction and aliasing. It contains tasks to sample a voice signal using MATLAB at different sampling rates, play it back, and illustrate the effect of sampling frequency on signal reconstruction. Reconstructing with a sampling frequency equal to or higher than twice the highest frequency ensures accurate recovery of the original signal without aliasing.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 18

Name Muhammad Irfan, Muhammad Basit

Reg. # 2020-EE-385, 2020-EE-401

University of Engineering & Technology Lahore


Experiment # 3
Title: Introduction to Sampling, Reconstruction and Aliasing.
Equipment Required: Personal computer (PC) with windows operating system and MATLAB
software.
Theory:
The course discusses discrete-time signal processing (DSP) in general that can be applied to any
discrete-time sequences. Since MATLAB works on a digital system, we can only simulate the
sampling of a continuous signal to produce a discrete time signal (i.e., we must approximate the
continuous signal by a discrete time signal obtained with a high sampling rate). The same comment
applies for A/D conversion and D/A conversions. But visual and audio representations of the
aliasing are still possible.
In this lab, we will learn how to convert speech signal into discrete-time sequences using analog to-
digital converter (ADC) available in the sound cards of every computer. A sound card can be
regarded as a combination of an analog-to-digital converter (ADC) and a digital-to-analog converter
(DAC). Any sound card has at least the following inputs:
 Line-In
 Mic Input
 Speaker Output
Your sound card’s ADC samples the signal coming from Line-In and Mic Inputs, whereas the DAC
converts samples to an analog signal and sends it to playback devices such as your laptop’s speaker.
In this lab, we will learn how to sample a speech signal using MATLAB.
Task 1
Read the following webpage and write a MATLAB script to sample your own voice at 11025
samples per second and 8-bit width samples.
https://www.mathworks.com/help/matlab/ref/audiorecorder.html
MATLAB Script:
clc;
clear;
close all;
Fs = 11025 ;
nBits = 8 ;
nChannels = 1 ;
ID = -1;
x = input('Enter the recording time: ');
dt = 1/Fs;
StopTime = x;
t = (0:dt:StopTime-dt)';

recObj = audiorecorder(Fs,nBits,nChannels,ID);
disp('Recording your Voice')
recordblocking(recObj, x);
disp('End Recording');
play(recObj);
Input_Signal=(getaudiodata(recObj))';
plot(t,Input_Signal);
xlabel('Time (s)');
ylabel('Amplitude');
title('Recorded Microphone Voice Signal');

filename = 'inputsignal.wav';
audiowrite(filename, Input_Signal, Fs);

Output:

Task 2
Play the recorded voice on your computer’s speakers using sound command in MATLAB at a DAC
playback rate of 8000 samples per second with 8 bits per sample.
https://www.mathworks.com/help/matlab/ref/sound.html
MATLAB Script:
clc;
clear;
close all;
[y,Fs] = audioread('inputsignal.wav');
disp('Recorded Voice @ DAC playback rate of 11025 samples per second with 8
bits per sample');
sound(y,11025,8);
pause(10);
disp('Recorded Voice @ DAC playback rate of 8000 samples per second with 8 bits
per sample');
sound(y,8000,8);
plot(y)
xlabel('Time');
ylabel('Amplitude');
title('Recorded Voice Signal');

Output:

Questions
1. Do you hear the same voice as yours? If not, why the voice played back on the speaker
is different?
We heard the voice and observed that the voice was not the same. The reason was that the
sampling rate was different. We sampled voice at DAC playback rate of 11025 samples per
second with 8 bits per sample and was playing at DAC playback rate of 8000 samples per
second with 8 bits per sample. The possible reason is that noise and distortion was also added
in my voice and played at different rate so played back voice was not same.
Task 3
a) x(t) = sin(2(π/3)f t ) sampled at a frequency of fs produces the discrete time signal and
reconstruct it by using zero order hold method. By varying the value of fs, it is possible to
illustrate the effect of fs in DAC.
b) Vary the frequency fs and plot four different plots. Use subplot to put four plots on one
screen. Explain why these plots are different to each other.
c) Can you predict the best fs to reconstruct the signal?
d) Write Zero order hold method drawbacks? Mention any other method to reconstruct the
signal on MATLAB?
Script Help
f = ; % signal frequency
t = -(1/f):1/(4*f):(1/f);
sig = sin(2*(pi/3)*f*t);
fs = ; % sampling frequency
t_full = -(1/f):1/(4*fs):1/f;
sig_full = sin(2*(pi/3)*(f)*t_full);
subplot(311); plot(t,sig,'b-','LineWidth',1); ylabel('Amplitude'); title('Signal to sample')
hold on
plot(t_full,sig_full,'rx','LineWidth',2,'MarkerSize',10); ylabel('Amplitude'); %title('Discrete
time points kept')
%%% RECONSTRUCTION%%%
subplot(312); plot(t_full,sig_full,'rx','LineWidth',2,'MarkerSize',10); title('The sample
points with a flat line (zero order) drawn between them'); hold on
subplot(312); stairs(t_full,sig_full,'b','LineWidth',2); ylabel('Amplitude')
subplot(313); stairs(t_full,sig_full,'b','LineWidth',2); ylabel('Amplitude'); title('The
reconstructed signal with the sample points removed')

Part a)
The discrete time signal produced by sampling the continuous time signal x(t) = sin(2(π/3)f t ) at a
frequency fs is a series of discrete values that represent the amplitude of the continuous time signal
at evenly spaced intervals. The zero-order hold method involves holding the last sample value until
the next sample is available.
MATLAB Script:
clc;
clear;
close all;
f = 100 ; % signal frequency
t = -(1/f):1/(4*f):(1/f);
sig = sin(2*(pi/3)*f*t); % continus
fs = 900; % sampling frequency
t_full = -(1/f):1/(4*fs):1/f; % discrete
sig_full = sin(2*(pi/3)*(f)*t_full);
subplot(3,1,1);
plot(t,sig,'b-','LineWidth',1);
ylabel('Amplitude');
title('Signal to sample f = 100 & fs = 900')
grid on;
hold on
plot(t_full,sig_full,'rx','LineWidth',2,'MarkerSize',10);
ylabel('Amplitude');
%%% RECONSTRUCTION%%%
subplot(3,1,2);
plot(t_full,sig_full,'rx','LineWidth',2,'MarkerSize',10);
title('The sample points with a flat line (zero order) drawn between them');
grid on;
hold on
subplot(3,1,2);
stairs(t_full,sig_full,'b','LineWidth',2);
ylabel('Amplitude')
grid on;
subplot(3,1,3);
stairs(t_full,sig_full,'b','LineWidth',2);
ylabel('Amplitude');
title('The reconstructed signal with the sample points removed')
grid on;

Output:

Effect of fs in DAC:
The sampling frequency has a significant impact on the quality of the sampled signal. If the
sampling frequency is too low, it will result in loss of information and aliasing, where high
frequency components of the original signal are misinterpreted as lower frequency components. On
the other hand, if the sampling frequency is too high, it leads to an unnecessary increase in the data
size and computational requirements. The optimal sampling frequency is equal to the Nyquist rate,
which is twice the highest frequency component present in the signal. Using a sampling frequency
equal to or higher than the Nyquist rate ensures that all the information in the original signal is
captured and reconstructed accurately.
Part b)
MATLAB Script:
clc;
clear;
close all;
%frequency # 1 f = 100, fs = 100
f = 100 ; % signal frequency
t = -(1/f):1/(4*f):(1/f);
sig = sin(2*(pi/3)*f*t); % continus
fs = 100; % sampling frequency
t_full = -(1/f):1/(4*fs):1/f; % discrete
sig_full = sin(2*(pi/3)*(f)*t_full);
figure(1);
subplot(3,2,1);
plot(t,sig,'b-','LineWidth',1);
ylabel('Amplitude');
title('Signal to sample f = 100 & fs = 100')
grid on;
hold on
plot(t_full,sig_full,'rx','LineWidth',2,'MarkerSize',10);
ylabel('Amplitude');
%%% RECONSTRUCTION%%%
subplot(3,2,3);
plot(t_full,sig_full,'rx','LineWidth',2,'MarkerSize',10);
title('The sample points with a flat line (zero order) drawn between them');
grid on;
hold on
subplot(3,2,3);
stairs(t_full,sig_full,'b','LineWidth',2);
ylabel('Amplitude')
grid on;
subplot(3,2,5);
stairs(t_full,sig_full,'b','LineWidth',2);
ylabel('Amplitude');
title('The reconstructed signal with the sample points removed')
grid on;

%frequency # 2 f = 100, fs = 200


f = 100 ; % signal frequency
t = -(1/f):1/(4*f):(1/f);
sig = sin(2*(pi/3)*f*t); % continus
fs = 200; % sampling frequency
t_full = -(1/f):1/(4*fs):1/f; % discrete
sig_full = sin(2*(pi/3)*(f)*t_full);
figure(1);
subplot(3,2,2);
plot(t,sig,'b-','LineWidth',1);
ylabel('Amplitude');
title('Signal to sample f = 100 & fs = 200')
grid on;
hold on
plot(t_full,sig_full,'rx','LineWidth',2,'MarkerSize',10);
ylabel('Amplitude');
%%% RECONSTRUCTION%%%
subplot(3,2,4);
plot(t_full,sig_full,'rx','LineWidth',2,'MarkerSize',10);
title('The sample points with a flat line (zero order) drawn between them');
grid on;
hold on
subplot(3,2,4);
stairs(t_full,sig_full,'b','LineWidth',2);
ylabel('Amplitude')
grid on;
subplot(3,2,6);
stairs(t_full,sig_full,'b','LineWidth',2);
ylabel('Amplitude');
title('The reconstructed signal with the sample points removed')
grid on;

%frequency # 3 f = 100, fs = 400


f = 100 ; % signal frequency
t = -(1/f):1/(4*f):(1/f);
sig = sin(2*(pi/3)*f*t); % continus
fs = 400; % sampling frequency
t_full = -(1/f):1/(4*fs):1/f; % discrete
sig_full = sin(2*(pi/3)*(f)*t_full);
figure(2);
subplot(3,2,1);
plot(t,sig,'b-','LineWidth',1);
ylabel('Amplitude');
title('Signal to sample f = 100 & fs = 400')
grid on;
hold on
plot(t_full,sig_full,'rx','LineWidth',2,'MarkerSize',10);
ylabel('Amplitude');
%%% RECONSTRUCTION%%%
subplot(3,2,3);
plot(t_full,sig_full,'rx','LineWidth',2,'MarkerSize',10);
title('The sample points with a flat line (zero order) drawn between them');
grid on;
hold on
subplot(3,2,3);
stairs(t_full,sig_full,'b','LineWidth',2);
ylabel('Amplitude')
grid on;
subplot(3,2,5);
stairs(t_full,sig_full,'b','LineWidth',2);
ylabel('Amplitude');
title('The reconstructed signal with the sample points removed')
grid on;

%frequency # 4 f = 100, fs = 600


f = 100 ; % signal frequency
t = -(1/f):1/(4*f):(1/f);
sig = sin(2*(pi/3)*f*t); % continus
fs = 600; % sampling frequency
t_full = -(1/f):1/(4*fs):1/f; % discrete
sig_full = sin(2*(pi/3)*(f)*t_full);
figure(2);
subplot(3,2,2);
plot(t,sig,'b-','LineWidth',1);
ylabel('Amplitude');
title('Signal to sample f = 100 & fs = 600')
grid on;
hold on
plot(t_full,sig_full,'rx','LineWidth',2,'MarkerSize',10);
ylabel('Amplitude');
%%% RECONSTRUCTION%%%
subplot(3,2,4);
plot(t_full,sig_full,'rx','LineWidth',2,'MarkerSize',10);
title('The sample points with a flat line (zero order) drawn between them');
grid on;
hold on
subplot(3,2,4);
stairs(t_full,sig_full,'b','LineWidth',2);
ylabel('Amplitude')
grid on;
subplot(3,2,6);
stairs(t_full,sig_full,'b','LineWidth',2);
ylabel('Amplitude');
title('The reconstructed signal with the sample points removed')
grid on;

Output:
figure(1)

figure(2)

Explain why these plots are different to each other.


The plots of the reconstructed signals will be different from each other based on the frequency fs.
As the value of fs is increased, the number of samples per unit time also increases. Hence, the
reconstructed signal will better approximate the continuous time signal. On the other hand,
increasing fs beyond a certain value leads to the phenomenon of aliasing, which results in the
reconstructed signal being distorted.
Part c)
Can you predict the best fs to reconstruct the signal?
The best value of fs to reconstruct the signal is equal to 2f, where f is the highest frequency
component present in the signal. This value is known as the Nyquist rate.
The Nyquist rate is given by the formula:
fs = 2f, where f is the highest frequency component present in the signal.
The Nyquist rate states that the sampling frequency must be at least twice the highest frequency
component in the signal in order to avoid aliasing and accurately represent the original signal in the
sampled form.
Part d)
Zero order hold method drawbacks:
The zero-order hold method has several drawbacks, including the sudden changes in the
reconstructed signal and high frequency components that lead to overshoots and undershoots.
Another method to reconstruct the signal is using a first-order hold method, where the slope
between two consecutive samples is used to generate the intermediate values. Another popular
method is using a low-pass filter to smooth out the reconstructed signal.
Method to reconstruct the signal on MATLAB:
There are several methods to reconstruct a signal after sampling in MATLAB, including:
1. Zero-order hold method: In this method, the last sample value is held until the next sample
is available. This results in a staircase-like waveform that approximates the original signal.
2. First-order hold method: This method uses the slope between two consecutive samples to
generate the intermediate values. It results in a smoother reconstructed signal compared to
the zero-order hold method.
3. Interpolation: In this method, an interpolation function is used to generate intermediate
values between the samples. Popular interpolation functions include linear interpolation,
cubic spline interpolation, and sinc interpolation.
4. Low-pass filtering: A low-pass filter can be used to remove high-frequency components
from the reconstructed signal and smooth out the waveform.
These methods can be implemented in MATLAB using various functions, including interp1,
interp2, and sinc. The choice of method depends on the specific requirements and constraints of
the application.
Task 4
Solve Example 1.4.2 by J.G. Proakis and D.G. Manolakis, Digital Signal Processing Principles,
Algorithms, and Applications.
Use subplot to put all plots on one screen.
Why part (d) plot is not similar to original signal?
Example 1.4.2
Consider the analog signal
x a ( t )=3 cos 100 π t
a) Determine the minimum sampling rate required to avoid aliasing.
b) Suppose that the signal is sampled at the rate Fs = 200 Hz. What is the discrete-time signal
obtained after sampling?
c) Suppose that the signal is sampled at the rate Fs = 75 Hz. What is the discrete-time signal
obtained after sampling?
d) What is the frequency 0 < F < Fs / 2? of a sinusoid that yields samples identical to those
obtained in part (c)?
Part a)
The minimum sampling rate to avoid aliasing is twice the highest frequency component in the signal
being sampled. This is known as the Nyquist frequency, and it states that to accurately reconstruct
the original signal from the sampled signal, the sampling rate must be at least twice the highest
frequency present in the signal.
The frequency of the analog signal is F = 50 Hz. Hence the minimum sampling rate required to
avoid aliasing is Fs = 100 Hz.
MATLAB Script:
clc;
clear;
close all;
% Define the original continuous-time signal
F = 50;
t = 0:0.001:0.2;
x = 3*cos(2*F*pi*t);

% Sample the signal at regular intervals


Fs = 100; % Sampling frequency
Ts = 1/Fs; % Sampling interval
t_s = 0:Ts:0.2; % Sampled time points
x_s = 3*cos(2*F*pi*t_s); % Sampled values

% Plot the original and sampled signals


subplot(3,1,1);
plot(t,x,LineWidth=2);
xlabel('Time');
ylabel('Amplitude');
legend('Original');
title('Original Signal');
grid on;

subplot(3,1,2);
stem(t_s,x_s,'filled',LineWidth=2);
xlabel('Time');
ylabel('Amplitude');
legend('Sampled');
title('Sampled Signal');
grid on;

% Interpolate the sampled signal to reconstruct the original


x_r = interp1(t_s,x_s,t,'spline');

% Plot the reconstructed signal


subplot(3,1,3);
plot(t,x_r,LineWidth=2);
xlabel('Time');
ylabel('Amplitude');
legend('Reconstructed');
title('Reconstructed Signal');
grid on;

Output:

Part b)
MATLAB Script:
clc;
clear;
close all;
% Define the original continuous-time signal
F = 50;
t = 0:0.001:0.2;
x = 3*cos(2*F*pi*t);

% Sample the signal at regular intervals


Fs = 200; % Sampling frequency
Ts = 1/Fs; % Sampling interval
t_s = 0:Ts:0.2; % Sampled time points
x_s = 3*cos(2*F*pi*t_s); % Sampled values
% Plot the original and sampled signals
subplot(3,1,1);
plot(t,x,LineWidth=2);
xlabel('Time');
ylabel('Amplitude');
legend('Original');
title('Original Signal');
grid on;

subplot(3,1,2);
stem(t_s,x_s,'filled',LineWidth=2);
xlabel('Time');
ylabel('Amplitude');
legend('Sampled');
title('Sampled Signal');
grid on;

% Interpolate the sampled signal to reconstruct the original


x_r = interp1(t_s,x_s,t,'spline');

% Plot the reconstructed signal


subplot(3,1,3);
plot(t,x_r,LineWidth=2);
xlabel('Time');
ylabel('Amplitude');
legend('Reconstructed');
title('Reconstructed Signal');
grid on;

Output:
Part c)
MATLAB Script:
clc;
clear;
close all;
% Define the original continuous-time signal
F=50;
t = 0:0.001:0.2;
x = 3*cos(2*F*pi*t);

% Sample the signal at regular intervals


Fs = 75; % Sampling frequency
Ts = 1/Fs; % Sampling interval
t_s = 0:Ts:0.2; % Sampled time points
x_s = 3*cos(2*F*pi*t_s); % Sampled values

% Plot the original and sampled signals


subplot(3,1,1);
plot(t,x,LineWidth=2);
xlabel('Time');
ylabel('Amplitude');
legend('Original');
title('Original Signal');
grid on;

subplot(3,1,2);
stem(t_s,x_s,'filled',LineWidth=2);
xlabel('Time');
ylabel('Amplitude');
legend('Sampled');
title('Sampled Signal');
grid on;

% Interpolate the sampled signal to reconstruct the original


x_r = interp1(t_s,x_s,t,'spline');

% Plot the reconstructed signal


subplot(3,1,3);
plot(t,x_r,LineWidth=2);
xlabel('Time');
ylabel('Amplitude');
legend('Reconstructed');
title('Reconstructed Signal');
grid on;

Output:

Part d)
MATLAB Script:
clc;
clear;
close all;
% Define the original continuous-time signal
F=50;
t = 0:0.001:0.2;
x = 3*cos(2*F*pi*t);

% Sample the signal at regular intervals


Fs = 75; % Sampling frequency
Ts = 1/Fs; % Sampling interval
t_s = 0:Ts:0.2; % Sampled time points
x_s = 3*cos(2*F*pi*t_s); % Sampled values

% Plot the original and sampled signals


subplot(3,1,1);
plot(t,x,LineWidth=2);
xlabel('Time');
ylabel('Amplitude');
legend('Original');
title('Original Signal');
grid on;

subplot(3,1,2);
stem(t_s,x_s,'filled',LineWidth=2);
xlabel('Time');
ylabel('Amplitude');
legend('Sampled');
title('Sampled Signal');
grid on;

% Interpolate the sampled signal to reconstruct the original


x_r = interp1(t_s,x_s,t,'spline');

% Plot the reconstructed signal


subplot(3,1,3);
plot(t,x_r,LineWidth=2);
xlabel('Time');
ylabel('Amplitude');
legend('Reconstructed');
title('Reconstructed Signal');
grid on;

Output:

Frequency:
1
Frequency=
Time Period
For original signal x a ( t )=3 cos 100 π t
T =0.02 s
1 1
F= = =50 Hz
T 0.02 s
For reconstructed signal x a ( t )=3 cos 50 π t
T =0.04 s
1 1
F= = =25 Hz
T 0.04 s
So the frequency of a sinusoid that yields samples identical to those obtained in part (c) will be
25Hz
and the sinusoid will be x a ( t )=3 cos 50 π t .
Why part (d) plot is not similar to original signal?
Due to aliasing part (d) plot is not similar to original signal. Aliasing is an artifact that occurs in
signal sampling when the sampling rate is not high enough to accurately represent the original
signal. It results in the misinterpretation of high-frequency components in the signal as lower
frequency components, causing a distorted and incorrect representation of the original signal.
Aliasing can lead to information loss, and it is an important consideration in the design of digital
signal processing systems, where the choice of an appropriate sampling rate is crucial to ensure
accurate signal representation.
The minimum sampling rate to avoid aliasing is twice the highest frequency component in the signal
being sampled. This is known as the Nyquist frequency, and it states that to accurately reconstruct
the original signal from the sampled signal, the sampling rate must be at least twice the highest
frequency present in the signal.
The frequency of the analog signal is F = 50 Hz. Hence the minimum sampling rate required to
avoid aliasing is Fs = 100 Hz. We sampled the signal at 75Hz that’s why aliasing occurs, and plots
were not the same.
Conclusion:
In this lab we have learnt about Sampling, reconstruction, and aliasing of signals. We
recorded out voice using audio recorder command played it at different playback rate, different
sampling frequency and observed the output voice. We also converted continuous signal to discrete
and then reconstructed it back to continuous using zero order hold and Interpolation methods by
using stairs and interp1 command in MATLAB.
The zero-order hold (ZOH) is a mathematical model of the practical signal reconstruction done by a
conventional digital-to-analog converter (DAC). It describes the effect of converting a discrete-time
signal to a continuous-time signal by holding each sample value for one sample interval.
In Interpolation method, an interpolation function is used to generate intermediate values between
the samples. Popular interpolation functions include linear interpolation, cubic spline interpolation,
and sinc interpolation. We use spline interpolation for reconstruction of signals.
Spline interpolation is a method of constructing a smooth curve that passes through a set of
specified points. The curve is represented as a series of polynomial functions, known as spline
segments, that are connected at points known as knots. Spline interpolation can be used to
approximate unknown data points or to smooth out noise in a dataset.
Spline interpolation has several advantages over other interpolation methods, including greater
flexibility in adjusting the shape of the curve and the ability to enforce smoothness constraints.
However, it can also be more computationally intensive than other methods, and it is more sensitive
to the choice of knots.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy