You are on page 1of 9

Laboratory Report

Q1. Formulate the Fourier Series expression for the given periodic waveform of the triangular wave
from the data given in Figure Q1. A general expression up to n = ∞ is expected. Assume that the
peak-to-peak amplitude, A = 10 V, the period is 25 ms and the DC value is 4 V.

Matlab Code:
clc
close all
clear all

syms t n
T = 25e-3 ;
u(t) = heaviside(t);
f1(t) = ((-80*t+2)*u(t)+(160*t-8)*u(t-50e-3));

a0= (1/T)*int(f1(t),t,0,T);
a(n)=(2/T)*int(f1(t)*cos(2*pi*t*n/T),t,0,T);
b(n)=(2/T)*int(f1(t)*sin(2*pi*t*n/T),t,0,T);

f(t)=2.6*(a0+symsum(a(n)*cos(n*2*pi*t/
T),n,1,300)+symsum(b(n)*sin(n*2*pi*t/T),n,1,100));

ezplot(f(t),[-0.75 0.75]);
xlabel('t')
ylabel('f(t)')
title('Triangular wave using fourier series')
grid on

Plot:
Q2. Create with the assistance of MATLAB and its associated toolboxes a discrete-time domain plot
with noise added of the ideal triangular wave from Q1 for 0 ൑ 𝑡 ൑ 0.25 𝑠. You may assume that the
wave was sampled at fs = 800 Hz with NFFT = 16384 discretetime samples and that you may use the
MATLAB function, “randn” to add noise to the triangular wave. Figure Q2 serves as a guide to
construct the time-domain plot.

(a) Compose the MATLAB codes used to generate both the discrete-time noisy triangular wave
as well as the single sided frequency log magnitude plots.

Matlab Code:
clc
clear all
close all

fs=800;
t=0:0.25/fs:16384/fs;
Q=length(t);
a=zeros(size(t));
for k=1:2.5:9
a=a+(8./((k*pi).^2)).*cos(k.*2.*pi.*t);
end
s=0.25.*randn(1,Q);
b=a+s;
plot(t,b);
xlabel('t');
ylabel('x(t)+b(t)');
title('triangular wave with noise');
figure
c=fft(b);
c=c(1:0.5*Q+1);
f=0:fs/Q:0.5*fs;
plot(f,abs(c)./Q);
xlabel('freq(Hz)');
ylabel('c(f)');
title('single sided magnitude response');

(b) Construct the discrete-time and log magnitude single-sided frequency plots from the
MATLAB codes used in Question 2(a).

Plots:
Q3. Implement a comb filter utilizing the concept of a parallel bank of IIR filters with the
Cheby2 Filter Template utilizing the concept of Figure Q3 to remove the noise from the noisy
triangular wave constructed in Q2. The filter specifications for each of the “bristles” of the
comb filter should use the following:
Centre frequencies: 40, 120, 200, 280 and 360 Hz
Bandwidth: 2 Hz
Stopband: ± 3 Hz from centre frequency
Passband Ripple: 1 dB
Stopband Attenuation: 60 dB

(a) For each of the passbands specified, construct the MATLAB code used to obtain the
desired specifications. Use an IIR filter template with Elliptic transfer function

(b) Set up the MATLAB code for the combined impulse response output from the five filters
in the bank using a 8192 sample unit sample response, showing the impulse response
plot in discrete-time for a 3 second duration.

(c) Use the designed parallel filter bank to filter the noisy triangular wave of Q2. You will
need to provide the discrete-time as well as the log magnitude single sided spectrum
plots of the filtered noisy triangular wave. The MATLAB code is not required. You may
use “edit plot” to provide the clearest possible plot indicating the effectiveness of the
filtering process of the parallel comb filter bank and filter latency. (Hint: choose first
second of the output). Comment on the latency and DC offset, by explaining the filtering
action of the parallel filter bank.

clc, close all


%% Initialization
Fs = 6000; % Sampling Rate
T = 1/Fs;
S = 499; % No of Samples
n = 0:1:S;
N = length(n);
%x = (exp(0.2)*cos(2*pi*1000*n*T))+(sqrt(3)*cos(2*pi*1700*n*T));
fs=800;
t=0:0.25/fs:16384/fs;
Q=length(t);
a=zeros(size(t));
for k=1:2.5:9
a=a+(8./((k*pi).^2)).*cos(k.*2.*pi.*t);
end
s=0.25.*randn(1,Q);
b=a+s;

