Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 51

ETM4096 DIGITAL SIGNAL PROCESSING 2020

MULTIMEDIA UNIVERSITY
Faculty of Engineering and Technology

ETM4096 Digital Signal Processing


Trimester 2, 2020/2021 (2020)
ASSIGNMENT

Name: Chok Mun Kit

S.I.D: 1171302489 Tutorial Group: ____________

Major: TE Signature:

Submission Date:

ASSIGNMENT REQUIREMENTS
- Google Document
- it must contain description of used design method
- please include all relevant calculations
- structure of used filter realization
- designed coefficients
- discussion of results
- please use the suitable noisy signal for each filter.
- please use OCTAVE or SCILAB. Please provide all the codes.

Question Max Score


1 15
2 15
3 15
4 15
5 40
Total 100
ETM4096 DIGITAL SIGNAL PROCESSING 2020

Question 1 [15 marks]

Design and implement Finite Infinite Impulse Response (FIR) and Infinite Impulse
Response (IIR) pass-band digital filters with the following parameters
(requirements):
1. pass-band edge frequencies 6 kHz and 8 kHz
2. stop-band frequencies 5 kHz and 9 kHz
3. sampling frequency: Select a suitable frequency. Justify your selection.
4. maximal allowed pass-band ripple is 0.1 dB
5. stop-band attenuation must be at least ( +20) dB
6) Plot the impulse response, approximation error, and magnitude response (in
dB) of the designed filter using OCTAVE/SCILAB.
7) Using the original audio for DSP2 LAB, add a suitable noise for this
filter.
8) Filter the noisy audio to show that the filter is designed correctly.

SOLUTIONS:
ETM4096 DIGITAL SIGNAL PROCESSING 2020

FIR filter:
close all;
%FIR apps

[signalo,fso]=audioread('1171302489o.wav');
n=length(signalo);
ft=20000;
fso=ft
fsn=ft;
% 1 seconds=pi/43200
t=0:1:65535;
x1=0.02*cos(900*t);
x1=transpose(x1);
x2=0.02*cos(600*t);
x2=transpose(x2);
signaln=signalo+x1+x2;
%save signal
audiowrite('1171302489Q1.1n.wav',signaln,20000)

%Original and Noisy Signal in Time domain:


figure(1);
subplot(2,1,1)
plot(signalo);
title('Original signal in time domain');
xlabel('second');
subplot(2,1,2)
plot(signaln);
title('Noisy signal in time domain');
xlabel('second');

%Original and Noisy Signal in Frequency domain:


figure(2);
subplot(3,1,1)
signaloxdc=signalo-mean(signalo);
fsignalo=fft(signaloxdc);
plot((1:n/2)/n*ft,abs(fsignalo(1:n/2)));
title('Original signal in frequency domain');
xlabel('Hz');
ylabel('magnitude');
grid;
subplot(3,1,2)
signalnxdc=signaln-mean(signaln);
fsignaln=fft(signalnxdc);
plot((1:n/2)/n*ft,abs(fsignaln(1:n/2)));
title('Noisy signal in frequency domain');
xlabel('Hz');
ylabel('magnitude');
grid;
%Comparison of Original and Noisy Signal in Frequency domain:
subplot(3,1,3)
plot((1:n/2)/n*ft,abs(fsignaln(1:n/2)),(1:n/2)/n*ft,abs(fsignalo(1:n/2)));
title('Comparison of Original signal and Noisy signal in frequency domain');
xlabel('Hz');
ylabel('magnitude');
grid;

%Filter Specificationns
fT = 20000;
fp1 = 6000;
fp2 = 8000;
fs1 = 5000;
fs2 = 9000;
ETM4096 DIGITAL SIGNAL PROCESSING 2020

delta_p=0.1;
delta_s = 56; %for Blackman
wc1 = (fs1+fp1)*0.5/(ft/2);
wc2 = (fs2+fp2)*0.5/(ft/2);
df = (fp1-fs1)/(fT/2);
N = ceil(5.5/df);
%Filtering
h = fir1(N,[wc1,wc2],'bandpass');
sf = filter(h,1,signaln);
%save signal
audiowrite('1171302489Q1.1f.wav',sf,20000)

%Filtered Signal
figure(3);
subplot(2,1,1)
sfxdc=sf-mean(sf);
fsf=fft(sfxdc);
plot((1:n/2)/n*fso,abs(fsf(1:n/2)));
title('Filtered signal in frequency domain');
xlabel('Hz');
ylabel('magnitude');
grid;
%Comparison of Original and Noisy Signal in Frequency domain:
subplot(2,1,2)
plot((1:n/2)/n*ft,abs(fsignaln(1:n/2)),(1:n/2)/n*ft,abs(fsf(1:n/2)));
title('Comparison of Original signal and Noisy signal in frequency domain');
xlabel('Hz');
ylabel('magnitude');
grid;

%stem(h)
figure(4)
freqz(h)

figure(5);
subplot(2,1,1)
[H,w]=freqz(h); %get Frequency Response%
plot(w/pi,abs(H));grid
title('Frequency response of the h[n]')
xlabel('Frequency')
ylabel('Amplitude')
subplot(2,1,2)
plot(w*fT/2/pi,abs(H));grid
title('Frequency response of the h[n]')
xlabel('Frequency')
ylabel('Amplitude')

figure(6)
k= impz(h);
stem(k)
title('Impulse response of the h[n]')
xlabel('n')
ylabel('Amplitude')
ETM4096 DIGITAL SIGNAL PROCESSING 2020
ETM4096 DIGITAL SIGNAL PROCESSING 2020

IIR filter:
%IIR apps
[signalo,fso]=audioread('1171302489o.wav');
n=length(signalo);
ft=20000;
fso=ft
fsn=ft;
% 1 seconds=pi/43200
t=0:1:65535;
x1=0.02*cos(900*t);
x1=transpose(x1);
x2=0.02*cos(600*t);
x2=transpose(x2);
signaln=signalo+x1+x2;
%save signal
audiowrite('1171302489Q1.2n.wav',signaln,20000)

%Original and Noisy Signal in Time domain:


figure(1);
subplot(2,1,1)
plot(signalo);
title('Original signal in time domain');
xlabel('second');
subplot(2,1,2)
plot(signaln);
title('Noisy signal in time domain');
xlabel('second');

%Original and Noisy Signal in Frequency domain:


figure(2);
subplot(3,1,1)
signaloxdc=signalo-mean(signalo);
fsignalo=fft(signaloxdc);
plot((1:n/2)/n*fso,abs(fsignalo(1:n/2)));
title('Original signal in frequency domain');
xlabel('Hz');
ylabel('magnitude');
grid;
subplot(3,1,2)
signalnxdc=signaln-mean(signaln);
fsignaln=fft(signalnxdc);
plot((1:n/2)/n*fsn,abs(fsignaln(1:n/2)));
title('Noisy signal in frequency domain');
xlabel('Hz');
ylabel('magnitude');
grid;
%Comparison of Original and Noisy Signal in Frequency domain:
subplot(3,1,3)
plot((1:n/2)/n*fsn,abs(fsignaln(1:n/2)),(1:n/2)/n*fso,abs(fsignalo(1:n/2)));
title('Comparison of Original signal and noisy signal in frequency domain');
xlabel('Hz');
ylabel('magnitude');
grid;

