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

DIGITAL SIGNAL

PROCESSING LABORATORY
EE39203 Autumn 2022-23

Hanumanthu Venkata Rao


20EE30013
Experiment – 4
Sampling and Reconstruction of Continuous-Time Signals, Interpolation and
Decimation

1. Introduction

1.1 Overview of Sampling

1.2 Sampling and Reconstruction Using Sample and Hold

Lab Report:
Do the following using Ts=1 sec, fc=0.45 Hz, and N = 20. Use Matlab to produce the plots
(magnitude only), for frequencies in the range: f = -1:0.001:1.
• Compute and plot the magnitude response of the system in Figure 2 without the sample-and-
hold device.
• Compute and plot the magnitude response of the complete system in Figure 2.
• Comment on the shape of the two magnitude responses. How might the magnitude response
of the sample-and-hold affect the design considerations of a high quality audio CD player?

Matlab Code:

Ts=1; %Sampling time period


fc = 0.45; %Cutoff frequency
Tc=1/fc;
fs = (2*pi)/Ts ; %Finding sampling frequency
N=20; %Order of Butterworth filter
f= -1:0.001:1; %Range of frequency
MagH1 = (1./((1+(f*(Tc)).^N)).^2); %magnitude equation
sgtitle("Hanumanthu Venkata Rao 20EE30013");
%Plotting the Magnitude respnse of the system without the sample and hold
subplot(2,1,1);
plot(f,MagH1);
xlabel("frequency(in Hz)");
ylabel("Magnitude");
title("Magnitude response Without S and H device");

%Plotting the Magnitude respnse of the system with the sample and hold
MagH2 = MagH1*abs((sin(f*Tc)/(f*Ts)));
subplot(2,1,2);
plot(f,MagH2);
xlabel("frequency(in Hz)");
ylabel("Magnitude");
title("Magnitude response With S and H device");
Plot:

Comments: As f tends to 0, magnitude of H(f) tends to zero Hence we can say that this
system resembles a low pass filter which we can also see from plot when we observe the
frequency response of the complete system(including Sample and Hold)

Here also we will observe that it behaves as a low pass filter but we can see that there is no
sharp transition from 1 to 0 at the cut off frequency compared to previous case it is because
of sinc function in H(f) which attenuates the frequencies closer to cut off frequency.
Generally, roll off is not desirable. Due to addition of Sample and hold the magnitude of the
frequency response doesn’t have sharp transition. Hence for a higher quality audio CD
player we need to take more samples which increases the memory requirement
2. Sampling and Reconstruction with an impulse Generator
Lab Report:
A sine wave is sampled at a frequency of 1 Hz; then the sampled discrete-time signal is used to generate
rectangular impulses of duration 0.3 sec and amplitude equal to the sample values. First, run the
simulation with the frequency of input sine wave set to 0.1 Hz (initial setting of the experiment). Let the
simulation run until it terminates to get an accurate plot of the output frequencies Submit the plot of the
input/output signals and the plot of the output signal and its frequency spectrum. On the plot of the
spectrum of the reconstructed signal, circle the aliases, i.e. the components that do NOT correspond to
the input sine wave.

Simulink model diagram

Plots:
Input signal
Output signal along with input signal

The output signal and its frequency spectrum

Lab Report:
In the initial setup, the pulse width is 0.3 sec, which is less than the sampling period of 1 sec. Try setting
the pulse width to 0.1 sec and run the simulation Submit the plot of the output frequency spectrum for
a pulse width of 0.1 sec. Indicate on your plot what has changed and explain why
Plot:
The output signal and the output frequency spectrum for a pulse width of 0.1 sec.

Lab Report:
Set the pulse width back to 0.3 sec and change the frequency of the sine wave to 0.8 Hz. Run the
simulation. Submit the plot of the input/output signals and the plot of the output signal and its
frequency spectrum. On the frequency plot, label the frequency peak that corresponds to the lowest
frequency (the fundamental component) of the output signal. Explain why the lowest frequency is no
longer the same as the frequency of the input sinusoid

