Question
Task 1: Plot the data in the range from 9 to 11 seconds! [30 Points] [ ] # TODO your plotting code goes here #
Task 1: Plot the data in the range from 9 to 11 seconds! [30 Points]
[ ]
# TODO your plotting code goes here
# please take the sampling rate into account to only plot the range
# between 9 and 11 seconds
Task 2: Use Neurokit to detect the R spikes! [30 Points]
[ ]
# now can you see the P Q R S T waves? it's actually a pretty good signal despite
# the shift in the baseline
[ ]
# we now need to install the neurokit2 library
!pip install neurokit2
import neurokit2 as nk
Requirement already satisfied: neurokit2 in /usr/local/lib/python3.6/dist-packages (0.0.42) Requirement already satisfied: scipy in /usr/local/lib/python3.6/dist-packages (from neurokit2) (1.4.1) Requirement already satisfied: sklearn in /usr/local/lib/python3.6/dist-packages (from neurokit2) (0.0) Requirement already satisfied: matplotlib in /usr/local/lib/python3.6/dist-packages (from neurokit2) (3.2.2) Requirement already satisfied: pandas in /usr/local/lib/python3.6/dist-packages (from neurokit2) (1.1.5) Requirement already satisfied: numpy in /usr/local/lib/python3.6/dist-packages (from neurokit2) (1.19.5) Requirement already satisfied: scikit-learn in /usr/local/lib/python3.6/dist-packages (from sklearn->neurokit2) (0.22.2.post1) Requirement already satisfied: python-dateutil>=2.1 in /usr/local/lib/python3.6/dist-packages (from matplotlib->neurokit2) (2.8.1) Requirement already satisfied: kiwisolver>=1.0.1 in /usr/local/lib/python3.6/dist-packages (from matplotlib->neurokit2) (1.3.1) Requirement already satisfied: cycler>=0.10 in /usr/local/lib/python3.6/dist-packages (from matplotlib->neurokit2) (0.10.0) Requirement already satisfied: pyparsing!=2.0.4,!=2.1.2,!=2.1.6,>=2.0.1 in /usr/local/lib/python3.6/dist-packages (from matplotlib->neurokit2) (2.4.7) Requirement already satisfied: pytz>=2017.2 in /usr/local/lib/python3.6/dist-packages (from pandas->neurokit2) (2018.9) Requirement already satisfied: joblib>=0.11 in /usr/local/lib/python3.6/dist-packages (from scikit-learn->sklearn->neurokit2) (1.0.0) Requirement already satisfied: six>=1.5 in /usr/local/lib/python3.6/dist-packages (from python-dateutil>=2.1->matplotlib->neurokit2) (1.15.0)
[ ]
# we can use neurokit's function to detect the R spikes
_, rpeaks = nk.ecg_peaks(ecg, sampling_rate=SAMPLING_RATE)
[ ]
# look at rpeaks - the numbers indicate the samples that are R spikes
print(rpeaks['ECG_R_Peaks'][0:10]) # here only the first ten
[ 125 343 552 748 944 1130 1317 1501 1691 1880]
[ ]
# you can also plot the first two detected R spikes
plot = nk.events_plot(rpeaks['ECG_R_Peaks'][0:2], ecg[0:SAMPLING_RATE])
[ ]
# TODO Can you write code to plot the R spikes between 9 and 11 seconds?
# Hint: one solution could be to just detect peaks in the range you specified for Task 1
# Hint 2: you might need to extend the range by +- 1 second to see the five peaks from Task 1
Task 3: Detect Heartrate. [15 Points]
[ ]
# TODO Does the patient have a regular heart rate? If yes, which one? (estimate is fine)
# Hint: you can use the rpeaks for that
[ ]
Task 4: Detect P Q S T Peaks. [25 Points]
[ ]
# We know the R spikes now but what about P Q S T waves?
# TODO Can you use nk.ecg_delineate to detect them in the range of 9 to 11 seconds?
# Hint: Look at the tutorial: https://neurokit2.readthedocs.io/en/latest/examples/ecg_delineate.html#Locate-other-waves-(P,-Q,-S,-T)-and-their-onset-and-offset
# Hint 2: The plot will have blue, orange, green, and red circles :)
[ ]
# TODO Does the detection work well?
Bonus Task: Detect P Q S T Peaks for the full signal before and after filtering. [33 Points]
[ ]
# TODO As above use the nk.ecg_delineate function to detect peaks for the whole signal
# Hint: For plotting, 1000 samples / 5 rpeaks gives a nice overview.
[ ]
# TODO Let's filter the signal and see if the detection improves.
# Hint: You can use nk.signal_filter to define a bandpass.
# Hint 2: Which lowcut and highcut frequencies work well? Play with it :)
# Hint 3: You don't need to re-calculate the rpeaks since they are pretty accurate in general.
[ ]
# TODO Does the filtering improve the result?
[ ]
#
# Great job!!
#
# , ; , .-'"""'-. , ; ,
# \\|/ .' '. \|//
# \-;-/ () () \-;-/
# // ; ; \\
# //__; :. .; ;__\\
# `-----\'.'-.....-'.'/-----'
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started