ft = 20000;
fp1 = 6000;
fp2 = 8000;
fs1 = 5000;
fs2 = 9000;
delta_p=0.1;
delta_s = 56;
wp1=fp1/(ft/2);
ETM4096 DIGITAL SIGNAL PROCESSING 2020

ws1=fs1/(ft/2);
wp2=fp2/(ft/2);
ws2=fs2/(ft/2);

%Filtered Signal
figure(3);
subplot(2,1,1)
sfxdc=sf-mean(sf);
fsf=fft(sfxdc);
plot((1:n/2)/n*fsn,abs(fsf(1:n/2)));
title('Filtered signal in frequency domain');
xlabel('Hz');
ylabel('magnitude');
grid;
%Comparison of Filtered and Noisy Signal in Frequency domain:
subplot(2,1,2)
plot((1:n/2)/n*fsn,abs(fsignaln(1:n/2)),(1:n/2)/n*fso,abs(fsf(1:n/2)));
title('Comparison of Filtered signal and noisy signal in frequency domain');
xlabel('Hz');
ylabel('magnitude');
grid;

%stem(h)
figure(4)
freqz(h)

[N, Wn] = buttord([wp1,wp2], [ws1,ws2], delta_p, delta_s);


[B,A] = butter(N,Wn,'bandpass');
sf=filter(B,A,signaln);
[H,w]=freqz(B,A); %get Frequency Response%
figure(5)
subplot(2,1,1)
plot(w/pi,abs(H));grid
title('Frequency response of the h[n]')
xlabel('Frequency')
ylabel('Amplitude')
subplot(2,1,2)
plot(w*ft/2/pi,abs(H));grid
title('Frequency response of the h[n]')
xlabel('Frequency')
ylabel('Amplitude')
%save signal
audiowrite('1171302489Q1.2i.wav',sf,20000)

figure(6)
k= impz(h);
stem(k)
title('Impulse response of the h[n]')
xlabel('n')
ylabel('Amplitude')
ETM4096 DIGITAL SIGNAL PROCESSING 2020
ETM4096 DIGITAL SIGNAL PROCESSING 2020

Question 2 [15 marks]

Design and implement Finite Infinite Impulse Response (FIR) and Infinite Impulse
Response (IIR) stop-band digital filters with the following parameters
(requirements):
1. pass-band edge frequencies 5 kHz and 9 kHz
2. stop-band frequencies 6 kHz and 8 kHz
3. sampling frequency: Select a suitable frequency. Justify your selection.
4. maximal allowed pass-band ripple is 0.1 dB
5. stop-band attenuation must be at least ( +20) dB
6) Plot the impulse response, approximation error, and magnitude response (in
dB) of the designed filter using OCTAVE/SCILAB.
7) Using the original audio for DSP2 LAB, add a suitable noise for this
filter.
8) Filter the noisy audio to show that the filter is designed correctly.

SOLUTIONS:
ETM4096 DIGITAL SIGNAL PROCESSING 2020

FIR filter:
close all;
%FIR apps
[signalo,fso]=audioread('1171302489o.wav');
n=length(signalo);
ft=20000;
fso=ft
fsn=ft;
% 1 seconds=pi/43200
t=0:1:65535;
x1=0.02*cos(5500*t);
x1=transpose(x1);
signaln=signalo+x1;
%save signal
audiowrite('1171302489Q2.1n.wav',signaln,20000)

%Original and Noisy Signal in Time domain:


figure(1);
subplot(2,1,1)
plot(signalo);
title('Original signal in time domain');
xlabel('second');
subplot(2,1,2)
plot(signaln);
title('Noisy signal in time domain');
xlabel('second');

%Original and Noisy Signal in Frequency domain:


figure(2);
subplot(3,1,1)
signaloxdc=signalo-mean(signalo);
fsignalo=fft(signaloxdc);
plot((1:n/2)/n*fso,abs(fsignalo(1:n/2)));
title('Original signal in frequency domain');
xlabel('Hz');
ylabel('magnitude');
grid;
subplot(3,1,2)
signalnxdc=signaln-mean(signaln);
fsignaln=fft(signalnxdc);
plot((1:n/2)/n*fsn,abs(fsignaln(1:n/2)));
title('Noisy signal in frequency domain');
xlabel('Hz');
ylabel('magnitude');
grid;
%Comparison of Original and Noisy Signal in Frequency domain:
subplot(3,1,3)
plot((1:n/2)/n*fsn,abs(fsignaln(1:n/2)),(1:n/2)/n*fso,abs(fsignalo(1:n/2)));
title('Comparison of Original signal and noisy signal in frequency domain');
xlabel('Hz');
ylabel('magnitude');
grid;

%Filter Specificationns
fT = 20000;
fp1 = 5000;
fp2 = 9000;
fs1 = 6000;
fs2 = 8000;
delta_p=0.1;
delta_s = 56; %for Blackman
wc1 = (fs1+fp1)*0.5/(fT/2);
ETM4096 DIGITAL SIGNAL PROCESSING 2020

wc2 = (fs2+fp2)*0.5/(fT/2);
df = (fs1-fp1)/(fT/2);
N = ceil(5.5/df);
%Filtering
h = fir1(N,[wc1,wc2],"stop");
sf = filter(h,1,signaln);
%save signal
audiowrite('1171302489Q2.1f.wav',sf,20000)

%Filtered Signal
figure(3);
subplot(2,1,1)
sfxdc=sf-mean(sf);
fsf=fft(sfxdc);
plot((1:n/2)/n*fsn,abs(fsf(1:n/2)));
title('Filtered signal in frequency domain');
xlabel('Hz');
ylabel('magnitude');
grid;
%Comparison of Original and Noisy Signal in Frequency domain:
subplot(2,1,2)
plot((1:n/2)/n*fsn,abs(fsignaln(1:n/2)),(1:n/2)/n*fso,abs(fsf(1:n/2)));
title('Comparison of Filtered signal and noisy signal in frequency domain');
xlabel('Hz');
ylabel('magnitude');
grid;

%stem(h)
figure(4)
freqz(h)

figure(5);
subplot(2,1,1)
[H,w]=freqz(h); %get Frequency Response%
plot(w/pi,abs(H));grid
title('Frequency response of the h[n]')
xlabel('Frequency')
ylabel('Amplitude')
subplot(2,1,2)
plot(w*fT/2/pi,abs(H));grid
title('Frequency response of the h[n]')
xlabel('Frequency')
ylabel('Amplitude')

figure(6)
k= impz(h);
stem(k)
title('Impulse response of the h[n]')
xlabel('Frequency')
ylabel('Amplitude')
ETM4096 DIGITAL SIGNAL PROCESSING 2020
ETM4096 DIGITAL SIGNAL PROCESSING 2020