Plots:
Input signal
Output signal along with input signal

The output signal and its frequency spectrum

Lab Report:
Leave the input frequency at 0.8 Hz. Now insert a filter right after the impulse generator. Use a 10th
order Butterworth filter with a cut-off frequency of 0.5 Hz. Connect the output of the filter to the
Spectrum Analyzer and the Mux. Run the simulation, Submit the plot of the input/output signals and
the plot of the output signal and its frequency spectrum. Explain why the output signal has the
observed frequency spectrum.
Simulink block diagram
Plots:
Input signal

Output signal along with input signal


The output signal and its frequency spectrum.

Comments:
i) we can observe that the output rectangular pulses width decreases as we reduce the
pulse width from 0.3 to 0.1. From the frequency spectrum plot, we can observe that,
compared to previous case, here(Pw=0.1) the frequencies for which the magnitude is
same as that of the fundamental frequency component increase. As we decrease the
pulse width further, the magnitude of all the frequencies becomes equal which is
case using the ideal Impulse Generator.
ii) When the input sinusoid frequency is 0.8 Hz. X_c(t) = sin(2pi0.8t), hence we get a
non-zero magnitude at frequencies 0.8 Hz and -0.8Hz in the frequency domain. After
the sampling with frequency of 1 Hz, due to convolution in frequency domain, we
get non zero magnitudes at the frequencies 0.8 Hz, -0.8 Hz , 0.2 Hz , 1.8 Hz, 1.2 Hz, …
and so on . Since the lowest frequency is no longer same as the frequency of the
input sinusoid.
iii) When we inserted a 10th order Butterworth filter with cut off frequency 0.5 Hz right
after the impulse generator, all the frequencies above 0.5 Hz in the previous case
gets attenuated and only the frequency of 0.2Hz is present which can be seen from
the output spectral density plot. Since it is not a ideal low pass filter we will also see
perks at frequencies greater than 0.5 Hz but their magnitudes are very small. Hence,
in the output signal plot, we get an sinusoid of frequency 0.2Hz whereas the input
sinusoid is of frequency 0.8Hz. This phenomenon of not being able to reconstruct the
original signal after sampling and low pass filtering is called Aliasing.
3.Sampling and Reconstruction with Sample and Hold
i) When the Sample and Hold Block is not connected

Lab Report:
In the initial setup, the Sample-and-Hold and Scope 3 are not connected. There is no sampling in this
system, just two cascaded low-pass filters. Run the simulation and observe the signals on the Scopes.
Submit the figure containing plots of the magnitude response, the phase response, and the impulse
response of this system. Use the tall mode to obtain a larger printout by typing orient(’tall’) directly
before you print.

Plots:
Input signal given to Butterworth Filter 1

Input signal given to Butterworth Filter 2 (or) Output signal from the Butterworth Filter 1
Output signal from the Butterworth filter 2

Magnitude, Phase Response and the Impulse Response of System


ii) When the Sample and Hold Block is connected

Lab Report:
Double-click the Sample-and-Hold and set its Sample time to 1. Now, insert the Sample-and-Hold in
between the two filters and connect Scope 3 to its output. Run the simulation and observe the signals
on the Scopes. Submit the figure containing plots of the magnitude response, the phase response, and
the impulse response of this system. Explain the reason for the difference in the shape of this
magnitude response versus the previous magnitude response. Give an analytical expression for the
behavior of the magnitude plot for frequencies below 0.45 Hz

Plots:
Input given to Butterworth Filter 1

Input signal given to sample and hold block


Output signal from the Sample and Hold block

Output signal from the Butterworth Filter 2


Magnitude, Phase Response and the Impulse Response of System

4.Discrete Time Interpolation

Simulink Block Diagram


Lab Report:
The components of the system are initially set to interpolate by a factor of 1. This means that the
input and output signals will be the same except for a delay. Run this model with the initial settings,
and observe the signals on the Scope. Submit your plot of signal c and its frequency spectrum. Circle
the aliased components in your plot.