bn = awgn(x,1,'measured');
t = n/Fs;

FL = 40/(Fs/2);
FH = 120/(Fs/2);
[b a]= ellip(2,[FL FH],'bandpass');
h = tf(b,a) % transfer function
%% Bandstop Filter :
FL1 = 200/(Fs/2);
FH1 = 360/(Fs/2);
[b1 a1] = ellip(2,[FL1 FH1],'stop');
h1 = tf(b1,a1) % transfer function

%% Apply both filter on the Signal


filtered_xn = filter(b,a,bn); % Apply Bandpass on noisy
signal
filtered_xn1 = filter(b1,a1,filtered_xn); % Apply Bandstop on noisy
signal

% apply FFT on the Filtered Signal


y1 = (2*abs(fft(filtered_xn1)))/N; % Amplitude Spectrum k>0
y1(1) = y(1)/2; % Amplitude Spectrum k=0

%% Plot all waveforms


figure(1)
% input signal
subplot(2,2,1);
plot(n,x);
xlabel('No of Samples'),ylabel('x(n)'),title('Input Signal of x(n)'),grid
on;

% Single Sided Amplitude Spectrum


subplot(2,2,2);
plot(f,y(1:N/2));
xlabel('Frequency(Hz)'),ylabel('Amplitude |A(k)|'),title('Single Sided
Amplitude Spectrum (input)'),grid on;

% Filtered Signal
subplot(2,2,3);
plot(1:N,filtered_xn1);
xlabel('No of Samples'),ylabel('Sample Value'),title('Filtered
Signal'),grid on;

% Single Sided Amplitude Spectrum (filtered)


subplot(2,2,4);
plot(f,y1(1:N/2));
xlabel('Frequency(Hz)'),ylabel('Amplitude |A(k)|'),title('Single Sided
Amplitude Spectrum (filtered)'),grid on;

% Magnitude & Phase Response (Bandpass)


figure(2)
freqz(b,a)
title('Magnitude & Phase Response (Bandpass)');

% Magnitude & Phase Response (Bandstop)


figure(21)
freqz(b1,a1)
title('Magnitude & Phase Response (Bandstop)');

Plots:
Q4. Recommend with the MATLAB function, “iirnotch” a notch filter design to remove the fifth
harmonic frequency component of the filtered triangular wave from the waveform of Q3(c). The
notch filter should have a bandwidth of 5 Hz, with a 3 dB dip at the frequency extremes. The
following codes and plots are required:

(a) MATLAB code to meet the design specification.

(b) A single sided log magnitude plot of the notch filtered waveform using the waveform of part
Q3(c) as the input. Comment on the effectiveness of the notch filter. Candidates are
reminded to use a consistent value of sampling frequency for all the four questions of this
assignment (hint: use fs = 800 Hz) to prevent frequency aliasing.

Matlab Code:

clc, close all


%% Initialization
Fs = 6000; % Sampling Rate
T = 1/Fs;
S = 499; % No of Samples
n = 0:1:S;
N = length(n);
%x = (exp(0.2)*cos(2*pi*1000*n*T))+(sqrt(3)*cos(2*pi*1700*n*T));
fs=800;
t=0:0.25/fs:16384/fs;
Q=length(t);
a=zeros(size(t));
for k=1:2.5:9
a=a+(8./((k*pi).^2)).*cos(k.*2.*pi.*t);
end
s=0.25.*randn(1,Q);
b=a+s;

bn = awgn(x,1,'measured');
t = n/Fs;

FL = 40/(Fs/2);
FH = 120/(Fs/2);
[b a]= ellip(2,[FL FH],'bandpass');
h = tf(b,a) % transfer function

FL1 = 200/(Fs/2);
FH1 = 360/(Fs/2);
[b1 a1] = ellip(2,[FL1 FH1],'stop');
h1 = tf(b1,a1) % transfer function

%% Apply both filter on the Signal


filtered_xn = filter(b,a,bn); % Apply Bandpass on noisy
signal
filtered_xn1 = filter(b1,a1,filtered_xn); % Apply Bandstop on noisy
signal

figure
plot(1:N,filtered_xn1);
xlabel('No of Samples'),ylabel('Sample Value'),title('Filtered
Signal'),grid on;

wo = 60/(300/2);
bw = wo/35;
[b,a] = iirnotch(wo,bw);

fvtool(b,a)

You might also like