Bioinformatics Book

You might also like

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

Bioinformatics

For Second Year Students

Prepared By
Dr. Ali Siam

2024
Chapter 1 Common Biological Signals and Patterns

Chapter 1

Common Biological Signals and Patterns

1.1 Fingerprint

Fingerprints are one of the many unique biometric signatures which we


can use to identify people very accurately.

The skin on the palms of our hands has a special pattern called friction
ridges that help us grab things effectively without slipping. These
patterns consist of ridges and valleys arranged in certain configurations
and is unique for each individual. Our finger tips also have them as you
can see from the above image.

When a finger comes in contact with a surface, the ridges make strong
contact with the surface. When we strongly grab something, the
moisture, grease, dirt, and dead skin cells on our finger can attach to
the surface of the material, leaving an impression we call a fingerprint.

1
Chapter 1 Common Biological Signals and Patterns

The recovery of fingerprints from a crime scene is an important


method of forensic science. Various forensic methods involving the
use of chemicals are used to extract such fingerprints from crime
scenes. But an optical fingerprint scanner works a bit differently, we
will discuss this later.

Intentional methods to obtain fingerprints from persons are by using


ink or other substances transferred from the peaks of friction ridges on
the skin to a smooth surface such as paper. Fingerprint records
normally contain impressions from the surface of the top joint of
fingers and thumbs, though fingerprints also typically record portions
of lower joint areas of the fingers.

Human fingerprints are unique, difficult to alter, and durable over the
life of an individual, making them suitable as long-term markers of
human identity. They may be employed by police or other authorities
to identify individuals who are suspicious in crimes, or to identify
people who are incapacitated or deceased and thus unable to identify
themselves, as in the aftermath of a natural disaster.

The matching of two fingerprints is among the most widely used and
most reliable biometric techniques. Fingerprint matching considers
only the obvious features of a fingerprint, by looking at the patterns of
fingerprints. These patterns are little ridges on the end of an
individual’s fingers and thumb that are arranged in a pattern of spirals
and loops.

2
Chapter 1 Common Biological Signals and Patterns

Fingerprint identification, known as dactyloscopy, is the process of


comparing two instances of friction ridge skin impressions from
human fingers to determine whether these impressions could have
come from the same individual. The flexibility of friction ridge skin
means that no two finger or palm prints are ever exactly alike in every
detail; even two impressions recorded immediately after each other
from the same hand may be slightly different. Fingerprint
identification, also referred to as individualization, involves an expert,
or an expert computer system operating under threshold scoring rules,
determining whether two friction ridge impressions are likely to have
originated from the same finger or palm.

In the early 19th century, people started to realize that no two persons
have exactly the same pattern of fingerprints to the extent that it varies
even in the case of identical twins. These patterns are formed during
the 12th week of gestation and remain permanent throughout a person’s
life. With this discovery that fingerprints are a unique method of
identifying individuals, Sir Francis Galton first introduced the
technique of comparing prints found at a crime scene with those of the
suspect. Later, Sir Edward Henry developed the system of classifying
fingerprints that was first adopted as the official system in England
which eventually spread throughout the world.

Edward Henry recognized that fingerprints could be described as


having three basic patterns: loop, whorl, and arch, which constitute 60–
65 percent, 30–35 percent, and 5 percent of all fingerprints,
respectively.

3
Chapter 1 Common Biological Signals and Patterns

Arch Loop Whorl

The three basic patterns of fingerprint ridges are:

• Arch: The ridges enter from one side of the finger, rise in the
center forming an arc, and then exit the other side of the finger.
• Loop: The ridges enter from one side of a finger, form a curve, and
then exit on that same side.
• Whorl: Ridges form circularly around a central point on the finger.

Scientists have found that family members often share the same
general fingerprint patterns (arch, loop, or whorl), leading to the belief
that these patterns are inherited.

Fingerprint as biometric (biological password)

• A fingerprint is a unique way of identifying a person. If you


compare a fingerprint with a key, you can say that you actually
have ten keys in a person’s hands, as each fingerprint is different.

4
Chapter 1 Common Biological Signals and Patterns

Even if one finger has a cut or the entire hand is in a cast, there
would still be enough fingers for making an identification.
• The fingerprint is a very certain method for identifying a person,
because all fingerprints are unique. Even identical twins have
different fingerprints.
• Compared with other methods of identification, such as a key,
access card, numerical code or a password, the fingerprint is very
secure. You cannot lose or forget it, and it cannot be stolen.
• It is also extremely practical, because you do not need to carry
anything with you. Your pockets can be empty, there is no need to
try and find anything at the bottom of your bag, and you can forget
those big bundles of keys.
• You can also save significant system management costs. In
extensive systems, such as access control in factories or offices or
visitor identification at sports clubs, there is no need for cards or
keys that need to be distributed, collected back again, or removed
from the register due to lost cards or keys. For example, when
using fingerprint identification, visitors can be granted access for
even just a single day.

Modern fingerprint sensors

A fingerprint sensor is an electronic device used to capture a digital


image of the fingerprint pattern. The captured image is called a live
scan. This live scan is digitally processed to create a biometric
template (a collection of extracted features) which is stored and used

5
Chapter 1 Common Biological Signals and Patterns

for matching. Many technologies have been used including optical,


capacitive, ultrasonic, and thermal.

• Optical scanners take a visual image of the fingerprint using a


digital camera.
• Capacitive scanners use capacitors and thus electrical current to
form an image of the fingerprint. This type is commonly used in
mobile phones fingerprint sensors.
• Ultrasonic fingerprint scanners use high frequency sound waves to
penetrate the epidermal (outer) layer of the skin to form an image
of the fingerprint.
• Thermal scanners sense the temperature differences on the contact
surface, in between fingerprint ridges and valleys.

Fingerprint identification

Fingerprint identification is based on pattern recognition where the


arches, loops and whorls of the fingerprint ridges are compared with
stored data. Identification is performed in four steps.

1. A picture is taken of the fingerprint. The picture can be taken


optically with a camera in the reader or electronically. The end
result is a digital black and white photograph of the ridges in the
fingerprint.
2. The fingerprint is then transformed into a numerical model which
stores the fingerprint’s unique features and characteristics.

6
Chapter 1 Common Biological Signals and Patterns

3. A recognized numerical model is compared with other numerical


model (or models) stored in the memory of the device to find
similarities and determine the degree to which they match.
4. The final decision is taken based on a threshold value for the
similarity level between the current model and the stored models to
determine if it is matched (or not matched) with any of models
stored in the memory. If the similarity level is above the threshold
value, the current model is accepted, otherwise, it will be rejected.

Usually, the threshold value determines the security level of the


fingerprint sensor. If you put the threshold level at high value, the
device accepts only the models with higher values of similarities
(more secure). On the other hand, if the threshold level is set at low
value, any model with a bit similarity with the stored models can be
accepted as one of trusted models (less secure).

Fingerprints are commonly matched by one (or both) of two


approaches: microscopic and macroscopic.

The microscopic approach is called minutia matching. The two minutia


types that are shown in figure below are a ridge ending and bifurcation.
An ending is a feature where a ridge terminates. A bifurcation is a
feature where a ridge splits from a single path to two paths at a Y-
junction. An approximate number of minutiae found in the fingerprint
is from 10 to 100. For matching purposes, each minutia is attributed
with some features. The features assigned for each minutia are type,
location (x, y), and direction (and other approaches use additional
features). These features are used to describe each minutia. A minutia
7
Chapter 1 Common Biological Signals and Patterns

template constitutes all minutiae features inside the fingerprint. All


minutiae templates are stored in a memory. At the verification stage,
the template of the person needed to be verified is compared against
templates inside the memory, and a matching score is obtained based
on the amount of similarities between the verified template and the
templates in the memory. Usually, the matching score is a range
between 0 and 100 (or 255 based on the classifier). Higher matching
score indicates higher confidence in a match.

Two other features are sometimes used in matching: core and delta.
The core can be thought of as the center of the fingerprint pattern. The
delta is a singular point from which three patterns deviate. The core
and delta locations can be used as landmark locations by which to
orientate two fingerprints for subsequent matching - though these
features are not present on all fingerprints.

The macroscopic approach to matching is called global pattern


matching or simply pattern matching. In this approach, the flow of
ridges is compared at all locations between a pair of fingerprint
images. The ridge flow constitutes a global pattern of the fingerprint.

8
Chapter 1 Common Biological Signals and Patterns

There are two methods of identification: a fingerprint to be identified is


compared with several stored fingerprints (1:N), or the fingerprint is
compared with a specific person’s fingerprint (1:1).

An example of the first method is a company’s access-control system.


This system uses stored fingerprint data to identify the fingerprint of a
person at the door, so that it can be verified whether the person to be
identified has the right to access the door.

An example of the second method is a permission-based system in


which each person has distinct permissions and grants that should be
uniquely assigned to that person.

Interfacing R307 optical fingerprint scanner with Arduino

Operation Principle

The fingerprint processing includes two stages: fingerprint enrollment


and fingerprint matching (the matching can be 1:1 or 1:N).

9
Chapter 1 Common Biological Signals and Patterns

During enrollment, the user is asked to enter the finger, needed to be


registered, two times. The system will process the two finger images,
generate a template of the finger based on processing results and store
the template.

During the matching process, the user puts the same finger on the
optical sensor, and the system will generate a template of the finger
and compare it with templates of the finger library.

For 1:1 matching, the system will compare the current finger with a
specific template stored in the Module. For 1:N matching, or
searching, the system will search the whole finger library for the
matching finger. In both circumstances, the system will return the
matching result, success or failure, based on the matching score.

Technical Parameters

• Supply voltage: DC 4.2 ~ 6.0V


• Supply current: Working current: 50mA (typical)
• Fingerprint image input time: < 0.3 seconds
• Window area: 14x18 mm
• Matching method: Comparison method (1: 1)
• Search method (1: N)
• Template file: 512 bytes
• Storage capacity: 1000 templates
• False acceptance rate (FAR): <0.001%
• False rejection rate (FRR): <1.0%
• Security level: 5 (1, 2, 3, 4, 5(highest))
• Search time: < 1.0 seconds
• Communication baud rate: (9600xN) bps Where N = 1 ~ 12
(default N = 6, i.e. 57600bps)

10
Chapter 1 Common Biological Signals and Patterns

FAR: is the rate that the sensor falsely accepts a fake template as a
genuine template.

FRR: is the rate that the sensor falsely rejects a genuine template.

Pinouts

Pin# Pin Name Color Details


1 5V Red Regulated 5V DC
2 GND Black Common Ground
3 TXD Yellow Data output - Connect to MCU RX
4 RXD White Data Input - Connect to MCU TX
Green Active Low output when there is touch on
5 TOUCH
sensor by finger
Blue Use this wire to give 3.3V to sensor instead
6 3.3V
of 5V

You can use the PC application to interface with the sensor through
your computer. The PC software can be downloaded from:
http://www.adafruit.com/datasheets/SFGDemoV2.0.rar

11
Chapter 1 Common Biological Signals and Patterns

Project description

In this project, we will implement a fingerprint-based system to control


2 LEDs; Green and Red. If the sensor identifies the fingerprint
successfully, the green LED will glow, if it cannot identify the
fingerprint, the red LED glows.

You can control other elements (such as motors, relays, etc.) based on
the matching result of the fingerprint sensor.

Required materials

- Arduino Uno
- R307 fingerprint sensor, or other similar fingerprint sensor.
- Green and Red LEDs
- 220 Ω resistor
- Jumper wires

12
Chapter 1 Common Biological Signals and Patterns

Connection

- R307 red wire → Arduino 5V


- R307 black wire → Arduino GND
- R307 yellow wire → Arduino digital pin 2
- R307 white wire → Arduino digital pin 3
- Green LED anode → Arduino digital pin 8
- Red LED anode → Arduino digital pin 9
- Green LED cathode → Arduino GND through a 220 Ω resistor
- Red LED cathode → Arduino GND through a 220 Ω resistor

