Trying to plot ECG points from a .hdr and .dat file in Python

44 views Asked by At

I am trying to run and plot ECG data that is stored in a .dat file. This .dat file has a corresponding header file attached to it that explains the details of the .dat file, including the gain and channels used. This .hdr file is below:

File Name:  tam_racap.dat
Frontend Gain: 1000.0000
Scan Rate: 1000.0000
Channels Used:
Output Filtered         Gain: 2.4400
ECG         Gain: 2.4400
D/A Output          Gain: 2.4400
Digital Output 4            Gain: 2.4400
Digital Output 6            Gain: 2.4400

Notes:

Currently, I am trying to read the data from channel 2, as from my understanding, that is the ECG channel outlined in the header file.

Below in my wave_plot function that I have written that uses the header file information to read the data in the .dat file. I am assuming the information in the .dat file is just binary data, as opening it in a text editor shows that it is unreadable.

The wave_plot function has some PROF_ stuff in it, but that is not the problem, that is just how I am finding the directory for the waveform.

def wave_plot(profile_name):
    header_file_path = os.path.join(os.getcwd(), f'PROF_{profile_name}', 'header.hdr')

    # Check if the header file exists
    if os.path.exists(header_file_path):
        with open(header_file_path, 'r') as header_file:
            header_lines = header_file.readlines()

        frontend_gain = float(header_lines[1].split(':')[1].strip())
        scan_rate = float(header_lines[2].split(':')[1].strip())
        channels_used = [line.split(':')[0].strip() for line in header_lines[4:]]

        # Read binary data file
        data_file_path = os.path.join(os.getcwd(), f'PROF_{profile_name}', 'data.dat')
        if os.path.exists(data_file_path):
            with open(data_file_path, 'rb') as data_file:
                # Skip to the start of channel one data
                data_file.seek(2)  # Assuming data format starts at the beginning of the file
                # Read the data from channel one
                ecg_data = np.fromfile(data_file, dtype=np.float64)  # Assuming data type is float64

            # Apply gain to the data
            ecg_data = ecg_data * frontend_gain

            # Create time axis
            time = np.arange(0, len(ecg_data) / scan_rate, 1 / scan_rate)

            # Plot first 50 points of ECG waveform
            plt.figure(figsize=(10, 6))
            plt.plot(time[:50], ecg_data[:50], color='blue', linestyle='-', linewidth=0.5)  # Adjust line style and color
            plt.xlabel('Time (s)')
            plt.ylabel('Voltage (mV)')
            plt.title('ECG Waveform')
            plt.grid(True)
            plt.show()
        
    else:
        print(f"Header file not found for profile '{profile_name}'.")

Whenever I try and run this program, however, the plot is not correct at all. I am not sure what I am doing wrong. I have tried plotting only the first x amount of points, as well as changing what channel I am reading from. Any help would be greatly appreciated. Thanks!

0

There are 0 answers