Loading HF2LI Lock-in Measurements with Matlab

The combination of Zurich Instruments devices and Matlab is a powerful tool set to get your application measured. In a first step I show a method to load the demodulator data of the Zurich Instruments HF2LI lock-in amplifier into the Matlab workspace. This approach uses the graphical user interface of the HF2LI to save the data in binary format. In a later blog entry a will show another method which allows for direct control of the HF2LI from Matlab. This enables scripting and recording of large measurement data sets.

Using the HF2LI Graphical User Interface ziControl

Loading the data from a file is a simple and fast approach. It is well suited if you would like to share the data between different applications.

It is best to save the data in binary format. Therefore, make sure that the check box Binary Save is enabled. Then press the Save button to start the measurement. This should save binary files with file extension *.ziBin into the specified directory.

Saving measurement data with the ziControl

Saving measurement data with the ziControl

Loading the Binary Data in Matlab

To load the measurement files we use a helper function which loads the file content into a structure. Download the file ziLoad.m.

function data = ziLoad(file)
% Loads the measurement data from a ziBin file
% E.g data = ziLoad('Freq1.ziBin');

fid = fopen(file, 'r', 'ieee-be');
if fid == -1
error(['File ' file ' not found.']);
raw = fread(fid, inf, '*double');
if mod(length(raw), 7) ~= 0
error('Illegal file size of ziBin file detected.');
data.timestamp = raw(1:7:end);
data.x = raw(2:7:end);
data.y = raw(3:7:end);
data.frequency = raw(4:7:end);
data.bits = uint32(raw(5:7:end));
data.auxin0 = raw(6:7:end);
data.auxin1 = raw(7:7:end);

To load the data in Matlab you just call the function ziLoad in Matlab.

% Load the measurement data of demodulator 1 into a structure
data = ziLoad('mydata/Freq1.ziBin');

Now the data is found on the workspace. It is a structure array with following fields.

data =
timestamp: [2855x1 double]
x: [2855x1 double]
y: [2855x1 double]
frequency: [2855x1 double]
bits: [2855x1 uint32]
auxin0: [2855x1 double]
auxin1: [2855x1 double]

Now we plot R over recording time. The plot2svg function can be downloaded here.

s = plot(data.timestamp - data.timestamp(1), ...
sqrt(data.x.^2 + data.y.^2) * 1e9, 'black');
% Format the plot
set(s, 'LineWidth', 2);
xlabel('Time [s]');
ylabel('Voltage R [nV]')
box on
grid on
% Save the plot as Scalable Vector Graphics (SVG)

R recorded with Zurich Instruments HF2LI

R recorded with Zurich Instruments HF2LI