How to read and compare two signals from separate ATFX files

The CI Data File Reader API consists of two DLL files that can be integrated with custom software to directly read and extract data from an ATFX file. This article provides an example of a simple Python script that reads and compares two different Sine spectrum signals.

For a simple way to import C# dll files and open an ATFX file in Python & Matlab, please refer to the How to Read CI Data Files in Python and/or How to Read CI Data Files in Matlab article.

For more detailed information on how to implement the following code sections and properties in a class, please refer to the CI Data File Reader manual and the provided C#, Python & Matlab Demo code. The package can be downloaded from our Programming Corner.

The following Python script uses two packages, numpy and matplotlib, that can be installed using the following commands in the operating system command prompt or integrated development environment software terminal.

pip install numpy

pip install matplotlib

Reading & Comparing Two Sine Signals
The following code example can be found in: Read_And_Compare_Two_Signals_Diff_ATFX_Files.py.

Import modules for the script are below.

#---Pythonnet clr import
from cProfile import label
import clr
# Change file path here to whereever the DLL files are
parentPath = "C:\\MyStuff\\DevelopmentalVer\\bin\\AnyCPU\\Debug\\Utility\\CIATFXReader\\"

clr.AddReference(parentPath + "CI.ATFX.Reader.dll")
clr.AddReference(parentPath + "Common.dll")
clr.AddReference('System.Linq')
clr.AddReference('System.Collections')

import numpy as np
import matplotlib.pyplot as plt

#---C# .NET imports & dll imports
from EDM.Recording import *
from EDM.RecordingInterface import *
from ASAM.ODS.NVH import *
from EDM.Utils import *
from Common import *
from Common import _SpectrumScalingType
from Common.Spider import *
from System import *
from System.Diagnostics import *
from System.Reflection import *
from System.Text import *

The open recording method for two Sine signals is below.

# Change file path here to whereever signal or recording files are
recordingPath = "C:\\Users\\KevinCheng\\Documents\\EDM\\SQLServer20210318203822_test\\Sine23\\"
# ATFX file path, change contain the file name and correctly reference it in RecordingManager.Manager.OpenRecording
recordingPathRegular1 = recordingPath + "Run1 Aug 05, 2022 14-07-26\\SIG0000.atfx"
recordingPathRegular2 = recordingPath + "Run2 Aug 05, 2022 14-15-22\\SIG0001.atfx"

#OpenRecording(string, out IRecording)
# openRecordSucceed is required for the OpenRecording as it is the returned boolean
# Make sure to reference the correct file string

openRecordSucceed, recording1 = RecordingManager.Manager.OpenRecording(recordingPathRegular1, None)
openRecordSucceed, recording2 = RecordingManager.Manager.OpenRecording(recordingPathRegular2, None)

After obtaining the IRecording object, the script will receive a list of signals from both IRecording objects and obtain the frame of a specific Sine spectrum signal.

# Get a list of signals
signalList1 = Utility.GetListOfAllSignals(recording1)
signalList2 = Utility.GetListOfAllSignals(recording2)

# Get the frame of a frequency signal depending on where it is in the list
# The Convert.ToInt32 is necessary for the the enum AccelerationUnitType to be read as a int instead of a string

signal1 = signalList1[4]
frame1 = signal1.GetFrame(0, _SpectrumScalingType.EURms, AccelerationUnitEnumString.ArrayString[Convert.ToInt32(AccelerationUnitType.g)])
signal2 = signalList2[4]
frame2 = signal2.GetFrame(0, _SpectrumScalingType.EURms, AccelerationUnitEnumString.ArrayString[Convert.ToInt32(AccelerationUnitType.g)])

The frame in the C# System.Double[] array object must be converted to a usable array that Python can read. This is where the numpy package is used to iterate through the System.Double[] array and convert to a numpy array.

A readable numpy array in Python can be used in the matplotlib package to plot signal graphs.

# Convert System.Double[] to numpy array
# and set any 0 in the frame to nan, useful for certain signals like sine spectrum

frame1X = np.fromiter(frame1[0], float)
frame1X[frame1X == 0] = np.nan
frame1Y = np.fromiter(frame1[1], float)
frame1Y[frame1Y == 0] = np.nan

frame2X = np.fromiter(frame2[0], float)
frame2X[frame2X == 0] = np.nan
frame2Y = np.fromiter(frame2[1], float)
frame2Y[frame2Y == 0] = np.nan

# Plot the signal frames
plt.plot(frame1X,frame1Y,'r', label="SIG0000 "+signal1.Name)
plt.plot(frame2X,frame2Y,
'b', label="SIG0001 "+signal2.Name)
plt.xlabel(signal2.Properties.xQuantity +
" (" + signal2.Properties.xUnit + ")")
plt.ylabel(signal2.Properties.yQuantity +
" (" + signal2.Properties.yUnit + ")")
plt.title(
"Plot of the " + signal1.Name + " and " + signal2.Name)
plt.legend()
plt.show()