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, iinfofrom scipy.io.wavfile import writefrom scipy.misc import electrocardiogramdef 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 singal 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.
Related posts