(This article was first published on holtmeier.de » Rstat, and kindly contributed to R-bloggers)
A few months ago, I bought a really cool book: Exploring Everyday Things (with R and Ruby). I learned many interesting and mostly useless things from the author, Sau Sheong Chang. Chapter 6 for example explains how to build a stethoscope with nothing more than
- a paper cup,
- a foil,
- an elastic band and
- iphone-earplugs.
First step: Homemade stethoscope
Bevor I start, let me show you what I've done to record a heartbeat. It wasn't that easy as I thought it would be. The first picture shows my attempt to build the stethoscope. The earphones have a build-in microphone. I placed them inside the paper cup. Two little holes in the cup's side-walls were made to let through the cables. Image may be NSFW.Clik here to view.
Clik here to view.
Second step: Record your heartbeat!
To record the heartbeat, I used an iphone-App called TwistedWave. You can try any other recording-software as far as it can save wav-files (mono). All my initial efforts to record the heartbeat were not successful. And no solution for these problems could be found in the book…!! I thought about this challenge an come up with an easy soulution: Sports! After running the stairs down to the frontdoor and back to the 4th floor I was able to record a heartbeat – because the heartbeat was louder then before. I saved theheartbeat.wav in my working directory for further analysis.Third step: Extracting data from sound
The wav-file format is described on page 160 and the explanation of the utilized Ruby-code you can find on page 163. Buy the book. It's worth it! The following code is my R-version (extremely short) of the original Ruby-code. I make use of the tuneR-package.My resulting sound-files lasts 4.99 seconds. 220000 Samples have been imported. Wow! Because of unwanted noises at the beginning and the end of heartbeat.wav, I selected samples above #55001 and under #275000.library(tuneR) heartbeat <- readWave("heartbeat.wav", from = 55001, to = 275000, units = "samples") print(heartbeat)
## ## Wave Object ## Number of Samples: 220000 ## Duration (seconds): 4.99 ## Samplingrate (Hertz): 44100 ## Channels (Mono/Stereo): Mono ## Bit (8/16/24/32): 16
Fourth step: Visualizing your hertbeat
Because I recorded the heart sounds in mono, I plot the left channel (there is no right channel). The following graph suggests, that the heart beats 10 times whithin the 5 seconds-periode.plot(heartbeat@left, type = "n", main = "Channel 1 + Channel 2", xlab = "Time", ylab = "Frequency") lines(heartbeat@left)
Image may be NSFW.Clik here to view.
120
bpm! Sounds plausible. If you're interested in deeper analysis of the sound file, you'll find more information in the book.
Last step: Publish the “knitred” html-file in my blog (WordPress) and on RPubs
So far so good. Now I want to publish the finished html report. What you read here is the result of my efforts. 1. RPubs: Very easy! I use RStudio, then I first press the “Knit HTML”-button and afterwards the “Publish”-button. Registration for RPubs is free! Here is the result: http://rpubs.com/stephan_cgn/3134 2. WordPress: Also very simple! At first I thought that I need to upload the images manually and adjust the paths. Surprise! This is not necessary. The generated html file already contains the binary data of the pictures! So I did not expect. So the html file can easily be opened in a text editor and the code between and is copied into the html view of the WordPress editor. Done. Unfortunately, I have so no syntax highlighting. That can be resolved, but not in this article. Here is the result:http://holtmeier.de/heartbeatTo leave a comment for the author, please follow the link and comment on his blog: holtmeier.de » Rstat.
R-bloggers.com offers daily e-mail updates about R news and tutorials on topics such as: visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series,ecdf, trading) and more...