IIR filter:
%IIR apps
[signalo,fso]=audioread('1171302489o.wav');
n=length(signalo);
ft=20000;
fso=ft
fsn=ft;
% 1 seconds=pi/43200
t=0:1:65535;
x1=0.02*cos(5500*t);
x1=transpose(x1);
signaln=signalo+x1;
%save signal
audiowrite('1171302489Q2.2n.wav',signaln,20000)

%Original and Noisy Signal in Time domain:


figure(1);
subplot(2,1,1)
plot(signalo);
title('Original signal in time domain');
xlabel('second');
subplot(2,1,2)
plot(signaln);
title('Noisy signal in time domain');
xlabel('second');

%Original and Noisy Signal in Frequency domain:


figure(2);
subplot(3,1,1)
signaloxdc=signalo-mean(signalo);
fsignalo=fft(signaloxdc);
plot((1:n/2)/n*fso,abs(fsignalo(1:n/2)));
title('Original signal in frequency domain');
xlabel('Hz');
ylabel('magnitude');
grid;
subplot(3,1,2)
signalnxdc=signaln-mean(signaln);
fsignaln=fft(signalnxdc);
plot((1:n/2)/n*fsn,abs(fsignaln(1:n/2)));
title('Noisy signal in frequency domain');
xlabel('Hz');
ylabel('magnitude');
grid;
%Comparison of Original and Noisy Signal in Frequency domain:
subplot(3,1,3)
plot((1:n/2)/n*fsn,abs(fsignaln(1:n/2)),(1:n/2)/n*fso,abs(fsignalo(1:n/2)));
title('Comparison of Original signal and noisy signal in frequency domain');
xlabel('Hz');
ylabel('magnitude');
grid;

ft = 20000;
fp1 = 5000;
fp2 = 9000;
fs1 = 6000;
fs2 = 8000;
delta_p=0.1;
delta_s = 56;
wp1=fp1/(ft/2);
ws1=fs1/(ft/2);
wp2=fp2/(ft/2);
ETM4096 DIGITAL SIGNAL PROCESSING 2020

ws2=fs2/(ft/2);

%Filtered Signal
figure(3);
subplot(2,1,1)
sfxdc=sf-mean(sf);
fsf=fft(sfxdc);
plot((1:n/2)/n*fsn,abs(fsf(1:n/2)));
title('Filtered signal in frequency domain');
xlabel('Hz');
ylabel('magnitude');
grid;
%Comparison of Filtered and Noisy Signal in Frequency domain:
subplot(2,1,2)
plot((1:n/2)/n*fsn,abs(fsignaln(1:n/2)),(1:n/2)/n*fso,abs(fsf(1:n/2)));
title('Comparison of Filtered signal and noisy signal in frequency domain');
xlabel('Hz');
ylabel('magnitude');
grid;

%stem(h)
figure(4)
freqz(h)

[N, Wn] = buttord([wp1,wp2], [ws1,ws2], delta_p, delta_s);


[B,A] = butter(N,Wn,'stop');
sf=filter(B,A,signaln);
[H,w]=freqz(B,A); %get Frequency Response%
figure(5)
subplot(2,1,1)
plot(w/pi,abs(H));grid
title('Frequency response of the h[n]')
xlabel('Frequency')
ylabel('Amplitude')
subplot(2,1,2)
plot(w*ft/2/pi,abs(H));grid
title('Frequency response of the h[n]')
xlabel('Frequency')
ylabel('Amplitude')
%save signal
audiowrite('1171302489Q2.2i.wav',sf,20000)

figure(6)
k= impz(h);
stem(k)
title('Impulse response of the h[n]')
xlabel('Frequency')
ylabel('Amplitude')
ETM4096 DIGITAL SIGNAL PROCESSING 2020
ETM4096 DIGITAL SIGNAL PROCESSING 2020

Question 3 [15 marks]

Design and implement Finite Infinite Impulse Response (FIR) and Infinite Impulse
Response (IIR) low-pass digital filters with the following parameters (requirements):
1) pass-band edge frequency 5 kHz
2) stop-band frequency 6 kHz
3) sampling frequency: Select a suitable frequency. Justify your selection.
4) maximal allowed pass-band ripple is 0.1 dB
5) stop-band attenuation must be at least ( +20) dB
6) Plot the impulse response, approximation error, and magnitude response (in
dB) of the designed filter using OCTAVE/SCILAB.
7) Using the original audio for DSP2 LAB, add a suitable noise for this
filter.
8) Filter the noisy audio to show that the filter is designed correctly.

Solution:
ETM4096 DIGITAL SIGNAL PROCESSING 2020

FIR filter:
close all;
%FIR apps
[signalo,fso]=audioread('1171302489o.wav');
n=length(signalo);
ft=20000;
fso=ft
fsn=ft;
% 1 seconds=pi/43200
t=0:1:65535;
x1=0.02*cos(500*t);
x1=transpose(x1);
signaln=signalo+x1;
%save signal
audiowrite('1171302489Q3.1n.wav',signaln,20000)

%Original and Noisy Signal in Time domain:


figure(1);
subplot(2,1,1)
plot(signalo);
title('Original signal in time domain');
xlabel('second');
subplot(2,1,2)
plot(signaln);
title('Noisy signal in time domain');
xlabel('second');

%Original and Noisy Signal in Frequency domain:


figure(2);
subplot(3,1,1)
signaloxdc=signalo-mean(signalo);
fsignalo=fft(signaloxdc);
plot((1:n/2)/n*fso,abs(fsignalo(1:n/2)));
title('Original signal in frequency domain');
xlabel('Hz');
ylabel('magnitude');
grid;
subplot(3,1,2)
signalnxdc=signaln-mean(signaln);
fsignaln=fft(signalnxdc);
plot((1:n/2)/n*fsn,abs(fsignaln(1:n/2)));
title('Noisy signal in frequency domain');
xlabel('Hz');
ylabel('magnitude');
grid;
%Comparison of Original and Noisy Signal in Frequency domain:
subplot(3,1,3)
plot((1:n/2)/n*fsn,abs(fsignaln(1:n/2)),(1:n/2)/n*fso,abs(fsignalo(1:n/2)));
title('Comparison of Original signal and noisy signal in frequency domain');
xlabel('Hz');
ylabel('magnitude');
grid;

%Filter Specificationns
ft= 20000;
fp = 5000;
fs = 6000;
delta_p=0.1;
delta_s = 56; %for Blackman
wc = (fs+fp)*0.5/(ft/2);
df = (fs-fp)/(ft/2);
N = ceil(5.5/df);
ETM4096 DIGITAL SIGNAL PROCESSING 2020

%Filtering
h = fir1(N,wc);
sf = filter(h,1,signaln);
%save signal
audiowrite('1171302489Q3.1f.wav',sf,20000)

%Filtered Signal
figure(3);
subplot(2,1,1)
sfxdc=sf-mean(sf);
fsf=fft(sfxdc);
plot((1:n/2)/n*fsn,abs(fsf(1:n/2)));
title('Filtered signal in frequency domain');
xlabel('Hz');
ylabel('magnitude');
grid;
%Comparison of Original and Noisy Signal in Frequency domain:
subplot(2,1,2)
plot((1:n/2)/n*fsn,abs(fsignaln(1:n/2)),(1:n/2)/n*fso,abs(fsf(1:n/2)));
title('Comparison of Filtered signal and noisy signal in frequency domain');
xlabel('Hz');
ylabel('magnitude');
grid;

