MATLAB |TUTORIAL
Running head: Signal analysis (sampling and filtering) 1
Signal analysis (sampling and filtering) 2
Student’s Name:
University affiliations:
Instructor’s Name:
Date of Submission:
Introduction
The purpose of this lab exercise is to perform different operations on a sampled signal which include oversampling, under-sampling and filtering. The oversampled and the under-sampled signals are to be played and the changes in the audio fidelity noted. According to Nyquist theorem, in order to reproduce the original signal, the sampling frequency should be at least twice the maximum frequency in the audio signal. The signal to be analyzed is to be loaded in MATLAB from which its sampling frequency and the number of bits per symbol are to be determined.
A LOW pass filter is to be designed and used to filter the original signal. The frequency response of the filter is to be plotted on a graph. The frequency spectrum of the signal passed through the LOW pass filter is to be plotted on a graph. The audio for the filtered signal is to be played through the computer speaker and the differences from the original signal noted.
A HIGH pass filter is to be designed and used to filter the original signal. The frequency response of the filter is to be plotted on a graph. The frequency spectrum of the signal passed through the HIGH pass filter is to be plotted on a graph. The audio for the filtered signal is to be played through the computer speaker and the differences from the original signal noted.
Methodology
The .mat signal was loaded into the workspace using the command:
YY=load('NewYork.mat');
The variable YY is a structure variable three fields x, Fs and BITS where x is the signal, Fs is the sampling frequency and BITS is the number of bits per sample. To access the individual elements, the dot operator is used.
X=YY.x;
Fs=YY.Fs;
N_BITS=YY.BITS;
After loading the signal in MATLAB, oversampling and under sampling of the signal were done on the original signal. The frequency spectrums for both the under-sampled and the oversampled signals were plotted on a graph. Both audios for the signal were played through the computer speakers. Oversampling of the signal was done by interpolating between two successive samples of the original signal.
A 10th butter-worth low pass filter with the cut-off frequency set at 33 kHz was designed and used to filter the original signal. The frequency response of the filter together with the frequency spectrum of the filtered signal was plotted on a graph. A 10th butter-worth HIGH pass filter with the cut-off frequency set at 33 kHz was designed and used to filter the original signal. The filter’s frequency response together with the frequency spectrum of the filtered signal was plotted on a graph.
The last part of the signal dealt with sampling the original signal at a sampling frequency equal to one sixth of the original sampling frequency. The frequency spectrum of the original signal and the sampled signal were plotted on a graph and their differences noted.
Results
The value of the sampling frequency and the number of bits per symbols were evaluated using MATLAB to be equal to 48 kHz and 16 respectively.
A plot of the original signal is as shown in the figure below:
Figure 1: Time domain plot and the frequency spectrum of the original signal
The frequency components of the signal lie between 0Hz to around 24 kHz.
With the sampling frequency equal to 4 times the original sampling frequency, the time domain plot and the frequency spectrum of the obtained signal is as shown in the figure below:
Figure 2: Time domain plot and the frequency spectrum of the oversampled signal
With the sampling frequency equal to half the original sampling frequency, the time domain plot and the frequency spectrum of the obtained signal is as shown in the figure below:
Figure 3: Time domain plot and the frequency spectrum of the under sampled signal
The frequency response of the LOW pass filter and the spectrum of the signal passed through the filter are as shown in the figure below:
Figure 4: Frequency response of the LOW pass filter and the spectrum of the signal passed through the filter
The figure below shows the frequency response of the HIGH pass filter together with the spectrum of the signal passed through the filter.
Figure 5: Frequency response of the HIGH pass filter and the spectrum of the signal passed through the filter
The frequency spectrums of the original signal and the original signal sampled at a frequency equal to one sixth of the original signal is are shown in the figure below.
Figure 6: The frequency spectrums of the original signal and the original signal sampled at a frequency equal to one sixth of the original signal
Conclusion
This lab exercise was a good insight in the study and analysis of a sampled signal. Oversampling of the signal had no effect on the quality of the audio signal. Under sampling distorted the quality of the original signal because it skipped some samples in the original signal. Passing the signal through a low pass filter eliminated the high pitched voice elements in the audio signal. Passing the signal through a high pass filter eliminated the low pitched voice elements in the music signal
Appendix
MATLAB CODE
clc
clear all
close all
load NewYork % Loads the audio signal x
sound(x,Fs,BITS) % Reproduces audio signal
pause (14)% pause for 15 seconds to allow the music to play
ts=1/Fs; % Fs: Sampling frequency, ts: sampling period
t=[0:ts:(length(x)-1)*ts]; % Time axis
subplot(3,1,1)
plot(t,x) % Plot signal in the time domain
ylabel('Amplitude [a.u.]')
xlabel('t [s]')
axis tight
subplot(3,1,2)
[Pxx,F]=pwelch(x,[],[],[],Fs,'onesided');
plot(F,Pxx) % Plots spectrum
axis tight
subplot(3,1,3)
pwelch(x,[],[],[],Fs); % Plots spectrum, dB
% PART 1A
% WE NEED to resample the data at a sampling frequency fs=4*Fs
FS_2=4*Fs; % Sampling frequency equal to 4 time the original sampling frequency
YS_2 = resample(x,FS_2,Fs);
ts=1/FS_2; % Fs: Sampling frequency, ts: sampling period
t=[0:ts:(length(YS_2)-1)*ts]; % Time axis
figure(2)
subplot(3,1,1)
plot(t,YS_2) % Plot signal in the time domain
ylabel('Amplitude [a.u.]')
xlabel('t [s]')
title(['Oversampled signal with Fs= ',num2str(FS_2),' Hz'])
axis tight
subplot(3,1,2)
[Pxx,F]=pwelch(YS_2,[],[],[],FS_2,'onesided');
plot(F,Pxx) % Plots spectrum
xlabel('Frequency (kHz)')
axis tight
subplot(3,1,3)
pwelch(x,[],[],[],Fs); % Plots spectrum, dB
sound(YS_2,FS_2,BITS)
pause(15)% pause for 15 seconds to allow the music to play
%%
% PARTA
% Determing the sampling frequency and the number of bits per sample
YY=load('NewYork.mat');
Fs=YY.Fs;
N_BITS=YY.BITS;
fprintf('The sampling frequency in the signal is %f HZ and the number of bits per sample is %f\n',Fs,N_BITS)
% PART 1 B Undersampling the signal
FS_2=0.5*Fs; % Sampling frequency equal to 4 time the original sampling frequency
YS_2=zeros(size(x));
YS_2(1:2:end)=x(1:2:end);
%YS_2 = resample(x,FS_2,Fs);
ts=1/FS_2; % Fs: Sampling frequency, ts: sampling period
t=[0:ts:(length(YS_2)-1)*ts]; % Time axis
figure(3)
subplot(3,1,1)
plot(t,YS_2) % Plot signal in the time domain
ylabel('Amplitude [a.u.]')
xlabel('t [s]')
title(['Undersampled signal with Fs= ',num2str(FS_2),' Hz'])
axis tight
subplot(3,1,2)
[Pxx,F]=pwelch(YS_2,[],[],[],FS_2,'onesided');
plot(F,Pxx) % Plots spectrum
xlabel('Frequency (kHz)')
axis tight
subplot(3,1,3)
pwelch(x,[],[],[],Fs); % Plots spectrum, dB
sound(YS_2,FS_2,BITS)
pause(15)% pause for 15 seconds to allow the music to play
%% PART 2
% Analyzing the frequency response of the LOW_PASS filter
% Using the LOW_PASS FILTER TO FILTER THE ORIGINAL SIGNAL
% PLOTTING THE FRQUENCY RESPONSE OF THE LOW_PASS FILTER
% PLOTING THE FREQUENCY SPECTRUM OF THE FILTERED SIGNAL
figure(4)
[b,a]=butter(10,0.8/24,'low'); % Defines lowpass filter
[H,W] = freqz(b,a,128,Fs);
subplot(2,1,1)
plot(W/1000,abs(H)) % Plots lowpass filter frequency response
grid on
ylabel(' | H(f) | ')
xlabel('Frequency (kHz)')
axis tight
x_fpb = filtfilt(b,a,x); % Lowpass filters audio signal
subplot(2,1,2)
pwelch(x_fpb,[],[],[],Fs); % Plots spectrum of filtered signal
sound(x_fpb,Fs,BITS)
pause(15)% pause for 15 seconds to allow the music to play
%% PART 3
% HIGH PASS FILTER
%Analyzing the frequency response of the HIGH_PASS filter
% Using the HIGH_PASS FILTER TO FILTER THE ORIGINAL SIGNAL
% PLOTTING THE FREQUENCY RESPONSE OF THE HIGH_PASS FILTER
% PLOTING THE FREQUENCY SPECTRUM OF THE FILTERED SIGNAL
figure (5)
[b,a]=butter(25,4/24,'high');% Defines highpass filter
[H,W] = freqz(b,a,128,Fs);
subplot(2,1,1)
plot(W,abs(H))% Plots highpass filter frequency response
ylabel(' | H(f) | ')
xlabel('Frequency (kHz)')
grid on
axis tight
x_fpa = filtfilt(b,a,x); % Highpass filters audio signal
subplot(2,1,2)
pwelch(x_fpa,[],[],[],Fs); % Plots spectrum of filtered signal
sound(x_fpa,Fs,BITS)
pause(15)% pause for 15 seconds to allow the music to play
%% PART 4
figure(6)
[F,Pxx]=pwelch(x,[],[],[],Fs); % Plots spectrum of x
subplot(2,1,1)
plot(Pxx,F) % Plot signal in the time domain
ylabel('Original')
xlabel('Frequency (kHz)')
axis tight
x_s=zeros(size(x)); % Samples original signal x
x_s(1:6:end)=x(1:6:end);
subplot(2,1,2)
[F,Pxx]=pwelch(x_s,[],[],[],Fs); % Plots spectrum of x s
plot(Pxx,F)
ylabel('Sampled')
xlabel('Frequency (kHz)')
axis tight
sound(x_s,Fs,BITS) % Reproduces audio signal
02468101214
t [s]
-0.5
0
0.5
A
m
p
l
i
t
u
d
e
[
a
.
u
.
]
Oversampled signal with Fs= 192000 Hz
0123456789
Frequency (kHz)
10
4
2
4
6
8
10
-4
05101520
Frequency (kHz)
-120
-100
-80
-60
-40
P
o
w
e
r
/
f
r
e
q
u
e
n
c
y
(
d
B
/
H
z
)
Welch Power Spectral Density Estimate
0510152025
t [s]
-0.5
0
0.5
A
m
p
l
i
t
u
d
e
[
a
.
u
.
]
Undersampled signal with Fs= 24000 Hz
020004000600080001000012000
Frequency (kHz)
2
4
10
-4
05101520
Frequency (kHz)
-120
-100
-80
-60
-40
P
o
w
e
r
/
f
r
e
q
u
e
n
c
y
(
d
B
/
H
z
)
Welch Power Spectral Density Estimate
05101520
Frequency (kHz)
0
0.5
1
|
H
(
f
)
|
05101520
Frequency (kHz)
-150
-100
-50
P
o
w
e
r
/
f
r
e
q
u
e
n
c
y
(
d
B
/
H
z
)
Welch Power Spectral Density Estimate
00.511.52
Frequency (kHz)
10
4
0
0.5
1
|
H
(
f
)
|
05101520
Frequency (kHz)
-150
-100
-50
P
o
w
e
r
/
f
r
e
q
u
e
n
c
y
(
d
B
/
H
z
)
Welch Power Spectral Density Estimate
00.511.52
Frequency (kHz)
10
4
2
4
6
8
O
r
i
g
i
n
a
l
10
-4
00.511.52
Frequency (kHz)
10
4
0.5
1
1.5
2
2.5
S
a
m
p
l
e
d
10
-5