Code

Installing and Using the Library

The first step in using the fingerprint sensor is to install the Adafruit
Fingerprint Library, which can be done using the Library Manager.
Open the Arduino IDE and navigate to Sketch > Include Library >
Manage Libraries.
13
Chapter 1 Common Biological Signals and Patterns

When the library manager loads search for “Fingerprint” and the first
result should be the Adafruit Fingerprint Sensor Library. Go ahead and
install it.

Below is a list of basic functions in the Adafruit Fingerprint Sensor


Library.
Function Description
Adafruit_Fingerprint(SoftwareSerial Instantiates sensor with Software
*ss, password) Serial.
@param ss: Pointer to
SoftwareSerial object.
@param password: 32-bit integer
password (default is 0).

14
Chapter 1 Common Biological Signals and Patterns

begin(uint32_t baudrate) Initializes serial interface and


baud rate.
@param baudrate: Sensor's baud
rate (usually 57600, 9600 or
115200).
verifyPassword(void) Verifies the sensors' access
password (default password is
0x0000000). A good way to
also check if the sensors is active
and responding.
getParameters(void) Get the sensors parameters:
status_reg, system_id, capacity,
security_level, device_addr,
packet_len and baud_rate.
getImage(void) Ask the sensor to take an image
of the finger pressed on surface.
image2Tz (uint8_t slot) Ask the sensor to convert image
to feature template.
@param slot: Location to place
feature template.
createModel(void) Ask the sensor to take two print
feature template and create a
model.
storeModel(uint16_t location) Ask the sensor to store the
calculated model for later
matching.
@param location: The model
location #.
loadModel(uint16_t location) Ask the sensor to load a
fingerprint model from flash into
buffer 1.
@param location: The model
location #.
getModel(void) Ask the sensor to transfer 256-
byte fingerprint template from
the buffer to the UART.
deleteModel(uint16_t location) Ask the sensor to delete a model
in memory.
@param location: The model
location #.
emptyDatabase(void) Ask the sensor to delete ALL
models in memory.
fingerFastSearch(void) Ask the sensor to search the
current fingerprint features to
match saved templates.

15
Chapter 1 Common Biological Signals and Patterns

LEDcontrol(bool on) Control the built in LED.


@param on: True if you want
LED on, False to turn LED off.
fingerSearch(uint8_t slot) Ask the sensor to search the
current slot fingerprint features to
match saved templates.
@param slot: The slot to use for
the print search, defaults to 1.
getTemplateCount(void) Ask the sensor for the number of
templates stored in memory.
setPassword(uint32_t password) Set the password on the sensor.
@param password: 32-bit
password code.

With the library installed in your computer, we can proceed to the code
of the project.

The code for this project is shown below.


// Written by Dr. Ali Siam

#include <Adafruit_Fingerprint.h>

SoftwareSerial mySerial(2, 3);

Adafruit_Fingerprint finger =
Adafruit_Fingerprint(&mySerial);

int led_Green=8, led_Red=9;