%stem(h)
figure(4)
freqz(h)

figure(5);
subplot(2,1,1)
[H,w]=freqz(h); %get Frequency Response%
plot(w/pi,abs(H));grid
title('Frequency response of the h[n]')
xlabel('Frequency')
ylabel('Amplitude')
subplot(2,1,2)
plot(w*ft/2/pi,abs(H));grid
title('Frequency response of the h[n]')
xlabel('Frequency')
ylabel('Amplitude')

figure(6)
k= impz(h);
stem(k)
title('Impulse response of the h[n]')
xlabel('Frequency')
ylabel('Amplitude')
ETM4096 DIGITAL SIGNAL PROCESSING 2020
ETM4096 DIGITAL SIGNAL PROCESSING 2020

IIR filter:
%IIR apps
[signalo,fso]=audioread('1171302489o.wav');
n=length(signalo);
ft=20000;
fso=ft
fsn=ft;
% 1 seconds=pi/43200
t=0:1:65535;
x1=0.02*cos(500*t);
x1=transpose(x1);
signaln=signalo+x1;
%save signal
audiowrite('1171302489Q3.2n.wav',signaln,20000)

%Original and Noisy Signal in Time domain:


figure(1);
subplot(2,1,1)
plot(signalo);
title('Original signal in time domain');
xlabel('second');
subplot(2,1,2)
plot(signaln);
title('Noisy signal in time domain');
xlabel('second');

%Original and Noisy Signal in Frequency domain:


figure(2);
subplot(3,1,1)
signaloxdc=signalo-mean(signalo);
fsignalo=fft(signaloxdc);
plot((1:n/2)/n*fso,abs(fsignalo(1:n/2)));
title('Original signal in frequency domain');
xlabel('Hz');
ylabel('magnitude');
grid;
%figure(4)
subplot(3,1,2)
signalnxdc=signaln-mean(signaln);
fsignaln=fft(signalnxdc);
plot((1:n/2)/n*fsn,abs(fsignaln(1:n/2)));
title('Noisy signal in frequency domain');
xlabel('Hz');
ylabel('magnitude');
grid;
%Comparison of Original and Noisy Signal in Frequency domain:
subplot(3,1,3)
plot((1:n/2)/n*fsn,abs(fsignaln(1:n/2)),(1:n/2)/n*fso,abs(fsignalo(1:n/2)));
title('Comparison of Original signal and Noisy signal in frequency domain');
xlabel('Hz');
ylabel('magnitude');
grid;

ft = 20000;
fp1 = 5000;
fs1 = 6000;
delta_p=0.1;
delta_s = 56;
wp1=fp1/(ft/2);
ws1=fs1/(ft/2);
ETM4096 DIGITAL SIGNAL PROCESSING 2020

%Filtered Signal
figure(3);
subplot(2,1,1)
sfxdc=sf-mean(sf);
fsf=fft(sfxdc);
plot((1:n/2)/n*fsn,abs(fsf(1:n/2)));
title('Filtered signal in frequency domain');
xlabel('Hz');
ylabel('magnitude');
grid;
%Comparison of Filtered and Noisy Signal in Frequency domain:
subplot(2,1,2)
plot((1:n/2)/n*fsn,abs(fsignaln(1:n/2)),(1:n/2)/n*fso,abs(fsf(1:n/2)));
title('Comparison of Filtered signal and noisy signal in frequency domain');
xlabel('Hz');
ylabel('magnitude');
grid;

%stem(h)
figure(4)
freqz(h)

[N, Wn] = buttord(wp1, ws1, delta_p, delta_s);


[B,A] = butter(N,Wn);
sf=filter(B,A,signaln);
[H,w]=freqz(B,A); %get Frequency Response%
figure(5)
subplot(2,1,1)
plot(w/pi,abs(H));grid
title('Frequency response of the h[n]')
xlabel('Frequency')
ylabel('Amplitude')
subplot(2,1,2)
plot(w*ft/2/pi,abs(H));grid
title('Frequency response of the h[n]')
xlabel('Frequency')
ylabel('Amplitude')
%save signal
audiowrite('1171302489Q3.2i.wav',sf,20000)

figure(6)
k= impz(h);
stem(k)
title('Impulse response of the h[n]')
xlabel('Frequency')
ylabel('Amplitude')
ETM4096 DIGITAL SIGNAL PROCESSING 2020
ETM4096 DIGITAL SIGNAL PROCESSING 2020

Question 4 [15 marks]

Design and implement Finite Infinite Impulse Response (FIR) and Infinite Impulse
Response (IIR) high-pass digital filters with the following parameters (requirements):
1) pass-band edge frequency 7 kHz
2) stop-band frequency 6 kHz
3) sampling frequency: Select a suitable frequency. Justify your selection.
4) maximal allowed passband ripple is 0.1 dB
5) stop-band attenuation must be at least ( +20) dB
6) Plot the impulse response, approximation error, and magnitude response (in
dB) of the designed filter using OCTAVE/SCILAB.
7) Using the original audio for DSP2 LAB, add a suitable noise for this
filter.
8) Filter the noisy audio to show that the filter is designed correctly.

= summation of each digit of your SID. E.g. 1+1+1+1+1+1+1+1+1+1 = 10

SOLUTION:
ETM4096 DIGITAL SIGNAL PROCESSING 2020

FIR filter:
close all;
%FIR apps

[signalo,fso]=audioread('1171302489o.wav');
n=length(signalo);
ft=20000;
fso=ft
fsn=ft;
% 1 seconds=pi/43200
t=0:1:65535;
x1=0.02*cos(6000*t);
x1=transpose(x1);
signaln=signalo+x1;
%save signal
audiowrite('1171302489Q4.1n.wav',signaln,20000)

%Original and Noisy Signal in Time domain:


figure(1);
subplot(2,1,1)
plot(signalo);
title('Original signal in time domain');
xlabel('second');
subplot(2,1,2)
plot(signaln);
title('Noisy signal in time domain');
xlabel('second');

%Original and Noisy Signal in Frequency domain:


figure(2);
subplot(3,1,1)
signaloxdc=signalo-mean(signalo);
fsignalo=fft(signaloxdc);
plot((1:n/2)/n*fso,abs(fsignalo(1:n/2)));
title('Original signal in frequency domain');
xlabel('Hz');
ylabel('magnitude');
grid;
subplot(3,1,2)
signalnxdc=signaln-mean(signaln);
fsignaln=fft(signalnxdc);
plot((1:n/2)/n*fsn,abs(fsignaln(1:n/2)));
title('Noisy signal in frequency domain');
xlabel('Hz');
ylabel('magnitude');
grid;
%Comparison of Original and Noisy Signal in Frequency domain:
subplot(3,1,3)
plot((1:n/2)/n*fsn,abs(fsignaln(1:n/2)),(1:n/2)/n*fso,abs(fsignalo(1:n/2)));
title('Comparison of Original signal and noisy signal in frequency domain');
xlabel('Hz');
ylabel('magnitude');
grid;

