Category Archives: Becoming a Cyborg

HRV – Calculating Frequency Domain values

Posted on by

I’ve gotten python routines to calculate time domain values for Heart Rate Variability (HRV).  Now I’m working on doing spectrum analysis with Frequency domain.

Frequency Domain step 1.  Interpolating beats into fixed time segments.

 

FFT (Fast Fourier Transform) and Welch are the two most common methods for getting a frequency power spectrum.  Both of those require data with evenly spaced time points.  Because heart beats are not evenly spaced they need to be interpolated.

Some time back I had captured links to a number of articles that have example code for frequency power spectrum hrv calculations.  Looking at the different ways that they use for handling the interpolation.  One of the articles shows how to compare a graph of the interpolated values with your original values so I have been doing that.

Going slowly and just doing one step at a time but am at this point just comparing interpolation methods before moving on to actually creating power spectrums.

I download my heart beat intervals into an array called “beattimes”.    “beattimes” is an array of beats, in milliseconds, from my database for one session.  I will leave it to you to figure out how to fill the beattimes array whether from database or importing beat data from a file.

Let’s start by graphing the data.

 

import numpy as np

import matplotlib.pyplot as plt

#beattimes is magically filled by a routine that gets the intervals out of the database

t = np.cumsum(beattimes) / 1000.0
t -= t[0]  #alternately t = t[1:]

#that created an array t of timestamps for each of your heartbeat intervals …  milliseconds divided by 1000 to be seconds

plt.title(“Original Inter Beat Intervals”)
plt.plot(t, beattimes, label=”Original”, color=’blue’)
plt.legend()
plt.show()

If you haven’t done much with graphing thought you might like a very simple graph routine.  You do have to fill the “beattimes” arrray with your own code but that will graph any of your sessions of heart rate data.

So now the interpolation.  I’ve looked at three sources of code so far and they all interpolate with somewhat different methods.

First article I looked at.

from scipy.interpolate import splrep, splev

tx = np.arange(t[0], t[-1], 1.0 / 4.0)

tck = splrep(t, beattimes, s=0)
rrix = splev(tx, tck, der=0)

tx is the new timestamps for each of the values.  In this case they are each 1/4 of a second.  (Which greatly increases the number of data points, not quite sure why 1/4 and not 1/2 or even 1 second.)  “rrix” is the new array of interpolated values that match the evenly spaced time stamps.

Second article I looked at did it a little differently.

from scipy.interpolate import interp1d

tx = np.linspace(t[0],t[-1],t[-1])
f = interp1d(t, beattimes, kind=’cubic’)
rrix = f(tx)

He created the evenly spaced timestamp array, tx, a little differently and did the interpolation from a different library in scipy.

Third source was the python hrv module.  They did the tx timestamp array the same as the first method but used numpy for the interpolation instead of scipy.

tx = np.arange(t[0], t[-1], 1.0 / 4.0)
rrix = np.interp(tx, t, beattimes)

So now I have three methods of interpolating my heart beat interval data into evenly spaced values.  How do I compare them?  Graphing is one method. I can see how the interpolated values look compared to the original.

plt.title(“Original and Interpolated Signal”)
plt.plot(t, beattimes, label=”Original”, color=’blue’)
plt.plot(tx, rrix, label=”Interpolated”, color=’red’)
plt.legend()
plt.show()

I’ve been playing with comparing graphs of all three methods to try to see which one has the best fit to the original data.  I find for visualizing it helps to change the order the two .plot lines because having the red on top shows you some differences but having the blue on top shows other differences.
Anyway if you can make code that fills the beattimes array with values from one of your sessions the rest of this code should work as written.

Heart Rate Monitoring

Posted on by

Feel a need to catch up on writing about what I’ve been doing with Heart Rate monitoring.

Started with an Arduino board and a simple ear clip heart rate monitor and gsr sensor from Seedstudio (Grove). Made a simple biofeedback program out of it.  Set up some Python programs to read the data and save to a MySQL database. Worked with that for a while though the limitation of being tied by cables to the machine was a restriction that limited the amount of times that I would actually sit down and take measurements. Also an ear clip monitor is not the most accurate way to measure heart beats, though it was doing the job.

Last winter decided to move up to a bluetooth chest strap to measure heart rate. Decided on a Polar H10 chest strap. Polar is well rated for their accuracy as consumer devices and I thought because they are somewhat popular that it probably would be something I could access with Linux.

