Reading and Writing TDMS-Files in MATLAB - MATLAB
Video Player is loading.
Current Time 0:00
Duration 7:00
Loaded: 0.00%
Stream Type LIVE
Remaining Time 7:00
 
1x
  • Chapters
  • descriptions off, selected
  • captions off, selected
      Video length is 7:00

      Reading and Writing TDMS-Files in MATLAB

      TDMS is a binary file format structured in three levels of hierarchy called file, channel group, and channel. The data is recorded in each channel and metadata can be added to each level of the hierarchy. Learn how to inspect and read TDMS-files in MATLAB® using Data Acquisition Toolbox™, how a datastore can be used to analyze a collection of TDMS-files, and finally, how to write data to a TDMS-file and update its metadata.

      The example showcases the vibrational analysis of a rotating shaft. On the rotating shaft, there are two displacement sensors—X and Y—that measure changes in distance between the machine’s rotating element and its stationary frame. There is a key phasor signal that provides a reference point in the machine’s rotation to correlate other vibration signals and measure shaft rotational speed.

      The 'tdmsinfo' function provides an overview of the file, while the 'ChannelList' field shows the channel groups and channels in the file. The 'tdmsreadprop' function provides the channel properties such as 'wf_increment', which displays the timestamp in seconds. The data is first read using the 'tdmsread' function and then plotted. Next, a frequency domain analysis is performed on the signals to determine where the machine is out of balance; this is based on the phase shift between the vibration sinusoidal peak and the phase reference pulse.

      Datastores in MATLAB allow the user to iterate over a single large file or a collection of files that have the same layout. The 'tdmsDatastore' function is used to iterate over a collection of vibration measurement. The 'transform' function is then run to take in a datastore and function handle, returning a transformed datastore object. Here, a custom function is applied to each of the recorded TDMS-files.

      The results of the analysis are written to a new TDMS-file, and data is read back to validate the results. The 'tdmsreadprop' function is used to update TDMS channel properties, such as ChannelGroupName, ChannelName, and unit_string.

      Published: 30 Jun 2023

      Welcome to reading and writing TDMS-files in MATLAB. This example shows how to inspect and read TDMS-files into MATLAB, how a data store can be used to analyze a collection of TDMS-files, and finally, how to write the results of the analysis to a TDMS-file and update its metadata. For this example, we have vibrational measurements of a rotating shaft recorded across multiple TDMS-files for weighting RPM.

      The goal is to read this data set into MATLAB, apply algorithms that check for rotational imbalance, and to generate a status report in TDMS-file format. On the rotating shaft, there are two displacement sensors labeled X and Y, which measure changes in distance between the machine's rotating element and its stationary frame. That is a key phasor signal to provide a reference point in the machine's rotation to correlate with other vibration signals and to provide a simple means of measuring shaft rotational speed.

      Let's look at one of the measurement files. The tdmsinfo function provides an overview of the file. The ChannelList field lets you view the channel groups and channels in the file. Here, the "Pulse" channel is the key phasor signal. Sensor X and sensor Y are displacements measured from sensors X and Y, respectively.

      Before reading the data into MATLAB, you need to find the timestamp with which the data is recorded. Using tdmsreadprop, you can obtain the channel properties. The wf_increment property reflects the timestamp in seconds. Read the data using the tdmsread function, then plot the data. In the plot, observe the X and Y displacement sensors indicate a high vibration at the same frequency as the shaft rotation.

      The next step is to perform a frequency domain analysis on these signals where the phase shift between the vibration sinusoidal peak and the phase reference pulse indicates where the machine is out of balance. Let's pass the data to a custom vibration analysis function. This function returns the rotation RPM along with a Boolean that indicates whether the shaft is within the balance limits of that RPM.

      Now, let's look at creating multiple files into MATLAB. MATLAB has the concept of a data store, which allows you to iterate over a single large file or a collection of files that have the same layout. In this example, we use the tdmsDatastore function to iterate over a collection of vibration measurements.

      The data store ReadSize parameter sets the iteration size. Here, we use a ReadSize of "file" to iterate one file after another. The typical pattern to iterate through the datastore is to read data in while loop until the end of the data set.

      There are various functions in MATLAB that can be applied to a datastore. One such function is the transform. The transform function takes in a datastore and a function handle and returns a transform datastore object. The function handle can be any operation that you want to apply to the data set.

      In this case, we want to apply the vibanalyse function to each of the recorded TDMS files. We created a transform datastore iterator and run the same iteration while loop, but this time, it accumulates the results of each file in the vibRes variable. The results show the rotating shaft is unbalanced at 900 RPM.

      Let's write the results of the analysis to a new TDMS-file called RotatingShaftReport.tdms. Observe the file now contains a default channel group and two channels, which are named according to the table variable names. Let's read back the data to validate what was written. Here again, we see the imbalance in 900 RPM.

      Let's add a title to the new file. You can read and write metadata using the tdmsreadprop and tdmswriteprop functions, respectively. A new TDMS-file is created with default properties, such as description, title, and author. Let's update the title of the file. You can also add custom properties using the same tdmswriteprop function.

      Let's define the rpm channel units to be rpm. We need to specify the ChannelGroupName and ChannelName when calling tdmswriteprop and read prop. Here again, you can see that a new TDMS-file comes with the default channel properties, such as description and unit_string. Let's update the unit_string. value to appear. Also note that a file channel group or channel must already exist when writing any of its properties.

      In this video, we showed how vibration measurements in a TDMS-file can be inspected and read into MATLAB, how custom transformation functions can be applied to a datastore to analyze a collection of TDMS-files, and how to write the results of the analysis to a new TDMS-file and update its metadata.

      The TDMS read features are available starting in R2022a and the write features are available starting in R2022b. Don't forget to consult MATLAB documentation to learn about other resources and functions for TDMS-files.

      Related Products

      View more related videos