void setup() {

Serial.begin(9600);

pinMode(led_Green,OUTPUT);

pinMode(led_Red,OUTPUT);

finger.begin(57600);

delay(5);

if (finger.verifyPassword()) {

16
Chapter 1 Common Biological Signals and Patterns

Serial.println("Found fingerprint sensor!");

} else {

Serial.println("Did not find fingerprint sensor");

while (1) { delay(1); }

finger.getTemplateCount();

if (finger.templateCount == 0) {

Serial.print("Sensor doesn't contain any fingerprint


data. Please enroll a fingerprint first.");

while (1) { delay(1); }

else {

Serial.print("Sensor contains ");


Serial.print(finger.templateCount); Serial.println("
templates");

void loop() {

matchFinger();

delay(50);

void matchFinger(){

Serial.println("Put your finger on the sensor ...");

uint8_t p = finger.getImage();

if (! p==FINGERPRINT_OK)

17
Chapter 1 Common Biological Signals and Patterns

return;

Serial.println("Image taken");

p = finger.image2Tz();

if (! p==FINGERPRINT_OK)

return;

Serial.println("Image converted");

p = finger.fingerSearch();

if (p == FINGERPRINT_OK) {

Serial.println("Found a print match!");

Serial.print("Found ID #");
Serial.print(finger.fingerID);

Serial.print(" with confidence of ");


Serial.println(finger.confidence);

digitalWrite(led_Green,HIGH);

delay(1000);

digitalWrite(led_Green,LOW);

else if (p == FINGERPRINT_NOTFOUND) {

Serial.println("Did not find a match");

digitalWrite(led_Red,HIGH);

delay(1000);

digitalWrite(led_Red,LOW);

18
Chapter 1 Common Biological Signals and Patterns

Upload the code to the Arduino board and test the project.

If you put a finger that have been already enrolled and stored into the
memory of the sensor, the green LED will be turned on for 1 second.

If you put a finger that cannot be identified, the red LED will be turned
on for 1 second.

To enroll a new fingerprint, use the example provided with the


Adafruit fingerprint sensor library. Open File > Examples > Adafruit
Fingerprint Sensor Library > enroll.

19
Chapter 1 Common Biological Signals and Patterns

Project testing

1) Successful matching

2) No matching

20
Chapter 1 Common Biological Signals and Patterns

Serial monitor output

21
Chapter 1 Common Biological Signals and Patterns

Applications

a) Download 10 fingerprint images from a public repository and


identify each fingerprint as arch, loop, or whorl.
You can download fingerprint images from:
- https://www.kaggle.com/ruizgara/socofing
- https://www.kaggle.com/anujrai07/fingerprint-1
- https://www.mathworks.com/matlabcentral/fileexchange/52507-
fingerprint-color-image-database-v1

or scan,

b) Import the fingerprint images used in (a) into MATLAB. For


each image, display the image on a MATLAB window, and
mark all minutiae found in the fingerprint image.

22
Chapter 1 Common Biological Signals and Patterns

1.2 Electrocardiogram

Electrocardiogram (ECG, sometimes called EKG) is a reflection of the


electrical activity of the heart. ECG is the most commonly used
biomedical signal in clinical diagnostics of the heart.

The word “electrocardiogram” is a combination of three words:


electro, meaning electric signal; cardio, which translates into heart; and
gram, which stands for recording. The recording of the electric activity
of the heart is called ECG.

The heart

The heart is the structure comprised of cardiac muscles that are


responsible for circulating blood through the body. The heart has four
major functions: collecting the blood that needs to be refined from all
parts of the body (through veins), pumping this collected blood to the
lungs, collecting the refined blood from the lungs, and pumping the
refined blood back to all parts of the body. The heart has four
chambers: two atria and two ventricles.

23
Chapter 1 Common Biological Signals and Patterns

The heart chambers continuously expands and contracts to allow the


process of collecting and pumping the blood, respectively.

The functions of the four chambers of the heart are shown in the
following table.

Chamber Relax Contract

Receive the oxygen- Pump the oxygen-


Right Atrium poor blood from the poor blood to the
body right ventricle

Receive the oxygen- Pump the oxygen-


Right Ventricle poor blood from the poor blood to the
right atrium lungs to be refined

Receive the refined Pump the refined


Left Atrium blood from the lungs blood to the left
ventricle.

Receive the refined Pump the refined


Left Ventricle blood from the left blood to the body
atrium

The repetitive contraction and expansion of the heart muscle is a result


of local electric potential differences, which can be measured on the
skin using electronic recording equipment. This group of signals,
called ECG, records the strength and timing of the electrical activity in

24
Chapter 1 Common Biological Signals and Patterns

the heart, and constitutes the most informative clinical signal


commonly used in the diagnosis of the cardiovascular system.

The ECG represents the repetitive electric depolarization and


repolarization pattern of the heart muscle. The waveform of ECG is
recorded on a graph that shows each phase of the electrical signal as it
travels through the heart.

A typical healthy ECG signal is shown in the following figure.

Characteristics of ECG

The schematic form of one period of the ECG is illustrated in the


figure below.

The ECG is characterized by five peaks (waves), represented by the


letters P, Q, R, S, T, and sometimes followed by a sixth peak, the U
wave. The P wave is the result of the depolarization of the atrium,
while the remaining waves are caused by the ventricle.
Usually, the combination of Q, R, and S peaks are referred to QRS
complex.
25
Chapter 1 Common Biological Signals and Patterns

Origin of ECG

The electrical signal begins in the sinoatrial node (SA) which is located
in the right atrium and travels to the right and left atria, causing them
to contract and pump blood into the ventricles. This electrical signal is
recorded as the P wave on the ECG. The PR Interval is the time, in
seconds, from the beginning of the P wave to the beginning of the QRS
complex.

The electrical signal passes from the atria to the ventricles through the
atrioventricular (AV) node. The signal slows down as it passes through
this node, allowing the ventricles to fill with blood. This slowing signal
appears as a flat line on the ECG between the end of the P wave and
the beginning of the Q wave. The PR segment represents the electrical
conduction through the atria and the delay of the electrical impulse in
the atrioventricular node.

26
Chapter 1 Common Biological Signals and Patterns

After the signal leaves the AV node it travels along a pathway called
the bundle of His and into the right and left bundle branches. The
signal travels across the heart’s ventricles causing them to contract,
pumping blood to the lungs and the body. This signal is recorded as the
QRS waves on the ECG. Because these waves occur in rapid
succession they are usually considered together as the QRS complex.

The ventricles then recover to their normal electrical state, shown as


the T wave. The muscles relax and stop contracting, allowing the atria
to fill with blood and the entire process repeats with each heartbeat.
The ST segment connects the QRS complex and the T wave and
represents the beginning of the electrical recovery of the ventricles.

The QT interval represents the time during which the ventricles are
stimulated and recover after the stimulation. This interval shortens at a
faster heart rate and lengthens at a slower heart rate.

ECG recording: First attempt

Electrocardiography made its introduction through the pioneering


efforts of the Dutch scientist Willem Einthoven in 1903. He used a
galvanometer to design a way to record the action potentials. He also
introduced the markers P, Q, R, S, and T on the standard ECG. In
1924, Einthoven received the Nobel Prize for the invention of
electrocardiography and its development.

The initial ECGs were recorded directly on paper and, in fact, still are
in many clinical cardiac electrophysiology laboratories. The
galvanometer was directly coupled to an ink pen. This way, a voltage

27
Chapter 1 Common Biological Signals and Patterns

leading to a deflection of the galvanometer would move or direct the


pen over the paper. Each individual electrode had its own
galvanometer and separate ink pen. This method still stands as the gold
standard for analog recordings. However, nowadays, the electrodes are
connected to amplifiers and filters for the amplification of the signal
and noise elimination.

The following figure depicts the initial procedure adopted by Willem


Einthoven to record the ECG signal from the surface of the skin.

ECG electrodes configuration for clinical recordings

The electrical activity going through the heart (ECG) can be measured
by attaching external electrodes to the skin. Commonly, 10 electrodes
attached to the body are used to form 12 ECG leads, with each lead
measuring a specific electrical potential difference. This method is
used in hospitals to obtain a clean ECG signal which can be used in
diagnosis of most heart diseases.

28
Chapter 1 Common Biological Signals and Patterns

The 10 electrodes in a 12-lead ECG are: RA, LA, RL, LL, V1, V2, V3,
V4, V5, and V6.

The 12 leads are formed as a type of combination of these 10


electrodes.

The positions of the 10 electrodes are shown in the following figures.

29
Chapter 1 Common Biological Signals and Patterns

AD8232 ECG sensor

AD8232 is a simple 3-electrodes (1-lead) ECG sensor used to monitor


the electrical activity of the heart. However, this sensor is not intended
for clinical use and not for diagnose or treat any conditions.

AD8232 is commonly interfaced with Arduino to acquire the ECG


signal from the surface of the skin.

Features

• Operating Voltage: 3.3V

• Output type: Analog

• Leads-Off Detection

• Shutdown Pin

• LED Indicator that will pulsate to the rhythm of a heart beat.

• 3.5mm Jack for Biomedical Pad Connection

Here, we will learn how to get an ECG signal using the AD8232 sensor
and Arduino uno board.
30
Chapter 1 Common Biological Signals and Patterns

Required materials

- Arduino Uno
- AD8232 sensor with electrodes
- 3 ECG sensor pads
- Breadboard
- Jumper wires

Connection

We will connect five of the nine pins on the board to the Arduino. The
five pins you need are labeled GND, 3.3v, OUTPUT, LO-, and LO+.

AD8232 Pin Pin Function Arduino Connection


GND Ground GND
3.3v 3.3v Power Supply 3.3v
OUTPUT Output Signal A0
LO- Leads-off Detect - Digital pin 6
LO+ Leads-off Detect + Digital pin 5
SDN Shutdown Not used

The other 3 pins RA, LA, and RL are already connected to the jack.

The circuit connection will look like this:

31
Chapter 1 Common Biological Signals and Patterns

Sensor pad placement

Connect the electrodes wire pin to the jack input of the sensor board, as
shown in the figure below.

Then, stick the three pads on the body at locations as shown in the
following figure. These locations are based on Einthoven's method.

32
Chapter 1 Common Biological Signals and Patterns

Note that the closer to the heart the pads are, the better the
measurement.

The Arduino code

AD8232 sensor does not need any special library, the communication
is based on the Serial interface.

Upload the following code to the Arduino board. After that, you can
see a waveform for the ECG on the Serial Plotter window provided
that the sensors are placed correctly and not moving.
void setup() {

// initialize the serial communication:

Serial.begin(9600);

pinMode(5, INPUT); // D5 Setup for leads off detection LO +

pinMode(6, INPUT); // D6 Setup for leads off detection LO -

void loop() {

if((digitalRead(5) == 1)||(digitalRead(6) == 1)){

Serial.println('!');

else{

// send the value of analog input 0:

Serial.println(analogRead(A0));
}

delay(5);

33
Chapter 1 Common Biological Signals and Patterns

To verify that the heart rate monitor is working as expected, open the
serial monitor at 9600 baud. You should see values printed on the
screen. Below is an example output on the serial monitor.

Now, open the serial plotter in the Arduino IDE. To open the serial
plotter, select Tools > Serial Plotter. You should see a waveform
similar to the image below when the sensors are placed correctly and
not moving.

34
Chapter 1 Common Biological Signals and Patterns

Troubleshooting

ECG is naturally noisy. This is because you are measuring muscle


activation. The further the sensor pads are from the heart, the more
muscle noise you will see. These are commonly referred to as "Motion
Artifacts". So here are some simple tips to improve the signal quality.

• Connect the Arduino board to a laptop running on batteries and


not plugged in the electricity.

• Keep sensor pads as close to the heart as you can.

• Make sure the RA and LA sensor pads are on correct sides of


the heart.

• Try not to move too much while taking a measurement.

• Try to use fresh pads for each measurement. The pads loose the
ability to pass signals with multiple applications.

• Prep and clean the area you plan to stick pads. This will help
make a good connection (hair is not a good conductor).

• You may have to adjust sensor placement for different


individuals.

35
Chapter 1 Common Biological Signals and Patterns

Applications

a) Download an ECG signal from a public repository dataset.


You can download a sample ECG dataset from:
https://www.physionet.org/content/mitdb/1.0.0/

b) Import the ECG signal into MATLAB. Then, display the first 1500
points of the signal. After that, mark the P, QRS complex, and T
waves found in the graph.
Note: signals are contained in (.dat) files. Use appropriate
MATLAB functions to read signals from these files.

36
Chapter 1 Common Biological Signals and Patterns

1.3 Photoplethysmogram (PPG)

With the heart pumping blood to the body parts, the amount of blood
reaching the body peripherals changes with every heartbeat.
Photoplethysmogram (PPG) signal describes the amount of change of
blood flow in peripherals with heartbeats. PPG is commonly measured
at fingertip or at earlobe.

A device used to acquire the PPG signal is the pulse oximeter.

The pulse oximeter has a Light Emitting Diode (LED) to shine a light
through the fingertip and a photodetector (PD) to measure the amount
of the reflected light.

The structure of the pulse oximeter is depicted in the figure below.

When the light is emitted from the LED through the fingertip, some of
light is absorbed by the blood and the other amount is reflected to PD.

The following figure describes the resulting waveform of the output of


the PD, which has a pulsatile waveform due to the periodic change of
the amount of the blood underneath the sensor due to the periodic
pumping of the blood from the heart, which affects the amount of the
reflected light (the more the amount of blood, the more absorbed light
and less reflected light arriving on the PD).

37
Chapter 1 Common Biological Signals and Patterns

The DC component in the resulting waveform is due to the reflectance


of light on bones, tissues, and other stationary parts, while the AC
component represents the pulsatile change of the arterial blood that
forms the PPG signal.

Figure: The resulting waveform for a single light source

1.4 Blood Oxygen Saturation (SpO2)

Blood oxygen saturation, termed as SpO2, is an estimation of the


amount of oxygen dissolved in blood, which is described as the
percentage of oxygenated hemoglobin to the total amount of
hemoglobin, and expressed as:
𝐻𝑏𝑂2
𝑆𝑝𝑂2 [%] = x 100
𝐻𝑏𝑂2 + 𝐻𝑏
SpO2 is one of clinical vitals preferably measured by specialists to
determine how much oxygen is saturated in the blood. Normal oxygen
saturation for most healthy persons is 94% to 100%.

38
Chapter 1 Common Biological Signals and Patterns

SpO2 is a key indicator of the effectiveness of the respiratory system


and can aid in the detection of hypoxemia. Furthermore, the SpO2
level is used to help with the early detection of COVID pneumonia,
which may cause initially unnoticeable low arterial oxygen saturation.
It is reported that COVID pneumonia patients have oxygen saturations
as low as 50 percent. The SpO2 level is commonly measured by pulse
oximeter having two LEDs, Red and Infrared (IR).

With two light beams with different wavelengths, Red (660 nm) and IR
(880 nm), it is reported that HbO2 and Hb absorb the two different
wavelengths with different amounts, see the following figure. Hb has a
higher absorption at 660 nm, while HbO2 has a higher absorption at
880 nm.

Figure: Oxygenated and deoxygenated hemoglobin absorption graph for red


(660 nm) and IR (880 nm) wavelengths

This characteristic denotes that the amount of absorbed light at 660 and
880 nm can be used to approximate the amount of dissolved oxygen in
39
Chapter 1 Common Biological Signals and Patterns

the blood (SpO2). The two separate PPG signals determined from the
Red and IR LEDs are used to find the ratio R which is used to calculate
the SpO2 level:

(𝐴𝐶 ⁄𝐷𝐶 )𝑅𝑒𝑑


𝑅=
(𝐴𝐶 ⁄𝐷𝐶 )𝐼𝑅

The accurate computation of SpO2 is based on empirical calibration


with the ratio R for the specific device. The following equation is often
used to approximate the SpO2 value vs. R.

𝑆𝑝𝑂2 [%] = 110 − 25(𝑅)

Another approximation to find the value of SpO2 using the ratio R is


developed by Maxim Integrated based on empirical calibration for
their medical products, and is defined as:

𝑆𝑝𝑂2 [%] = 104 − 17(𝑅)

In this book, the MAX30102 sensor, a product of Maxim Integrated, is


adopted to measure the SpO2 level and the heart rate.

1.5 Heart Rate

The heart rate is denoted as the frequency at which the heart pumps
blood to the arteries, and is measured as the count of contractions of
the heart per minute. The heart rate is a reflection of the physical and
mental state of the body and can vary conditionally according to the
body’s physical needs, for example when the oxygen saturation level
is low.

40
Chapter 1 Common Biological Signals and Patterns

Pulse oximeters are able to determine the frequency of the heartbeats


by calculating the time between consecutive peaks in the PPG signal
using a single light source (e.g. Red LED).

The heart rate is typically measured in beats per minute (bpm). The
normal heart rate of healthy adult persons is between 60 and 100 bpm
while they are at rest.

41
Chapter 1 Common Biological Signals and Patterns

MAX30102 sensor

The MAX30102 is a reflective pulse oximeter, which consists of a


light source (IR Led, Red Led) and a photodiode, and some other
components such as amplifier and filter. The communication is through
a standard I2C interface.

The MAX30102 is almost used in wearable health devices as heart rate


monitor and pulse oximeter (measures SpO2). Also, it is used in the
acquisition of the PPG signal.

The idea of its work depends on measuring the amount of reflected


light, when it collides with a liquid (blood), resulting in the PPG
signal. Hence, the heart rate can be calculated by counting the number
of peaks in the resulting signal. Also, there is a relationship between
the amount of reflected light and the amount of oxygen in the blood,
from which the oxygen level in the blood can be estimated. The
following figure shows the top view of the MAX30102 sensor.

Figure: MAX30102 board


Here, we will learn how to use the MAX30102 sensor to acquire the
PPG signal, and to measure the heart rate and the blood oxygen
saturation (SpO2).
42
Chapter 1 Common Biological Signals and Patterns

Required materials

- Arduino Uno
- MAX30102 board
- Breadboard
- Jumper wires

Connection

MAX30102 sensor has seven pins from which only four pins are used.
The four pins needed are: Vin, SCL, SDA, and GND. These pins are
connected to the Arduino board as follows:

AD8232 Pin Pin Function Arduino Connection


Vin Power Supply 3.3v or 5v
GND Ground GND
SCL I2C SCL pin A5
SDA I2C SDA pin A4

The circuit connection will look like this:

43
Chapter 1 Common Biological Signals and Patterns

Sensor library

The sensor library can be installed using the Library Manager. Open
the Arduino IDE and navigate to Sketch > Include Library > Manage
Libraries.

When the library manager loads search for “max30102”. Select


‘SparkFun MAX3010x Pulse and Proximity Sensor Library’ and
install it.

44
Chapter 1 Common Biological Signals and Patterns

Using MAX30102 to plot the PPG signal

Here, we aim to show the heart beat on Arduino's serial plotter.

- Connect the MAX30102 sensor to the Arduino board as described


earlier.
- Upload the following code to the Arduino board.
- Put the fingertip on the sensor. The sensor can be attached to the
finger with a rubber band for fixing. Note that the readings can be
negatively impacted by excess motion or too much pressure.
- Open the serial plotter window to view the output PPG signal. To
open the serial plotter, Open Tools -> 'Serial Plotter'.
- Make sure the drop down list is set to 115200 baud.

For flexibility when using the sensor more times, the sensor can be put
inside a clip, as shown in the following figures. This also provides
convenience for a user instead of tying a rubber band around his finger
every time he needs to use the sensor. Use a cable of four wires (for
example, you can use an old mouse cable) to connect the sensor board
to the Arduino.

45
Chapter 1 Common Biological Signals and Patterns

Code
#include <Wire.h>

#include "MAX30105.h"

MAX30105 particleSensor;

void setup()

Serial.begin(115200);

Serial.println("Initializing...");

// Initialize sensor

if (!particleSensor.begin(Wire, I2C_SPEED_FAST))

Serial.println("MAX30102 was not found. Please check


wiring/power. ");

while (1);

46
Chapter 1 Common Biological Signals and Patterns

//Setup to sense a nice looking saw tooth on the plotter

byte ledBrightness = 0x2A; //Options: 0=Off to 255=50mA

byte sampleAverage = 16; //Options: 1, 2, 4, 8, 16, 32

byte ledMode = 2; //Options: 1 = Red only, 2 = Red + IR, 3


= Red + IR + Green

int sampleRate = 800; //Options: 50, 100, 200, 400, 800,


1000, 1600, 3200

int pulseWidth = 411; //Options: 69, 118, 215, 411

int adcRange = 16384; //Options: 2048, 4096, 8192, 16384

particleSensor.setup(ledBrightness, sampleAverage, ledMode,


sampleRate, pulseWidth, adcRange); //Configure sensor with
these settings

void loop()

Serial.println(particleSensor.getIR()); //Send raw data to


plotter

47
Chapter 1 Common Biological Signals and Patterns

Output on the serial plotter

Using MAX30102 to measure the heart rate and SpO2

Here, we aim to measure the heart rate (BPM: beats per minute) and
the blood oxygen saturation (SpO2) using MAX30102 sensor, and print
the measurements on Arduino's serial monitor.

The previous procedure, which we used to plot the PPG signal, will be
followed except the code will changed to the following code. Also, you
should open the serial monitor instead of the serial plotter.

Code

#include <Wire.h>

#include "MAX30105.h"

#include "spo2_algorithm.h"

MAX30105 particleSensor;

48
Chapter 1 Common Biological Signals and Patterns

long IR_val;

const int bufferLength = 100;

uint32_t irBuffer[bufferLength]; //infrared LED sensor data

uint32_t redBuffer[bufferLength]; //red LED sensor data

int32_t spo2; //SPO2 value

int8_t validSPO2; //indicator to show if the SPO2 calculation


is valid

int32_t heartRate; //heart rate value

int8_t validHeartRate; //indicator to show if the heart rate


calculation is valid

void setup()

Serial.begin(115200);

Serial.println("Initializing...");

// Initialize sensor

if (!particleSensor.begin(Wire, I2C_SPEED_FAST))

Serial.println("MAX30102 was not found. Please check


wiring/power. ");

while (1);

//Setup to sense a nice looking saw tooth on the plotter

byte ledBrightness = 0x2A; //Options: 0=Off to 255=50mA

49
Chapter 1 Common Biological Signals and Patterns

byte sampleAverage = 8; //Options: 1, 2, 4, 8, 16, 32

byte ledMode = 2; //Options: 1 = Red only, 2 = Red + IR, 3


= Red + IR + Green

int sampleRate = 1600; //Options: 50, 100, 200, 400, 800,


1000, 1600, 3200

int pulseWidth = 411; //Options: 69, 118, 215, 411

int adcRange = 16384; //Options: 2048, 4096, 8192, 16384

particleSensor.setup(ledBrightness, sampleAverage, ledMode,


sampleRate, pulseWidth, adcRange); //Configure sensor with
these settings

void loop()

IR_val = particleSensor.getIR();

if (IR_val < 10000){

Serial.println(" No finger!!");

for (byte i = 0 ; i < bufferLength ; i++)

redBuffer[i] = particleSensor.getRed();

irBuffer[i] = particleSensor.getIR();

particleSensor.nextSample(); //We're finished with this


sample so move to next sample

50
Chapter 1 Common Biological Signals and Patterns

//calculate heart rate and SpO2 after first 100 samples

maxim_heart_rate_and_oxygen_saturation(irBuffer,
bufferLength, redBuffer, &spo2, &validSPO2, &heartRate,
&validHeartRate);

if(validHeartRate && validSPO2)

Serial.print("Heart Rate = ");

Serial.print(heartRate, DEC);

Serial.print(" , SPO2= ");

Serial.println(spo2, DEC);

Output on the serial monitor

51
Chapter 1 Common Biological Signals and Patterns

Applications

a) Make a project to measure the heart rate and SpO2 using the
MAX30102 sensor and display the measurements on an LCD
display.
In this project, you will need:
- Arduino Uno
- MAX30102 sensor
- 16x2 LCD display
- 10 KΩ potentiometer to control the brightness of the LCD
display.
- 220 Ω resistor to control the backlight of the LCD display.
- Breadboard
- Jumper wires

b) Make a project to measure the heart rate and SpO2 using the
MAX30102 sensor and send the measurements to a mobile
application.
In this project, you will need:
- Arduino Uno
- MAX30102 sensor
- Bluetooth module
- Breadboard
- Jumper wires

You can create your own mobile application using MIT App Inventor
on : http://ai2.appinventor.mit.edu/

Or you can use available software on Play/App Store to communicate


with Bluetooth module. Search for ‘Arduino Bluetooth Control’ and
choose one to install.

52
Chapter 1 Common Biological Signals and Patterns

1.6 Respiration Rate


Body cells and tissues need oxygen to live. Respiration process plays a
crucial role for all living beings, since it is the process through which
the oxygen needed for living cells is entered into the lungs and then
circulated throughout the body.
During the inhalation/exhalation cycle, oxygen is carried from the
lungs and absorbed by the red blood cells (RBC). Hemoglobin (Hb) is
the protein that carries oxygen in the RBCs and transports it
throughout the body. The heart pumps oxygenated hemoglobin (HbO2)
from the lungs to the whole body cells and tissues through the
circularity system, and receives the deoxygenated Hb and pumps it
towards the lungs again to be oxygenated.
The cell respiration formula is given below, which describes the
biochemical process in which the cells of the body obtain energy by
combining oxygen and glucose, resulting in the release of carbon
dioxide, water, and ATP (Adenosine Tri-Phosphate) which is the
primary energy carrier in living beings.

The respiration rate is the number of inhalation/exhalation cycles in a


minute, defined as breaths per minute and expressed as BPM. Normal
respiration rate for adult healthy persons at rest ranges from 12 to 20
BPM, and for a new born (< 1 year), it ranges from 30 to 40 BPM. The

53
Chapter 1 Common Biological Signals and Patterns

lower or higher respiration rate, while resting, is considered as being


abnormal. Consequently, the respiration rate or respiration pattern
disorders are considered as an early indicator for the deterioration of
the person health state. Even more, the respiratory rate is thought to be
a more discriminatory parameter between normal and abnormal
patients than the heart rate.
Respiration rate measurement is achieved through either contact or
contactless methods.
In contact-based measurement, a physical electronic sensor is attached
to the body skin or tied to the patient clothes to sense the motion of the
chest. The resulting motion signal is then processed to extract the
respiration rate. However, these methods are not appropriate in some
cases such as sensitive or burned skin, or for premature babies. In
addition, contact sensors are less convenient and more susceptible to
motion noise, which may affect the measured output. Also, the high
cost of contact sensors makes them unfavorable for developing
countries. The following figure shows some examples of contact-based
respiration sensors.

54
Chapter 1 Common Biological Signals and Patterns

Contactless respiration measurements involve capturing respiration-


related phenomena through computer vision techniques and tracking
the variations in these respiration-induced phenomena. Some examples
of such contact-free techniques include the tracking of contraction and
expansion of chest and abdomen region with each
inhalation/exhalation cycle, and also the tracking of temperature
variations around the nose and mouth, caused by air flow movement
that could be captured using a thermal camera. Thus, contactless
respiration rate monitoring is favorable to overcome the issues related
to attaching sensors to sensitive or burned skin, or tying uncomfortable
sensors to clothes, which limits the adoption of these sensors in home-
based monitoring. In addition, contactless techniques are proper
solutions for sleeping-related monitoring.

The figure below shows a sample of respiratory signal acquired


through computer vision approach. The peaks in the signal may be
used to indicate breathing cycles.

55
Chapter 1 Common Biological Signals and Patterns

Applications

a) Implement a computer vision-based approach to plot the breathing


pattern for a human during his sleep.
You can download a dataset for this task from:
https://figshare.com/articles/dataset/sleep_dataset_zip/5518996/2

56
Chapter 1 Common Biological Signals and Patterns

1.7 Face recognition

In our daily lives, the face is perhaps the most common and familiar
biometric feature. With the invention of photography, government
departments and private entities have kept facial photographs (from
personal identity documents, passports, or membership cards). These
collections have been used in forensic investigations, as referential
databases, to match and compare a respondent’s facial images (e.g.,
criminal, witness, or victim). Besides, the broad use of digital cameras
and smartphones made facial images easy to produce every day; these
images can be easily distributed and exchanged by rapidly established
social networks such as Facebook and Twitter.

Face recognition has a long history; it is studied by neurologists,


psychologists, and computer scientists. The human face is not an ideal
modality compared to other biometric traits; it is typically less precise
than other biometric modalities such as iris or fingerprint, and can
potentially be influenced by cosmetics, disguises, and lighting.
However, the face has the advantages that make it one of the most
favored biometric characteristics for identity recognition, we can note:

• Natural character: The face is a very realistic biometric feature


used by humans in the individual’s recognition, making it possibly
the most related biometric feature for authentication and
identification purposes. For example, in access control, it is simple
for administrators to monitor and evaluate approved persons after
authentication, using their facial characteristics. On the other hand,

57
Chapter 1 Common Biological Signals and Patterns

identifying fingerprints requires an expert with professional


experience to provide accurate confirmation.
• Nonintrusive: In contrast to fingerprint images, facial images can
quickly be obtained without physical contact.
• Less cooperation: Face recognition requires less assistance from
the user compared with iris or fingerprint. For some limited
applications such as surveillance, a face recognition device may
recognize an individual without active subject involvement.

Face Recognition History

Here, we review the most significant historical stages that have


contributed to the advancement of face recognition technology:

• 1964: The American researchers Bledsoe et al. studied facial


recognition computer programming. They implemented a semi-
automatic method, where operators are asked to enter twenty
computer measures, such as the size of the mouth or the eyes.
• 1977: The system was improved by adding 21 additional markers
(e.g., lip width, hair color).
• 1988: Artificial intelligence was introduced to develop previously
used theoretical tools. Mathematics (“linear algebra”) was used to
interpret images differently and find a way to simplify and
manipulate them independent of human markers.
• 1991: Researchers of MIT presented the first successful example
of facial recognition technology, Eigenfaces, which uses the
statistical Principal component analysis (PCA) method.

58
Chapter 1 Common Biological Signals and Patterns

• 1998: To encourage industry and the academy to move forward on


this topic, the Defense Advanced Research Projects Agency
(DARPA) developed the Face recognition technology (FERET)
program, which provided to the world a sizable, challenging
database composed of 2400 images for 850 persons.
• 2005: The Face Recognition Grand Challenge (FRGC) competition
was launched to encourage and develop face recognition
technology designed to support existent facial recognition
initiatives.
• 2011: Everything accelerates due to deep learning, a machine
learning method based on artificial neural networks. The computer
selects the points to be compared: it learns better when it supplies
more images.
• 2014: Facebook knows how to recognize faces due to its internal
algorithm, Deepface. The social network claims that its method
approaches the performance of the human eye near to 97%.

59
Chapter 1 Common Biological Signals and Patterns

Today, facial recognition technology advancement has encouraged


multiple investments in commercial, industrial, legal, and
governmental applications. For example:

• In its new updates, Apple introduced a facial recognition


application where its implementation has extended to retail and
banking.
• Mastercard developed the Selfie Pay, a facial recognition
framework for online transactions.
• From 2019, people in China who want to buy a new phone will
now consent to have their faces checked by the operator.
• Chinese police used a smart monitoring system based on live facial
recognition; using this system, they arrested a suspect of
“economic crime” at a concert where his face, listed in a national
database, was identified in a crowd of 50,000 persons.

Main Steps in Face Recognition Systems

In engineering, the issue of automated face recognition includes three


key steps (as described in the following figure):

(1) Face detection

(2) Extraction of features

(3) Classification (verification or identification).

60
Chapter 1 Common Biological Signals and Patterns

Face detection is the first step in the automated face recognition


system. It usually determines whether or not an image includes a
face(s). If it does, its function is to trace one or several face locations
in the picture.

Feature extraction consists of extracting from the detected face a


feature vector named the signature, which must be enough to represent
a face.

Classification operation can be either verification or identification.


Verification requires matching one face to another to authorize access
to a requested identity. On the other hand, identification compares a
face to several other faces that are given with several possibilities to
find the face’s identity.

The face recognition system can operate either in verification or


identification mode, depending on the application (as shown below).
61
Chapter 1 Common Biological Signals and Patterns

Sometimes, some steps are not separated. For example, the facial
features (eyes, mouth, and nose) used for feature extraction are
frequently used during face detection. Detection and extraction of
features can be performed simultaneously.

Facial Landmarks

After detection of the face region in the first step, which results a
bounding box that surround the face in the image, a number of feature
points (landmarks) are determined (manually or automatically) inside
the bounding box.

There are usually two types of facial landmarks: the facial key points
and interpolated landmarks. The facial key points are the dominant
landmarks on face, such as the eye corners, nose tip, mouth corners,
etc. They possess unique local appearance/shape patterns. The
interpolated landmark points either describe the facial contour or
connect the key points (as shown in the following figure). In the early
research, only sparse key landmark points are annotated and detected.
Recently, more points are annotated in the new databases. For
62
Chapter 1 Common Biological Signals and Patterns

example, in BioID, 20 landmarks are annotated, while there are 68 and


194 landmarks annotated in ibug and Heledominantn databases,
respectively. Recently, MediaPipe library can extract 468 facial points.

During a registration of a new face, a number of landmarks are


determined, and their locations are stored for future matching. These
landmarks, determined during registration, are called ground truth
landmarks.

During validation, the same procedure is followed to extract the


landmarks and their locations. A matching score is computed between
the extracted landmarks and ground truth landmarks. The matching
score determines the distance between each landmark and its
correspondent ground truth landmark. Based on the matching score,
the tested face image is accepted or rejected.

Measuring performance of face recognition systems

The performance of a face recognition system is computed by


comparing the predictions (locations of landmarks) made by the
system to a set of ‘ground truth’ landmarks which have been annotated
automatically or manually by one or more human experts.

63
Chapter 1 Common Biological Signals and Patterns

The simplest comparison is a root mean squared error (RMSE)


assessment; where the average distance between each of the N
𝑝 𝑝
predicted landmarks (𝑥𝑖 , 𝑦𝑖 ) and the corresponding ‘ground truth’
(𝑥𝑖𝑡 , 𝑦𝑖𝑡 ) is calculated on a per landmark basis. Landmarks that are
poorly predicted will be positioned far of their corresponding ground
truth locations and thus contribute to increasing the RMSE value.
Often the root mean squared error is normalized by the distance
between two specific ‘ground truth’ points (NMRSE) such as the left
(𝑥𝑙𝑡𝑡 , 𝑦𝑙𝑡𝑡 ) and the right (𝑥𝑟𝑡
𝑡 𝑡
, 𝑦𝑟𝑡 ) outer corners of the eyes dnorm to allow
a fair comparison between faces of different sizes.

𝑁
1 𝑝 2 𝑝 2
𝑅𝑀𝑆𝐸 = ∑ √(𝑥𝑖 − 𝑥𝑖𝑡 ) + (𝑦𝑖 − 𝑦𝑖𝑡 )
𝑁
𝑖=1

𝑁 √ 𝑝 2 2
1 (𝑥𝑖 − 𝑥𝑖𝑡 ) + (𝑦𝑖𝑝 − 𝑦𝑖𝑡 )
𝑁𝑅𝑀𝑆𝐸 = ∑
𝑁 𝑑𝑛𝑜𝑟𝑚
𝑖=1

𝑑𝑛𝑜𝑟𝑚 = √(𝑥𝑙𝑡𝑡 − 𝑥𝑟𝑡


𝑡 )2
+ (𝑦𝑙𝑡𝑡 − 𝑦𝑟𝑡
𝑡 )2

Examples of facial images databases

There are two types of databases: databases collected under the


“controlled” conditions or databases with “in-the-wild” images.

64
Chapter 1 Common Biological Signals and Patterns

(a) Databases under “controlled” conditions

Databases under “controlled” conditions refer to databases with


video/images collected indoor with certain restrictions (e.g. pre-
defined expressions, head poses etc.).

– BioID: The data set contains 1521 gray scale indoor images with a
resolution of 384 × 286 from 23 subjects. Images are taken under
different illumination and backgrounds. Subjects may show moderate
expression variations. It contains landmark annotations of 20 points.

– AR: The set contains 4,000 frontal color images of 126 people with
expressions, illumination, and facial occlusions (e.g. sun glasses and
scarf). 22 landmark annotations are provided.

– Extended YaleB: The extended Yale Face database B contains


16,128 images of 28 subjects under 9 poses and 64 illumination
conditions. The database provides three annotated landmarks.

– FERET: The Facial Recognition Technology (FERET) database


contains 14,051 gray scale facial images, covering about 20 discrete
head poses that differ in yaw angles. Frontal faces also have
illumination and facial expression variations. 11 landmarks on selected
profile faces are provided.

– Multi-PIE database: The Multi-PIE face database contains more than


750,000 images of 337 subjects. The facial images are taken under 15
view points and 19 illumination conditions. A few facial expressions
are included, such as neutral, smile, surprise, squint, disgust, and

65
Chapter 1 Common Biological Signals and Patterns

scream. 68 or 39 facial landmarks are annotated, depending on the


head poses.

(b) “In-the-wild” databases

Recently, researchers focus on developing more robust and effective


algorithms to handle facial landmark detection in real day-life
situations. To evaluate the algorithms in those conditions, a few “in-
the-wild” databases are collected from the webs, such as Flicks,
facebook etc. They contain all sorts of variations, including head pose,
facial expression, illumination, ethnicity, occlusion, etc. They are
much more difficult than images with “controlled” conditions. Sample
of those databases are listed as follows:

– AFLW: The Annotated Facial Landmark in the Wild (AFLW)


database contains about 25K images. The annotations include up to 21
landmarks based on their visibility.

– LFPW: The Labeled face parts in the wild (LFPW) database


contains 1,432 facial images. Since only the URLs are provided, some
images are no longer available. 29 landmark annotations are provided
by the original database.

– Helen database: The Helen database contains 2,330 high resolution


images with dense 194 facial landmark annotations.

– AFW: The Annotated Faces in the Wild (AFW) database contains


about 205 images with relatively larger pose variations than the other

66
Chapter 1 Common Biological Signals and Patterns

“in-the-wild” databases. 6 facial landmark annotations are provided by


the database.

– Ibug 300-W: The ibug dataset from 300 faces in the Wild (300-W)
database 3 is the most challenging database so far with significant
variations. It only contains 114 faces of 135 images with annotations of
68 landmarks.

– Ibug 300-VW: The 300 Video in the Wild (300) database contains
114 video sequences for three different scenarios from easy to difficult.
68 facial landmark annotations are provided.

Face recognition and smartphones

Adopting face recognition on mobile devices offers many advantages.


In addition to the employment facility, the users do not have to
remember either the PIN or password; it can be conveniently
implemented on tablets and smartphones because only the frontal
camera is required.

Face recognition systems have been used in recent years to secure the
devices and control access to many different services through
smartphones, such as a purchase or online payments on the store.

Face recognition and Internet of Things (IoT)

Facial recognition offers numerous solutions for future and upcoming


technologies. Currently the internet of things (IoT) technology is
booming as well as the way to connect domestic or urban devices to

67
Chapter 1 Common Biological Signals and Patterns

the Internet to make them “smart”. By integrating facial recognition


into the IoT, various simplifications of life will be available. For
example, the door of an apartment could recognize the resident and
open automatically. A more common example is the simple activation
of the smartphone by facial recognition via its front camera. Similarly,
this technology can help us to find lost relatives when visiting an
unknown city. A program will search for correspondence to the
person’s profile photo in a predefined database by uploading a photo to
a website. Finally, facial recognition can increase security by
identifying criminals, assuming that this technology’s accuracy rate is
nearly 100%.

68
Chapter 1 Common Biological Signals and Patterns

Applications

The following Matlab code detects the face in the image using the
Viola-Jones algorithm. After that, it lets the user to annotate 20
landmarks in the image, and return their locations.

I = imread('kid.jpg');
faceDetector = vision.CascadeObjectDetector();

Facebboxes = faceDetector(I);

IFaces = insertShape(I, 'Rectangle', Facebboxes,


'LineWidth',3);
figure; imshow(IFaces);

for i=1:20
p=round(getPosition(impoint));
loc(i,1)=p(1);
loc(i,2)=p(2);
end

69
Chapter 1 Common Biological Signals and Patterns

70
Chapter 1 Common Biological Signals and Patterns

1.8 Electroencephalogram (EEG)

Electroencephalogram is the combination of the three words electro,


encephalo, and gram. The first term, electro, pertains to electric and the
second term, encephalo, which stems from the Greek words en-
kephale, means “in-head” and stands for the brain. The third term,
gram, represents the act of recording. Putting the three words together,
electroencephalogram means the recording of the electric activities of
the brain.

The human brain

The brain is an organ that is part of the central nervous system (CNS).
The brain monitors and regulates unconscious bodily processes, such
as digestion and breathing. The brain also coordinates most voluntary
movements of your body. It is also the site of conscious thinking,
allowing you to learn and create.

Brain is divided into two hemispheres known as left hemisphere and


right hemisphere. It has been determined that for right-handed people,
the left hemisphere manages speech, languages, reading, writing,
planning, problem solving, and it controls the right side of the body.
The right hemisphere, on the other hand, manages artistic and creative
ability and movements on the left side of the body and is primarily
involved in artistic creativity.

Each of the hemispheres are divided into four lobes: frontal, parietal,
temporal and occipital.

71
Chapter 1 Common Biological Signals and Patterns

Frontal lobe: It is the largest lobe which is located behind the


forehead. It is where higher executive functions including emotional
regulation, planning, reasoning and problem solving occur.

Parietal lobe: It is located behind the frontal lobe. It is responsible for


processing sensory information, including touch, taste, temperature,
pressure and pain.

Occipital lobe: It is located at the lower back of the head. It is


primarily responsible for vision.

Temporal lobe: It is located on the sides of the brain under parietal


and behind frontal lobe. It is responsible for processing hearing,
recognizing language, and forming memories.

72
Chapter 1 Common Biological Signals and Patterns

EEG device

EEG device is used for recording the electrical activity of the brain
from the scalp. This device comprised of electrodes to be attached to
the scalp, conductive gel, amplifiers, and analog-to-digital converter.

Electrodes are placed on the scalp in special positions. These positions


are specified using the International 10/20 system. Each electrode site
is labeled with a letter and a number. The letter refers to the area of
brain underlying the electrode. The letters F, T, C, P, and O represent
the anatomical sections referred to as frontal, temporal, central,
parietal, and occipital, respectively. Midline region is referred by a
label with ‘Z’. Even numbers denote the right side of the head and odd
numbers denote the left side of the head. For example, C3 is located on
the left central lobe. The placement of electrodes according to the
10/20 system is shown in the following figures.

73
Chapter 1 Common Biological Signals and Patterns

EEG waves
The EEG recording shows different types of brain waves. A wave is
any type of brain activity, which appears as a ‘wave’ shape on the EEG
recording. There are different names for the brain waves which are put
into bands according to their frequencies or number of ‘waves’ per

74
Chapter 1 Common Biological Signals and Patterns

second, and each type of wave looks different on the EEG. Some brain
waves happen at particular times or in different areas of the brain.

The main waves in the human EEG are:

Delta wave (0.5 – 4 Hz): It is the highest in amplitude and the slowest
wave. The delta waves are detected in infants up to one year and
sleeping adults.

Theta wave (4 – 8 Hz): It is measured in resting and sleeping adults


and in children up to 13 years while awake.

Alpha wave (8 – 13 Hz): It is the major rhythm seen in normal relaxed


adults. It is present during most of life especially after the thirteenth
year. It appears when closing the eyes and relaxing, and disappears
when opening the eyes or alerting by any mechanism (thinking,
calculating). Since these waves were discovered first, they were
labeled as alpha waves.

Beta wave (13 – 30 Hz): Beta waves are often associated with
problem solving and logical thinking.

75
Chapter 1 Common Biological Signals and Patterns

EEG and artificial intelligence

Currently, EEG is often used in combination with machine learning.


EEG data are pre-processed to be passed on to machine learning
algorithms. These algorithms are then trained to recognize different
diseases like schizophrenia, epilepsy or dementia. Furthermore, they
are increasingly used to study seizure detection.

Brain-Computer Interface (BCI) is a technology used to control an


external device through EEG signals. First, EEG signals associated
with defined actions are recorded. Usually, simple actions are
performed (for example, forward, backward, right, left, and stop). EEG
signals are used to train a model which then used to classify new EEG
signals to actions that is trained for. The following are examples of
commercial BCI development.

• In 2007, NeuroSky released the first affordable consumer based


EEG along with the game NeuroBoy.
• In 2009, NeuroSky released the Mindflex, a game that used an
EEG to steer a ball through a number of obstacles.
• In 2011, NeuroSky released the MindWave, an EEG device
designed for educational purposes and games. The MindWave won
the Guinness Book of World Records award for "Heaviest machine
moved using a brain control interface".
• In 2015, Mind Solutions Inc released the smallest consumer BCI to
date, the NeuroSync. This device functions as a dry sensor at a size
no larger than a Bluetooth ear piece.

76
Chapter 1 Common Biological Signals and Patterns

1.9 Electromyography (EMG)

EMG is the process of measuring the electrical activity of the muscles.

Every cell in the human body creates some electric activity as part of
its normal functioning. This rule is also applicable to the muscle cells.
Muscle cells produce electrical signals that cause muscles to contract
and relax. EMG records these signals using either electrodes placed on
the surface of the skin or inserting needles with electrodes in the
muscle.

The raw EMG (before filtering) is often considered as the most noisy
signal among almost all biomedical signals The following figure
shows an example of an EMG signal recorded from a muscle
performing consecutive contraction and relax.

77
Chapter 1 Common Biological Signals and Patterns

In computer science, EMG can be employed to classify muscle


activity during performing different movements and actions. Pattern
recognition approaches can be employed to identify specific actions
based on their EMG patterns. For example, gesture recognition is used
to recognize different hand patterns, by identifying each pattern’s
EMG, and translates each pattern to a specified action. The action can
be a physical action, such as controlling an instrument or opening a
door, etc., or to translate the pattern to a meaningful expression as a
way of communication between persons, such as sign language.

Examples of gesture patterns that could be recognized using EMG


signals are shown below.

EMG has also been used as a control signal for computers and other
devices. An interface device based on an EMG Switch can be used to
control moving objects, such as mobile robots or an electric
wheelchair. This may be helpful for individuals that cannot operate a
joystick-controlled wheelchair. Surface EMG recordings may also be
a suitable control signal for some interactive video games.

Also, EMG can be used to recognize unvoiced or silent speech by


observing the EMG activity of muscles associated with speech. It is

78
Chapter 1 Common Biological Signals and Patterns

targeted for use in noisy environments, and may be helpful for people
without vocal cords.

Practical EMG circuit

Here, we will implement a circuit to acquire the EMG signal from a


muscle (for example, the biceps) and use this signal to control a LED
(of course you can control any other device). We will use AD8232
sensor, which is used previously to record the ECG signal, to record
the EMG signal.

Required materials

- Arduino Uno
- AD8232 sensor with electrodes
- 3 ECG sensor pads
- Breadboard
- LED
- 220 Ω resistor
- Jumper wires

Connection

The connection of the components will be as follows:

- AD8232 GND -> Arduino GND


- AD8232 3.3v -> Arduino 3.3 V
- AD8232 OUTPUT -> Arduino analog pin A0
- LED cathode -> Arduino GND
- LED anode -> One terminal of 220 Ω resistor
- 220 Ω resistor other terminal -> Arduino digital pin 2

The circuit connection will look like this:

79
Chapter 1 Common Biological Signals and Patterns

Sensor pad placement

Connect the electrodes wire pin to the jack input of the sensor board, as
shown in the figure below.

After determining which muscle group you want to target (for example
we will use the right bicep) and cleaning the skin thoroughly, place one

80
Chapter 1 Common Biological Signals and Patterns

electrode on the middle of the desired muscle. Next, place a second


electrode at one end of the muscle.

Last, place the third electrode on a bony part of the body nearby the
muscle group. We'll call this the reference electrode. For example, for
the biceps, we are placing the reference electrode on the bony end of
my forearm close to the elbow. The electrodes are placed on the
muscle as shown in the following figure.

The Arduino code

Upload the following code to the Arduino board. After that, you can
see a waveform for the EMG on the Serial plotter.
int ledPin = 2;

void setup() {

Serial.begin(9600);

pinMode(ledPin,OUTPUT);

81
Chapter 1 Common Biological Signals and Patterns

void loop() {

// read the input on analog pin 0:

int sensorValue = analogRead(A0);

// print out the value you read:

Serial.println(sensorValue);

if (sensorValue > 750)

digitalWrite(ledPin, HIGH);

delay(1000);

digitalWrite(ledPin, LOW);

delay(1);

Testing

Clench your muscle so that the value of the EMG signal exceeds the
threshold value (750 in this case, you can change this value to adjust
the threshold value). The LED will glow when the EMG signal reaches
the threshold value.

The following figures show the output of the Serial Monitor and Serial
Plotter, respectively, during the actual testing of the circuit.

82
Chapter 1 Common Biological Signals and Patterns

83
Chapter 1 Common Biological Signals and Patterns

Applications

a) Build a project to control a servo motor by your EMG signal


acquired from the forearm (or the biceps) muscle.

b) You can extend the project in (a) to control a robot hand (with five
fingers, with each finger is attached to a servo motor) with your
hand, such that each EMG signal from one finger of your hand is
used to control one finger of the robot hand. In this case, you may
need five sensors, each one to acquire the EMG signal from one
finger.

84
Chapter 1 Common Biological Signals and Patterns

1.10 Electrooculogram (EOG)

EOG is a technique used to measure the electrical activity of eyes.


EOG is often used to measure the gaze angle of the eye when it moves
towards one direction. An EOG records eye movement by detecting a
voltage difference between the cornea and retina.

To measure eye movement, pairs of electrodes are typically placed


either above and below the eye (to measure the vertical movement) or
to the left and right of the eye (to measure the horizontal movement).
In each case, differential amplifiers are used to detect any potential
difference between each pair of electrodes caused by the motion of the
eye. There are more complete measurements of EOG that simply use
more electrodes and capture both horizontal and vertical motion and
gazing for both eyes.

The cornea (front) has a positive polarity. The retina (back) has a
negative polarity. As the eye moves, the vector of the electric field
between cornea and retina (i.e., corneoretinal potential) changes with
respect to recording electrodes placed in the skin at fixed points. These
bioelectrical signals are typically very small in amplitude (µV) and an
amplifier is required to accurately record the EOG.
85
Chapter 1 Common Biological Signals and Patterns

As shown in below figure, positive or negative pulses will be generated


when the eyes roll upward or downward. The amplitude of pulse will
be increased with the increment of rolling angle, and the width of the
positive (or negative) pulse is proportional to the duration of the
eyeball rolling process. Simultaneously, the EOG is recorded in the
two directions, horizontal and vertical, using five electrodes (two
vertical, two horizontal and one for ground) placed around the eyes.

86
Chapter 1 Common Biological Signals and Patterns

87
Chapter 1 Common Biological Signals and Patterns

As an artificial intelligence application, EOG can be used to detect the


four main eye movements (left, right, up, and down) along with
blinking. Based on the classification of EOG of different movements
of the eye, EOG can be used to control external devices (e.g.,
wheelchair, robot arm, game, etc.).

88
Chapter 1 Common Biological Signals and Patterns

Applications

a) The following dataset contains EOG data from a number of


persons moving their eyes to different directions.
https://www.um.edu.mt/cbc/ourprojects/EOG/EOGdataset
Import a sample file of this EOG data into Matlab, and plot the
first 1500 samples of the signal.

b) Build a machine learning algorithm to classify these EOG data


into labels attached with each signal.

89
Chapter 2 Biomedical Signal Processing

Chapter 2

Biomedical Signal Processing

2.1 Frequency domain

Frequency domain is a representation which describes the frequency


contents in a signal.

Recall that the time domain represents the variation of the signal
amplitude over time. Unlike the time domain, the frequency domain is
a relationship between the frequency in x-axis and the signal amplitude
in y-axis.

The Fourier Transform (FT) is a mathematical transformation which is


used to transform the signal from the time domain to the frequency
domain. The inverse operation is performed using the Inverse Fourier
Transform (IFT). The Fourier transform converts the time function into
a set of sine waves that represent different frequencies. The frequency-
domain representation of a signal is known as the "spectrum" of
frequency components.

There are two versions of the Fourier transform; the conventional


Fourier transform, which is concerned with analog signals, and the
discrete Fourier transform (DFT), which is concerned with digital
signals. Since most of signals that we deal with are digitized, we will
use DFT and IDFT for signals transformations.

90
Chapter 2 Biomedical Signal Processing

The analog signals have infinitely variable values at all times, denoted
as x(t).

Digital signals are discrete in time and in value. It is often obtained by


sampling analog signals, resulting a number of samples at discrete
times. The rate at which the signal is sampled is called the sampling
frequency, and measured as the number of samples taken per second.
Sampling process produces sequence of numbers (e.g. x[0], x[1], x[2],
...). The value of each sample is limited to a number of defined levels,
which are determined by the number of bits used to describe each
sample. The device that is used to convert an analog signal to a digital
signal is called ADC (analog-to-digital converter). Digital signal is
simple to process, store, and transmit. Digital signal can be converted
again to analog signal using an DAC (digital-to-analog converter).

91
Chapter 2 Biomedical Signal Processing

Consider a discrete signal x(n), where n = 0, 1,…, N.

The DFT of such a signal is defined as follows:

𝑁−1
−𝑗2𝜋𝑘𝑛
𝑋 (𝑘 ) = ∑ 𝑥 (𝑛 ) 𝑒 𝑁
𝑛=0

92
Chapter 2 Biomedical Signal Processing

The inverse of this transform, i.e., inverse discrete Fourier transform


(IDFT), is calculated using the following equation:

𝑁−1
1 𝑗2𝜋𝑘𝑛
𝑥 (𝑛 ) = ∑ 𝑋 (𝑘 ) 𝑒 𝑁
𝑁
𝑘=0

In the previous example, a combined wave shape is created by adding


two sine waves together. A 1V sine wave at 1kHz and a 0.1V
waveform at 10kHz. We will look at this combined waveform in the
frequency domain. In the frequency domain, amplitude is on the
vertical axis and frequency is on the horizontal axis. In the frequency
domain a “Fourier Spectrum” plot always shows the amplitude of
sinusoidal waveforms versus frequency. In this example, we are
analyzing the signal Vout which is composed of two sinusoidal
waveforms. Thus, in this example the frequency domain has two
components at 1kHz and 10kHz with amplitudes of 1V and 0.1V,
respectively.

93
Chapter 2 Biomedical Signal Processing

The key point here is that the frequency domain only displays the
amplitude for sinusoidal waveforms. Any arbitrary waveform (non-
sinusoidal) can be decomposed into an infinite series of sinusoidal
waveforms. This infinite series is called a Fourier series. An example
of a Fourier series for a triangle waveform is shown below.

The mathematical function shows that the series starts with sin(ωt) and
continues with 1/9*sin(3ωt), 1/25*sin(5ωt) and so on. The first and
largest frequency component, sin(ωt), is called the fundamental
frequency. Subsequent sinusoidal components such as sin(3ωt) and
sin(5ωt) are called harmonics, and they will diminish in amplitude as
the series continues. Theoretically, to get a perfect representation of a
triangle wave, an infinite number of sine waves is required. However
practically if you use the fundamental and several harmonics you will
get a pretty good representation of the triangle wave. This example
shows a fundamental with four harmonics in both the time and
frequency domain. Notice in the time domain waveform, the triangle

94
Chapter 2 Biomedical Signal Processing

waveform at the bottom which is the direct sum of the waveforms


above. The frequency domain of the triangle waveform is generated
using the Fourier transform. The Fourier transform is a general way to
transform any time domain signal into its frequency domain
equivalent.

The frequency domain is an efficient technique to study and process


most of signals, especially for eliminating the noise effect on the
signal. When you see a corrupted signal in the time domain, it is
because that much of unwanted signals with different frequencies
(noise) are added to the original signal. This effect will be clear if you
visualize the frequency domain of that signal. You will see the
frequency components of both the original signal and the noise. By
identifying the locations of the intended signal and the noise contents
in the frequency domain representation, it will be simple to extract the
original signal or to eliminate the noise effect by using frequency
filters.

The following figures display the time and frequency domain


representation of a corrupted sine wave. It can be seen from the
frequency domain representation that the original signal, having the
maximum amplitude, has a single frequency component at 1 kHz.
However, some noise with lower amplitudes exist and span over a long
range of frequencies.

95
Chapter 2 Biomedical Signal Processing

Example

Consider a time signal shown in in the following figure.

96
Chapter 2 Biomedical Signal Processing

We will use MATLAB to calculate the DFT of the signal. The


command for DFT calculation in MATLAB is “fft”. In order to get the
magnitude of DFT of a signal, one can use the command “abs” to
calculate the magnitude of complex variables.

The following code generates the graph shown in below figure, which
represents the magnitude of the DFT of the signal x:

y = fft(x);
y_magnitude = abs(y);
n = length(y_magnitude);
plot(y_magnitude(1:n/2-1) );
xlabel('Frequency');
ylabel('Magnitude of DFT');

97
Chapter 2 Biomedical Signal Processing

Since the DFT of a real signal is symmetric across the middle


frequency point, it is often more desirable to see only half of the signal
as the remaining half is repetitious. The last two lines of the code are
used to label the axes.

2.2 Filters

A filter is a device or process that removes some unwanted frequencies


or frequency bands from a signal. However, filters concept is expanded
to be not limited to frequency domain, they can be applied to images.

Usually, filters are used to suppress noise from signals (or images).
The corrupted signal is passed through a filter, the output will be a
clean version of the input signal.
98
Chapter 2 Biomedical Signal Processing

The filter performs some mathematical operations on the input signal


in the frequency domain. These mathematical operations determine the
relationship between the input and output of the filter. The relationship
between the input and output of the filter is called the frequency
response of the filter.

Let the frequency response (Fourier transform) of the input signal is


X(f), and the frequency response of the filter is H(f).

Then, the output of the filter will be, 𝑌(𝑓) = 𝐻 (𝑓) ∗ 𝑋(𝑓).

Filter types

1) Low-pass filter (LPF)


It allows low frequency or DC components in the signal to pass,
suppressing high frequency components or interference and noise.
The frequency response of an ideal low-pass filter is as below.

99
Chapter 2 Biomedical Signal Processing

All frequencies below a certain frequency, called cut-off frequency


𝑓𝑐 , will pass, and frequencies higher than the cutoff frequency will
be suppressed.

2) High-pass filter (HPF)


It allows high-frequency components in the signal to pass,
suppressing low-frequency or DC components. In this filter, signals
having frequency components higher than the cut-off frequency will
pass, and the frequency components lower than the cut-off
frequency will be suppressed.
The frequency response of an ideal high-pass filter is shown in the
following figure.

3) Band-pass filter (BPF)


It allows signals in certain frequency bands to pass, suppressing
signals below or above this band. The frequency response of an
ideal band-pass filter is shown in the following figure.

100
Chapter 2 Biomedical Signal Processing

In this filter, two cut-off frequencies are specified: the lower cut-off
frequency 𝑓𝑐 𝑙 , and the higher cut-off frequency 𝑓𝑐 ℎ . In BPF, all
frequency components below the lower cut-off frequency and above
the higher cut-off frequency will be suppressed, passing only the
frequency components between the two cut-off frequencies.
4) Band-stop filter (BSF)
It suppresses signals in a certain frequency band and allows signals
outside the band to pass. A special case of the band-stop filter is the
notch filter in which it rejects just one specific frequency. The
frequency response of an ideal band-stop filter is shown in the
following figure.

101
Chapter 2 Biomedical Signal Processing

In BSF, frequency components between the lower and upper cut-off


frequencies will be suppressed. All other components will pass.

Practical filters

In practical applications, due to some practical limitations in


constructing the ideal filters as well as unwanted effects of sharp rises
or falls in ideal filters, some approximations of these ideal filters are
used. These approximations, while similar to the ideal filters in general
shape, are smooth and have no sharp jumps in them. The order n
identifies both the shape and the complexity of the filter, i.e., the larger
n gets, the sharper filter is achieved and be more similar to the ideal
filter; however, the sharper filter will be more complex.

One example of such filter realizations is the family of Butterworth


filters. The low-pass Butterworth filter has a smooth transition from the
amplitude 1 at frequency 0 to amplitude 0 at high frequencies. As seen,
one can roughly approximate the ideal filter with these smooth curves.

102
Chapter 2 Biomedical Signal Processing

In these approximations, the cut-off frequency is the frequency at which


the magnitude of the filter falls to 1/√2 of the peak value, which is 1.
Below is the frequency response of a practical Butterworth low-pass
filter.

Similarly, frequency responses for practical high-pass, band-pass, and


band-stop filters are shown in the following figures, respectively.

103
Chapter 2 Biomedical Signal Processing

Filter design in MATLAB

In MATLAB, in order to design a Butterworth filter with the order of n,


one can use the following command:

[b,a] = butter (n,Wn,ftype)

In this command, n is the filter order, Wn is the normalized cutoff


frequency of the filter, and ftype determines the type of the filter.

104
Chapter 2 Biomedical Signal Processing

Normalized cutoff frequency means that the frequency value is divided


by 𝐹𝑠 /2, where 𝐹𝑠 is the sampling frequency.

Filter type is specified as one of the following:

• 'low' specifies a lowpass filter with normalized cutoff frequency Wn.


'low' will be the default type if ftype option is not specified.
• 'high' specifies a highpass filter with normalized cutoff frequency
Wn.
• 'bandpass' specifies a bandpass filter of order 2n if Wn is a two-
element vector. 'bandpass' is the default when Wn has two elements.
• 'stop' specifies a bandstop filter of order 2n if Wn is a two-element
vector.

Example:

We use MATLAB to design a low-pass Butterworth filter. We are to


design a low-pass Butterworth filter of order 10 with the cutoff
frequency of 300 Hz. The sampling frequency is 1000 Hz.

For these specifications, we code the following lines in MATLAB and


it will provide us the filter parameters: a and b.

n=10;
Fs=1000;
fc=300;
[b,a] = butter (n,fc/(Fs/2),‘low’);

105
Chapter 2 Biomedical Signal Processing

Next, in order to visualize the frequency response of the designed filter,


we use the command “freqz” to draw the frequency response of the
aforementioned low-pass Butterworth filter, i.e.,

freqz(b,a,128,1000);

where 128 is the number of points in which the frequency response is


evaluated and 1000 is the sampling frequency. The below figure shows
the frequency response of this low-pass Butterworth filter.

Exercise: Using MATLAB, design a high-pass Butterworth filter of


order 9 with cutoff frequency 200 Hz. The sampling frequency is 1000
Hz. Plot the frequency response of the filter.

Example

Suppose we have a signal that is compound of two sine waves with


frequencies 10 and 50 Hz, respectively. The compound signal is as
shown in the following figure.

Display the signal in frequency domain. Then, we have to separate the


two signals from the compound signal.

106
Chapter 2 Biomedical Signal Processing

a) Signal creation
Fs=1000; % sampling frequency
f1=10; % frequency of first signal
f2=50; % frequency of second signal
dt=1/Fs; % time step
duration = 1; % signal duration in seconds
t=0:dt:duration-dt; % time axis
S1=sin(2*pi*f1*t); % the first signal
S2=sin(2*pi*f2*t); % the second signal
S=S1+S2; % the compound signal (sum of the two signals)
plot(t,S)
xlabel('Time (seconds)')
title('Compound signal, S = S1+S2') % set title to the figure

