# Python and the Tell-Tale Heart

I was browsing through SciPy documentation this evening and ran across a function in `scipy.misc` called `electrocardiogram`. What?!

It’s an actual electrocardiogram, sampled at 360 Hz. Presumably it’s included as convenient example data. Here’s a plot of the first five seconds.

I wrote a little code using it to turn the ECG into an audio file.

```from numpy import int16, iinfo
from scipy.io.wavfile import write
from scipy.misc import electrocardiogram

def to_integer(signal):
# Take samples in [-1, 1] then scale to 16-bit integers
m = iinfo(int16).max
M = max(abs(signal))
return int16(signal*m/M)

ecg = electrocardiogram()
write("heartbeat.wav", 360, to_integer(ecg))
```

I had to turn the volume way up to hear it, and that made me think of Edgar Allan Poe’s story The Tell-Tale Heart.

I may be doing something wrong. According to the documentation for the `write` function, I shouldn’t need to convert the signal to integers. I should just be able to leave the signal as floating point and normalize it to [−1, 1] by dividing by the largest absolute value in the signal. But when I do that, the output file will not play.

## 2 thoughts on “Python and the Tell-Tale Heart”

1. clif howell

Hey I’m a doctor would like to do something with the EKGs that I need a good programmer to help me with even though I know python as a beginner.
I want to do this as a project to advance the study of EKGs

2. Omer Sayli

Hi,

The signal is electrical signal, not phonocardiogram sound recording (sound recording of heart sounds). Hence not ideal for listening actually.

Nevertheless, I recorded this signal without int16 conversion, beats are heard actually (you may increase sound level though).