EmbeddedRelated.com
The 2024 Embedded Online Conference

Plotting 16-bit binary data

Stephen Friederichs May 17, 2013 Coded in Matlab

This is Octave/Matlab compatible code that reads 16-bit unsigned values (in little-endian byte order) from a binary file named "accel_data.txt", plots it and saves the plot as a PNG.  It is compatible with the MinGW version of Octave, version 3.6.4.

%Script to load and plot 16-bit accelerometer data
printf "Running acceleration data analysis script\r\n"

clear *	%Clear all variables

ts = (1/1000);	%1KHz sampling rate

%Path to TXT file with accelerometer samples
accel_data_path = "accel_data.txt";

%Open the acceleration data file as read-only, binary mode
file_accel_data = fopen(accel_data_path,"rb");

%Read unit16 samples from TXT file into an array
%count is # of samples, val is array of values
[val,count] = fread(file_accel_data,Inf,"uint16");

fclose(file_accel_data);

%Generate a time vector from t=0 to the end determined by count and sampling time
tmax = (count-1)*ts;
t=0:ts:tmax;

%Open figure 1
figure(1)

%Plot accelerometer samples
plot(t,val','1')

%Make the plot look pretty
title("Raw Sampled Accelerometer Data")
xlabel("Time (s)")
ylabel("Accelerometer Data")

%Save the plot to disk
print("plots/raw_accel_data.png")

The 2024 Embedded Online Conference