b) Display the frequency domain


n = length(S); % Length of signal
y = fft(S); % compute Fourier transform of the signal
y_magnitude = abs(y); % get real part of Fourier transform
dF=Fs/n; % frequency step
f=0:dF:(Fs/2-dF); % Frequency axis
plot(f, y_magnitude(1:n/2));
xlabel('Frequency (Hz)');
ylabel('Magnitude of DFT');
title('Single-Sided Amplitude Spectrum of S(t)')
107
Chapter 2 Biomedical Signal Processing

From the spectrum of the signal, it is appear that the signal contains 2
components:
1- Component with 10 Hz (this is the frequency of the first signal)
2- Component with 50 Hz (this is the frequency of the second signal)

The main goal is to extract the two signals separately, and it is achieved
as follows:

a- The lower frequency component (10 Hz) can be extracted using a


low-pass filter, with cutoff frequency 15 Hz. The goal is to remove
all components in the spectrum that have values greater than 15 Hz.
We will design a Butterworth LPF with order 4.

108
Chapter 2 Biomedical Signal Processing

% Extract the signal of 10 Hz

Fn=Fs/2; % to normalize frequencies


fc=15; % cutoff frequency
order=4; % filter order
[b a]=butter(order,fc/Fn); % Butterworth filter

figure
freqz(b,a,128,Fs); % visualize the frequency response of
the filter
title ('Frequency response of an LPF, fc=15 Hz, order=4')

