NomanHamza13021 Lab14 of DSP

You might also like

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

Department of Electrical Engineering

Faculty of Engineering & Applied Sciences


Riphah International University, Islamabad, Pakistan

Program: B.Sc. Electrical Engineering Semester: VII

Subject: EE-443-L Digital signal Processing Date: ………

Experiment No.14: To Design suitable digital filters to remove unwanted frequency from
signals.
• To designed and implement the digital filter to remove unwanted frequency

Inquiry Level = 1

Noman Hamza 13021


Haris Khan 2899
Um-e-Habiba 13762

Performance Lab Report

Description Total Marks Description Total Marks


Marks Obtained Marks Obtained
Ability to 5 Organization/Structure 5
conduct
Experiment

Simulation & 5 MATLAB Code & 5


Results Results
Total Marks obtained

Remarks (if any): ………………………………….

Name & Signature of faculty: …………………………………


LAB REPORT

Tittle:
To Design suitable digital filters to remove unwanted frequency from signals.
Objectives:
• To design and implement the digital filtering on MATLAB.
• To examine the waveform of digital filtering to remove unwanted frequency on MATLAB.
Introduction:
Digital filtering is one of the most useful types of processing for discrete-time signals. Digital
filters arise naturally in the modeling of physical systems and have been used for numerical
calculations at least since the invention of calculus in the seventeenth century. They are used for
separating signals from noise and for frequency analysis, an operation which often reveals
important features in the signal. The theory of digital filters is so well developed that it is usually
easy to design filters that meet even complicated sets of specifications. Much of this theory directly
parallels the theory of analog filters which, historically, was developed first. Indeed, even in the
present time, digital filters are often intended to simulate the operation of continuous-time physical
systems. In these notes, we will emphasize digital filters because of their importance in the
laboratory exercises, and because the underlying mathematical concepts are easier to understand
with discrete-time signals than with continuous-time signals.
The name “filter” is used because these signal-processing elements typically “pass” or amplify
certain frequency components of the signal, while they “stop” or attenuate others. Such a
“frequency domain” analysis of digital filters will be examined in later lab activities. The goal of
this lab is to define digital filters and describe their basic properties.
Filters defined by linear difference equations
A discrete-time system is any mathematical transformation that maps a discrete-time input signal
x[n] into an output signal y[n]. We will begin by considering discrete-time systems defined by a
linear, constant-coefficient difference equation (LCCDE), which constitute an important class of
digital filters:

This Equation can be used to compute the output y[n] at time n from a finite number of previous
values of the input (x[n]) and the output. The maximum of the numbers M and K is called the
order of the filter. If the input signal is only defined after a certain time, say for
n ≥ n0, then values of both the input and output for a short time prior to n0 must be known in order
to initialize the difference equation. Specifically, y[n] must be known for
n0 − K ≤ n ≤ n0 − 1, and x[n] for n0 − M ≤ n ≤ n0 − 1. In many applications, it is justified to
assume that these values are zero if the system has not received any input for a long time before
n0, so that its response to any previous inputs has decayed to zero.
Examples of digital filters defined by LCCDEs
1. Simple gain, or amplifier:
y[n] = Gx[n]
2. Delay of n0 samples:
y[n] = x[n − n0]
These first two examples are essential in that any digital filter is a combination of gains and delays.
3. Two-point moving average:
y[n] = ½ (x[n]+ x[n − 1])
4. Euler’s formula for approximating the derivative of a continuous-time function:
𝑥[𝑛]− 𝑥[𝑛 − 1]
y[n] =
𝑇𝑠
where Ts is the sampling interval.
5. Averaging over N consecutive epochs of duration L:
1
y[n] = ∑𝑁−1
𝐾=0 𝑥[𝑛 − 𝑘𝐿]
𝑁
6. Trapezoidal integration formula:
𝑦[𝑛 − 1]+(𝑥[𝑛]+𝑥[𝑛−1])𝑇𝑠
y[n] =
2
7. Digital “leaky integrator”, or first-order lowpass filter:
y[n] = ay[n − 1] + x[n] with 0 < a < 1
This filter is the digital equivalent of an RC analog circuit. It is a useful “building block” for
designing complex filters.
Digital filters can be classified on the basis of the coefficients.
1. FIR filters. If all the ak coefficients in Eq. 6.1 are zero, then the output depends only on a
finite number of values of the input. Such filters are called finite impulse response (FIR),
all-zero, or moving average (MA) filters. The term “moving average” is clear because the
output is a weighted sum (or average) of samples of the input. Examples 1–5 above are
FIR filters.
2. IIR filters. If at least one of the ak coefficients in Eq. 6.1 is nonzero, the filter is said to
have an infinite impulse response (IIR). IIR filters include both autoregressive (AR) and
autoregressive, moving average (ARMA) filters, defined as follows:
(a) Autoregressive (AR) filters. If all of the bm coefficients except b0 are zero, the
output depends only on the current value of the input and a finite number of past
values of the output. Such filters are called all-pole, purely recursive, or
autoregressive (AR) filters. The term “autoregressive” means that the output is
approximately a sum of its own past values.
(b) Autoregressive, moving-avera ge (ARMA) filters. In the general case, both ak and
bm in Eq. 6.1 are nonzero, with K > 1 and M > 0. Such filters are called pole-zero or
autoregressive, moving-average (ARMA) filters.

