Roundtrip from MatLab - FieldTrip to Syncopy and Back#

Data created with Syncopy can be loaded into MATLAB using the matlab-syncopy interface. It’s still in early development and supports only a subset of data classes. Also, the MATLAB interface does not support loading data that do not fit into local memory.

For this illustrative example we start by generating synthetic data in FieldTrip:

cfg = [];
cfg.method  = 'superimposed';
cfg.fsample = 1000;
cfg.numtrl  = 13;
cfg.trllen  = 7;
cfg.s1.freq = 50;
cfg.s1.ampl = 1;
cfg.s1.phase = 0;
cfg.noise.ampl = 0;
data = ft_freqsimulation(cfg);
data.dimord = '{rpt}_label_time';

Next, download the latest release of Syncopy’s MATLAB interface and add the folder containing the +spy directory to your MATLAB path.


Now, we save the synthetic dataset as Syncopy AnalogData dataset in the respective user home

cfg = []; cfg.filename = '~/syn_data.analog';
spy.ft_save_spy(cfg, data)

The previous call generated two files: an HDF5 data-file ~/syn_data.analog and the accompanying JSON meta-data ~/ (please refer to Reading and Writing Data for more information about Syncopy’s file format).

We start an (i)Python session, import Syncopy and use load() to read the data from disk:

import syncopy as spy
data = spy.load('~/syn_data.analog')

Now, let’s compute a power-spectrum using Syncopy’s parallel computing engine:

cfg = spy.get_defaults(spy.freqanalysis)
cfg.method = 'mtmfft'
cfg.output = 'pow'
cfg.parallel = True
spec = spy.freqanalysis(cfg, data)


Using SLURM on HPC clusters for datasets this small usually does not yield any performance gain due to the comparatively large overhead of starting a SLURM worker pool compared to the total computation time.

We save the resulting SpectralData object alongside the corresponding AnalogData source:, filename='~/syn_data')

Note that automatically appends the appropriate filename extension (.spectral in this case).

Back in MATLAB, we can import the computed spectrum using:

spec = spy.ft_load_spy('~/syn_data.spectral')