DSP Lab 3
DSP Lab 3
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;
Output:
figure(1)
figure(2)
subplot(3,1,2);
stem(t_s,x_s,'filled',LineWidth=2);
xlabel('Time');
ylabel('Amplitude');
legend('Sampled');
title('Sampled 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);
subplot(3,1,2);
stem(t_s,x_s,'filled',LineWidth=2);
xlabel('Time');
ylabel('Amplitude');
legend('Sampled');
title('Sampled 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);
subplot(3,1,2);
stem(t_s,x_s,'filled',LineWidth=2);
xlabel('Time');
ylabel('Amplitude');
legend('Sampled');
title('Sampled 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);
subplot(3,1,2);
stem(t_s,x_s,'filled',LineWidth=2);
xlabel('Time');
ylabel('Amplitude');
legend('Sampled');
title('Sampled 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.