out_signal=filter(b,a,S); % apply the filter on the


compound signal
figure
plot(t,out_signal) % plot the first extracted signal
xlabel('Time (seconds)')
title('The first extracted signal with frequency = 10 Hz')

% we will display the extracted signal in frequency domain


to see the frequency contents in the signal
y = fft(out_signal);
y_magnitude = abs(y);

figure
plot(f, y_magnitude(1:n/2));
xlabel('Frequency (Hz)');
ylabel('Magnitude of DFT');
title('Single-Sided Amplitude Spectrum of S1(t)')

109
Chapter 2 Biomedical Signal Processing

110
Chapter 2 Biomedical Signal Processing

It is clear that, from the frequency domain of the first extracted signal,
there is a single frequency component at 10 Hz. This means that we
have successfully extracted the first signal form the compound signal.

b- The higher frequency component (50 Hz) can be extracted using a


high-pass filter, with cutoff frequency 40 Hz. The goal is to remove
all components in the spectrum that have values less than 40 Hz.
We will design a Butterworth HPF with order 4.

111
Chapter 2 Biomedical Signal Processing

% Extract the signal of 50 Hz

Fn=Fs/2; % to normalize frequencies


fc=40; % cutoff frequency
order=4; % filter order
[b a]=butter(order,fc/Fn,'high'); % Butterworth filter

