Results 1 to 12 of 12

Thread: Extract data from sound card for ECG

  1. #1

    Thread Starter
    New Member
    Join Date
    Oct 2008
    Posts
    6

    Extract data from sound card for ECG

    Hello there, I hope this is the place to ask this question ...

    I am doing a little project titled Heartbeat Monitor. I have finished my hardware (to detect the ECG signal) and now I just need to connect my circuit to the PC and program away in VB to calculate the heart rate. In order to save cost I would use the PC's sound card as my ADC.

    So here are my questions:
    1. How do I praogram in VB to use the sound card as an ADC
    2. How do I extract that digital data
    3. What do I need to do in order to calculate the heart rate

    I have some basic knowledge on C++ and a bit familiar with Visual Basic C++, but not familiar with Visual Basic. Anyone could help me out or maybe recommend a good book?

    A guy did a project very similar to mine and code the software that can be found on this page. I am rather at a loss in trying to understand his codes.

  2. #2
    New Member
    Join Date
    Sep 2008
    Posts
    9

    Re: Extract data from sound card for ECG

    Seems quite a simple one in theory.

    Basically what the other chap is doing is feeding a amplified signal into his microphone socket, he doesn't say what hardware he uses, some kind of opamp or FET maybe to amplify the signal?

    A Computer has a built in ADC in the form of a sound card, for example, we can hook up a microphone and save whatever sound is fed in as a wave file or MP3.

    The digital data is read from sound buffers, which can be programmed as part of the standard windows API.

    The heart rate calculation can most probably be done on the samples per second and bits per sample, effectively creating an oscilloscope, which you can set a trigger or plot X and Y.

    This is how I understand it anyway?

    Microsoft Visual Basic 6 comes with some sample code to do with the Windows Audio API, it may be part of the MSDN that is shipped with VB/VS 6.

    Hope this helps?

    Wilksey.

  3. #3

    Thread Starter
    New Member
    Join Date
    Oct 2008
    Posts
    6

    Re: Extract data from sound card for ECG

    Sounds simple ... I'll go through the codes again and try out some VB tutorials. The signal was amplified since its original amplitude is around 0.5mV and he needs to amplify it so it will be recognisable. Thank you for your help. I now know where to start ... hope I do not lose my way.

  4. #4
    New Member
    Join Date
    Sep 2008
    Posts
    9

    Re: Extract data from sound card for ECG

    Yes, just be careful not to over amplify it else the signal will start to distort and you will get invalid data.

    But obviously a stronger signal will make better data acquisition thus in theory returning more acurate results, might be worth putting a potentiometer inline to adjust the amplification level, and see how the results vary.

    Looking at the code quickly, seems as though it has pretty much everything you need in there, just need to tweak it to suit your exact needs.

    Functions.bas (Module1) has all the API you need for recording / sampling, the defined type "WAVEFORMAT" is the one that contains the sampling rate and bits per second.

    And in the main form(Form1) there is a function called "edge_trigger" which seems to calculate the heart rate. This is called from a function called "findy" which is called from "Update_Graph" which is called from Timer1.

    Update_Graph appears to be the function that calulates the sample data.


    Wilksey.

  5. #5

    Thread Starter
    New Member
    Join Date
    Oct 2008
    Posts
    6

    Re: Extract data from sound card for ECG

    Hello there, I am back again. The codes are giving me a major headache. "Functions.bas" contains all the Audio API needed so I don't have to mess around with anything there. However in "Function.bas" there is a portion of codes that the programmer added himself:

    Code:
    ' Now my stuff
    Public Const CALLBACK_NULL = &H0
    Public Const WHDR_DONE = 3
    Public Const TIME_BYTES = 4
    Public Const TIME_SAMPLES = 2
    
    Public Declare Function waveInGetID Lib "winmm.dll" (lphWaveIn As Long, ByVal uDeviceID As Long) As Long
    Declare Function waveInGetErrorText Lib "winmm.dll" Alias "waveInGetErrorTextA" (ByVal err As Long, ByVal lpText As String, ByVal uSize As Long) As Long
    Declare Function waveInGetPosition Lib "winmm.dll" (ByVal hwi As Long, pmmt As MMTIME, ByVal cbmmt As Long) As Long
    
    ' Functions needed to allocate memory blocks
    Declare Function HeapCreate Lib "kernel32" (ByVal flOptions As Long, ByVal dwInitialSize As Long, ByVal dwMaximumSize As Long) As Long
    Declare Function HeapAlloc Lib "kernel32" (ByVal hHeap As Long, ByVal dwFlags As Long, ByVal dwBytes As Long) As Long
    Declare Function HeapDestroy Lib "kernel32" (ByVal hHeap As Long) As Integer
    
    
    Type MMTIME
       wType As Integer
       dwData As Long
       dwData2 As Long  ' Other data will not be used but there to
       dwData3 As Long  '  make the call think there's enough room
       dwData4 As Long
    End Type
    I am at complete loss as to what those things mean. waveInGetID is needed to get the device identifier for the given waveform-audio input device and I have searched the rest at Microsoft's MSDN website. What I do not understand is how does the programmer knows what to add in since he said "Now my stuff".

    Secondly, I am wondering where is the portion that changes the analogue ECG signal to digital? Or has the signal been digitised automatically by the soundcard and all he does is call the digital signal and then play with it ... if so which part of the code would that be?

    The rest of the complete codes can be found here.

  6. #6
    Frenzied Member Jim Davis's Avatar
    Join Date
    Mar 2001
    Location
    Mars base one Username: Jim Davis Password: yCrm33
    Posts
    1,284

    Re: Extract data from sound card for ECG

    Yes, just be careful not to over amplify it else the signal will start to distort and you will get invalid data.
    Correct. The invalid data may result invalid countings. But there two methods are exists to amplify an input audio to the proper amplitude.

    One is called Limiter. http://en.wikipedia.org/wiki/Limiting
    The other method is called as Compressor. http://en.wikipedia.org/wiki/Dynamic_range_compression

    The compressor will give you a manipulated audio result, but in some cases it is better than using a limiter.


    Secondly, I am wondering where is the portion that changes the analogue ECG signal to digital?
    The soundcard have got built in A/D (ADC) D/A (DAC) converters, ADC to sampling an analogue input into digital values (digitizes), also a DAC to reproduce the digital values to analogue output (the sound you hear on the speakers). So, the values (results) you will get from the api calls, will be amplitude values between -32767 to +32767 (in case you are use 16bit recording).

    Here is a quite detailed article about what is the A/D conversion.
    http://en.wikipedia.org/wiki/Analog_...ital_converter

    I'm also recommend you to go for a good quality sound card that have a quite low SNR (signal to noise, around -100dB will be ok), or an even more specific one, that is mainly built for heart monitoring.
    Last edited by Jim Davis; Dec 16th, 2008 at 01:23 AM.

  7. #7
    Frenzied Member Jim Davis's Avatar
    Join Date
    Mar 2001
    Location
    Mars base one Username: Jim Davis Password: yCrm33
    Posts
    1,284

    Re: Extract data from sound card for ECG

    http://www.planet-source-code.com/vb...70064&lngWId=1

    Here, i have found a quite advanced (using subclassing) sound recorder app. It contains all the methods you have to use to input the samples from the soundcard.

    I'm working on a cut-down frontend, that will show you how to use its main part. brb

  8. #8

    Thread Starter
    New Member
    Join Date
    Oct 2008
    Posts
    6

    Re: Extract data from sound card for ECG

    I am wondering about something, the files named Module2.bas and Class1.cls. Do I need to use them? If not then what are they used for. Thanks.

  9. #9

    Thread Starter
    New Member
    Join Date
    Oct 2008
    Posts
    6

    Re: Extract data from sound card for ECG

    Hello there again,

    I went through the codes in "Functions.bas" and saw the following APIs:

    1. waveOutStart
    2. WaveOutStop
    3. WaveOutAddBuffer

    I looked through Microsoft's MSDN website, but could not find anything on those three APIs. The EXE file that the programmer compiled works just fine, so I am wondering what those three APIs are for since I could not find anything on the internet. Thank you to those who replied.

  10. #10
    New Member
    Join Date
    Sep 2008
    Posts
    9

    Re: Extract data from sound card for ECG

    I think waveout api's are part of the winmm dll but it is only for writing WAV files, you are interested in the Waveinstart etc API's to get sound data into the computer.

    And in response to your previous question, no, it doesn't look as though you do need them, they look like they are for callback functions which you dont need.

    Regards,

    Wilksey

  11. #11
    Frenzied Member Jim Davis's Avatar
    Join Date
    Mar 2001
    Location
    Mars base one Username: Jim Davis Password: yCrm33
    Posts
    1,284

    Re: Extract data from sound card for ECG

    Yes, waveout apis are for output, but you have to go for the input apis. The recorder app you can find on the link i posted before, will show you the only way how can you "monitor" the wave input (using buffers), that is you can use in your application to process the peak data.

    The most important apis (you cant live without em) are the following:
    * waveInOpen
    * waveInGetErrorText (for retrieving error text)
    * waveInPrepareHeader
    * waveInAddBuffer
    * waveInStart

    But for using these apis, you have to declare buffers, and then subclassing your form. I have seen no any other way to retrieve the peak data, in a precise way. It is may be a bit more advanced than you expected, but it is worth to dig into.

    By using the above declarations (in modWaveIn.bas), i have got an app that is measures the bpm. It is may be ideal for heart monitoring, but i'm not sure what common algorithms are present for this purpose. My app is simply finds out the maximum peak value of a buffer input, then compares it to the treshold (Tsh scrollbar) value. If maximum is greater than treshold then adds one to the counter (that is a heart beat), then stores the value, it is can be compared later, to find out the next maximum value in order, that is will be the next beat.

    Here is a screenshot were my app processes a sample input of a 17x bpm heart beat i speed up for testing.
    Attached Images Attached Images  

  12. #12

    Thread Starter
    New Member
    Join Date
    Oct 2008
    Posts
    6

    Re: Extract data from sound card for ECG

    Thank you Jim Davis, and yes it is very advanced, but I have to finish up with my small program in a month. If those are the most important APIs then does it mean I do not need the other waveIn APIs and waveOut APIs? In addition, what is the purpose of the following APIs being declared in the codes as there were no waveIns APIs realted to them being used:

    *waveOutGetDevCaps
    *waveOutGetNumDevs
    *waveOutWrite


    Apart from that what are the uses of the following since I am not sure what they are used for apart from mmioOpen that opens a file for unbuffered or buffered I/O and mmioClose is to close it.

    *mmioAscend
    ascends out of a chunk in a RIFF file descended into with the mmioDescend function or created with the mmioCreateChunk function.

    *mmioDescend
    descends into a chunk of a RIFF file that was opened by using the mmioOpen function. It can also search for a given chunk.

    *mmioRead
    The mmioRead function reads a specified number of bytes from a file opened by using the mmioOpen function.

    *mmioStringToFOURCC
    The mmioStringToFOURCC function converts a null-terminated string to a four-character code.

    *mmioDescendParent & mmioReadFormat - could find nothing on these on the internet


    In addition I have found nothing on:
    * waveOutStart
    * WaveOutStop
    * WaveOutAddBuffer


    Does this mean they do not exist, and so I could just delete them from the codes?
    Last edited by aredhel; Feb 10th, 2009 at 08:05 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width