Data Reading method and Matlab program of MIT-BIH ECG signal

Source: Internet
Author: User

 
(20110622: Update download link)


I recently wrote a paper on the ECG signal compression algorithm based on wavelet transform. I searched Baidu and professional forums for how to obtain the ECG signal test data, I found many friends worried about it. Now that my thesis has been written, I will write out how to obtain and process the ECG data in my contribution for your reference, saving the trouble of hard work in Baidu and forums.

I. First, there are two ways to observe, analyze, and diagnose ECG signals:

(1) download from a MIT-BIH Database
Please refer to the article I published a few days ago, "MIT-BIH ECG data download and read diagrams", which has a detailed introduction.

Http://blog.csdn.net/chenyusiyuan/archive/2008/01/06/2027887.aspx

(2) Special matlab ecg data reading program
On the 10th, I found the MATLAB program (rddata. m) Reading ECG data in the "Medical Devices" section of Hao Hui electronic forum (http://www.hheet.com/bbs! This program is written by foreigners and can read the MIT-BIH database. ,. dat ,. based on the data in the three files of HEA, the actual ECG signal value is calculated and the signal waveform is drawn. The program is not large, and the comments are complete, but only in English. If you need this program, please download it at the following link.

Http://download.csdn.net/source/3383369


2. If you want to compress and encode the ECG signal

The data obtained by the above program is not easy to use, because it is converted to actual ECG data, and the signal data value is generally between-2 and ~ The Unit is mV. So, are you looking for a new ECG reader to obtain data? No! In fact, the program rddata. m itself is to convert the binary data stored in the MIT. dat file to the decimal data, and then further convert it to the actual ECG signal value. During signal processing, we need to use the initial decimal data from binary data conversion, because. in the DAT file, three bytes are stored in two numbers, that is, each number is 12 bits. The converted decimal number range should be 0 ~ 2048. The data storage method I understand is illustrated as follows. I do not know if it is correct. It is for reference only:

Because rddata. the comments in the M program are in English and cannot be clearly stated in some places. I extract some code from the program to convert binary data into decimal data, and convert the comments into Chinese, I hope it will be helpful to you!

The specific program code is as follows:

% -------------------------------------------------------------------------
% Program fun_readecgdata is used to read ECG signal data and convert the original binary data to the decimal number.
% Input parameters and examples:
% PATH = 'd:/Matlab/r2007b/work/ECG data '; % specify the data storage path
% Headerfile = '2017. hea'; %. Hea format, header file, which can be opened in Notepad
% Datafile = '2014. dat '; %. dat format, ECG data
% Samples2read = 2048; % specify the number of samples to be read
% If there are two channels in the. dat file:
% Reads 2 * samples2read data
% Output parameter: m -- a data matrix with two columns of samples2read rows. Each column represents the signal value of a channel.
% -------------------------------------------------------------------------

Function M = fun_readecgdata (path, headerfile, datafile, samples2read)

% ------ Load header data --------------------------------------------------
% ------ Read header file data -----------------------------------------------------
%
% Example: Data in the 117.hea file opened in Notepad
%
% 117 2 360 650000
% 117.dat 212 200 11 1024 839 31170 0 mlii
% 117.dat 212 200 11 1024 930 28083 0 v2
% #69 m 950 654x2
% # None
%
% -------------------------------------------------------------------------


% -------------------------------------------------------------------------
% [Note] the fprintf function writes formatted data to a specified file.
% Expression: Count = fprintf (FID, format, ,...)
% Under the control of the string 'format', format the real data of matrix A and write it to the file object FID. This function returns the number of bytes of the written data, Count.
% FID is an integer File Identifier obtained through the fopen function. FID = 1, indicating the standard output (that is, the output is displayed on the screen); FID = 2, indicating the standard deviation.
% -------------------------------------------------------------------------

Fprintf (1, '// N $> working on % s.../N', headerfile); % displays the current working status in the MATLAB command line window
Signalh = fullfile (path, headerfile); % use the fullfile function to obtain the complete path of the header file
Fid1 = fopen (signalh, 'R'); % open the header file. Its identifier is fid1 and its attribute is 'R' -- "read-only"
Z = fgetl (fid1); % read the first line of the header file, in string format
A = sscanf (z, '% * S % d % d', [1, 3]); % convert data by format '% * S % d % d' and store it in matrix
Nosig = A (1); % Number of Signal Channels
Sfreq = a (2); % data sampling frequency
Clear a; % clear matrix A, ready to get the next row of data
For k = 1: nosig % reads the data information of each channel Signal
Z = fgetl (fid1 );
A = sscanf (z, '% * S % d % d', [1, 5]);
Dformat (K) = A (1); % signal format; only 212 format is allowed here
Gain (K) = a (2); % The number of integers per mv
Bitres (K) = A (3); % sampling accuracy (bit resolution)
Zerovalue (K) = A (4); % integer corresponding to the zero point of the ECG signal
Firstvalue (K) = A (5); % The first integer of the signal (used for deviation testing)
End;
Fclose (fid1 );
Clear;

% ------ Load binary data --------------------------------------------------
% ------ Reading two-value ECG data ----------------------------------------------
%
% Description: Data Format of the. dat file
%
% Reads n samples in uint8 format and stores them in matrix A. Then, column A contains N rows and 3 columns, each column contains one byte,
% Indicates that each row uses three bytes to represent two numbers m1 and m2, and each number is 12 bits, which is also known as the 212 format.
% M1 low 8 bits are stored in a (:, 1), M2 low 8 bits are stored in a (:, 3 ),
The 4-bit high of % M1 is stored in the 4-bit low of A (:, 2), and the 4-bit high of M2 is stored in the 4-bit high of A (:, 2 ).
%
% Based on the above data format, a series of shifts, bits, and operations can be used to extract two-channel signal data in decimal format
%
% -------------------------------------------------------------------------

If dformat ~ = [212,212], error ('this script does not apply Binary formats different to 212. '); end;
Signald = fullfile (path, datafile); % reads ECG signal data in the 212 format
Fid2 = fopen (signald, 'R ');
A = fread (fid2, [3, samples2read], 'uint8') '; % matrix A has samples2read rows and three columns, each of which is read in the uint8 format, note that the data has become a decimal number through the uint8 reading method.
Fclose (fid2 );
M2h = bitshift (A (:, 2),-4); % byte shifts to the right four bits, that is, take the four bits of the byte, which belongs to the four bits of the signal 2
M1h = bitand (A (:, 2), 15); % takes the four lower bits of the byte, which belongs to the four high bits of the signal 1
Prolactin = bitshift (bitand (A (:, 2), 8), 9); % sign-bit removes the highest byte among the four low bits, and shifts nine bits to the left
PRR = bitshift (bitand (A (:, 2), 128), 5); % sign-bit extracts the highest byte in four bits, and shifts it to the left by five bits
M (:, 1) = bitshift (m1h, 8) + a (:, 1)-prolactin; % shift the m1h and m2h values to the left of 8 bits, that is, multiply by 2 ^ 8, add a (:, 1), a (:, 2 ),
M (:, 2) = bitshift (m2h, 8) + a (:, 3)-PRR; % because the symbol bit is also moved during the Left shift, the value of the symbol bit is subtracted.
M = M'; % to facilitate later data processing, the output matrix m is transposed into two rows of samples2read columns.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.