figure
freqz(b,a,128,Fs); % visualize the frequency response of
the filter
title ('Frequency response of an HPF, fc=40 Hz, order=4')

out_signal=filter(b,a,S); % apply the filter on the


compound signal
figure
plot(t,out_signal) % plot the first extracted signal
xlabel('Time (seconds)')
title('The second extracted signal with frequency = 50 Hz')

% we will display the extracted signal in frequency domain


to see the
% frequency contents in the signal
y = fft(out_signal);
y_magnitude = abs(y);
figure
plot(f, y_magnitude(1:n/2));
xlabel('Frequency (Hz)');
ylabel('Magnitude of DFT');
title('Single-Sided Amplitude Spectrum of S2(t)')

112
Chapter 2 Biomedical Signal Processing

113
Chapter 2 Biomedical Signal Processing

It is clear that, from the frequency domain of the second extracted


signal, there is a single frequency component at 50 Hz. This means that
we have successfully extracted the second signal form the compound
signal.

Example:

A single channel EEG sensor is used to acquire the EEG signal from a
person. The sampling frequency of the sensor is 512 Hz. Samples of the
signal are stored in an Excel file (EEG.xlsx). It is required to extract the
four waves of the brain (Alpha, Beta, Theta, and Delta) from the EEG
signal.