Simulations on MATLAB Simulink:


function [pwav]=p_wav(x,a_pwav,d_pwav,t_pwav,li)
l=li;
a=a_pwav;
x=x+t_pwav;
b=(2*l)/d_pwav;
n=100;
p1=1/l;
p2=0;
for i = 1:n
harm1=(((sin((pi/(2*b))*(b-(2*i))))/(b-
(2*i))+(sin((pi/(2*b))*(b+(2*i))))/(b+(2*i)))*(2/pi))*cos((i*pi*x)/l);
p2=p2+harm1;
end
pwav1=p1+p2;
pwav=a*pwav1;

function [qwav]=q_wav(x,a_qwav,d_qwav,t_qwav,li)
l=li;
x=x+t_qwav;
a=a_qwav;
b=(2*l)/d_qwav;
n=100;
q1=(a/(2*b))*(2-b);
q2=0;
for i = 1:n
harm5=(((2*b*a)/(i*i*pi*pi))*(1-cos((i*pi)/b)))*cos((i*pi*x)/l);
q2=q2+harm5;
end
qwav=-1*(q1+q2);

function [qrswav]=qrs_wav(x,a_qrswav,d_qrswav,li)
l=li;
a=a_qrswav;
b=(2*l)/d_qrswav;
n=100;
qrs1=(a/(2*b))*(2-b);
qrs2=0;
for i = 1:n
harm=(((2*b*a)/(i*i*pi*pi))*(1-cos((i*pi)/b)))*cos((i*pi*x)/l);
qrs2=qrs2+harm;
end
qrswav=qrs1+qrs2;

function [swav]=s_wav(x,a_swav,d_swav,t_swav,li)
l=li;
x=x-t_swav;
a=a_swav;
b=(2*l)/d_swav;
n=100;
s1=(a/(2*b))*(2-b);
s2=0;
for i = 1:n
harm3=(((2*b*a)/(i*i*pi*pi))*(1-cos((i*pi)/b)))*cos((i*pi*x)/l);
s2=s2+harm3;
end
swav=-1*(s1+s2);

function [twav]=t_wav(x,a_twav,d_twav,t_twav,li)
l=li;
a=a_twav;
x=x-t_twav-0.045;
b=(2*l)/d_twav;
n=100;
t1=1/l;
t2=0;
for i = 1:n
harm2=(((sin((pi/(2*b))*(b-(2*i))))/(b-
(2*i))+(sin((pi/(2*b))*(b+(2*i))))/(b+(2*i)))*(2/pi))*cos((i*pi*x)/l);
t2=t2+harm2;
end
twav1=t1+t2;
twav=a*twav1;

function [uwav]=u_wav(x,a_uwav,d_uwav,t_uwav,li)
l=li;
a=a_uwav
x=x-t_uwav;
b=(2*l)/d_uwav;
n=100;
u1=1/l
u2=0
for i = 1:n
harm4=(((sin((pi/(2*b))*(b-(2*i))))/(b-
(2*i))+(sin((pi/(2*b))*(b+(2*i))))/(b+(2*i)))*(2/pi))*cos((i*pi*x)/l);
u2=u2+harm4;
end
uwav1=u1+u2;
uwav=a*uwav1;