Found a blog post specifically about getting the H10 to connect.
https://nob.ro/post/polar_h10_ubuntu/

If it wasn’t for that blog I might have not gotten my device working under Linux but with those instructions was able to get a connection with gattool and start measuring heart beats. I was not able to get Python to connect with pygatt or pybluez but by using the one liner for gattool I could run it as a system command and capture back the data in Python.

So, I don’t have the gsr readings that I was getting with my Arduino solution but it is a more accurate heart rate monitor and I am not tied to my machine while recording. I have Python routines that activate the device, collect the data and save it to database. I have an interactive, biofeedback type, of program that shows not only my heart rate but a number of heart rate variability values. HRV can give you various ways to do biofeedback to achieve certain states that improve the values or can simply be an indication of over all heart health.

Besides being able to do live sessions displaying HRV values as I am recording heart rate I have some routines that give me statistics and create graphs of existing sessions that have been saved in the database.

It has been a nice project and there are plenty of tweaks to make it even more useful. One feature of the Polar H10 is that it can save a session of data in memory and then transfer the data back to your machine. However, I can find no information on the codes necessary to make it do that. There is an Android application Polar Beat, andofficial app for the Polar heart rate monitors, which will tell the strap to save a session in memory but still no documentation on how to do it yourself.

I’m hoping that by capturing bluetooth packets I might be able to figure out the codes myself. That is the current project. This is a high level overview of what I’ve done with heart rate over the past year.  I will intend to try and write up some more details of the projects I have accomplished with the Polar HRM to get to where I am currently and follow those to where I’m trying to go.

EEG

Posted on by

I’ve read articles for years in various magazines and sites that show EEG readings from studies or patients demonstrating what is going on in someone’s brain under different conditions.  That has always seemed like something that you would only encounter in a research lab or a hospital.  How cool would it be if we could take casual EEG readings of ourselves all the time under different conditions.  Can we tell if meditation is actually doing anything?  How about comparing that with recordings of experienced meditators?  What does your EEG look like when listening to music?  When viewing pictures?  When you are in a happy mood?  How about when you are mad or sad?

Well, in the past ten years, and especially in the past few years, suddenly EEG is available to the casual home user.   What a great tool for biofeedback, personal development and even keeping historical records of your brain activity.

Of course someone who is a serious hobbyist would be willing to pay a thousand or few for a device and there are some very nice machines in that price range but there are also very low cost devices that pretty much anyone can afford as more of a casual toy.

One of the first commercial EEG headbands is the Neurosky Mindwave.  It is roughly $100 and because it is so cheap and has been around quite a while there is a lot of support and software for it.  The limitation of such a cheap device is that it only reads one EEG channel (the left side of your frontal cortex to be precise).  Also, I tried one and it was too small for my head, causing the electrode to not sit flat and make a dent in my forehead.  Seems to fit women or teenagers just fine but not so much for an adult male.  (Though I have seen articles where people have taken apart the headband to make something more comfortable out of the parts.)

Looking at options there are quite a few in the consumer price range  In order of increasing price;  Melon headset, OpenEEG, Muse, Emotiv Insight, OpenBCI, Emotiv Epoc.

Well, I am a Linux enthusiast and don’t have a computer with Windows installed.  Some of the headsets support Android which I can use but I want to really be able to graph, view and save my sessions on my PC.  While the Muse and Emotiv headsets have limited Linux support they don’t really do it well, most of their software is geared towards Windows.  That left me with the OpenEEG and OpenBCI.  They are more primitive in that you have to individually place your electrodes instead of having a nice fancy headset like the others but on the other hand that means I can place electrodes wherever I want to and measure different areas of the brain where the fixed headsets only measure a few specific fixed points.

OpenEEG is a project that lists free circuit diagrams for people to build their own EEG machine.  That is a little beyond me but fortunately there is a company, Olimex, that will ship a pre-assembled and boxed machine for 99 Euros (EEG-SMT).  It is only a two channel EEG but I’ve decided to start with it as the cheaper option.  If I decide in the future I want something fancier and pantsier I will opt for an OpenBCI board (8 channels).  It is possible that if Muse gets official support for Brainbay and OpenVibe software I could really consider getting one of those.  (Emotiv products can work with Brainbay or OpenVibe but you have to pay an extra $200 for the SDK which is required to interface with those applications, which makes them a lot less attractive.)

 

Category: Becoming a Cyborg