114
Chapter 2 Biomedical Signal Processing

Recall that, the frequency bands for the four waves are:
Alpha wave: 8 – 13 Hz
Beta wave: 13 – 30 Hz
Theta wave: 4 – 8 Hz
Delta wave: 0.5 – 4 Hz

Each wave can be separated by designing an BPF to pass only


frequency components within the desired band.

The following MATLAB code can be used to design four Butterworth


BPFs to extract the four EEG waves from the signal stored in Excel file.
Assuming the filters are of order 10.

% read the signal from the Excel file


S=xlsread('EEG.xlsx');

Fs=512; % sampling frequency


Fn=Fs/2; % for normalizing frequencies
n=5; % for BPF and BSF, filter order=2*n
Fc_alpha=[8 13]; % pass band for Alpha wave filter
Fc_beta=[13 30]; % pass band for Beta wave filter
Fc_theta=[4 8]; % pass band for Delta wave filter
Fc_delta=[0.5 4]; % pass band for Beta wave filter

% design of BPF for Alpha wave


[b_alpha,a_alpha] = butter (n, Fc_alpha/Fn);

% design of BPF for Beta wave


[b_beta,a_beta] = butter (n, Fc_beta/Fn);

% design of BPF for Theta wave


[b_theta,a_theta] = butter (n, Fc_theta/Fn);