Main Function:
x=0.01:0.01:2;
default=input('Press 1 if u want default ecg signal else press 2:\n');
if(default==1)
li=30/72;

a_pwav=0.25;
d_pwav=0.09;
t_pwav=0.16;

a_qwav=0.025;
d_qwav=0.066;
t_qwav=0.166;

a_qrswav=1.6;
d_qrswav=0.11;

a_swav=0.25;
d_swav=0.066;
t_swav=0.09;

a_twav=0.35;
d_twav=0.142;
t_twav=0.2;

a_uwav=0.035;
d_uwav=0.0476;
t_uwav=0.433;
else
rate=input('\n\nenter the heart beat rate :');
li=30/rate;

%p wave specifications
fprintf('\n\np wave specifications\n');
d=input('Enter 1 for default specification else press 2: \n');
if(d==1)
a_pwav=0.25;
d_pwav=0.09;
t_pwav=0.16;
else
a_pwav=input('amplitude = ');
d_pwav=input('duration = ');
t_pwav=input('p-r interval = ');
d=0;
end

%q wave specifications
fprintf('\n\nq wave specifications\n');
d=input('Enter 1 for default specification else press 2: \n');
if(d==1)
a_qwav=0.025;
d_qwav=0.066;
t_qwav=0.166;
else
a_qwav=input('amplitude = ');
d_qwav=input('duration = ');
t_qwav=0.166;
d=0;
end

%qrs wave specifications


fprintf('\n\nqrs wave specifications\n');
d=input('Enter 1 for default specification else press 2: \n');
if(d==1)
a_qrswav=1.6;
d_qrswav=0.11;
else
a_qrswav=input('amplitude = ');
d_qrswav=input('duration = ');
d=0;
end

%s wave specifications
fprintf('\n\ns wave specifications\n');
d=input('Enter 1 for default specification else press 2: \n');
if(d==1)
a_swav=0.25;
d_swav=0.066;
t_swav=0.09;
else
a_swav=input('amplitude = ');
d_swav=input('duration = ');
t_swav=0.09;
d=0;
end

%t wave specifications
fprintf('\n\nt wave specifications\n');
d=input('Enter 1 for default specification else press 2: \n');
if(d==1)
a_twav=0.35;
d_twav=0.142;
t_twav=0.2;
else
a_twav=input('amplitude = ');
d_twav=input('duration = ');
t_twav=input('s-t interval = ');
d=0;
end

%u wave specifications
fprintf('\n\nu wave specifications\n');
d=input('Enter 1 for default specification else press 2: \n');
if(d==1)
a_uwav=0.035;
d_uwav=0.0476;
t_uwav=0.433;
else
a_uwav=input('amplitude = ');
d_uwav=input('duration = ');
t_uwav=0.433;
d=0;
end

end
pwav=p_wav(x,a_pwav,d_pwav,t_pwav,li);

%qwav output
qwav=q_wav(x,a_qwav,d_qwav,t_qwav,li);

%qrswav output
qrswav=qrs_wav(x,a_qrswav,d_qrswav,li);
%swav output
swav=s_wav(x,a_swav,d_swav,t_swav,li);

%twav output
twav=t_wav(x,a_twav,d_twav,t_twav,li);

%uwav output
uwav=u_wav(x,a_uwav,d_uwav,t_uwav,li);
%ecg output
ecg=pwav+qrswav+twav+swav+qwav+uwav;
figure(1)
plot(x,ecg);

Results:
Press 1 if u want default ecg signal else press 2:
1
a = 0.0350
u1 = 2.4000
u2 = 0
Conclusion:
In today’s lab design and implement the digital filtering on ECG signal on MATLAB. then
examine the waveform of filtering on ECG signal on MATLAB. Then design and implement the
filtering in frequency domain on MATLAB. Then examine the waveform of filtering in
frequency domain on MATLAB. After today’s lab I am fully able to do this kind of experiment
on my own.

You might also like