Difference between revisions of "TPC5 File Format"

From Elsys Wiki - Help and Recources for TranAX and TraNET DAQ
Jump to: navigation, search
(Created page with "Category:TranAX Category:API TranAX is saving trace date, spectral data and TabPages in the [http://www.hdfgroup.org/HDF5/index.html| HDF5 file format]. Depending on...")
 
m
Line 5: Line 5:
  
 
The HDF5 file format was developed at the University of Illinois. The HDF5 specifications and the libraries source codes are open.
 
The HDF5 file format was developed at the University of Illinois. The HDF5 specifications and the libraries source codes are open.
 +
 +
== Matlab Data Import ==
 +
More information about HDF5 support in Matlab can be found on the [https://ch.mathworks.com/help/matlab/hdf5-files.html| MathWorks Website].
 +
* [[Media:MatlabDataImportExample.zip| Download Source Code]]
 +
 +
<syntaxhighlight lang="matlab">
 +
 +
filename = 'Example.tpc5';
 +
% Read the file structure, just for get an overview
 +
fileStruct = hdf5info(filename);
 +
 +
 +
% Read out channel settings for the first channel
 +
 +
% Digital marker signals are a part of the binaray measurement values.
 +
% For separating digital from analog values the following two
 +
% bit maskes are needet
 +
analogMask = uint16(hdf5read(filename,'/measurements/00000001/channels/00000001/analogMask'));
 +
markerMask = uint16(hdf5read(filename,'/measurements/00000001/channels/00000001/markerMask'));
 +
 +
 +
% Measurement values are stored as they are comming from the A/D converter.
 +
% For getting voltage values or scaled physical values the corresponding
 +
% convertion values must be read out.
 +
 +
% Conversion factor for scaling to voltage values
 +
binToVoltFactor        = hdf5read(filename,'/measurements/00000001/channels/00000001/binToVoltFactor');
 +
binToVoltConstant      = hdf5read(filename,'/measurements/00000001/channels/00000001/binToVoltConstant');
 +
% Conversion factor for scaling to physical values
 +
voltToPhysicalFactor    = hdf5read(filename,'/measurements/00000001/channels/00000001/voltToPhysicalFactor');
 +
voltToPhysicalConstant  = hdf5read(filename,'/measurements/00000001/channels/00000001/voltToPhysicalConstant');
 +
 +
% Read out the data from the first channel, first block
 +
data = hdf5read(filename, '/measurements/00000001/channels/00000001/blocks/00000001/raw');
 +
% Mask out the digital marker bits
 +
analogData = bitand(data,analogMask);
 +
 +
% Scale data to voltage values
 +
dbData = cast(analogData,'double');
 +
dataScaled = cast((dbData * binToVoltFactor) + binToVoltConstant,'double');
 +
 +
% Scale data to physical values
 +
dataPhysical = (dataScaled * voltToPhysicalFactor) + voltToPhysicalConstant;
 +
 +
</syntaxhighlight>
 +
  
 
== C++ Reading Example ==
 
== C++ Reading Example ==

Revision as of 14:30, 26 September 2017


TranAX is saving trace date, spectral data and TabPages in the HDF5 file format. Depending on the data type the file extention is *.tpc5, *.tps5 or *.tpd.

The HDF5 file format was developed at the University of Illinois. The HDF5 specifications and the libraries source codes are open.

Matlab Data Import

More information about HDF5 support in Matlab can be found on the MathWorks Website.

filename = 'Example.tpc5';
% Read the file structure, just for get an overview 
fileStruct = hdf5info(filename);


% Read out channel settings for the first channel

% Digital marker signals are a part of the binaray measurement values.
% For separating digital from analog values the following two 
% bit maskes are needet
analogMask = uint16(hdf5read(filename,'/measurements/00000001/channels/00000001/analogMask'));
markerMask = uint16(hdf5read(filename,'/measurements/00000001/channels/00000001/markerMask'));


% Measurement values are stored as they are comming from the A/D converter.
% For getting voltage values or scaled physical values the corresponding 
% convertion values must be read out. 

% Conversion factor for scaling to voltage values
binToVoltFactor         = hdf5read(filename,'/measurements/00000001/channels/00000001/binToVoltFactor');
binToVoltConstant       = hdf5read(filename,'/measurements/00000001/channels/00000001/binToVoltConstant');
% Conversion factor for scaling to physical values
voltToPhysicalFactor    = hdf5read(filename,'/measurements/00000001/channels/00000001/voltToPhysicalFactor');
voltToPhysicalConstant  = hdf5read(filename,'/measurements/00000001/channels/00000001/voltToPhysicalConstant');

% Read out the data from the first channel, first block
data = hdf5read(filename, '/measurements/00000001/channels/00000001/blocks/00000001/raw');
% Mask out the digital marker bits
analogData = bitand(data,analogMask);

% Scale data to voltage values
dbData = cast(analogData,'double');
dataScaled = cast((dbData * binToVoltFactor) + binToVoltConstant,'double');

% Scale data to physical values
dataPhysical = (dataScaled * voltToPhysicalFactor) + voltToPhysicalConstant;


C++ Reading Example

#include "stdafx.h"
#include <iostream>
#include <fstream>

using std::cout;
using std::endl;
using std::ofstream;

#include <string>
#include "H5Cpp.h"		// Using the C++ High Level API
using namespace H5;


int _tmain(int argc, _TCHAR* argv[])
{

	unsigned int const BLOCK_SIZE = 256 * 1024;

	try {
		H5File file("Example.tpc5", H5F_ACC_RDONLY);

		// Read out channel settings for the first channel

		// Use HDFView for further analysis of the file structure of your file

		// Digital marker signals are a part of the binaray measurement values.
		// For separating digital from analog values the following two
		// bit maskes are needet (is the same for all channel of one board)
		Group g1 = file.openGroup("/measurements/00000001/channels/00000001");
		Attribute ch1_analogmask = g1.openAttribute("analogMask");

		unsigned int analogmask_ch1;
		ch1_analogmask.read(PredType::NATIVE_INT, &analogmask_ch1);
		ch1_analogmask.close();

		Attribute ch1_markermask = g1.openAttribute("markerMask");
		unsigned int markermask_ch1;
		ch1_markermask.read(PredType::NATIVE_INT, &markermask_ch1);
		ch1_markermask.close();

		// Measurement values are stored as they are comming from the A / D converter.
		// For getting voltage values or scaled physical values the corresponding
		// convertion values must be read out. (could be different for each channel)
		Attribute ch1_binToVoltFactor = g1.openAttribute("binToVoltFactor");
		Attribute ch1_binToVoltConstant = g1.openAttribute("binToVoltConstant");
		float binToVoltFactor_ch1, binToVoltConstant_ch1;
		ch1_binToVoltFactor.read(PredType::NATIVE_FLOAT, &binToVoltFactor_ch1);
		ch1_binToVoltConstant.read(PredType::NATIVE_FLOAT, &binToVoltConstant_ch1);
		ch1_binToVoltFactor.close();
		ch1_binToVoltConstant.close();

		// Conversion factor for scaling to physical values
		Attribute ch1_voltToPhysicalFactor = g1.openAttribute("voltToPhysicalFactor");
		Attribute ch1_voltToPhysicalConstant = g1.openAttribute("voltToPhysicalConstant");
		float voltToPhysicalFactor_ch1, voltToPhysicalConstant_ch1;
		ch1_voltToPhysicalFactor.read(PredType::NATIVE_FLOAT, &voltToPhysicalFactor_ch1);
		ch1_voltToPhysicalConstant.read(PredType::NATIVE_FLOAT, &voltToPhysicalConstant_ch1);
		ch1_voltToPhysicalFactor.close();
		ch1_voltToPhysicalConstant.close();

		// Read out the data from the first channel, first block
		DataSet dataset_ch1_block1 = file.openDataSet("/measurements/00000001/channels/00000001/blocks/00000001/raw");

		int* rawData = new int[BLOCK_SIZE];
		dataset_ch1_block1.read(rawData, PredType::NATIVE_INT);
		dataset_ch1_block1.close();

		// Mask out the digital marker bits
		int* analogData = new int[BLOCK_SIZE];
		int* markerData = new int[BLOCK_SIZE];
		float* voltageData = new float[BLOCK_SIZE];
		float* physicalData = new float[BLOCK_SIZE];

		// Output Text file
		ofstream myfile;
		myfile.open("example.txt");

		for (int i = 0; i < BLOCK_SIZE; i++){
			analogData[i] = rawData[i] & analogmask_ch1;
			markerData[i] = rawData[i] & markermask_ch1;

			// Scale Data to voltage and/or physical data
			voltageData[i] = ((float)analogData[i] * binToVoltFactor_ch1) + binToVoltConstant_ch1;
			physicalData[i] = (voltageData[i] * voltToPhysicalFactor_ch1) + voltToPhysicalConstant_ch1;

			// Log Data
			myfile << physicalData[i] << endl;
		}

		myfile.close();
		g1.close();
		file.close();

		delete rawData;
		delete analogData;
		delete markerData;
		delete voltageData;
		delete physicalData;
	}
	catch (AttributeIException error)
	{
		error.printError();
		return -1;
	}
	catch (...){}
	return 0;
}