%Filter Specificationns
ft= 20000;
fp = 7000;
fs = 6000;
delta_p=0.1;
delta_s =56; %for Hamming
wc = (fs+fp)*0.5/(ft/2);
df = (fp-fs)/(ft/2);
ETM4096 DIGITAL SIGNAL PROCESSING 2020

N = ceil(5.5/df);
%Filtering
h = fir1(N,wc,"high");
sf = filter(h,1,signaln);
%save signal
audiowrite('1171302489Q4.1f.wav',sf,20000)

%Filtered Signal
figure(3);
subplot(2,1,1)
sfxdc=sf-mean(sf);
fsf=fft(sfxdc);
plot((1:n/2)/n*fsn,abs(fsf(1:n/2)));
title('Filtered signal in frequency domain');
xlabel('Hz');
ylabel('magnitude');
grid;
%Comparison of Original and Noisy Signal in Frequency domain:
subplot(2,1,2)
plot((1:n/2)/n*fsn,abs(fsignaln(1:n/2)),(1:n/2)/n*fso,abs(fsf(1:n/2)));
title('Comparison of Filtered signal and noisy signal in frequency domain');
xlabel('Hz');
ylabel('magnitude');
grid;

%stem(h)
figure(4)
freqz(h)

figure(5);
subplot(2,1,1)
[H,w]=freqz(h); %get Frequency Response%
plot(w/pi,abs(H));grid
title('Frequency response of the h[n]')
xlabel('Frequency')
ylabel('Amplitude')
subplot(2,1,2)
plot(w*ft/2/pi,abs(H));grid
title('Frequency response of the h[n]')
xlabel('Frequency')
ylabel('Amplitude')

figure(6)
k= impz(h);
stem(k)
title('Impulse response of the h[n]')
xlabel('Frequency')
ylabel('Amplitude')
ETM4096 DIGITAL SIGNAL PROCESSING 2020
ETM4096 DIGITAL SIGNAL PROCESSING 2020

IIR filter:
%IIR apps
[signalo,fso]=audioread('1171302489o.wav');
n=length(signalo);
ft=20000;
fso=ft
fsn=ft;
% 1 seconds=pi/43200
t=0:1:65535;
x1=0.02*cos(6000*t);
x1=transpose(x1);
signaln=signalo+x1;
%save signal
audiowrite('1171302489Q4.2n.wav',signaln,20000)

%Original and Noisy Signal in Time domain:


figure(1);
subplot(2,1,1)
plot(signalo);
title('Original signal in time domain');
xlabel('second');
subplot(2,1,2)
plot(signaln);
title('Noisy signal in time domain');
xlabel('second');

%Original and Noisy Signal in Frequency domain:


figure(2);
subplot(3,1,1)
signaloxdc=signalo-mean(signalo);
fsignalo=fft(signaloxdc);
plot((1:n/2)/n*fso,abs(fsignalo(1:n/2)));
title('Original signal in frequency domain');
xlabel('Hz');
ylabel('magnitude');
grid;
subplot(3,1,2)
signalnxdc=signaln-mean(signaln);
fsignaln=fft(signalnxdc);
plot((1:n/2)/n*fsn,abs(fsignaln(1:n/2)));
title('Noisy signal in frequency domain');
xlabel('Hz');
ylabel('magnitude');
grid;
%Comparison of Original and Noisy Signal in Frequency domain:
subplot(3,1,3)
plot((1:n/2)/n*fsn,abs(fsignaln(1:n/2)),(1:n/2)/n*fso,abs(fsignalo(1:n/2)));
title('Comparison of Original signal and noisy signal in frequency domain');
xlabel('Hz');
ylabel('magnitude');
grid;

ft = 20000;
fp1 = 7000;
fs1 = 6000;
delta_p=0.1;
delta_s = 56;
wp1=fp1/(ft/2);
ws1=fs1/(ft/2);

%Filtered Signal
ETM4096 DIGITAL SIGNAL PROCESSING 2020

figure(3);
subplot(2,1,1)
sfxdc=sf-mean(sf);
fsf=fft(sfxdc);
plot((1:n/2)/n*fsn,abs(fsf(1:n/2)));
title('Noisy signal in frequency domain');
xlabel('Hz');
ylabel('magnitude');
grid;
%Comparison of Filtered and Noisy Signal in Frequency domain:
subplot(2,1,2)
plot((1:n/2)/n*fsn,abs(fsignaln(1:n/2)),(1:n/2)/n*fso,abs(fsf(1:n/2)));
title('Comparison of Filtered signal and noisy signal in frequency domain');
xlabel('Hz');
ylabel('magnitude');
grid;

%stem(h)
figure(4)
freqz(h)

[N, Wn] = buttord(wp1, ws1, delta_p, delta_s);


[B,A] = butter(N,Wn,"high");
sf=filter(B,A,signaln);
[H,w]=freqz(B,A); %get Frequency Response%
figure(5)
subplot(2,1,1)
plot(w/pi,abs(H));grid
title('Frequency response of the h[n]')
xlabel('Frequency')
ylabel('Amplitude')
subplot(2,1,2)
plot(w*ft/2/pi,abs(H));grid
title('Frequency response of the h[n]')
xlabel('Frequency')
ylabel('Amplitude')
%save signal
audiowrite('1171302489Q4.2i.wav',sf,20000)

figure(6)
k= impz(h);
stem(k)
title('Impulse response of the h[n]')
xlabel('Frequency')
ylabel('Amplitude')
ETM4096 DIGITAL SIGNAL PROCESSING 2020
ETM4096 DIGITAL SIGNAL PROCESSING 2020

Question 5 [40 Marks]

(a)
Download the file “etm4096assdata1” [1]. The file contains the waveform of a signal
embedded with the noise with frequency of fn, and the sampling frequency is fT.

(i) Plot the signal in time and frequency domain


(ii) Determine the value for fn and fT.
(iii) Design an IIR and FIR filter to remove the noise.
(iv) Design a notch filter to remove the noise.
(v) Plot the filtered signal in time and frequency domain.
(vi) Compare the results of the filtered signals in (iii) and (iv).

(b)
Repeat (a) for the following signals: etm4096assdata2, etm4096assdata3 and
etm4096assdata4 [1]

[1] Google Classroom.

SOLUTION:
ETM4096 DIGITAL SIGNAL PROCESSING 2020

etm4096assdata1

(i)
signaln = importdata('etm4096assdata1.txt');
fsn = 150
fT = 2*fsn
n=length(signaln)
%save signal
audiowrite('1171302489Q5.1n.wav',signaln,200)

figure(1);
subplot(2,1,1)
plot(signaln);
title('Noisy signal in time domain');
xlabel('second');
grid;
% Noisy Signal in Frequency domain:
subplot(2,1,2)
signalnxdc=signaln-mean(signaln);
fsignaln=fft(signalnxdc);
plot((1:n/2)/n*fT,abs(fsignaln(1:n/2)));
title('Noisy signal in frequency domain');
xlabel('Hz');
ylabel('magnitude');
grid;
ETM4096 DIGITAL SIGNAL PROCESSING 2020