% design of BPF for Delta wave


[b_delta,a_delta] = butter (n, Fc_delta/Fn);

115
Chapter 2 Biomedical Signal Processing

% visualizing the frequency responses of filters


figure
freqz(b_alpha,a_alpha,128,Fs);
title('Frequency response of filter for Alpha wave,
8 - 13 Hz');

figure
freqz(b_beta,a_beta,128,Fs);
title('Frequency response of filter for Beta wave,
13 - 30 Hz');

figure
freqz(b_theta,a_theta,128,Fs);
title('Frequency response of filter for Theta wave,
4 - 8 Hz');

figure
freqz(b_delta,a_delta,128,Fs);
title('Frequency response of filter for Delta wave,
0.5 - 4 Hz');

% now apply the designed filters on the main EEG


signal to extract each wave separately.
% Alpha wave
Out_alpha= filter(b_alpha,a_alpha,S);
% Beta wave
Out_beta= filter(b_beta,a_beta,S);
% Theta wave
Out_theta= filter(b_theta,a_theta,S);
% Delta wave
Out_delta= filter(b_delta,a_delta,S);

116
Chapter 2 Biomedical Signal Processing

117
Chapter 2 Biomedical Signal Processing

118
Chapter 2 Biomedical Signal Processing

Example:

An ECG sensor is used acquire the ECG signal from a person. During
the acquisition process, ECG signal is exposed to some sources of noise
which affect the overall acquired signal. Usually, the noise has higher
frequencies. Normal ECG signal has lower frequency, up to 10 Hz. It is
required to design a filter to remove the noise to obtain a clean ECG
signal. The noisy ECG signal is stored in an Excel file,
noisyECG_withTrend.xlsx. The sampling frequency of the signal is
1000 sample/second.

Since it is required to preserve the low frequencies and eliminate the


high frequencies, the more suitable filter type is the low-pass filter.

The following MATLAB code is used to design a Butterworth LPF to


extract low frequency components from the signal, which probably
contain the desired ECG signal, and eliminate higher frequency
components, which probably contain most of noise. The filter order is
taken to be 1.

% design an LPF to remove the noise


close all
% read the signal from the Excel file
S=xlsread('noisyECG_withTrend.xlsx');
% plot the input signal
figure
plot(S)
title('Noisy ECG signal');

Fs=1000; % sampling frequency


Fn=Fs/2; % for normalizing frequencies
n=1; % filter order
Fc = 12; % cutoff frequency of filter.

119
Chapter 2 Biomedical Signal Processing

% design of LPF for ECG signal


[b,a] = butter (n, Fc/Fn); % since the cutoff frequency
is a single frequency, the default type is LPF.

% apply the filter on the input noisy signal


LPF_signal= filter(b,a,S);

% plot the low-pass filtered signal


figure
plot(LPF_signal) % plot the filtered signal
title('Low-pass filtered signal, Fc = 12 Hz')

120
Chapter 2 Biomedical Signal Processing

Additional step may be required to remove the trend line which exists
in the filtered signal. The trend line is caused by a very low signal,
below 2 Hz, which makes the original signal to goes up and down,
continuously.

Thus, a high-pass filter may be designed to remove the trend line from
the signal.

% design an HPF to remove the trend line


Fc_h = 2; % cutoff frequency of filter.

% design of HPF for ECG signal


[b,a] = butter (n, Fc_h/Fn, 'high'); % a Butterworth HPF
with Fc=2 Hz

% apply the filter on the previously filtered signal


HPF_signal= filter(b,a,LPF_signal);

121
Chapter 2 Biomedical Signal Processing

% plot the high-pass filtered signal


figure
plot(HPF_signal) % plot the filtered signal without the
trend line
title('High-pass filtered signal, Fc = 2 Hz')

Alternatively, a BPF with cut-off frequencies of 2 Hz and 12 Hz may be


designed to get the same results in a single step.

122
Chapter 2 Biomedical Signal Processing

2.3 Moving average filter

The moving average filter is a simple low-pass filter, based on


averaging a number of consecutive samples. This filter type is
commonly used for smoothing fluctuations in sampled signals. It takes
M samples of input at a time and computes the average of those
samples to produce a single output point.

For example,

1
𝑦 (𝑛 ) = [𝑥 (𝑛) + 𝑥(𝑛 − 1)]
2

It is a simple two-point moving average filter, which sets the current


sample output to the average of the current and the last samples.

Similarly,

1
𝑦(𝑛) = [𝑥(𝑛) + 𝑥(𝑛 − 1) + 𝑥(𝑛 − 2) + 𝑥(𝑛 − 3) + 𝑥(𝑛 − 4) + 𝑥(𝑛 − 5)]
6

It sets the current sample value to the average of the current and the last
5 samples.

In MATLAB, the function movmean(noisy,window)is used to


perform a moving average filter on the points of signal noisy, with a
number of averaged samples is specified in window.
The following figures show the effect of a moving average filter on a
noisy ECG signal, with different windows.

123
Chapter 2 Biomedical Signal Processing

124
Chapter 2 Biomedical Signal Processing

2.4 Image binarization

Image binarization is a technique used to increase the contrast of a


grayscale image by setting the pixel values to only two value: 0 or 255
(i.e. black or white). Each pixel value is set to 0 or 255 based on a
threshold level, such that all pixels which have values less than that
threshold level will be set to 0, and pixels which have values greater
than the threshold will be set to 255.

In MATLAB, the command BW=imbinarize(I,T)is used to


binarize the input grayscale image I using a threshold level T. BW is the
output binary image. The threshold level T must have values between 0
and 1, which corresponds to the range between 0 and 255.

The following MATLAB code is used to binarize a blurred fingerprint


image.

original=imread('fingerprint.png'); % read original


image
imshow(original); % display the original image
title('Original image')
gray_image=rgb2gray(original); % convert the image to
grayscale

BW=imbinarize(gray_image,0.75); % binarize the image


with threshold = 0.75
figure
imshow(BW) % display the binarized image
title('Binarized image, T=0.75')

125
Chapter 2 Biomedical Signal Processing

126
Chapter 2 Biomedical Signal Processing

Application

A signal contained in noisy.xlsx file. This signal contains different


sources of noise which affect the main signal. The main signal is a sine
wave signal with 5 Hz frequency. The following steps describe how to
eliminate each source of noise to get the clean signal.

Original Signal

close all
a=xlsread('noisy.xlsx'); % Read Input Signals from Excel

time=a(:,1);
noisy_signal=a(:,2);

plot(time, noisy_signal);

Noisy Signal in Time Domain

127
Chapter 2 Biomedical Signal Processing

Frequency Domain for the Signal

Fs = 25000; % Sampling frequency = 25000 sample per second


S = noisy_signal; % Signal to be Processed
L = length(S); % Length of signal

Y = fft(S); % Compute Fourier Transform for the Signal


P2 = abs(Y); % Get Real Part of Fourier Transform

dF=Fs/L;

f=0:dF:(Fs/2-dF); % Frequency axis


figure
plot(f,P2(1:L/2))
title('Single-Sided Amplitude Spectrum of S(t)')
xlabel('f (Hz)')
ylabel('|P1(f)|')

Noisy Signal in Frequency Domain

From the spectrum of the signal, it is appear that the signal contains 5
components:

128
Chapter 2 Biomedical Signal Processing

1- Component with 5 Hz (intended part)


2- Component with 50 Hz (noise from power source)
3- Component with 100 Hz (noise from surrounding environment)
4- Component at 0 Hz (DC component)
5- Noise at all frequencies (white noise)

The main goal is to remove all noise types from the signal, and it is
achieved as follows:

a- Removing the White Noise

% The goal is to remove all components in the spectrum that have


values less than 10.
P2(abs(P2)<10)=0;
figure
plot(f,P2(1:L/2))

Signal in Frequency Domain after removing the White Noise

% IFFT to get back into Time Domain

sig3_modified1=ifft(P2);
figure
plot(time,sig3_modified1);

129
Chapter 2 Biomedical Signal Processing

Signal in Time Domain after removing the White Noise

b- Removing 50 Hz Component

% Design a Band Stop Filter centered around 50 Hz

d = designfilt('bandstopiir','FilterOrder',2, ...
'HalfPowerFrequency1',40,'HalfPowerFrequency2',60, ...
'DesignMethod','butter','SampleRate',Fs);

% Apply the Filter to the signal


sig3_modified2 = filtfilt(d,sig3_modified1);

% Plot the resulting Signal in Time Domain


figure
plot(time,sig3_modified2)

% Compute FFT for the resulting Signal


Y_modified2=fft(sig3_modified2);

% Plot the spectrum for the resulting Signal


figure
plot(f,abs(Y_modified2(1:L/2)));

130
Chapter 2 Biomedical Signal Processing

Signal in Time Domain after removing the 50 Hz noise

Signal in Frequency Domain after removing the 50 Hz noise

131
Chapter 2 Biomedical Signal Processing

c- Removing 100 Hz Component

% Design a Band Stop Filter centered around 100 Hz

d = designfilt('bandstopiir','FilterOrder',2, ...
'HalfPowerFrequency1',90,'HalfPowerFrequency2',110, ...
'DesignMethod','butter','SampleRate',Fs);

% Apply the Filter to the signal


sig3_modified3 = filtfilt(d,sig3_modified2);

% Plot the resulting Signal in Time Domain


figure
plot(time,sig3_modified3)

% Compute FFT for the resulting Signal


Y_modified3=fft(sig3_modified3);

% Plot the spectrum for the resulting Signal


figure
plot(f,abs(Y_modified3(1:L/2)));

Resulting signal in Time Domain after removing the 100 Hz noise.

132
Chapter 2 Biomedical Signal Processing

Signal in Frequency Domain after removing the 100 Hz noise


showing the 5 Hz Signal plus DC component

d- Removing DC Component
% Dc Component can be removed by subtracting the mean of the
signal from the signal.
sig3_modified4=sig3_modified3-mean(sig3_modified3);
figure
plot(time, sig3_modified4)

Resulting signal of 5 Hz in Time Domain without DC component.

133

You might also like