[FieldTrip] Creating a New CTF Ds file

Tom Holroyd tomh at kurage.nimh.nih.gov
Wed Jul 29 18:12:05 CEST 2015


On Wed, 29 Jul 2015 15:12:59 +0000
Luke Bloy <luke.bloy at gmail.com> wrote:

> Tom, rewriting the meg4 file would certainly work for what I need to
> do. Would you be willing to share an example (Python would be fine)
> of writing out to the meg4 file.

Here's a snippet. If you are reading it with Matlab, the important
thing you'll need is the array of channel gains. I don't know where
Fieldtrip stashes those, but they are there somewhere.

# Get the channel gains. There are three sets of gains in the
# res4 file, and they are just multiplied together to get one
# gain for each of the M channels.

gain = zeros(M)
for m in range(M):
    sr = res4.sensRes[m][0]
    gain[m] = sr[sr_properGain] * sr[sr_qGain] * sr[sr_ioGain]

# Format to write big endian 32-bit integers. CTF .meg4 files
# are big endian 32-bit ints. Here, newlen is the length of
# a trial in samples.

be_int = Struct(">%di" % newlen)

# Open the file and write the header.

meg4name = os.path.join(newdsname, newset + ".meg4")
f = open(meg4name, "w")
f.write("MEG41CP\x00")

# This loop only writes one trial. If there's more than
# one, the next trial just goes right after the first one.
# Within a trial it's channels by samples.

for m in range(M):
    #print "channel", res4.chanName[m]

    # Get the data to write into trialbuf, in Tesla.
    trialbuf[] = ...

    # convert to int and write
    trialbuf *= gain[m]
    l = map(int, trialbuf + .5) # round to nearest int
    f.write(be_int.pack(*l))    # writes newlen ints

f.close()

If the mailer screws up the formatting, just let me know
and I can send you this whole app, which is dsimDs, which
creates simulated MEG data from dipoles and noise. This is
easy enough to convert to Matlab, anyway, once you know
where the gains are (I'm not an expert in Matlab so I don't
know how to write files ...)

-- 
Dr. Tom
--
"There are not more than five musical notes,
yet the combinations of these five give rise to
more melodies than can ever be heard." -- Sun Tzu



More information about the fieldtrip mailing list