(ii)
Based on Figure above, the noise frequency (fn) is around 150Hz.
Sampling frequency (fT) is 2 times the frequency of signal = 300Hz

(iii) & (v)


FIR Filter:
%Filter Specifications
fp1 = 10;
fp2 = 25;
fs1 = 14;
fs2 = 20;
delta_p=3;
delta_s = 50;
wc1 = ((fs1+fp1)*0.5)/fsn;
wc2 = ((fs2+fp2)*0.5)/fsn;
df = (fs1-fp1)/fsn;
N = ceil(3.3/df);
%Filtering
h = fir1(N,[wc1,wc2],'stop');
sf = filter(h,1,signaln);
%save signal
audiowrite('1171302489Q5.1f.wav',sf,200)

%Filtered Signal
figure(2);
subplot(3,1,1)
plot(sf);
title('Filtered signal in time domain');
xlabel('second');
grid;
subplot(3,1,2)
sfxdc=sf-mean(sf);
fsf=fft(sfxdc);
plot((1:n/2)/n*fsn,abs(fsf(1:n/2)));
title('Filtered signal in frequency domain');
xlabel('Hz');
ylabel('magnitude');
grid;
%Comparison of Original and Noisy Signal in Frequency domain:
subplot(3,1,3)
plot((1:n/2)/n*fT,abs(fsignaln(1:n/2)),(1:n/2)/n*fT,abs(fsf(1:n/2)));
title('Comparison of Filtered signal and Noisy signal in frequency
domain');
xlabel('Hz');
ylabel('magnitude');
grid;
ETM4096 DIGITAL SIGNAL PROCESSING 2020
ETM4096 DIGITAL SIGNAL PROCESSING 2020

IIR Filter:
%Butterworth
wp1=fp1/fsn;
ws1=fs1/fsn;
wp2=fp2/fsn;
ws2=fs2/fsn;
[N, Wn] = buttord([wp1,wp2], [ws1,ws2], delta_p, delta_s);
[B,A] = butter(N,Wn,'stop');
sf1=filter(B,A,signaln);
[H,w]=freqz(sf1); %get Frequency Response%
%save signal
audiowrite('1171302489Q5.1i.wav',sf1,200)

%Filtered Signal
figure(3);
subplot(3,1,1)
plot(sf1);
title('Filtered signal in time domain');
xlabel('second');
grid;
subplot(3,1,2)
sf1xdc=sf1-mean(sf1);
fsf1=fft(sf1xdc);
plot((1:n/2)/n*fsn,abs(fsf1(1:n/2)));
title('Filtered signal in frequency domain');
xlabel('Hz');
ylabel('magnitude');
grid;
%Comparison of Original and Noisy Signal in Frequency domain:
subplot(3,1,3)
plot((1:n/2)/n*fT,abs(fsignaln(1:n/2)),(1:n/2)/n*fT,abs(fsf1(1:n/2)));
title('Comparison of Filtered signal and Noisy signal in frequency
domain');
xlabel('Hz');
ylabel('magnitude');
grid;
ETM4096 DIGITAL SIGNAL PROCESSING 2020

(iv)
%notch filtering
[b,a]=iirnotch( 19/ ft, 35/ft );
[H,w]=freqz(b); %get Frequency Response%
sf2 = filter(b,a,signaln);
%save signal
audiowrite('1171302489Q5.1notch.wav',sf2,200)

%Filtered Signal
figure(4);
subplot(3,1,1)
plot(sf2);
title('Filtered signal in time domain');
xlabel('second');
grid;
subplot(3,1,2)
sf2xdc=sf2-mean(sf2);
fsf2=fft(sf2xdc);
plot((1:n/2)/n*fsn,abs(fsf2(1:n/2)));
title('Filtered signal in frequency domain');
xlabel('Hz');
ylabel('magnitude');
grid;
%Comparison of Original and Noisy Signal in Frequency domain:
subplot(3,1,3)
plot((1:n/2)/n*fT,abs(fsignaln(1:n/2)),(1:n/2)/n*fT,abs(fsf2(1:n/2)));
title('Comparison of Filtered signal and Noisy signal in frequency
domain');
xlabel('Hz');
ylabel('magnitude');
grid;

(vi)
For etm4096assdata1 signal, Hamming window in FIR filter has the best performance
compared to Butterworth in IIR filter and notch filter because it reduces the most noise.
ETM4096 DIGITAL SIGNAL PROCESSING 2020

etm4096assdata2

(i)
[signaln,fsn] = audioread('etm4096assdata2.wav');
fT = 2*fsn;
n=length(signaln);
%save signal
audiowrite('1171302489Q5.2n.wav',signaln,fsn)

figure(1);
subplot(2,1,1)
plot(signaln);
title('Noisy signal in time domain');
xlabel('second');
grid;
% Noisy Signal in Frequency domain:
subplot(2,1,2)
signalnxdc=signaln-mean(signaln);
fsignaln=fft(signalnxdc);
plot((1:n/2)/n*fT,abs(fsignaln(1:n/2)));
title('Noisy signal in frequency domain');
xlabel('Hz');
ylabel('magnitude');
grid;

(ii)
Based on Figure above, the noise frequency (fn) is around 8000Hz.
Sampling frequency (fT) is 2 times the frequency of signal = 16000Hz
ETM4096 DIGITAL SIGNAL PROCESSING 2020

(iii) & (v)


FIR Filter:
%Filter Specifications
fp1 = 1200;
fp2=1600;
fs1 = 1300;
fs2 = 1500;
delta_p=3;
delta_s = 50;
wc1 = ((fs1+fp1)*0.5)/fsn;
wc2 = ((fs2+fp2)*0.5)/fsn;
df = (fs1-fp1)/fsn;
N = ceil(3.3/df);
%Filtering
h = fir1(N,[wc1,wc2],'stop');
sf = filter(h,1,signaln);
%save signal
audiowrite('1171302489Q5.2f.wav',sf,fsn)

%Filtered Signal
figure(2);
subplot(3,1,1)
plot(sf);
title('Filtered signal in time domain');
xlabel('second');
grid;
subplot(3,1,2)
sfxdc=sf-mean(sf);
fsf=fft(sfxdc);
plot((1:n/2)/n*fsn,abs(fsf(1:n/2)));
title('Filtered signal in frequency domain');
xlabel('Hz');
ylabel('magnitude');
grid;
%Comparison of Original and Noisy Signal in Frequency domain:
subplot(3,1,3)
plot((1:n/2)/n*fT,abs(fsignaln(1:n/2)),(1:n/2)/n*fT,abs(fsf(1:n/2)));
title('Comparison of Filtered signal and Noisy signal in frequency
domain');
xlabel('Hz');
ylabel('magnitude');
grid;
ETM4096 DIGITAL SIGNAL PROCESSING 2020
ETM4096 DIGITAL SIGNAL PROCESSING 2020

IIR Filter:
%Butterworth
wp1=fp1/fsn;
ws1=fs1/fsn;
wp2=fp2/fsn;
ws2=fs2/fsn;
[N, Wn] = buttord([wp1,wp2], [ws1,ws2], delta_p, delta_s);
[B,A] = butter(N,Wn,'stop');
sf1=filter(B,A,signaln);
[H,w]=freqz(sf1); %get Frequency Response%
%save signal
audiowrite('1171302489Q5.2i.wav',sf1,fsn)

%Filtered Signal
figure(3);
subplot(3,1,1)
plot(sf1);
title('Filtered signal in time domain');
xlabel('second');
grid;
subplot(3,1,2)
sf1xdc=sf1-mean(sf1);
fsf1=fft(sf1xdc);
plot((1:n/2)/n*fsn,abs(fsf1(1:n/2)));
title('Filtered signal in frequency domain');
xlabel('Hz');
ylabel('magnitude');
grid;
%Comparison of Original and Noisy Signal in Frequency domain:
subplot(3,1,3)
plot((1:n/2)/n*fT,abs(fsignaln(1:n/2)),(1:n/2)/n*fT,abs(fsf1(1:n/2)));
title('Comparison of Filtered signal and Noisy signal in frequency
domain');
xlabel('Hz');
ylabel('magnitude');
grid;
ETM4096 DIGITAL SIGNAL PROCESSING 2020

(vi)
%notch filtering
ft=fsn/2;
[b,a]=iirnotch( 700 / ft, 2/ft );
[H,w]=freqz(b); %get Frequency Response%
sf2 = filter(b,a,signaln);
%save signal
audiowrite('1171302489Q5.2notch.wav',sf2,fsn)

%Filtered Signal
figure(4);
subplot(3,1,1)
plot(sf2);
title('Filtered signal in time domain');
xlabel('second');
grid;
subplot(3,1,2)
sf2xdc=sf2-mean(sf2);
fsf2=fft(sf2xdc);
plot((1:n/2)/n*fsn,abs(fsf2(1:n/2)));
title('Filtered signal in frequency domain');
xlabel('Hz');
ylabel('magnitude');
grid;
%Comparison of Original and Noisy Signal in Frequency domain:
subplot(3,1,3)
plot((1:n/2)/n*fT,abs(fsignaln(1:n/2)),(1:n/2)/n*fT,abs(fsf2(1:n/2)));
title('Comparison of Filtered signal and Noisy signal in frequency
domain');
xlabel('Hz');
ylabel('magnitude');
grid;

(vi)
For etm4096assdata2 signal, notch filter has the best performance compared to FIR and IIR
because it reduces the most noise.
ETM4096 DIGITAL SIGNAL PROCESSING 2020

etm4096assdata3

(i)
signaln = importdata('etm4096assdata3.dat');
fsn = 150
fT = 2*fsn
n=length(signaln)
%save signal
audiowrite('1171302489Q5.3n.wav',signaln,1000)

figure(1);
subplot(2,1,1)
plot(signaln);
title('Noisy signal in time domain');
xlabel('second');
grid;
% Noisy Signal in Frequency domain:
subplot(2,1,2)
signalnxdc=signaln-mean(signaln);
fsignaln=fft(signalnxdc);
plot((1:n/2)/n*fT,abs(fsignaln(1:n/2)));
title('Noisy signal in frequency domain');
xlabel('Hz');
ylabel('magnitude');
grid;

(ii)
Based on Figure above, the noise frequency (fn) is about 150Hz.
Sampling frequency (fT) is 2 times the frequency of signal = 300Hz
ETM4096 DIGITAL SIGNAL PROCESSING 2020

(iii) & (v)


FIR Filter:
%Filter Specifications
fpa1 = 10;
fpa2=25;
fsa1 = 14;
fsa2 = 21;
delta_p=3;
delta_s = 50;
wca1 = ((fsa1+fpa1)*0.5)/fsn;
wca2 = ((fsa2+fpa2)*0.5)/fsn;

fpb1 = 45;
fpb2=65;
fsb1 = 50;
fsb2 = 60;
delta_p=0.1;
delta_s = 50;
wcb1 = ((fsb1+fpb1)*0.5)/fsn;
wcb2 = ((fsb2+fpb2)*0.5)/fsn;

fpc1 = 80;
fpc2=100;
fsc1 = 85;
fsc2 = 95;
delta_p=0.1;
delta_s = 50;
wcc1 = ((fsc1+fpc1)*0.5)/fsn;
wcc2 = ((fsc2+fpc2)*0.5)/fsn;

fpd1 = 125;
fpd2=145;
fsd1 = 130;
fsd2 = 140;
delta_p=0.1;
delta_s = 50;
wcd1 = ((fsd1+fpd1)*0.5)/fsn;
wcd2 = ((fsd2+fpd2)*0.5)/fsn;

df = (fsa1-fpa1)/fsn;
N = ceil(3.3/df);
%Filtering
h = fir1(N,[wca1,wca2,wcb1,wcb2,wcc1,wcc2,wcd1,wcd2],'stop');
sf = filter(h,1,signaln);
%save signal
audiowrite('1171302489Q5.3f.wav',sf,1000)

%Filtered Signal
figure(2);
subplot(3,1,1)
plot(sf);
title('Filtered signal in time domain');
xlabel('second');
grid;
subplot(3,1,2)
sfxdc=sf-mean(sf);
fsf=fft(sfxdc);
plot((1:n/2)/n*fsn,abs(fsf(1:n/2)));
title('Filtered signal in frequency domain');
xlabel('Hz');
ETM4096 DIGITAL SIGNAL PROCESSING 2020

ylabel('magnitude');
grid;
%Comparison of Original and Noisy Signal in Frequency domain:
subplot(3,1,3)
plot((1:n/2)/n*fT,abs(fsignaln(1:n/2)),(1:n/2)/n*fT,abs(fsf(1:n/2)));
title('Comparison of Filtered signal and Noisy signal in frequency
domain');
xlabel('Hz');
ylabel('magnitude');
grid;
ETM4096 DIGITAL SIGNAL PROCESSING 2020

IIR Filter:
%Butterworth
fp1 = 10;
fp2 = 25;
fs1 = 14;
fs2 = 20;
delta_p_1=3;
delta_s_1 = 50;
wp1=fp1/(fT/2);
ws1=fs1/(fT/2);
[N, Wn] = buttord(wp1, ws1, delta_p_1, delta_s_1);
[B,A] = butter(N,Wn);
sf1=filter(B,A,signaln);
[H,w]=freqz(sf1); %get Frequency Response%
%save signal
audiowrite('1171302489Q5.3i.wav',sf1,1000)

%Filtered Signal
figure(3);
subplot(3,1,1)
plot(sf1);
title('Filtered signal in time domain');
xlabel('second');
grid;
subplot(3,1,2)
sf1xdc=sf1-mean(sf1);
fsf1=fft(sf1xdc);
plot((1:n/2)/n*fsn,abs(fsf1(1:n/2)));
title('Filtered signal in frequency domain');
xlabel('Hz');
ylabel('magnitude');
grid;
%Comparison of Original and Noisy Signal in Frequency domain:
subplot(3,1,3)
plot((1:n/2)/n*fT,abs(fsignaln(1:n/2)),(1:n/2)/n*fT,abs(fsf1(1:n/2)));
title('Comparison of Filtered signal and Noisy signal in frequency
domain');
xlabel('Hz');
ylabel('magnitude');
grid;
ETM4096 DIGITAL SIGNAL PROCESSING 2020

(iv)
%notch filtering
ft=fsn/2;
[b1,a1] = iirnotch(9/ft,2/ft);
[b2,a2] = iirnotch(27/ft,2/ft);
a0 = conv(a1, a2);
b0 = conv(b1, b2);
[b4,a4] = iirnotch(45/ft,2/ft);
[b5,a5] = iirnotch(51/ft,2/ft);
a3 = conv(a4, a5);
b3 = conv(b4, b5);
[b7,a7] = iirnotch(63/ft,2/ft);
[b8,a8] = iirnotch(69/ft,2/ft);
a6 = conv(a7, a8);
b6 = conv(b7, b8);
a9 = conv(a0, a3);%convolution of first half
b9 = conv(b0, b3);
a10 = conv(a9, a6);%convolution of last half
b10 = conv(b9, b6);
[H,w]=freqz(b10); %get Frequency Response%
sf2 = filter(b10,a10,signaln);
%save signal
audiowrite('1171302489Q5.3notch.wav',sf2,1000)

%Filtered Signal
figure(4);
subplot(3,1,1)
plot(sf2);
title('Filtered signal in time domain');
xlabel('second');
grid;
subplot(3,1,2)
sf2xdc=sf2-mean(sf2);
fsf2=fft(sf2xdc);
plot((1:n/2)/n*fsn,abs(fsf2(1:n/2)));
title('Filtered signal in frequency domain');
xlabel('Hz');
ylabel('magnitude');
grid;
%Comparison of Original and Noisy Signal in Frequency domain:
subplot(3,1,3)
plot((1:n/2)/n*fT,abs(fsignaln(1:n/2)),(1:n/2)/n*fT,abs(fsf2(1:n/2)));
title('Comparison of Filtered signal and Noisy signal in frequency
domain');
xlabel('Hz');
ylabel('magnitude');
grid;
ETM4096 DIGITAL SIGNAL PROCESSING 2020

(vi)
For etm4096assdata3 signal, notch filter has the best performance compared to IIR and
Hamming window in FIR filter because it eliminates the noise.
ETM4096 DIGITAL SIGNAL PROCESSING 2020

etm4096assdata4

(i)
signaln = importdata('etm4096assdata4.dat');
fsn = 150
fT = 2*fsn
n=length(signaln)
%save signal
audiowrite('1171302489Q5.4n.wav',signaln,1000)

figure(1);
subplot(2,1,1)
plot(signaln);
title('Noisy signal in time domain');
xlabel('second');
grid;
% Noisy Signal in Frequency domain:
subplot(2,1,2)
signalnxdc=signaln-mean(signaln);
fsignaln=fft(signalnxdc);
plot((1:n/2)/n*fT,abs(fsignaln(1:n/2)));
title('Noisy signal in frequency domain');
xlabel('Hz');
ylabel('magnitude');
grid;

(ii)
Based on Figure above, the noise frequency (fn) is around 150Hz.
Sampling frequency (fT) is 2 times the frequency of signal = 300Hz.
ETM4096 DIGITAL SIGNAL PROCESSING 2020

(iii) & (v)


FIR Filter:
%Filtering using Hamming
fp1 = 5;
fs1 = 2;
delta_p=3;
delta_s = 50;
wc1 = ((fs1+fp1)*0.5)/fsn;
df = (fp1-fs1)/fsn;
N = ceil(3.3/df);
%Filtering
h = fir1(N,wc1,'high');
sf = filter(h,1,signaln);
%save signal
audiowrite('1171302489Q5.4f.wav',sf,1000)

%Filtered Signal
figure(2);
subplot(3,1,1)
plot(sf);
title('Filtered signal in time domain');
xlabel('second');
grid;
subplot(3,1,2)
sfxdc=sf-mean(sf);
fsf=fft(sfxdc);
plot((1:n/2)/n*fsn,abs(fsf(1:n/2)));
title('Filtered signal in frequency domain');
xlabel('Hz');
ylabel('magnitude');
grid;
%Comparison of Original and Noisy Signal in Frequency domain:
subplot(3,1,3)
plot((1:n/2)/n*fT,abs(fsignaln(1:n/2)),(1:n/2)/n*fT,abs(fsf(1:n/2)));
title('Comparison of Filtered signal and Noisy signal in frequency
domain');
xlabel('Hz');
ylabel('magnitude');
grid;
ETM4096 DIGITAL SIGNAL PROCESSING 2020
ETM4096 DIGITAL SIGNAL PROCESSING 2020

IIR Filter:
%Butterworth
wp1=fp1/fsn;
ws1=fs1/fsn;
[N, Wn] = buttord(wp1, ws1, delta_p, delta_s);
[B,A] = butter(N,Wn,'high');
sf1=filter(B,A,signaln);
[H,w]=freqz(sf1); %get Frequency Response%
%save signal
audiowrite('1171302489Q5.4i.wav',sf1,1000)

%Filtered Signal
figure(3);
subplot(3,1,1)
plot(sf1);
title('Filtered signal in time domain');
xlabel('second');
grid;
subplot(3,1,2)
sf1xdc=sf1-mean(sf1);
fsf1=fft(sf1xdc);
plot((1:n/2)/n*fsn,abs(fsf1(1:n/2)));
title('Filtered signal in frequency domain');
xlabel('Hz');
ylabel('magnitude');
grid;
%Comparison of Original and Noisy Signal in Frequency domain:
subplot(3,1,3)
plot((1:n/2)/n*fT,abs(fsignaln(1:n/2)),(1:n/2)/n*fT,abs(fsf1(1:n/2)));
title('Comparison of Filtered signal and Noisy signal in frequency
domain');
xlabel('Hz');
ylabel('magnitude');
grid;
ETM4096 DIGITAL SIGNAL PROCESSING 2020

(iv)
%notch filtering
ft=fsn/2;
[b,a]=iirnotch( 1/ft , 1/ft );
[H,w]=freqz(b); %get Frequency Response%
sf2 = filter(b,a,signaln);
%save signal
audiowrite('1171302489Q5.4notch.wav',sf2,1000)

%Filtered Signal
figure(4);
subplot(3,1,1)
plot(sf2);
title('Filtered signal in time domain');
xlabel('second');
grid;
subplot(3,1,2)
sf2xdc=sf2-mean(sf2);
fsf2=fft(sf2xdc);
plot((1:n/2)/n*fsn,abs(fsf2(1:n/2)));
title('Filtered signal in frequency domain');
xlabel('Hz');
ylabel('magnitude');
grid;
%Comparison of Original and Noisy Signal in Frequency domain:
subplot(3,1,3)
plot((1:n/2)/n*fT,abs(fsignaln(1:n/2)),(1:n/2)/n*fT,abs(fsf2(1:n/2)));
title('Comparison of Filtered signal and Noisy signal in frequency
domain');
xlabel('Hz');
ylabel('magnitude');
grid;

(vi)
For etm4096assdata4 signal, notch filter performs the best compared to FIR and IIR filter
because it eliminates the noise from original signal.

You might also like