Monday 19 June 2023

Simple Python Data Plotter For DSP Log Files

I often log DSP data to .log files, which are typically text files with comma separated columns. In order to plot the data I use a variant of the following Python file, which extracts the data columns and plots the results.

For easier human reading, I typically use ", " for separating the columns, rather than just a single ",". This requires the use of the separator specifier and the use of the 'python' engine because the 'c' engine does not support regex separators.

The nice thing about this is that human readable text can be inserted between the columns of data.

The main thing to be aware of is that all rows in the file must include the same number of columns otherwise read_csv() fails to read the file correctly.

To use this program:

python plot.py log_file.log


# Script to plot the data in the extracted columns of the log file
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import sys

def main(argv):
  print(argv[0])

  df = pd.read_csv(argv[0], header=None, sep=', ', engine='python')
  df = df.head(50000)                                       # Truncate samples for better plotting

  f, ax = plt.subplots()

  inputSamples = df.iloc[:,1]
  plt.plot(inputSamples, label="inputSamples")

  gainedSamples = df.iloc[:,3]
  plt.plot(gainedSamples, label="gainedSamples")

  meanSquaredSum = df.iloc[:,7]
  plt.plot(meanSquaredSum, label="meanSquaredSum")

  ymin, ymax = ax.get_ylim()
  ax.set_yticks(np.round(np.linspace(ymin, ymax, 8), 2))    # Print 8 y-axis ticks

  plt.legend(loc="upper left")
  plt.show()

if __name__ == "__main__":
   main(sys.argv[1:])