How would I read in a wave file in matlab and determine the respiratory rate/breath rate?

Posted on 2008-11-10
Last Modified: 2016-02-10
Does anyone know how to write a program or where I could find a way to read in a wave file in matlab and determine the respiratory rate/breathing rate which has a range of 12-20 breaths per minute? How would I do this?
Question by:yankeebushsoftware
    LVL 1

    Expert Comment

    you need to be more specific about what a 'wave file' is - what software created the file? Most files people import into matlab are 'waves' of some kind.

    if a 'wave' file means a .wav, there is a function matlab 'wavread' that might do the trick.

    if it is some other common program output, it still might be a standard format that matlab can read.

    If not, it might get a bit more complex, but still totally doable. You need to know the sampling rate at which the data was gathered. and you need a specification document for the file type

    If your data is in a format that is not directly supported by matlab. No matter, as long as you have a specification for the file format.

    if you know the sampling rate and have some kind of specification for the file, matlab can read the file byte by byte and transform it into something you can use. But you'll need to write code; I can give instructions. I probably have code somewhere that does this for MRI data/

    If the file is non-standard, get back to me with as much specific information about the filetype and originating program as you can (maybe even an example file) and I'll let you know how to get it running


    Author Comment

    The .wav files were recorded with an electronic stethoscope. I have a program that was written in matlab that reads in a wavefile and uses a spectrogram to measure wheezing in the wave files. My client wants me to find the heart rate and respiratory rate which were also recorded on the .wav files, but I don't know how to do that. Do you have any sample code on how to do that? Thanks, Jim
    LVL 1

    Expert Comment

    what you (probably) have is a wav file that you can read with 'wavread'. So there's no real matlab issue here. I'll give you some non-computer related info though:

    What has probably happened is that the stethoscope has measured a single signal from the heart, and also anything else that happened to be picked up such as vibrations from the lungs.

    The signals from the lungs, heart, and everything else the stethoscope is sensitive to will all be picked up. They will all be mixed together in one signal, which we call a 'timeseries'l. Extracting specific signals from one big timeseries is done in a variety of ways and is the focus of the field called 'biological signal processing'. The most common, and still most fundamental method is called Fourier analysis. This involves some mathematics to fully understand. You need to find out for yourself what this is all about; it's not a matlab or computer issue. Your spectogram was the fourier transform of the timeseries you had, over some chosen period of time.

    'wheezing', in general is going to be much higher frequency than the heart beat. Also, I'd expect wheezing to to have a profile of some color of noise, like white noise. That means the wheezing will be non-periodic (that is why it is called 'noise').  Non-periodic means non-repeating. like the snow on a TV.

    So, on the spectogram, you'll see high power at a *range* of higher frequencies .Maybe near 100Hz or 500hz, I don't know. but high.

    For the heart beat, you'd expect to see more well defined peaks (less broad range) in the spectrogram at a very much lower frequency like 1-5Hz, and at a specific frequency rather than a range of frequencies. But if the heart beat is irregular, you won't pick that up so easily - the spectogram will give the average power of the heart signal over the time window you've chosen.

    look at (READ ABOUT) something called a fourier transform. That is the method by which the spectrogram has been produced.

    good luck - I'm afraid your question is really about signal processing, not matlab itself. There is plenty of literature on that, and if you are a student (or even if you're not) it is important that you read up on that.

    one other thing - fourier analysis involves a time/resolution trade off. If you want fine grained temporal resolution of what the heart rate is doing (what beats AND when), I'd suggest (after you've looked at fourier) looking at something called wavelet analysis

    biological signal processing is a big topic, requiring years of study. Sorry to disappoint, but there is no magic bullet. This is not really a matlab issue.

    you might have a look at some of the software packages that speech psychologists use. one might be called praat

    for fourier in matlab use the function fft. For wavelet you need the wavelet toolbox. Neither of these are trivial to use. Find out what a nyquist frequency is before you even start running code. A spectrogram plotted on your screen does not mean you've got it right!



    LVL 1

    Expert Comment

    ok, I'll be a bit nice. I'm not busy atm, so I'll tell you what I'd do in your position::

    get a fourier spectrogram over some big time window, maybe the whole timeseries. there will be a low frequency, very high power (amplitude) peak near the rate of oscillation you'd expect for the heart. What does the heart oscillate at? say 0.5-5Hz (half to five beats per second). It ought to be a nice peak, with much more power (amplitude, strength, 'height') than the wheezing. Why? The power (amplitude) of the wheezing has been spread over lots of frequencies, giving each one less power. Since the heart rate is changing though, this initial Fourier transform should be a little spread out since its being averaged over the whole trial, and changes during that trial.

    If the spectrum doesn't look like that, I'd guess there's been a mistake in data acquisition or analysis.

    Now take a range: 0.5-5Hz (e.g - but experiment with different values in a principled way) for the heart rate (you need to make an educated guess about how fast the subject's heart is beating).

    Create a wavelet transform using the matlab wavelet toolbox that looks only at those frequencies you've found in the spectrogram that you think are the heart. Look up carefully how to make this wavelet transform. It requires study and careful thought. The big difference between wavelet and fourier in practical terms is that wavelet can give you better info about when things occur, but at the cost that you have to restrict the range of frequencies you look at. It's all much more complex than that, but that is part of it.

    run the wavelet analysis on your timeseries (but which wavelet function? try morlett to begin with, but you need to read about it) . Wavelet analysis looks only at a chosen frequency range, that you decide upon. So,  It will ignore the high frequency wheezing noise stuff and give you:

    What frequency the heart is pumping and at what time, more or less. Tada.

    Now, do you want to correlate that to the wheezing?

    You could do this:: Now that you've done your your wavelet analysis on the heart, you know where in the timeseries to look for changes in wheezing amplitude (which is a broad range of freqs remember).

    So, for each *change* of heart rate  (and thanks to the wavelet, you know temporally where those are - PLOT THEM, RECORD THEM) do a fourier on the timerseries, but ONLY between times you've identified as heart-change-times. This will give  get the wheezing amplitude at those times. make sure all your fourier windows are the *same size* though, or things will get confusing for you.

    You'll get a bunch of wheeze spectrograms, each one associated with a change in heart rate from the prior wavelet analysis of the heart. Each of these should have different properties depending on the heart rate that time  (which you now know). Your job, presumably, is to find out what those properties are.

    The sampling rate of your data is crucial. What is a sampling rate? It means: how much data did you record per unit of real time? The higher this is, the easier it is to use fourier to find out whats happening. And if the sampling rate is lower than half  the average frequency of the wheezing, I'm afraid you are screwed.

    If not, and you have a nice sampling rate, the sequence of low temporal resolution Fourier transforms over those time periods you've identified as important in terms of the wavelet/heart analysis should show different kinds of wheezing for different kinds of heart rate - if indeed that relationship is there in the data (this is your hypothesis?).

    A quick look on the web shows that some speech sounds like sibilants

    in which air is forced through the vocal tract have a frequency of 4000Hz. I'd not be terribly surprised if wheezing was similarly high frequency. You need to find out. if it 4000Hz, you need to sample at 8000Hz to get an accurate Fourier transform.  Could be tricky. Maybe wheezing has a very broad range of freqs, and you could just look at the lower ones.

    ask your client.  As a computer professional, you have the right to be provided that information in the requirements document.

    lucky you! you've got a plan. And now my interest is all piqued I'd like to hear how you get on and offer more advice


    LVL 1

    Expert Comment

    I've just looked at the questions you've asked re: this on other areas of the site.

    If all you want to do is detect asthma, then all you need to do is detect a certain kind of noise in the data. All you need to to do that is to compare spectra between normal and asthmatics. A running average is not necessary; the rigorous approach is just to compare spectra. There will be more noise, at higher amplitude and with a distinctive spectrum (different kinds of noise have different spectra; white noise, pink noise etc)  in the asthmatic case.

    The solution I've outlined above allows you to do much more than this. You could show how the rate of wheezing varies with the heart rate.You could show how the heart rate and the wheezing rate interact during attacks, and start to answer questions like whether the heart rate increases before the attack begins, or after it.

    The heart increases it's rate as the sympathetic nervous system becomes more active, i.e. as one becomes more stressed. Does this trigger the asthma attack, or the other way around?

    That's what I thought you were interested in. If not, just compare the frequency components (remembering nyquist and sampling rate)  of normals and asthmatics using fourier! :) and it's easy to tell the difference between normal lung operation and an asthma attack


    LVL 1

    Accepted Solution

    don't use the method of looking for peaks above the general noise level in the time domain that the other fellow suggested. It is not rigorous, and would not be an acceptable method for a scientific publication. What that means is that it might give you spurious 'heart beats': how do you know that a peak was a heart beat? You don't. Lots of things will have an influence on the time series recorded from a stethoscope. Not everything above the general noise level will be a heart beat. Other factors like muscle movements of the arm will also do that, or factors from the environment (even someone closing a door, etc)

    a wavelet analysis will give you the best result you need. Why? Because that way you'll know that the amplitudes of all the beats are similar, so they are all probably heart beats. Something just being 'above a threshold' cannot do that, although if you insist on that route I'd suggest using a peak finding algorithm like the one below. Then you can determine the amplitudes of the peaks you find in the time domain and do with them what you will. But you have no basis for asserting that a peak of a certain amplitude is a heart beat or not. With a spectral method, discrete events will be smoothed away

    You'll find the wavelet method far superior, and ultimately easier. The wavelet transform will capture the *periodicity* of the heart pulses, which muscle movements do not have, because they are discrete.

    I'm a scientist and an ex-computer programmer. The thresholding method is a computer programmer's solution. It assumes the data makes sense and is free of pollution. Scientist's do not assume this.

    As a scientist (and now a teacher) moving from computing into science, I've learned the hard way that data from the real world does not behave in a simple way that is amenable to an algorithmic solution such as the one suggested.

    signal processing methods are much more robust in this respect; that's why I suggested them, and that's why scientists use them.

    your client will be happy, and I will be happy to accept an acknowledgement at the end of your published paper ;-> (that's a joke by the way)

    for finding peaks in data use the function pickextr contained in the file here:

    Author Closing Comment

    Thank you for your help. I found the wheezing part but was unsure of the other parts. Thanks, again, Jim. I'll post the solution on the site when I'm finished with it. The project is being done for a grant proposal.
    LVL 1

    Expert Comment

    Grant proposals have very rigorous standards. You'd be well advised to look at fourier analysis first. Then wavelets.

    Actually, the basic idea of fourier is simple: any periodic timeseries, however complex it might look, is just the sum of sine and cosine waves of differing frequencies and amplitudes. The power (amplitude) of the signal at a given frequency is the amplitude of the sine wave at that frequency. The reason you need cosine waves too is to account for the phase of the components which you need to keep the shape of the original signal. You should figure out why that is. After you get the sine/cosine notion of phase and amplitude of components, you should start to look at complex numbers - these can be used to represent phase as well, and fourier analysis begins to get deeper as you start to understand that.

    In practice, it is more complicated than that. Fourier assumed that the timeseries was infinite in length, but in the real world this is never the case obviously. Fourier also assumed that the waveform under analysis (the timeseries) was continuous. But on a computer, all we have is a discrete approximation. That is why the sampling rate is so crucial.

    because real timeseries are finite, there are edge effects at the beginning and end of the window you've 'chosen' into the putative infinite series.

    but watch out! all this only works for time-series that are stationary: The mean never changes (and should be 0) and the signal is periodic. Real signals sometimes have non-stationarities (in EEG, for example, an ERP is a non-stationarity. In the heart, an arrhythmia or stochastic flutter of some kind would be a non-stationarity)

    All this is in the standard literature.

    I don't know what you are studying or where, but I do know that I am not a natural mathematician, but I've nonetheless got to grips with this stuff and even more advanced stuff -  even taught it occasionally.

    So, you can do it.

    But vagaries in grant proposals will not do. It needs to be perfect. We've worked on a grant for 2 years. We just got it. The sum of money is very large, so it can be done, but it is harder to get a grant than to get a paper accepted. You need to be *very* professional, especially in these hard times. Technical mistakes *will* be caught and you will look foolish, and get a rejection.

    good luck,



    Featured Post

    Top 6 Sources for Identifying Threat Actor TTPs

    Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

    Join & Write a Comment

    Suggested Solutions

    Title # Comments Views Activity
    sum28 challenge 31 82
    fix34  challenge 9 70
    mapAB Challlenge 35 52
    Homework Math Question 1 38
    One of Google's most recent algorithm changes affecting local searches is entitled "The Pigeon Update." This update has dramatically enhanced search inquires for the keyword "Yelp." Google searches with the word "Yelp" included will now yield Yelp a…
    This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
    Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
    The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

    729 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    16 Experts available now in Live!

    Get 1:1 Help Now