Plots:
Signal a, Signal b

Signal a, Signal c
Signal c and its frequency spectrum

Lab Report:
Next modify the system to upsample by a factor of 4 by setting this parameter in the Upsampler. You
will also need to set the Sample time of the DT filter to 0.25. This effectively increases the sampling
frequency of the system to 4 Hz. Run the simulation again and observe the behavior of the system
Submit your plot of signal 𝑐 and its frequency spectrum. On your frequency plot, circle the first aliased
component and label the value of its center frequency. Comment on the shape of the envelope of the
spectrum.

Plots:
Signal a, Signal b
Signal a, Signal c

Signal c and its frequency spectrum


Lab Report:
Determine the gain factor of the Gain block and the cut-off frequency of the Discrete-time LP filter
needed to produce the desired interpolated signal. Run the simulation and observe the behavior of the
system. After you get an accurate plot of the output frequency spectrum, print the output of the
Spectrum Analyzer. Identify the change in the location of the aliased components in the output signal.
Submit your plot of signal 𝑐 and its frequency spectrum. Give the values of the cutoff frequency and gain
that were used. On your frequency plot, circle the location of the first aliased component. Explain why
discrete-time interpolation is desirable before reconstructing a sampled signal.

In this case, we will set the cut-off frequency of the DT Low Pass Filter is set to
0.1 Hz
Plots:
Signal a and Signal b

Signal a and Signal c


Signal 𝑐 and its frequency spectrum

Comments: In the first part, when L = 1 we get the output signal same as in the case of
sample and hold block. From the plots we can observe that signal a and c are same except
for a delay. In the frequency spectrum we can observe that aliasing occurs at frequencies
0.9Hz, 1Hz.. and so on i.e which are not correspond to the input sine. In the second part, we
set L = 4 and the Sample time of the DT filter is 0.25. This effectively increases the sampling
frequency of the system to 4Hz. In the case, we can observe that the signal c is up sampled
version of signal a. From the frequency spectrum of the signal c, we can observe that the
first aliased frequency occurs at 0.9Hz (1Hz-0.1Hz) and its center frequency is 1Hz. In the
case of the pulse width generator, frequency response can be obtained by multiplying the
frequency response as in the case of the ideal impulse generator with a sinc function. In the
third part we set the Gain to 4 which is same as the up-sampling factor to make the
amplitude of the reconstructed signal same as that of the input signal. When cut off
frequency of the DT Low Pass filter is set to 0.5Hz, we can observe from the frequency
spectrum of signal c that the first aliased frequency goes to 3.9Hz (4Hz - 0.1Hz). Hence,
using the Discrete time Interpolation is desirable before reconstructing a sampled signal
because it is difficult to make an ideal low pass filter practically. To get a better
reconstructed signal even with a non-ideal low pass filter, we do the Discrete time
interpolation before reconstructing a sampled signal.
5. Discrete time Decimation

Lab Report:
Download the signal music.au. Read in the signal contained in music.au using audioread, and then play
it back with sound. The signal contained in music.au was sampled at 16 kHz, so it will sound much too
slow when played back at the default 8 kHz sampling rate. To correct the sampling rate of the signal,
form a new signal, sig1, by selecting every other sample of the music vector. Play the new signal using
sound, and listen carefully to the new signal. Next compute a second subsampled signal, sig2, by first
low pass filtering the original music vector using a discrete-time filter of length 20, and with a cut-off
frequency of 𝜋/2. Then decimate the filtered signal by 2, and listen carefully to the new signal.
Hand in the Matlab code for this exercise. Also, comment on the quality of the audio signal generated
by using the two decimation methods. Was there any noticeable distortion in sig1? If so, describe the
distortion.

Matlab Code:
%Discrete time Decimation

[S,Frequency] = audioread("music.au"); %Reading the signal containing in music.au


