Digital Signal Processing
EEL-325
Name Muhammad Ali Khan
Registration Number
Class BCE
Instructor name
Lab# 07 Sampling and Multi-rate Signal Processing
Objective:
The purpose of this lab is to acquaint students with the following concepts practically:
Sampling
Rates of sampling
Up sampling/ Interpolation
Down sampling/ Decimation
Filtering within Sampling – More commonly known as the process of anti-aliasing.
Task 1:
Use your ‘sin’ function to generate a sine wave with f = 3kHz and sample it with fs = 8kHz.
Calculate its fft with zero frequency component in the middle. Plot it on a properly scaled w-
axis. Specify if there is aliasing or not?
Code:
n=-50:50;
f=3000;
fs=8000;
x=sin(2*pi*(f/fs)*n)
l=length(x);
NFFT=2^nextpow2(l);
y=fft(x,NFFT);
f_ax1=fs/2*linspace(0,1,NFFT/2+1);
f_ax2=fs/2*linspace(-1,0,NFFT/2-1);
f_ax=[f_ax1 f_ax2];
stem(f_ax,2*abs(y))
xlable('Hz');
ylable('Amplitude');
Output figure Aliasing or not
Ans: Yes.
Little impulses in the DFT represent the energy leakage due to the finite length of the signal.
Task 2:
Use your ‘sin’ function to generate a sine wave with f = 3kHz and sample it with fs = 5kHz.
Calculate its fft with zero frequency component in the middle. Plot it on a properly scaled w-axis.
Specify if there is aliasing or not?
Code:
n=-50:50;
f=3000;
fs=5000;
x=sin(2*pi*(f/fs)*n)
l=length(x);
NFFT=2^nextpow2(l);
y=fft(x,NFFT);
f_ax1=fs/2*linspace(0,1,NFFT/2+1);
f_ax2=fs/2*linspace(-1,0,NFFT/2-1);
f_ax=[f_ax1 f_ax2];
stem(f_ax,2*abs(y))
xlable('Hz');
ylable('Amplitude');
Output figure Aliasing or not
Ans: Yes
Changing Sampling rate in a Discrete Signal
1. y = decimate(x,R) – To down-sample a signal. Here ‘x’ is the array containing the signal to
be down-sampled and ‘R’ is the down-sampling factor/ratio
2. y = interp(x,R) – To up-sample a signal. Here ‘x’ is the array containing the signal to be up-
sampled and ‘R’ is the up-sampling factor/ratio
Task 3: Aliasing In a Speech Signal
Up till now you have studied and performed theoretical concepts, whether these theoretical tools
you have studied in the DSP theory and simulated in DSP lab have any practical significance?
How a signal is processed, in this section we are going to perform an example of aliasing on a
speech signal. We are going to perform following steps for this purpose.
1. Record your voice in MATLAB or use a recorded audio file and play the audio using
play, sound or audioplayer
2. Take its fft and plot it
3. Decimate the speech signal first by 2 and later by 3 and then perform step ii. Now
compare the fft of original and decimated signals and deduce your opinion about the
effect of aliasing by listening the reconstructed signal and their fft plots.
4. Now take the interpolated signal by 3 and perform step ii, afterwards decimate it by 2.
Play the signal listen to it check whether its quality has deteriorated or not due to aliasing.
Decimate the signal further by 2 until aliasing has appeared and voice quality of the
signal has deteriorated. Also observe how fft plot is affected due decimation and
interpolation.
Code:
clc
clear all
close all
recObj = audiorecorder(44100, 16, 2); % Create a recording object with
16 bit, 2 channel and sampling frequency
fs=44100 %sampling frequency
get(recObj) % To Check details of the created recording object
recObj = audiorecorder; % Start recording
disp('Start speaking.')
recordblocking(recObj, 10); % Set the time of recording, 10 sec in this
case
disp('End of Recording.');
myRecording = getaudiodata(recObj); % Get audio data out of the
recording object
% Plot the waveform.
plot(myRecording);
xlabel('Recorded Signal')
% Plot the audio data
%play (myRecording);
% Play the audio data
sound(myRecording); % Another way to Play the audio data
L = length(myRecording);
NFFT = 2^nextpow2(L);
y = fft(myRecording,NFFT);
f_ax1 = fs/2*linspace(0,1,NFFT/2+1);
f_ax2 = fs/2*linspace(-1,0,NFFT/2-1);
f_ax = [f_ax1 f_ax2];
figure,stem(f_ax,2*abs(y));
xlabel('Orignal Signal')
x = decimate(myRecording,2);
%x = decimate(x,3);
sound(x);
L = length(x);
NFFT = 2^nextpow2(L);
L = length(x);
NFFT = 2^nextpow2(L);
y = fft(x,NFFT);
f_ax1 = fs/2*linspace(0,1,NFFT/2+1);
f_ax2 = fs/2*linspace(-1,0,NFFT/2-1);
f_ax = [f_ax1 f_ax2];
figure,stem(f_ax,2*abs(y));
xlabel('DownSampling')
x = interp (x,2);
L = length(x);
NFFT = 2^nextpow2(L);
L = length(x);
NFFT = 2^nextpow2(L);
y = fft(x,NFFT);
f_ax1 = fs/2*linspace(0,1,NFFT/2+1);
f_ax2 = fs/2*linspace(-1,0,NFFT/2-1);
f_ax = [f_ax1 f_ax2];
figure,stem(f_ax,2*abs(y));
xlabel('up Sampling')
sound(x); % Another way to Play the audio data
Output:
Plot the fft of the original sound signal
Plot the fft of the down-sampled version
Plot the fft of the up-sampled version
Task 4:
Use your ‘sin’ or ‘cos’ function to generate a sinusoid wave having two components as f1 =
3kHz and f2 = 5kHz and then sample it with fs = 10kHz. Calculate its fft with zero frequency
component in the middle. Plot it on a properly scaled w-axis. Specify if there is aliasing or not?
If there is aliasing specify which component is casing the aliasing.
Aliasing is occurred at point 0.
Code and output
n=-50:50;
f1=3000;
f2=5000;
fs=10000;
x=sin(2*pi*(f1/fs)*n);
l=length(x);
nfft=2^nextpow2(l);
y=fft(x,nfft);
f_ax1=fs/2*linspace(0,1,nfft/2+1);
f_ax2=fs/2*linspace(-1,0,nfft/2-1);
f_ax=[f_ax1 f_ax2];
figure(1)
stem(f_ax,2*abs(y));
xlabel('Hz')
ylabel('amplitude')
x=sin(2*pi*(f2/fs)*n);
l=length(x);
nfft=2^nextpow2(l);
y=fft(x,nfft);
f_ax1=fs/2*linspace(0,1,nfft/2+1);
f_ax2=fs/2*linspace(-1,0,nfft/2-1);
f_ax=[f_ax1 f_ax2];
figure(2)
stem(f_ax,2*abs(y));
xlabel('Hz')
ylabel('amplitude')
Output:
For F1= 3kHz
For F2= 5kHz
Task 5:
Use your ‘sin’ or ‘cos’ function to generate a sinusoid wave having two components as f1 =
3kHz and f2 = 5kHz and then sample it with fs = 8kHz. Calculate its fft with zero frequency
component in the middle. Plot it on a properly scaled w-axis. Specify if there is aliasing or not?
If there is aliasing specify which component is casing the aliasing.
Aliasing is occurred at point 0.
Code and output
clc
clear all
close all
n=-50:50;
f1=3000;
f2=5000;
fs=8000;
x=sin(2*pi*(f1/fs)*n);
l=length(x);
nfft=2^nextpow2(l);
y=fft(x,nfft);
f_ax1=fs/2*linspace(0,1,nfft/2+1);
f_ax2=fs/2*linspace(-1,0,nfft/2-1);
f_ax=[f_ax1 f_ax2];
figure(1)
stem(f_ax,2*abs(y));
xlabel('Hz')
ylabel('amplitude')
x=sin(2*pi*(f2/fs)*n);
l=length(x);
nfft=2^nextpow2(l);
y=fft(x,nfft);
f_ax1=fs/2*linspace(0,1,nfft/2+1);
f_ax2=fs/2*linspace(-1,0,nfft/2-1);
f_ax=[f_ax1 f_ax2];
figure(2)
stem(f_ax,2*abs(y));
xlabel('Hz')
ylabel('amplitude')
Output:
For F1= 3kHz
For F2 = 5khz
.
Task 6:
Take a speech signal, plot its fft, now decimate by 2,3,4 … until the spectrum goes outside the
range {-fs/2, fs/2} (That’s when aliasing appears). Now do the reverse process i.e. interpolate by
… 4,3,2 and see whether the original signal has recovered or not listening the reconstructed
signal. Note that you are required to plot fft at all stages.
Code and output
clc
clear all
close all
recObj = audiorecorder(44100, 16, 2);
fs=44100
get(recObj)
recObj = audiorecorder; % Start recording
disp('Start speaking.')
recordblocking(recObj, 10);
disp('End of Recording.');
myRecording = getaudiodata(recObj);
% Plot the waveform.
plot(myRecording);
xlabel('Recorded Signal')
sound(myRecording); % Another way to Play the audio data
L = length(myRecording);
NFFT = 2^nextpow2(L);
y = fft(myRecording,NFFT);
f_ax1 = fs/2*linspace(0,1,NFFT/2+1);
f_ax2 = fs/2*linspace(-1,0,NFFT/2-1);
f_ax = [f_ax1 f_ax2];
figure,stem(f_ax,2*abs(y));
xlabel('Orignal Signal')
%x = decimate(myRecording,2);
%x = decimate(myRecording,3);
%x = decimate(myRecording,4);
x = decimate(myRecording,5);
sound(x);
L = length(x);
NFFT = 2^nextpow2(L);
L = length(x);
NFFT = 2^nextpow2(L);
y = fft(x,NFFT);
f_ax1 = fs/2*linspace(0,1,NFFT/2+1);
f_ax2 = fs/2*linspace(-1,0,NFFT/2-1);
f_ax = [f_ax1 f_ax2];
figure,stem(f_ax,2*abs(y));
xlabel('DownSampling')
%x = interp (x,2);
%x = interp (x,3);
%x = interp (x,4);
x = interp (x,5);
L = length(x);
NFFT = 2^nextpow2(L);
L = length(x);
NFFT = 2^nextpow2(L);
y = fft(x,NFFT);
f_ax1 = fs/2*linspace(0,1,NFFT/2+1);
f_ax2 = fs/2*linspace(-1,0,NFFT/2-1);
f_ax = [f_ax1 f_ax2];
figure,stem(f_ax,2*abs(y));
xlabel('up Sampling')
sound(x); % Another way to Play the audio data
Output:
Conclusion:
In this lab, we have implemented different sampling process on a continuous time signal
to convert it into a discrete time signal with some sampling frequency. In this lab w first
calculate its fft with zero frequency and then performed up sampling as well as down sampling
on a given signal. Also, we have studied about the change of rate of sampling in the given lab.
We have successfully performed the given tasks