signal_1 = S(1:2:end);%To correct the sampling rate of the signal
sound(signal_1, Frequency); %To listen to the new audio signal
pause(10);

% Length(M)= 20, Cut-off frequency(W) = pi/2

Inpulseresponse_h = fir1(20,1/2); % To find the impuslse response


signal_2 = conv(S,Inpulseresponse_h); %low pass filtering the original music vector
signal_2_AfterDeci = signal_2(1:2:end);
sound(signal_2_AfterDeci,Frequency);%To listen to the new audio signal

Comments: The given signal was sampled at 16kHz. When we play this audio at a
frequency of 8kHz sampling rate, it will sound to slow. hence we down sample the audio
signal by a factor of 2. in the first case, we down sample the audio signal directly whereas in
the second case we use a low pass filter to limit the bandwidth of the input signal and then
down sample so that after decimation there is no aliasing. From both the audio signals, we
can observe that, signal 2 has less distortions compared to signal 1.

Other than Manual:


3.17
Background Work:
Matlab Code:
t = -0.1:0.0001:0.1; %choosing interval with incremwnt = 0.0001
t2 = -0.1:0.0001:0.1; %choosing interval with incremwnt = 0.0001
Ts = 0.005; %Sampling Time peroiod
w = -pi:0.001:pi; %frequency interval

n = -20:1:20; % Number of Samples

x = 4+1*cos(150*pi*t+(pi/3))+4*sin(350*pi*t); %Defining given analog signl


sgtitle("Hanumanthu Venkata Rao 20EE30013");
subplot(4,1,1);
plot(t,x); %Plotting given analog signal x_a(t)
xlabel("t");
ylabel("x_a(t)");
title("Original signal");

X = 4+2*cos(150*pi*n*Ts+(pi/3))+4*sin(350*pi*(n*Ts)); %Defining x[n] = x_a(n*Ts)


subplot(4,1,2);
stem(n,X); %Plotting the Discrete time signal x[n]
xlabel("n");
ylabel("x[n]");
title("Discrete time signal x[n] = x_a(n*Ts)","Ts = 0.005");

XD= DTFT(X,0,0.001); %Finding DTFT of x[n]


subplot(4,1,3);
plot(w,abs(XD)); %Plotting Magnitude spectrum of X(e^(jw))
xlabel("Frequency(in rad/sec)");
ylabel("Magnitude");
title("Magnitude of X(e^j^w)");

% Reconstruction using Sinc interpolation


% Fs is less than twice of the highest frequency(Fmax = 175 Hz);
%Therefore aliasing takes place. so the recpnstructed signal will distorted
%we can observe from below

T1 = 0.005;
s3 = 0;

for i = 1:length(X)

s3 = s3 + X(i)*sinc((pi*(t2-(i-21)*T1))/T1) ;

end

subplot(4,1,4);
plot(t2,s3); %Plotting the Reconstructing signal
xlabel("t");
ylabel("x_a(t)");
title("Reconstructed signal by using sinc interpolation","Ts=0.005");
Plots:
Original signal, x[n[, Magnitude of X(e^(jw))

Original signal, Reconstructed signal by using sinc interpolation (Ts = 0.005)


3.18

Background Work:
Matlab Code:

syms t w
p=-20000:1:20000;

x = exp(-1000*abs(t)); %Defining given analog signal


y = fourier(x,p); %To find Fourier Transform
t1 = -0.1:0.00009:0.1;
x1 = exp(-1000*abs(t1));

sgtitle("Hanumanthu Venkata Rao 20EE30013");


subplot(2,1,1);
plot(t1,x1); %Plotting the original signal
xlabel("t");
ylabel("x_a(t)");
title("Original x_a(t)");

subplot(2,1,2);
plot(p,abs(y)); %Plotting the Fourier transform of x_a(t)
xlabel("Frequency(in rad/sec)");
ylabel("Magnitude of X(jw)");
title("Magnitude of Fourier Transform of x_a(t)");

Plot:
Orignal x_a(t) , Magnitude of Fourier Transform of x_a(t)
3.19

From the samples x1(n) in Example 3.19a, reconstruct x_a(t)


Matlab Code:
syms t w
p=-20000:1:20000;
n= -100:1:100;
t1 = -0.1:0.00009:0.1;
x1 = exp(-1000*abs(t1)); %defining the given signal x_a(t)

sgtitle("Hanumanthu Venkata Rao 20EE30013");


subplot(3,2,1);
plot(t1,x1); %Plotting the original signal
xlabel("t");
ylabel("x_a(t)")
title("Original x_a(t)");

x = exp(-1000*abs(t));
y = fourier(x,p);

Fs1 = 5000;
Fs2 = 1000;

X1 = exp(-1000*abs(n/Fs1));
X2 = exp(-1000*abs(n/Fs2));

sgtitle("Hanumanthu Venkata Rao 20EE30013");


subplot(3,2,3);
stem(n,X1); %Plotting the Discrete time signal with Fs1 = 5000
xlabel("n");
ylabel("x_1[n]");
title("Signal x_1[n](Fs = 5000 Hz)");

subplot(3,2,4);
stem(n,X2); %P)lotting the Discrete time signal with Fs2 = 1000
xlabel("n");
ylabel("x_2[n]");
title("Signal x_2[n](Fs = 1000 Hz)");

w = -pi:0.001*pi:pi;

X1_dtft = DTFT(X1,1,0.001*pi);
X2_dtft = DTFT(X2,1,0.001*pi);

subplot(3,2,5);
plot(w,abs(X1_dtft));%Plotting the Magnitude of X_1(e^(jw))
xlabel("Frequency(in rad/sec)");
ylabel("Magnitude")
title("Magnitude of X_1(e^j^w)");

subplot(3,2,6);
plot(w,abs(X2_dtft)); %Plotting the Magnitude of X_2(e^(jw))
xlabel("Frequency(in rad/sec)");
ylabel("Magnitude")
title("Magnitude of X_2(e^j^w)");
Matlab Code:

Y =0;
Ts1 = 0.0002;

%Reconstruction using sinc interpolation

for i = 1:length(X1)

Y = Y + X1(i)*((sin(pi*(t1-(-101+i)*Ts1)/Ts1)./((pi*(t1-(-101+i)*Ts1))/Ts1)));

end

subplot(3,2,2);
plot(t1,Y); % Plotting the Reconstructing Signal
xlabel("t");
ylabel("x_a(t)");
title("Reconstructed x_a(t) with Fs = 5000");

Plots:
Signal x1[n] (Fs = 5000) , Magnitude of X1(e^(jw))
Signal x2[n] (Fs = 1000) , Magnitude of X2(e^(jw))

Original x_a(t) , Reconstructed x_a(t) with Fs = 5000


Original x_a(t) , Reconstructed x_a(t) with Fs = 1000

3.21
Plot the reconstructed signal from the samples x1(n) in Example 3.19 using the ZOH and the
FOH interpolations. Comment on the plots
3.22
From the samples x1(n) and x2(n) in Example 3.19. reconstruct x_a(t) using the spline
function. Comment on the results
Matlab Code:
syms t w
p=-20000:1:20000;
n=-100:1:100;
t1 = -0.1:0.0001:0.1;
x1 = exp(-1000*abs(t1)); %Defining the given signal

x = exp(-1000*abs(t));
y = fourier(x,p);

T1 = 0.0002;
s = 0;

X1 = exp(-1000*abs(n*0.0002)); %Defining the discrte time signal with Fs1 = 5000

for i = 1:length(X1)
s = s + X1(i)*(((t1-(i-101)*T1)>0) - ((t1-(i-101)*T1)>T1)) ;

end

sgtitle("Hanumanthu Venkata Rao 20EE30013");


subplot(3,2,1);
plot(t1,x1); %Plotting the original signal
xlabel("t");
ylabel("x_a(t)")
axis([-0.03 0.03 0 1]);
title("Original x_a(t)")

subplot(3,2,2);
plot(t1,s); %Plotting the Reconstructed signal using ZOH
xlabel("t");
ylabel("x_a(t)");
axis([-0.03 0.03 0 1]);
title("Reconstructed x_a(t) by using ZOH with Fs = 5000");

s1 = 0;

for i = 1:length(X1)
s1 = s1 + X1(i)*(((t1-(i-101)*T1)>(-T1)) - ((t1-(i-101)*T1)>T1)).*(1-((abs(t1-
(i-101)*T1))/T1));

end

subplot(3,2,3);
plot(t1,s1); %Plotting the Reconstructed signal using FOH
xlabel("t");
ylabel("x_a(t)");
axis([-0.03 0.03 0 1]);
title("Reconstructed x_a(t) by using FOH with Fs = 5000");

subplot(3,2,4);
plot(t1,x1,"b");
xlabel("t");
ylabel("x_a(t)")
axis([-0.01 0.01 0 1]);
hold on
plot(t1,s,"y");
hold on
plot(t1,s1,"g");
title("Reconstructed x_a(t) with Fs = 5000");

S21 = spline(n*0.0002,X1,t1);
subplot(3,2,5);
Matlab Code:
S21 = spline(n*0.0002,X1,t1);
subplot(3,2,5);
plot(t1,S21);%Plotting the reconstruction signal using spline function
xlabel("t");
ylabel("x_a(t)")
title("Reconstruction of x_a(t) using spline function","Fs = 5000");

X2 = exp(-1000*abs(n*0.001));%Defining the discrte time signal with Fs2 = 1000

S22 = spline(n*0.001,X2,t1);
subplot(3,2,6);
plot(t1,S22); %Plotting the reconstruction signal using spline function
xlabel("t");
ylabel("x_a(t)")
title("Reconstruction of x_a(t) using spline function","Fs = 1000");

Plots:
Original Signal, Reconstructed x_a(t) using ZOH with Fs = 5000
Original Signal, Reconstructed x_a(t) using FOH with Fs = 5000

Original signal, Reconstructed Signal x_a(t) using ZOH and FOH with Fs = 5000
Original Single, Reconstruction of x_a(t) using spline function with Fs = 5000

Original Single, Reconstruction of x_a(t) using spline function with Fs = 1000


3.23
Matlab Code:

n = -2:1:10; %Samples

t = -0.2:0.001:1; %time increment = 0.001

y = (n>=0)-(n>=5)+((0.5).^(n-4)).*((n>=5)-(n>=10)); %Defining the expression


sgtitle("Hanumanthu Venkata Rao 20EE30013")
subplot(3,1,1);
stem(n,y); %Plotting input sequence
xlabel("n");
ylabel("y[n]");
title("Input Sequence");

Ts = 0.1; %Sampling Timeperiod = 0.1s

T1 = 0.05;
p = (t>(-T1)) - (t>(T1));

s = 0; % initializing
t1 = 0:0.00001:1;

for i = 1:length(y)

s = s + y(i)*(((t1-(i-3)*Ts)>(-T1)) - ((t1-(i-3)*Ts)>T1)) ;

end

subplot(3,1,2);
plot(t1,s); %Plotting output sequence
xlabel("t(in Sec)");
ylabel("p(t)");
title("Output waveform"); %T1 = 0.05

s1 =0; %initializing

T2 = 0.1;
t2 = -0.1:0.00001:1; %time increment

for i = 1:length(y)

s1 = s1 + y(i)*(((t2-(i-3)*Ts)>(-T2)) - ((t2-(i-3)*Ts)>T2)).*(1-10*abs(t2-(i-
3)*Ts)) ;

end

subplot(3,1,3);
plot(t2,s1); %Plotting output sequence
xlabel("t(in Sec)");
ylabel("p(t)");
title("Output waveform"); %T2 = 0.1
Plots:
a,b,c

You might also like