├── .github ├── dependabot.yml └── workflows │ └── moxunit.yml ├── .gitignore ├── LICENSE.txt ├── README.rst └── matlab ├── fiff_copy_tree.m ├── fiff_define_constants.m ├── fiff_dir_tree_find.m ├── fiff_end_block.m ├── fiff_end_file.m ├── fiff_find_evoked.m ├── fiff_finish_writing_raw.m ├── fiff_invert_transform.m ├── fiff_list_dir_tree.m ├── fiff_make_ch_rename.m ├── fiff_make_dir_tree.m ├── fiff_open.m ├── fiff_pick_channels.m ├── fiff_pick_channels_evoked.m ├── fiff_pick_info.m ├── fiff_pick_types.m ├── fiff_pick_types_evoked.m ├── fiff_read_bad_channels.m ├── fiff_read_coord_trans.m ├── fiff_read_ctf_comp.m ├── fiff_read_epochs.m ├── fiff_read_events.m ├── fiff_read_evoked.m ├── fiff_read_evoked_all.m ├── fiff_read_extended_ch_info.m ├── fiff_read_hpi_result.m ├── fiff_read_meas_info.m ├── fiff_read_mri.m ├── fiff_read_named_matrix.m ├── fiff_read_proj.m ├── fiff_read_raw_segment.m ├── fiff_read_raw_segment_times.m ├── fiff_read_tag.m ├── fiff_read_tag_info.m ├── fiff_rename_comp.m ├── fiff_rename_list.m ├── fiff_reset_ch_pos.m ├── fiff_setup_read_raw.m ├── fiff_split_name_list.m ├── fiff_start_block.m ├── fiff_start_file.m ├── fiff_start_writing_raw.m ├── fiff_transform_eeg_chs.m ├── fiff_transform_meg_chs.m ├── fiff_write_ch_info.m ├── fiff_write_ch_infos.m ├── fiff_write_complex.m ├── fiff_write_complex_matrix.m ├── fiff_write_coord_trans.m ├── fiff_write_ctf_comp.m ├── fiff_write_dau16.m ├── fiff_write_dig_file.m ├── fiff_write_dig_point.m ├── fiff_write_double.m ├── fiff_write_double_complex.m ├── fiff_write_double_complex_matrix.m ├── fiff_write_double_matrix.m ├── fiff_write_epochs.m ├── fiff_write_events.m ├── fiff_write_evoked.m ├── fiff_write_float.m ├── fiff_write_float_matrix.m ├── fiff_write_float_sparse_ccs.m ├── fiff_write_float_sparse_rcs.m ├── fiff_write_id.m ├── fiff_write_int.m ├── fiff_write_int_matrix.m ├── fiff_write_name_list.m ├── fiff_write_named_matrix.m ├── fiff_write_proj.m ├── fiff_write_raw_buffer.m ├── fiff_write_raw_segment.m ├── fiff_write_raw_segment_times.m ├── fiff_write_short.m ├── fiff_write_string.m ├── mne_add_coil_defs.m ├── mne_babyMEG_dig_trig.m ├── mne_block_diag.m ├── mne_combine_xyz.m ├── mne_compensate_to.m ├── mne_ex_average_epochs.m ├── mne_ex_cancel_noise.m ├── mne_ex_compute_inverse.m ├── mne_ex_data_sets.m ├── mne_ex_evoked_grad_amp.m ├── mne_ex_read_epochs.m ├── mne_ex_read_evoked.m ├── mne_ex_read_raw.m ├── mne_ex_read_write_raw.m ├── mne_ex_rt.m ├── mne_file_name.m ├── mne_find_channel.m ├── mne_find_events.m ├── mne_find_source_space_hemi.m ├── mne_fread3.m ├── mne_fwrite3.m ├── mne_get_current_comp.m ├── mne_label_time_courses.m ├── mne_load_coil_def.m ├── mne_make_combined_event_file.m ├── mne_make_compensator.m ├── mne_make_projector.m ├── mne_make_projector_info.m ├── mne_mesh_edges.m ├── mne_morph_data.m ├── mne_omit_first_line.m ├── mne_patch_info.m ├── mne_pick_channels_cov.m ├── mne_pick_channels_forward.m ├── mne_prepare_inverse_operator.m ├── mne_read_bem_surfaces.m ├── mne_read_cov.m ├── mne_read_curvature.m ├── mne_read_epoch.m ├── mne_read_events.m ├── mne_read_forward_solution.m ├── mne_read_inverse_operator.m ├── mne_read_label_file.m ├── mne_read_morph_map.m ├── mne_read_noise_cov.m ├── mne_read_source_spaces.m ├── mne_read_stc_file.m ├── mne_read_stc_file1.m ├── mne_read_surface.m ├── mne_read_surfaces.m ├── mne_read_w_file.m ├── mne_read_w_file1.m ├── mne_reduce_surface.m ├── mne_rt_buffer_reader.class ├── mne_rt_buffer_reader.java ├── mne_rt_client.m ├── mne_rt_cmd_client.m ├── mne_rt_data_client.m ├── mne_rt_define_commands.m ├── mne_set_current_comp.m ├── mne_source_spectral_analysis.m ├── mne_transform_coordinates.m ├── mne_transform_source_space_to.m ├── mne_transpose_named_matrix.m ├── mne_write_cov.m ├── mne_write_cov_file.m ├── mne_write_events.m ├── mne_write_inverse_sol_stc.m ├── mne_write_inverse_sol_w.m ├── mne_write_label_file.m ├── mne_write_stc_file.m ├── mne_write_stc_file1.m ├── mne_write_surface.m ├── mne_write_w_file.m ├── mne_write_w_file1.m └── test ├── data ├── process_raw.sh ├── test-ave.fif ├── test-cov.fif ├── test-eve.fif ├── test.ave ├── test.cov ├── test_fiftrees_ref.mat ├── test_long_raw.fif ├── test_raw-eve.fif ├── test_raw.fif └── test_setup_raw_ref.mat ├── test_ex_compute_inverse.m ├── test_ex_read_epochs.m ├── test_ex_read_raw.m ├── test_fiff_define_constants.m ├── test_fiff_finish_writing_raw.m ├── test_fiff_make_dir_tree.m ├── test_fiff_open.m ├── test_fiff_read_meas_info.m ├── test_fiff_read_raw_segment.m ├── test_fiff_read_tag.m ├── test_fiff_read_tag_info.m ├── test_fiff_readwrite.m ├── test_fiff_setup_read_raw.m ├── test_fiff_start_writing_raw.m ├── test_fiff_write_int.m ├── test_fiff_write_raw_buffer.m ├── test_mne_source_spectral_analysis.m └── test_read_forward.m /.github/dependabot.yml: -------------------------------------------------------------------------------- 1 | version: 2 2 | updates: 3 | - package-ecosystem: "github-actions" 4 | directory: "/" 5 | schedule: 6 | interval: "monthly" 7 | groups: 8 | actions: 9 | patterns: 10 | - "*" 11 | -------------------------------------------------------------------------------- /.github/workflows/moxunit.yml: -------------------------------------------------------------------------------- 1 | name: 'MOxUnit' 2 | on: 3 | push: 4 | branches: 5 | - '*' 6 | pull_request: 7 | branches: 8 | - '*' 9 | workflow_dispatch: 10 | 11 | jobs: 12 | test: 13 | runs-on: ubuntu-latest 14 | steps: 15 | - uses: actions/checkout@v4 16 | - uses: joergbrech/moxunit-action@master 17 | with: 18 | tests: matlab/test 19 | src: matlab 20 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | foo.fif 2 | -------------------------------------------------------------------------------- /LICENSE.txt: -------------------------------------------------------------------------------- 1 | Copyright (c) 2011, Matti Hamalainen and Alexandre Gramfort 2 | All rights reserved. 3 | 4 | Redistribution and use in source and binary forms, with or without 5 | modification, are permitted provided that the following conditions are met: 6 | * Redistributions of source code must retain the above copyright 7 | notice, this list of conditions and the following disclaimer. 8 | * Redistributions in binary form must reproduce the above copyright 9 | notice, this list of conditions and the following disclaimer in the 10 | documentation and/or other materials provided with the distribution. 11 | * Neither the name of the Massachusetts General Hospital nor the 12 | names of its contributors may be used to endorse or promote products 13 | derived from this software without specific prior written permission. 14 | 15 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 16 | ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 17 | WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 18 | DISCLAIMED. IN NO EVENT SHALL MASSACHUSETTS GENERAL HOSPITAL BE LIABLE FOR ANY 19 | DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 20 | (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 21 | LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 22 | ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 23 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 24 | SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -------------------------------------------------------------------------------- /matlab/fiff_copy_tree.m: -------------------------------------------------------------------------------- 1 | function fiff_copy_tree(fidin, in_id, nodes, fidout) 2 | % 3 | % fiff_copy_tree(fidin, in_id, nodes, fidout) 4 | % 5 | % Copies directory subtrees from fidin to fidout 6 | % 7 | 8 | % 9 | % Author : Matti Hamalainen, MGH Martinos Center 10 | % License : BSD 3-clause 11 | % 12 | % Revision 1.2 2009/04/01 21:25:50 msh 13 | % Fixed problems with fiff nodes with no tags in them 14 | % 15 | % Revision 1.1 2009/03/30 11:37:37 msh 16 | % Added copying of measurement info blocks from the original like in mne_browse_raw 17 | % 18 | % 19 | 20 | me = 'MNE:fiff_copy_tree'; 21 | 22 | global FIFF; 23 | if isempty(FIFF) 24 | FIFF = fiff_define_constants(); 25 | end 26 | 27 | if nargin ~= 4 28 | error(me, 'Incorrect number of arguments'); 29 | end 30 | 31 | if length(nodes) <= 0 32 | return 33 | end 34 | 35 | FIFFV_NEXT_SEQ = 0; 36 | 37 | for k = 1:length(nodes) 38 | fiff_start_block(fidout, nodes(k).block); 39 | if ~isempty(nodes(k).id) 40 | if ~isempty(in_id) 41 | fiff_write_id(fidout, FIFF.FIFF_PARENT_FILE_ID, in_id); 42 | end 43 | fiff_write_id(fidout, FIFF.FIFF_BLOCK_ID); 44 | fiff_write_id(fidout, FIFF.FIFF_PARENT_BLOCK_ID, nodes(k).id); 45 | end 46 | for p = 1:nodes(k).nent 47 | % 48 | % Do not copy these tags 49 | % 50 | if nodes(k).dir(p).kind == FIFF.FIFF_BLOCK_ID || nodes(k).dir(p).kind == FIFF.FIFF_PARENT_BLOCK_ID || nodes(k).dir(p).kind == FIFF.FIFF_PARENT_FILE_ID 51 | continue; 52 | end 53 | % 54 | % Read and write tags, pass data through transparently 55 | % 56 | if fseek(fidin, nodes(k).dir(p).pos, 'bof') == -1 57 | error(me, 'Could not seek to the tag'); 58 | end 59 | tag.kind = fread(fidin, 1, 'int32'); 60 | tag.type = fread(fidin, 1, 'uint32'); 61 | tag.size = fread(fidin, 1, 'int32'); 62 | tag.next = fread(fidin, 1, 'int32'); 63 | tag.data = fread(fidin, tag.size, 'uchar'); 64 | count = fwrite(fidout, tag.kind, 'int32'); 65 | if count ~= 1 66 | error(me, 'write failed.'); 67 | end 68 | count = fwrite(fidout, tag.type, 'int32'); 69 | if count ~= 1 70 | error(me, 'write failed.'); 71 | end 72 | count = fwrite(fidout, tag.size, 'int32'); 73 | if count ~= 1 74 | error(me, 'write failed'); 75 | end 76 | count = fwrite(fidout, int32(FIFFV_NEXT_SEQ), 'int32'); 77 | if count ~= 1 78 | error(me, 'write failed'); 79 | end 80 | count = fwrite(fidout, tag.data, 'uchar'); 81 | if count ~= tag.size 82 | error(me, 'write failed'); 83 | end 84 | end 85 | for p = 1:nodes(k).nchild 86 | fiff_copy_tree(fidin, in_id, nodes(k).children(p), fidout); 87 | end 88 | fiff_end_block(fidout, nodes(k).block); 89 | end 90 | 91 | return 92 | 93 | 94 | -------------------------------------------------------------------------------- /matlab/fiff_dir_tree_find.m: -------------------------------------------------------------------------------- 1 | function [nodes] = fiff_dir_tree_find(tree, kind) 2 | % 3 | % [nodes] = fiff_dir_tree_find(tree,kind) 4 | % 5 | % Find nodes of the given kind from a directory tree structure 6 | % 7 | 8 | % Author : Matti Hamalainen, MGH Martinos Center 9 | % License : BSD 3-clause 10 | % 11 | % Revision 1.4 2006/11/30 05:43:29 msh 12 | % Fixed help text in fiff_dir_tree_find 13 | % Fixed check for the existence of parent MRI block in mne_read_forward_solution 14 | % 15 | % Revision 1.3 2006/04/23 15:29:40 msh 16 | % Added MGH to the copyright 17 | % 18 | % Revision 1.2 2006/04/12 10:29:02 msh 19 | % Made evoked data writing compatible with the structures returned in reading. 20 | % 21 | % Revision 1.1 2006/04/10 23:26:54 msh 22 | % Added fiff reading routines 23 | % 24 | 25 | me = 'MNE:fiff_dir_tree_find'; 26 | 27 | if nargin ~= 2 28 | error(me, 'Incorrect number of arguments'); 29 | end 30 | 31 | nodes = struct('block', {}, 'id', {}, 'parent_id', {}, 'nent', {}, 'nchild', {}, 'dir', {}, 'children', {}); 32 | % 33 | % Am I desirable myself? 34 | % 35 | if tree.block == kind 36 | nodes = [ nodes tree ]; 37 | end 38 | % 39 | % Search the subtrees 40 | % 41 | for k = 1:tree.nchild 42 | nodes = [ nodes fiff_dir_tree_find(tree.children(k), kind) ]; 43 | end 44 | 45 | return 46 | 47 | -------------------------------------------------------------------------------- /matlab/fiff_end_block.m: -------------------------------------------------------------------------------- 1 | function fiff_end_block(fid, kind) 2 | % 3 | % fiff_end_block(fid, kind) 4 | % 5 | % Writes a FIFF_BLOCK_END tag 6 | % 7 | % fid An open fif file descriptor 8 | % kind The block kind to end 9 | % 10 | 11 | % 12 | % Author : Matti Hamalainen, MGH Martinos Center 13 | % License : BSD 3-clause 14 | % 15 | % Revision 1.3 2006/04/23 15:29:40 msh 16 | % Added MGH to the copyright 17 | % 18 | % Revision 1.2 2006/04/10 23:26:54 msh 19 | % Added fiff reading routines 20 | % 21 | % Revision 1.1 2005/12/05 16:01:04 msh 22 | % Added an initial set of fiff writing routines. 23 | % 24 | 25 | me = 'MNE:fiff_end_block'; 26 | 27 | if nargin ~= 2 28 | error(me, 'Incorrect number of arguments'); 29 | end 30 | 31 | FIFF_BLOCK_END = 105; 32 | fiff_write_int(fid, FIFF_BLOCK_END, kind); 33 | 34 | return 35 | -------------------------------------------------------------------------------- /matlab/fiff_end_file.m: -------------------------------------------------------------------------------- 1 | function fiff_end_file(fid) 2 | % 3 | % fiff_end_file(fid) 4 | % 5 | % Writes the closing tags to a fif file and closes the file 6 | % 7 | % fid An open fif file descriptor 8 | % 9 | 10 | % 11 | % Author : Matti Hamalainen, MGH Martinos Center 12 | % License : BSD 3-clause 13 | % 14 | % Revision 1.3 2006/04/23 15:29:40 msh 15 | % Added MGH to the copyright 16 | % 17 | % Revision 1.2 2006/04/10 23:26:54 msh 18 | % Added fiff reading routines 19 | % 20 | % Revision 1.1 2005/12/05 16:01:04 msh 21 | % Added an initial set of fiff writing routines. 22 | % 23 | 24 | global FIFF; 25 | if isempty(FIFF) 26 | FIFF = fiff_define_constants(); 27 | end 28 | 29 | me = 'MNE:fiff_end_file'; 30 | 31 | if nargin ~= 1 32 | error(me, 'An open file id required as an argument'); 33 | end 34 | 35 | datasize = 0; 36 | count = fwrite(fid, int32(FIFF.FIFF_NOP), 'int32'); 37 | if count ~= 1 38 | error(me, 'write failed'); 39 | end 40 | count = fwrite(fid, int32(FIFF.FIFFT_VOID), 'int32'); 41 | if count ~= 1 42 | error(me, 'write failed'); 43 | end 44 | count = fwrite(fid, int32(datasize), 'int32'); 45 | if count ~= 1 46 | error(me, 'write failed'); 47 | end 48 | count = fwrite(fid, int32(FIFF.FIFFV_NEXT_NONE), 'int32'); 49 | if count ~= 1 50 | error(me, 'write failed'); 51 | end 52 | fclose(fid); 53 | 54 | return 55 | 56 | -------------------------------------------------------------------------------- /matlab/fiff_find_evoked.m: -------------------------------------------------------------------------------- 1 | function [data_sets] = fiff_find_evoked(fname) 2 | % 3 | % [data_sets] = fiff_find_evoked(fname) 4 | % 5 | % Find all evoked data sets in a fif file and create a list of descriptors 6 | % 7 | 8 | 9 | % 10 | % Author : Matti Hamalainen, MGH Martinos Center 11 | % License : BSD 3-clause 12 | % 13 | 14 | global FIFF; 15 | if isempty(FIFF) 16 | FIFF = fiff_define_constants(); 17 | end 18 | 19 | me = 'MNE:fiff_find_evoked'; 20 | 21 | % 22 | % Open the file 23 | % 24 | [ fid, tree ] = fiff_open(fname); 25 | data_sets = struct('comment',{},'aspect_kind',{},'aspect_name',{}); 26 | % 27 | % Find all evoked data sets 28 | % 29 | evoked = fiff_dir_tree_find(tree, FIFF.FIFFB_EVOKED); 30 | if length(evoked) == 0 31 | fclose(fid); 32 | return 33 | end 34 | % 35 | % Identify the aspects 36 | % 37 | naspect = 0; 38 | for k = 1:length(evoked) 39 | sets(k).aspects = fiff_dir_tree_find(evoked(k), FIFF.FIFFB_ASPECT); 40 | sets(k).naspect = length(sets(k).aspects); 41 | naspect = naspect + sets(k).naspect; 42 | end 43 | % 44 | % Collect the desired information 45 | % 46 | count = 1; 47 | for k = 1:length(evoked) 48 | evoked_comment = find_tag(evoked(k), FIFF.FIFF_COMMENT); 49 | for a = 1:sets(k).naspect 50 | aspect_comment = find_tag(sets(k).aspects(a), FIFF.FIFF_COMMENT); 51 | aspect_kind = find_tag(sets(k).aspects(a), FIFF.FIFF_ASPECT_KIND); 52 | if ~isempty(aspect_comment) 53 | data_sets(count).comment = aspect_comment.data; 54 | elseif ~isempty(evoked_comment) 55 | data_sets(count).comment = evoked_comment.data; 56 | else 57 | data_sets(count).comment = 'No comment'; 58 | end 59 | if ~isempty(aspect_kind) 60 | data_sets(count).aspect_kind = aspect_kind.data; 61 | else 62 | data_sets(count).aspect_kind = -1; 63 | end 64 | switch data_sets(count).aspect_kind 65 | case FIFF.FIFFV_ASPECT_AVERAGE 66 | data_sets(count).aspect_name = 'Average'; 67 | case FIFF.FIFFV_ASPECT_STD_ERR 68 | data_sets(count).aspect_name = 'Standard error'; 69 | case FIFF.FIFFV_ASPECT_SINGLE 70 | data_sets(count).aspect_name = 'Single'; 71 | case FIFF.FIFFV_ASPECT_SUBAVERAGE 72 | data_sets(count).aspect_name = 'Subaverage'; 73 | case FIFF.FIFFV_ASPECT_ALTAVERAGE 74 | data_sets(count).aspect_name = 'Alt. subaverage'; 75 | case FIFF.FIFFV_ASPECT_SAMPLE 76 | data_sets(count).aspect_name = 'Sample'; 77 | case FIFF.FIFFV_ASPECT_POWER_DENSITY 78 | data_sets(count).aspect_name = 'Power density spectrum'; 79 | case FIFF.FIFFV_ASPECT_DIPOLE_WAVE 80 | data_sets(count).aspect_name = 'Dipole source waveform'; 81 | otherwise 82 | data_sets(count).aspect_name = 'Unknown'; 83 | end 84 | count = count + 1; 85 | end 86 | end 87 | 88 | fclose(fid); 89 | 90 | return 91 | 92 | function [tag] = find_tag(node, findkind) 93 | 94 | for p = 1:node.nent 95 | kind = node.dir(p).kind; 96 | pos = node.dir(p).pos; 97 | if kind == findkind 98 | tag = fiff_read_tag(fid, pos); 99 | return 100 | end 101 | end 102 | tag = []; 103 | return 104 | end 105 | 106 | end 107 | -------------------------------------------------------------------------------- /matlab/fiff_finish_writing_raw.m: -------------------------------------------------------------------------------- 1 | function fiff_finish_writing_raw(fid) 2 | % 3 | % function fiff_finish_writing_raw(fid) 4 | % 5 | % fid of an open raw data file 6 | % 7 | % 8 | 9 | % 10 | % Author : Matti Hamalainen, MGH Martinos Center 11 | % License : BSD 3-clause 12 | % 13 | % 14 | % Revision 1.2 2008/05/08 13:35:09 msh 15 | % Ending FIFFB_MEAS was missing 16 | % 17 | % Revision 1.1 2007/11/07 16:05:05 msh 18 | % New routines for writing raw files 19 | % 20 | 21 | me = 'MNE:fiff_finish_writing_raw'; 22 | if nargin ~= 1 23 | error(me, 'File id required as an argument'); 24 | end 25 | 26 | global FIFF; 27 | if isempty(FIFF) 28 | FIFF = fiff_define_constants(); 29 | end 30 | fiff_end_block(fid, FIFF.FIFFB_RAW_DATA); 31 | fiff_end_block(fid, FIFF.FIFFB_MEAS); 32 | fiff_end_file(fid); 33 | 34 | return 35 | -------------------------------------------------------------------------------- /matlab/fiff_invert_transform.m: -------------------------------------------------------------------------------- 1 | function [itrans] = fiff_invert_transform(trans) 2 | % 3 | % [itrans] = fiff_invert_transform(trans) 4 | % 5 | % Invert a coordinate transformation 6 | % 7 | 8 | 9 | % 10 | % Author : Matti Hamalainen, MGH Martinos Center 11 | % License : BSD 3-clause 12 | % 13 | % Revision 1.2 2006/04/23 15:29:40 msh 14 | % Added MGH to the copyright 15 | % 16 | % Revision 1.1 2006/04/14 00:45:42 msh 17 | % Added channel picking and fiff_invert_transform 18 | % 19 | % 20 | 21 | me = 'MNE:fiff_invert_transform'; 22 | 23 | if nargin ~= 1 24 | error(me, 'Incorrect number of arguments'); 25 | end 26 | 27 | itrans = trans; 28 | help = itrans.from; 29 | itrans.from = itrans.to; 30 | itrans.to = help; 31 | itrans.trans = inv(itrans.trans); 32 | 33 | return 34 | 35 | 36 | -------------------------------------------------------------------------------- /matlab/fiff_list_dir_tree.m: -------------------------------------------------------------------------------- 1 | function fiff_list_dir_tree(out, tree, indent) 2 | 3 | % 4 | % fiff_list_dir_tree(fid, tree) 5 | % 6 | % List the fiff directory tree structure 7 | % 8 | 9 | % 10 | % Author : Matti Hamalainen, MGH Martinos Center 11 | % License : BSD 3-clause 12 | % 13 | % Revision 1.3 2009/04/01 21:25:50 msh 14 | % Fixed problems with fiff nodes with no tags in them 15 | % 16 | % Revision 1.2 2006/04/23 15:29:40 msh 17 | % Added MGH to the copyright 18 | % 19 | % Revision 1.1 2006/04/10 23:26:54 msh 20 | % Added fiff reading routines 21 | % 22 | 23 | global FIFF; 24 | if isempty(FIFF) 25 | FIFF = fiff_define_constants(); 26 | end 27 | 28 | me = 'MNE:fiff_list_dir_tree'; 29 | 30 | if nargin == 2 31 | indent = 0; 32 | end 33 | 34 | for k = 1:indent 35 | fprintf(out, '\t'); 36 | end 37 | if tree.block ~= 0 38 | fprintf(out, '{ %d\n', tree.block); 39 | end 40 | 41 | count = 1; 42 | for k = 1:tree.nent 43 | if k == 1 44 | print = true; 45 | for p = 1:indent 46 | fprintf(out, '\t'); 47 | end 48 | fprintf(out, 'tag : %d', tree.dir(k).kind); 49 | else 50 | if tree.dir(k).kind == tree.dir(k - 1).kind 51 | count = count + 1; 52 | else 53 | if count > 1 54 | fprintf(out, ' [%d]\n', count); 55 | else 56 | fprintf(out, '\n'); 57 | end 58 | for p = 1:indent 59 | fprintf(out, '\t'); 60 | end 61 | fprintf(out, 'tag : %d', tree.dir(k).kind); 62 | count = 1; 63 | end 64 | end 65 | end 66 | if count > 1 67 | fprintf(out, ' [%d]\n', count); 68 | else 69 | fprintf(out, '\n'); 70 | end 71 | 72 | for k = 1:tree.nchild 73 | fiff_list_dir_tree(out, tree.children(k), indent + 1); 74 | end 75 | 76 | for k = 1:indent 77 | fprintf(out, '\t'); 78 | end 79 | 80 | if tree.block ~= 0 81 | fprintf(out, '} %d\n', tree.block); 82 | end 83 | 84 | return 85 | 86 | -------------------------------------------------------------------------------- /matlab/fiff_make_ch_rename.m: -------------------------------------------------------------------------------- 1 | function ch_rename = fiff_make_ch_rename(chs) 2 | 3 | me='MNE:fiff_make_ch_rename'; 4 | if nargin ~= 1 5 | error(me,'Incorrect number of arguments'); 6 | end 7 | 8 | ch_rename = {}; 9 | need_rename = false; 10 | counts = {}; 11 | for k = 1:length(chs) 12 | if length(chs(k).ch_name) > 15 13 | need_rename = true; 14 | end 15 | end 16 | if ~need_rename 17 | return; 18 | end 19 | 20 | % count how many of each 15-char one we have 21 | for k = 1:length(chs) 22 | short_name = chs(k).ch_name; 23 | short_name = short_name(1:min(length(short_name),15)); 24 | if length(counts) == 0 || ~any(strcmp(counts(:, 1), short_name)) 25 | counts = [counts; {short_name, 0, 0}]; 26 | end 27 | idx = find(strcmp(counts(:, 1), short_name)); 28 | counts{idx(1), 2} = counts{idx(1), 2} + 1; 29 | end 30 | % now do the assignments, taking into account duplicates and adding -1, -2, etc 31 | for k = 1:length(chs) 32 | short_name = chs(k).ch_name; 33 | if length(short_name) > 15 34 | short_name = short_name(1:15); 35 | idx = find(strcmp(counts(:, 1), short_name)); 36 | n = counts{idx(1), 2}; 37 | if n > 1 38 | fw = ceil(log10(n)); 39 | n = counts{idx(1), 3} + 1; 40 | counts{idx(1), 3} = n; 41 | short_name = sprintf(sprintf('%%s-%%0%dd', fw), short_name(1:15-fw-1), n); 42 | end 43 | ch_rename = [ch_rename; {chs(k).ch_name, short_name}]; 44 | end 45 | end 46 | 47 | return; 48 | end 49 | -------------------------------------------------------------------------------- /matlab/fiff_open.m: -------------------------------------------------------------------------------- 1 | function [fid, tree, dir] = fiff_open(fname) 2 | % 3 | % [fid, tree, dir] = fiff_open(fname) 4 | % 5 | % Open a fif file and provide the directory of tags 6 | % 7 | % fid the opened file id 8 | % tree tag directory organized into a tree 9 | % dir the sequential tag directory 10 | % 11 | 12 | % 13 | % Author : Matti Hamalainen, MGH Martinos Center 14 | % License : BSD 3-clause 15 | % 16 | % Revision 1.7 2009/03/30 11:37:37 msh 17 | % Added copying of measurement info blocks from the original like in mne_browse_raw 18 | % 19 | % Revision 1.6 2008/11/16 21:31:23 msh 20 | % Added mne_transform_coordinates and new coordinate frame definitions 21 | % 22 | % Revision 1.5 2006/05/03 19:03:19 msh 23 | % Eliminated the use of cast function for Matlab 6.5 compatibility 24 | % 25 | % Revision 1.4 2006/04/23 15:29:40 msh 26 | % Added MGH to the copyright 27 | % 28 | % Revision 1.3 2006/04/18 20:44:46 msh 29 | % Added reading of forward solution. 30 | % Use length instead of size when appropriate 31 | % 32 | % Revision 1.2 2006/04/17 15:01:34 msh 33 | % More small improvements. 34 | % 35 | % Revision 1.1 2006/04/10 23:26:54 msh 36 | % Added fiff reading routines 37 | % 38 | 39 | persistent FIFF previous_tree previous_dir previous_fname 40 | 41 | if isequal(previous_fname, fname) 42 | % the previous file name is the same as the current one, just open the file 43 | fid = fopen(fname,'rb','ieee-be'); 44 | tree = previous_tree; 45 | dir = previous_dir; 46 | return; 47 | end 48 | 49 | if isempty(FIFF) 50 | FIFF = fiff_define_constants(); 51 | end 52 | 53 | me = 'MNE:fiff_open'; 54 | verbose = false; 55 | fid = fopen(fname,'rb','ieee-be'); 56 | 57 | if fid < 0 58 | error(me,'Cannot open file %s', fname); 59 | end 60 | % 61 | % Check that this looks like a fif file 62 | % 63 | tag = fiff_read_tag_info(fid); 64 | if tag.kind ~= FIFF.FIFF_FILE_ID 65 | error(me,'file does not start with a file id tag'); 66 | end 67 | if tag.type ~= FIFF.FIFFT_ID_STRUCT 68 | error(me,'file does not start with a file id tag'); 69 | end 70 | if tag.size ~= 20 71 | error(me,'file does not start with a file id tag'); 72 | end 73 | tag = fiff_read_tag(fid); 74 | if tag.kind ~= FIFF.FIFF_DIR_POINTER 75 | error(me,'file does have a directory pointer'); 76 | end 77 | if nargout == 1 78 | fseek(fid,0,'bof'); 79 | return; 80 | end 81 | % 82 | % Read or create the directory tree 83 | % 84 | if verbose 85 | fprintf(1,'\tCreating tag directory for %s...',fname); 86 | end 87 | dirpos = double(tag.data); 88 | if dirpos > 0 89 | tag = fiff_read_tag(fid,dirpos); 90 | dir = tag.data; 91 | else 92 | k = 0; 93 | fseek(fid,0,'bof'); 94 | dir = struct('kind',{},'type',{},'size',{},'pos',{}); 95 | while tag.next >= 0 96 | pos = ftell(fid); 97 | tag = fiff_read_tag_info(fid); 98 | k = k + 1; 99 | dir(k).kind = tag.kind; 100 | dir(k).type = tag.type; 101 | dir(k).size = tag.size; 102 | dir(k).pos = pos; 103 | end 104 | end 105 | % 106 | % Create the directory tree structure 107 | % 108 | tree = fiff_make_dir_tree(fid,dir); 109 | if verbose 110 | fprintf(1,'[done]\n'); 111 | end 112 | % 113 | % Back to the beginning 114 | % 115 | fseek(fid,0,'bof'); 116 | 117 | % keep track of some things 118 | previous_tree = tree; 119 | previous_dir = dir; 120 | previous_fname = fname; 121 | 122 | return 123 | -------------------------------------------------------------------------------- /matlab/fiff_pick_channels.m: -------------------------------------------------------------------------------- 1 | function [sel] = fiff_pick_channels(ch_names,include,exclude) 2 | % 3 | % [sel] = fiff_pick_channels(ch_names,include,exclude) 4 | % 5 | % Make a selector to pick desired channels from data 6 | % 7 | % ch_names - The channel name list to consult 8 | % include - Channels to include (if empty, include all available) 9 | % exclude - Channels to exclude (if empty, do not exclude any) 10 | % 11 | % 12 | 13 | % 14 | % Author : Matti Hamalainen, MGH Martinos Center 15 | % License : BSD 3-clause 16 | % 17 | % 18 | % Revision 1.5 2008/04/06 16:35:35 msh 19 | % Added reasonable handling of duplicate channel names. 20 | % 21 | % Revision 1.4 2006/06/29 22:12:28 msh 22 | % Fixed errors in channel picking 23 | % 24 | % Revision 1.3 2006/04/27 22:38:37 msh 25 | % Splitting an empty list now results in an empty output. 26 | % Added fiff_write_double and fiff_write_short 27 | % Write an array of floats, ints, and doubles instead of just one value. 28 | % Fixed help text of fiff_pick_channels. 29 | % 30 | % Revision 1.2 2006/04/23 15:29:40 msh 31 | % Added MGH to the copyright 32 | % 33 | % Revision 1.1 2006/04/20 23:33:20 msh 34 | % Separated fiff_pick_channels and fiff_pick_types from the _evoked versions 35 | % 36 | % 37 | 38 | me='MNE:fiff_pick_channels'; 39 | 40 | nchan = length(ch_names); 41 | if nargin == 1 42 | sel = ones(1,nchan); 43 | for k = 1:nchan 44 | sel(k) = k; 45 | end 46 | return; 47 | elseif nargin == 2 48 | exclude = []; 49 | elseif nargin ~= 3 50 | error(me,'Incorrect number of arguments'); 51 | end 52 | 53 | if isempty(include) 54 | % 55 | % Include all initially 56 | % 57 | sel = zeros(1,nchan); 58 | for k = 1:nchan 59 | sel(k) = k; 60 | end 61 | nzero = 0; 62 | for k = 1:length(exclude) 63 | c = strmatch(exclude{k},ch_names,'exact'); 64 | if length(c) > 0 65 | sel(c(1)) = 0; 66 | nzero = nzero + 1; 67 | end 68 | end 69 | % 70 | % Check for exclusions 71 | % 72 | if nzero > 0 73 | newsel = zeros(1,nchan-nzero); 74 | p = 0; 75 | for k = 1:nchan 76 | if sel(k) > 0 77 | p = p + 1; 78 | newsel(p) = sel(k); 79 | end 80 | end 81 | sel = newsel; 82 | end 83 | else 84 | % 85 | % First do the channels to be included 86 | % 87 | sel = zeros(1,length(include)); 88 | nzero = 0; 89 | for k = 1:length(include) 90 | c = strmatch(include{k},ch_names,'exact'); 91 | if ~length(c) 92 | error(me,'Missing channel %s',include{k}); 93 | elseif length(c) > 1 94 | disp(sprintf('Ambiguous channel, taking first occurence: %s',include{k})); 95 | end 96 | % 97 | % Is this channel in the exclusion list? 98 | % 99 | sel(k) = c(1); 100 | if ~isempty(exclude) 101 | c = strmatch(include{k},exclude,'exact'); 102 | if length(c) > 0 103 | sel(k) = 0; 104 | nzero = nzero + 1; 105 | end 106 | end 107 | end 108 | % 109 | % Check whether some channels were excluded 110 | % 111 | if nzero > 0 112 | newsel = zeros(1,length(include)-nzero); 113 | p = 0; 114 | for k = 1:length(include) 115 | if sel(k) > 0 116 | p = p + 1; 117 | newsel(p) = sel(k); 118 | end 119 | end 120 | sel = newsel; 121 | end 122 | end 123 | 124 | return; 125 | 126 | end 127 | 128 | 129 | 130 | -------------------------------------------------------------------------------- /matlab/fiff_pick_channels_evoked.m: -------------------------------------------------------------------------------- 1 | function [res] = fiff_pick_channels_evoked(orig,include,exclude) 2 | % 3 | % [res] = fiff_pick_channels_evoked(orig,include,exclude) 4 | % 5 | % Pick desired channels from evoked-response data 6 | % 7 | % orig - The original data 8 | % include - Channels to include (if empty, include all available) 9 | % exclude - Channels to exclude (if empty, do not exclude any) 10 | % 11 | % 12 | 13 | % 14 | % Author : Matti Hamalainen, MGH Martinos Center 15 | % License : BSD 3-clause 16 | % 17 | % 18 | % Revision 1.9 2006/10/04 20:12:37 msh 19 | % Added fiff_read_evoked_all 20 | % Modified fiff_pick_channels_evoked to handle multiple data sets 21 | % Added bad channel writing to fiff_write_evoked 22 | % 23 | % Revision 1.8 2006/06/29 22:12:28 msh 24 | % Fixed errors in channel picking 25 | % 26 | % Revision 1.7 2006/04/26 00:14:24 msh 27 | % Return empty structure from fiff_pick_channels evoked and fiff_pick_types_evoked if there is no match. 28 | % Accuracy checking was incorrect in mne_add_coil_defs 29 | % 30 | % Revision 1.6 2006/04/23 15:29:40 msh 31 | % Added MGH to the copyright 32 | % 33 | % Revision 1.5 2006/04/22 10:59:30 msh 34 | % Added fiff_pick_info 35 | % 36 | % Revision 1.4 2006/04/20 23:33:20 msh 37 | % Separated fiff_pick_channels and fiff_pick_types from the _evoked versions 38 | % 39 | % Revision 1.3 2006/04/18 20:44:46 msh 40 | % Added reading of forward solution. 41 | % Use length instead of size when appropriate 42 | % 43 | % Revision 1.2 2006/04/14 15:49:49 msh 44 | % Improved the channel selection code and added ch_names to measurement info. 45 | % 46 | % Revision 1.1 2006/04/14 00:45:42 msh 47 | % Added channel picking and fiff_invert_transform 48 | % 49 | % 50 | 51 | me='MNE:fiff_pick_channels_evoked'; 52 | 53 | if nargin == 1 54 | res = orig; 55 | return; 56 | elseif nargin == 2 57 | exclude = []; 58 | elseif nargin ~= 3 59 | error(me,'Incorrect number of arguments'); 60 | end 61 | 62 | if isempty(include) && isempty(exclude) 63 | res = orig; 64 | return; 65 | end 66 | 67 | sel = fiff_pick_channels(orig.info.ch_names,include,exclude); 68 | if isempty(sel) 69 | res = []; 70 | fprintf(1,'Warning : No channels match the selection.\n'); 71 | return; 72 | end 73 | 74 | res = orig; 75 | % 76 | % Modify the measurement info 77 | % 78 | res.info = fiff_pick_info(res.info,sel); 79 | % 80 | % Create the reduced data set 81 | % 82 | for k = 1:length(res.evoked) 83 | res.evoked(k).epochs = res.evoked(k).epochs(sel,:); 84 | end 85 | 86 | return; 87 | 88 | end 89 | 90 | 91 | 92 | -------------------------------------------------------------------------------- /matlab/fiff_pick_info.m: -------------------------------------------------------------------------------- 1 | function [res] = fiff_pick_info(info,sel) 2 | % 3 | % [res] = fiff_pick_info(info,sel) 4 | % 5 | % Pick desired channels from measurement info 6 | % 7 | % res - Info modified according to sel 8 | % info - The original data 9 | % sel - List of channels to select 10 | % 11 | 12 | % 13 | % Author : Matti Hamalainen, MGH Martinos Center 14 | % License : BSD 3-clause 15 | % 16 | % 17 | % Revision 1.2 2006/04/23 15:29:40 msh 18 | % Added MGH to the copyright 19 | % 20 | % Revision 1.1 2006/04/22 10:59:30 msh 21 | % Added fiff_pick_info 22 | % 23 | % 24 | 25 | me='MNE:fiff_pick_info'; 26 | 27 | if nargin == 1 28 | sel = []; 29 | elseif nargin ~= 2 30 | error(me,'Incorrect number of arguments'); 31 | end 32 | 33 | res = info; 34 | if isempty(sel) 35 | return; 36 | end 37 | 38 | 39 | res.chs = res.chs(sel); 40 | res.ch_names = res.ch_names(sel); 41 | res.nchan = length(sel); 42 | 43 | return; 44 | 45 | end 46 | -------------------------------------------------------------------------------- /matlab/fiff_pick_types.m: -------------------------------------------------------------------------------- 1 | function [sel] = fiff_pick_types(info,meg,eeg,stim,include,exclude) 2 | % 3 | % [sel] = fiff_pick_types(info,meg,eeg,stim,exclude) 4 | % 5 | % Create a selector to pick desired channel types from data 6 | % 7 | % info - The measurement info 8 | % meg - Include MEG channels 9 | % eeg - Include EEG channels 10 | % stim - Include stimulus channels 11 | % include - Additional channels to include (if empty, do not add any) 12 | % exclude - Channels to exclude (if empty, do not exclude any) 13 | % 14 | % 15 | 16 | % 17 | % Author : Matti Hamalainen, MGH Martinos Center 18 | % License : BSD 3-clause 19 | % 20 | % 21 | % Revision 1.5 2006/06/29 22:12:28 msh 22 | % Fixed errors in channel picking 23 | % 24 | % Revision 1.4 2006/05/03 18:53:04 msh 25 | % Approaching Matlab 6.5 backward compatibility 26 | % 27 | % Revision 1.3 2006/04/23 15:29:40 msh 28 | % Added MGH to the copyright 29 | % 30 | % Revision 1.2 2006/04/21 11:33:17 msh 31 | % Added raw segment reading routines 32 | % 33 | % Revision 1.1 2006/04/20 23:33:20 msh 34 | % Separated fiff_pick_channels and fiff_pick_types from the _evoked versions 35 | % 36 | % 37 | 38 | me='MNE:fiff_pick_types'; 39 | 40 | global FIFF; 41 | if isempty(FIFF) 42 | FIFF = fiff_define_constants(); 43 | end 44 | 45 | if nargin == 5 46 | exclude = []; 47 | elseif nargin == 4 48 | include = []; 49 | exclude = []; 50 | elseif nargin == 3 51 | include = []; 52 | exclude = []; 53 | stim = false; 54 | elseif nargin == 2 55 | include = []; 56 | exclude = []; 57 | stim = false; 58 | eeg = false; 59 | elseif nargin ~= 6 60 | error(me,'Incorrect number of arguments'); 61 | end 62 | 63 | pick = zeros(1,info.nchan); 64 | 65 | for k = 1:info.nchan 66 | kind = info.chs(k).kind; 67 | if (kind == FIFF.FIFFV_MEG_CH || kind == FIFF.FIFFV_REF_MEG_CH) && meg 68 | pick(k) = true; 69 | elseif kind == FIFF.FIFFV_EEG_CH && eeg 70 | pick(k) = true; 71 | elseif kind == FIFF.FIFFV_STIM_CH && stim 72 | pick(k) = true; 73 | end 74 | end 75 | 76 | p = 0; 77 | for k = 1:info.nchan 78 | if pick(k) 79 | p = p + 1; 80 | myinclude{p} = info.ch_names{k}; 81 | end 82 | end 83 | 84 | if ~isempty(include) 85 | for k = 1:length(include) 86 | p = p + 1; 87 | myinclude{p} = include{k}; 88 | end 89 | end 90 | 91 | if p == 0 92 | sel = []; 93 | else 94 | sel = fiff_pick_channels(info.ch_names,myinclude,exclude); 95 | end 96 | 97 | return; 98 | 99 | end 100 | -------------------------------------------------------------------------------- /matlab/fiff_pick_types_evoked.m: -------------------------------------------------------------------------------- 1 | function [res] = fiff_pick_types_evoked(orig,meg,eeg,stim,include,exclude) 2 | % 3 | % [res] = fiff_pick_types_evoked(orig,meg,eeg,stim,include,exclude) 4 | % 5 | % Pick desired channels types from evoked-response data 6 | % 7 | % orig - The original data 8 | % meg - Include MEG channels 9 | % eeg - Include EEG channels 10 | % stim - Include stimulus channels 11 | % include - Additional channels to include (if empty, do not add any) 12 | % exclude - Channels to exclude (if empty, do not exclude any) 13 | % 14 | % 15 | 16 | % 17 | % Author : Matti Hamalainen, MGH Martinos Center 18 | % License : BSD 3-clause 19 | % 20 | % 21 | % Revision 1.10 2008/06/12 19:59:19 msh 22 | % Fixed the help text 23 | % 24 | % Revision 1.9 2007/12/23 17:01:24 msh 25 | % Fixed error handling multiple datasets 26 | % 27 | % Revision 1.8 2006/04/26 00:14:24 msh 28 | % Return empty structure from fiff_pick_channels evoked and fiff_pick_types_evoked if there is no match. 29 | % Accuracy checking was incorrect in mne_add_coil_defs 30 | % 31 | % Revision 1.7 2006/04/23 15:29:40 msh 32 | % Added MGH to the copyright 33 | % 34 | % Revision 1.6 2006/04/21 11:33:17 msh 35 | % Added raw segment reading routines 36 | % 37 | % Revision 1.5 2006/04/20 23:33:20 msh 38 | % Separated fiff_pick_channels and fiff_pick_types from the _evoked versions 39 | % 40 | % Revision 1.4 2006/04/17 11:52:15 msh 41 | % Added coil definition stuff 42 | % 43 | % Revision 1.3 2006/04/15 12:21:00 msh 44 | % Several small improvements 45 | % 46 | % Revision 1.2 2006/04/14 15:49:49 msh 47 | % Improved the channel selection code and added ch_names to measurement info. 48 | % 49 | % Revision 1.1 2006/04/14 00:45:42 msh 50 | % Added channel picking and fiff_invert_transform 51 | % 52 | % 53 | 54 | me='MNE:fiff_pick_types_evoked'; 55 | 56 | if nargin == 5 57 | exclude = []; 58 | elseif nargin == 4 59 | include = []; 60 | exclude = []; 61 | elseif nargin == 3 62 | include = []; 63 | exclude = []; 64 | stim = false; 65 | elseif nargin == 2 66 | include = []; 67 | exclude = []; 68 | stim = false; 69 | eeg = false; 70 | elseif nargin ~= 6 71 | error(me,'Incorrect number of arguments'); 72 | end 73 | 74 | sel = fiff_pick_types(orig.info,meg,eeg,stim,include,exclude); 75 | 76 | if isempty(sel) 77 | res = []; 78 | fprintf(1,'Warning : No channels match the selection.\n'); 79 | return; 80 | end 81 | 82 | res = orig; 83 | res.info.chs = res.info.chs(sel); 84 | res.info.ch_names = res.info.ch_names(sel); 85 | % 86 | % Create the reduced data set 87 | % 88 | for k = 1:length(res.evoked) 89 | res.evoked(k).epochs = res.evoked(k).epochs(sel,:); 90 | end 91 | res.info.nchan = length(sel); 92 | 93 | return; 94 | 95 | end 96 | -------------------------------------------------------------------------------- /matlab/fiff_read_bad_channels.m: -------------------------------------------------------------------------------- 1 | function [bads] = fiff_read_bad_channels(fid,node,ch_rename) 2 | % 3 | % [bads] = fiff_read_bad_channels(fid,node[,ch_rename]) 4 | % 5 | % Reas the bad channel list from a node if it exists 6 | % 7 | % fid - The file id 8 | % node - The node of interes 9 | % ch_rename - Short-to-long channel name mapping 10 | % 11 | 12 | % 13 | % 14 | % Author : Matti Hamalainen, MGH Martinos Center 15 | % License : BSD 3-clause 16 | % 17 | % Revision 1.5 2006/05/03 18:53:04 msh 18 | % Approaching Matlab 6.5 backward compatibility 19 | % 20 | % Revision 1.4 2006/05/03 15:51:17 msh 21 | % Re-added fiff_read_bad_channels 22 | % 23 | % Revision 1.2 2006/04/23 15:29:40 msh 24 | % Added MGH to the copyright 25 | % 26 | % Revision 1.1 2006/04/12 17:09:28 msh 27 | % Added routines for reading noise-covariance matrices 28 | % 29 | % 30 | 31 | me='MNE:fiff_read_bad_channels'; 32 | 33 | if nargin == 2 34 | ch_rename = {}; 35 | elseif nargin ~= 3 36 | error(me,'Incorrect number of arguments'); 37 | end 38 | 39 | global FIFF; 40 | if isempty(FIFF) 41 | FIFF = fiff_define_constants(); 42 | end 43 | 44 | node = fiff_dir_tree_find(node,FIFF.FIFFB_MNE_BAD_CHANNELS); 45 | 46 | bads = []; 47 | if ~isempty(node) 48 | tag = find_tag(node,FIFF.FIFF_MNE_CH_NAME_LIST); 49 | if ~isempty(tag) 50 | bads = fiff_split_name_list(tag.data); 51 | end 52 | end 53 | bads = fiff_rename_list(bads, ch_rename); 54 | 55 | return; 56 | 57 | function [tag] = find_tag(node,findkind) 58 | 59 | for p = 1:node.nent 60 | kind = node.dir(p).kind; 61 | pos = node.dir(p).pos; 62 | if kind == findkind 63 | tag = fiff_read_tag(fid,pos); 64 | return; 65 | end 66 | end 67 | tag = []; 68 | return 69 | end 70 | 71 | end 72 | -------------------------------------------------------------------------------- /matlab/fiff_read_coord_trans.m: -------------------------------------------------------------------------------- 1 | function [trans_head2mri] = fiff_read_coord_trans(transfile) 2 | % 3 | % usage: [trans_head2mri] = fiff_read_coord_trans(transfile) 4 | % 5 | % input: 6 | % transfile = name of transformation fif file (usually stored in 7 | % the subject's Freesurfer directory in /mri/T1-neuromag/sets). 8 | % 9 | % output: 10 | % trans_head2mri = transformation structure from head to MRI coordinate systems. 11 | % 12 | % Note: the inverse transformation, from MRI to head coordinate systems 13 | % can be obtained by just taking the inverse: 14 | % trans_mri2head.from=5; trans_mri2head.to=4; 15 | % trans_mri2head.trans=inv(trans_head2mri.trans); 16 | % 17 | % author: Rey Ramirez email: rrramir@uw.edu 18 | 19 | FIFF = fiff_define_constants; 20 | [fid,~,dir] = fiff_open(transfile); 21 | a = find([dir.kind] == FIFF.FIFF_COORD_TRANS); 22 | pos = dir(a(1)).pos; 23 | tag = fiff_read_tag(fid,pos); 24 | trans_head2mri = tag.data; 25 | -------------------------------------------------------------------------------- /matlab/fiff_read_events.m: -------------------------------------------------------------------------------- 1 | function [events, mappings] = fiff_read_events(source, tree) 2 | % 3 | % [events, mappings] = fiff_read_events(source, tree) 4 | % 5 | % Read the events 6 | % 7 | % If tree is specified, source is assumed to be an open file id, 8 | % otherwise a the name of the file to read. If tree is missing, the 9 | % meas output argument should not be specified. 10 | % 11 | % 12 | % Author : Martin Luessi, MGH Martinos Center 13 | % License : BSD 3-clause 14 | 15 | 16 | global FIFF; 17 | if isempty(FIFF) 18 | FIFF = fiff_define_constants(); 19 | end 20 | 21 | me='MNE:fiff_read_events'; 22 | 23 | if nargin ~= 2 & nargin ~= 1 24 | error(me,'Incorrect number of arguments'); 25 | end 26 | 27 | if nargin == 1 28 | [ fid, tree ] = fiff_open(source); 29 | open_here = true; 30 | else 31 | fid = source; 32 | open_here = false; 33 | end 34 | 35 | % 36 | % Find the desired blocks 37 | % 38 | data = fiff_dir_tree_find(tree, FIFF.FIFFB_MNE_EVENTS); 39 | if length(data) == 0 40 | if open_here 41 | fclose(fid); 42 | end 43 | error(me,'Could not find events'); 44 | end 45 | 46 | % 47 | % Read the events 48 | % 49 | for k = 1:data.nent 50 | kind = data.dir(k).kind; 51 | pos = data.dir(k).pos; 52 | if kind == FIFF.FIFF_MNE_EVENT_LIST 53 | tag = fiff_read_tag(fid, pos); 54 | events = tag.data; 55 | break 56 | end 57 | end 58 | 59 | if ~exist('events','var') 60 | if open_here 61 | fclose(fid); 62 | end 63 | error(me,'Events not found'); 64 | end 65 | 66 | events = reshape(events, 3, length(events) / 3)'; 67 | 68 | % 69 | % Read the event mappings 70 | % 71 | mappings = ''; 72 | data = fiff_dir_tree_find(tree, FIFF.FIFFB_MNE_EVENTS); 73 | 74 | for k = 1:data.nent 75 | kind = data.dir(k).kind; 76 | pos = data.dir(k).pos; 77 | if kind == FIFF.FIFF_DESCRIPTION 78 | tag = fiff_read_tag(fid, pos); 79 | mappings = tag.data; 80 | break 81 | end 82 | end 83 | 84 | if open_here 85 | fclose(fid); 86 | end 87 | 88 | return; 89 | 90 | 91 | 92 | -------------------------------------------------------------------------------- /matlab/fiff_read_extended_ch_info.m: -------------------------------------------------------------------------------- 1 | function [chs, ch_rename] = fiff_read_extended_ch_info(chs,meas_info,fid) 2 | 3 | me='MNE:fiff_read_extended_ch_info'; 4 | if nargin ~= 3 5 | error(me,'Incorrect number of arguments'); 6 | end 7 | 8 | global FIFF; 9 | if isempty(FIFF) 10 | FIFF = fiff_define_constants(); 11 | end 12 | 13 | ch_rename = {}; 14 | 15 | ch_infos = fiff_dir_tree_find(meas_info,FIFF.FIFFB_CH_INFO); 16 | 17 | if length(ch_infos) == 0 18 | return; 19 | elseif length(ch_infos) ~= length(chs) 20 | error('extended channel info does not match channels') 21 | end 22 | for k=1:length(chs) 23 | new = ch_infos(k); 24 | ch = chs(k); 25 | for p = 1:new.nent 26 | kind = new.dir(p).kind; 27 | if kind == FIFF.FIFF_CH_DACQ_NAME 28 | tag = fiff_read_tag(fid, new.dir(p).pos); 29 | data = tag.data; 30 | ch_rename = [ch_rename; {ch.ch_name, data}]; 31 | ch.ch_name = data; 32 | break 33 | end 34 | end 35 | chs(k) = ch; 36 | end 37 | 38 | return; 39 | 40 | end 41 | -------------------------------------------------------------------------------- /matlab/fiff_read_hpi_result.m: -------------------------------------------------------------------------------- 1 | function [ res ] = fiff_read_hpi_result(name) 2 | 3 | % 4 | % [ res ] = fiff_read_hpi_result(name) 5 | % 6 | % Read the HPI result block from a measurement file 7 | % 8 | 9 | 10 | 11 | global FIFF; 12 | if isempty(FIFF) 13 | FIFF = fiff_define_constants(); 14 | end 15 | 16 | me='MNE:fiff_read_hpi_result'; 17 | 18 | if nargin ~= 1 19 | error(me,'Incorrect number of arguments'); 20 | end 21 | 22 | [ fid, tree ] = fiff_open(name); 23 | 24 | hpi = fiff_dir_tree_find(tree,FIFF.FIFFB_HPI_RESULT); 25 | 26 | k = 0; 27 | for p = 1:hpi.nent 28 | kind = hpi.dir(p).kind; 29 | pos = hpi.dir(p).pos; 30 | switch kind 31 | case FIFF.FIFF_DIG_POINT 32 | k = k + 1; 33 | tag = fiff_read_tag(fid,pos); 34 | res.dig(k) = tag.data; 35 | res.dig(k).coord_frame = FIFF.FIFFV_COORD_DEVICE; 36 | case FIFF.FIFF_HPI_DIGITIZATION_ORDER 37 | tag = fiff_read_tag(fid,pos); 38 | res.dig_order = tag.data; 39 | case FIFF.FIFF_HPI_FIT_GOODNESS 40 | tag = fiff_read_tag(fid,pos); 41 | res.goodness = tag.data; 42 | case FIFF.FIFF_HPI_COILS_USED 43 | tag = fiff_read_tag(fid,pos); 44 | res.used = tag.data; 45 | case FIFF.FIFF_HPI_FIT_GOOD_LIMIT 46 | tag = fiff_read_tag(fid,pos); 47 | res.accept_limit = tag.data; 48 | case FIFF.FIFF_HPI_FIT_DIST_LIMIT 49 | tag = fiff_read_tag(fid,pos); 50 | res.dist_limit = tag.data; 51 | case FIFF.FIFF_HPI_FIT_ACCEPT 52 | tag = fiff_read_tag(fid,pos); 53 | res.accepted = tag.data; 54 | case FIFF.FIFF_HPI_COIL_MOMENTS 55 | tag = fiff_read_tag(fid,pos); 56 | res.moments = tag.data; 57 | case FIFF.FIFF_COORD_TRANS 58 | tag = fiff_read_tag(fid,pos); 59 | res.trans = tag.data; 60 | end 61 | end 62 | res.coord_frame = FIFF.FIFFV_COORD_DEVICE; 63 | 64 | fclose(fid); 65 | 66 | return; 67 | 68 | end 69 | 70 | -------------------------------------------------------------------------------- /matlab/fiff_read_raw_segment_times.m: -------------------------------------------------------------------------------- 1 | function [data,times] = fiff_read_raw_segment_times(raw,from,to,sel) 2 | % 3 | % [data,times] = fiff_read_raw_segment_times(raw,from,to) 4 | % 5 | % Read a specific raw data segment 6 | % 7 | % raw - structure returned by fiff_setup_read_raw 8 | % from - starting time of the segment in seconds 9 | % to - end time of the segment in seconds 10 | % sel - optional channel selection vector 11 | % 12 | % data - returns the data matrix (channels x samples) 13 | % times - returns the time values corresponding to the samples (optional) 14 | % 15 | 16 | % 17 | % Author : Matti Hamalainen, MGH Martinos Center 18 | % License : BSD 3-clause 19 | % 20 | % Revision 1.3 2006/04/23 15:29:40 msh 21 | % Added MGH to the copyright 22 | % 23 | % Revision 1.2 2006/04/21 14:23:16 msh 24 | % Further improvements in raw data reading 25 | % 26 | % 27 | 28 | me='MNE:fiff_read_raw_segment_times'; 29 | 30 | if nargin == 3 31 | sel = []; 32 | elseif nargin ~= 4 33 | error(me,'Incorrect number of arguments'); 34 | end 35 | % 36 | % Convert to samples 37 | % 38 | from = floor(from*raw.info.sfreq); 39 | to = ceil(to*raw.info.sfreq); 40 | % 41 | % Read it 42 | % 43 | [ data, times ] = fiff_read_raw_segment(raw,from,to,sel); 44 | 45 | return; 46 | 47 | end 48 | 49 | -------------------------------------------------------------------------------- /matlab/fiff_read_tag_info.m: -------------------------------------------------------------------------------- 1 | function [tag] = fiff_read_tag_info(fid) 2 | % 3 | % [fid,dir] = fiff_open(fname) 4 | % 5 | % Open a fif file and provide the directory of tags 6 | % 7 | 8 | % 9 | % Author : Matti Hamalainen, MGH Martinos Center 10 | % License : BSD 3-clause 11 | % 12 | % Revision 1.2 2006/04/23 15:29:40 msh 13 | % Added MGH to the copyright 14 | % 15 | % Revision 1.1 2006/04/10 23:26:54 msh 16 | % Added fiff reading routines 17 | % 18 | % 19 | 20 | me='MNE:fiff_read_tag_info'; 21 | 22 | FIFFV_NEXT_SEQ=0; 23 | 24 | data = fread(fid,4,'int'); 25 | if ~isempty(data) 26 | tag.kind = data(1); 27 | tag.type = data(2); 28 | tag.size = data(3); 29 | tag.next = data(4); 30 | else 31 | warning(me,'Invalid tag found'); 32 | tag.kind = []; 33 | tag.type = []; 34 | tag.size = []; 35 | tag.next = []; 36 | end 37 | 38 | if tag.next == FIFFV_NEXT_SEQ 39 | fseek(fid,tag.size,'cof'); 40 | elseif tag.next > 0 41 | fseek(fid,tag.next,'bof'); 42 | end 43 | 44 | -------------------------------------------------------------------------------- /matlab/fiff_rename_comp.m: -------------------------------------------------------------------------------- 1 | function comp = fiff_rename_comp(comp, ch_rename) 2 | 3 | me='MNE:fiff_rename_comp'; 4 | 5 | if nargin ~= 2 6 | error(me,'Incorrect number of arguments'); 7 | end 8 | 9 | comp.data.row_names = fiff_rename_list(comp.data.row_names, ch_rename); 10 | comp.data.col_names = fiff_rename_list(comp.data.col_names, ch_rename); 11 | 12 | return 13 | 14 | end 15 | -------------------------------------------------------------------------------- /matlab/fiff_rename_list.m: -------------------------------------------------------------------------------- 1 | function lst = fiff_rename_list(lst, ch_rename) 2 | 3 | me = 'MNE:fiff_rename_list'; 4 | 5 | if nargin ~= 2 6 | error(me,'Incorrect number of arguments'); 7 | end 8 | if isempty(lst) 9 | return; 10 | end 11 | if ~iscell(lst) 12 | error(me,'input must be a cell array:%s',lst); 13 | end 14 | 15 | for k = 1:length(lst) 16 | name = lst(k); 17 | if length(ch_rename) 18 | idx = find(strcmp(name, ch_rename{:, 1})); 19 | if length(idx) 20 | name = ch_rename{idx(1), 2}; 21 | end 22 | end 23 | lst(k) = name; 24 | end 25 | 26 | return 27 | 28 | end 29 | -------------------------------------------------------------------------------- /matlab/fiff_reset_ch_pos.m: -------------------------------------------------------------------------------- 1 | function [res] = fiff_reset_ch_pos(chs) 2 | % 3 | % [res] = fiff_reset_ch_pos(chs) 4 | % 5 | % Reset channel position data to their original values as listed in 6 | % the fif file 7 | % 8 | % NOTE: Only the coil_trans field is modified by this routine, not 9 | % loc which remains to reflect the original data read from the fif file 10 | % 11 | % 12 | 13 | % 14 | % Author : Matti Hamalainen, MGH Martinos Center 15 | % License : BSD 3-clause 16 | % 17 | % 18 | % Revision 1.4 2006/05/03 18:53:05 msh 19 | % Approaching Matlab 6.5 backward compatibility 20 | % 21 | % Revision 1.3 2006/04/23 15:29:40 msh 22 | % Added MGH to the copyright 23 | % 24 | % Revision 1.2 2006/04/18 20:44:46 msh 25 | % Added reading of forward solution. 26 | % Use length instead of size when appropriate 27 | % 28 | % Revision 1.1 2006/04/13 22:37:03 msh 29 | % Added head_head_trans field to info. 30 | % 31 | % 32 | 33 | me='MNE:fiff_reset_channel_pos'; 34 | 35 | global FIFF; 36 | if isempty(FIFF) 37 | FIFF = fiff_define_constants(); 38 | end 39 | 40 | if nargin ~= 1 41 | error(me,'Wrong number of arguments'); 42 | end 43 | 44 | res = chs; 45 | for k = 1:length(res) 46 | loc = res(k).loc; 47 | if res(k).kind == FIFF.FIFFV_MEG_CH || ... 48 | res(k).kind == FIFF.FIFFV_REF_MEG_CH 49 | res(k).coil_trans = [ [ loc(4:6) loc(7:9) ... 50 | loc(10:12) ... 51 | loc(1:3) ] ; [ 0 0 0 1 ] ]; 52 | res(k).coord_frame = FIFF.FIFFV_COORD_DEVICE; 53 | elseif res(k).kind == FIFF.FIFFV_EEG_CH 54 | res(k).eeg_loc = [ loc(1:3) loc(4:6) ]; 55 | res(k).coord_frame = FIFF.FIFFV_COORD_HEAD; 56 | end 57 | end 58 | 59 | return; 60 | 61 | end 62 | -------------------------------------------------------------------------------- /matlab/fiff_split_name_list.m: -------------------------------------------------------------------------------- 1 | function [ names ] = fiff_split_name_list(list); 2 | % 3 | % [names] = fiff_split_name_list(list) 4 | % 5 | % 6 | % Split a name list containing colon-separated entries into a cell array 7 | % containing the strings 8 | % 9 | 10 | 11 | % 12 | % Author : Matti Hamalainen, MGH Martinos Center 13 | % License : BSD 3-clause 14 | % 15 | % Revision 1.3 2006/04/27 22:38:37 msh 16 | % Splitting an empty list now results in an empty output. 17 | % Added fiff_write_double and fiff_write_short 18 | % Write an array of floats, ints, and doubles instead of just one value. 19 | % Fixed help text of fiff_pick_channels. 20 | % 21 | % Revision 1.2 2006/04/23 15:29:40 msh 22 | % Added MGH to the copyright 23 | % 24 | % Revision 1.1 2006/04/10 23:26:54 msh 25 | % Added fiff reading routines 26 | % 27 | % 28 | 29 | rem = list; 30 | nnames = 0; 31 | if isempty(rem) 32 | names = []; 33 | return; 34 | end 35 | while true 36 | [ str, rem ] = strtok(rem,':'); 37 | if isempty(str) 38 | break; 39 | end 40 | nnames = nnames + 1; 41 | names{nnames} = str; 42 | end 43 | 44 | return; 45 | 46 | -------------------------------------------------------------------------------- /matlab/fiff_start_block.m: -------------------------------------------------------------------------------- 1 | function fiff_start_block(fid,kind) 2 | % 3 | % fiff_start_block(fid,kind) 4 | % 5 | % Writes a FIFF_BLOCK_START tag 6 | % 7 | % fid An open fif file descriptor 8 | % kind The block kind to start 9 | % 10 | 11 | % 12 | % 13 | % Author : Matti Hamalainen, MGH Martinos Center 14 | % License : BSD 3-clause 15 | % 16 | % Revision 1.3 2006/04/23 15:29:40 msh 17 | % Added MGH to the copyright 18 | % 19 | % Revision 1.2 2006/04/10 23:26:54 msh 20 | % Added fiff reading routines 21 | % 22 | % Revision 1.1 2005/12/05 16:01:04 msh 23 | % Added an initial set of fiff writing routines. 24 | % 25 | % 26 | 27 | me='MNE:fiff_start_block'; 28 | 29 | if nargin ~= 2 30 | error(me,'Incorrect number of arguments'); 31 | end 32 | 33 | FIFF_BLOCK_START=104; 34 | fiff_write_int(fid,FIFF_BLOCK_START,kind); 35 | 36 | return; 37 | -------------------------------------------------------------------------------- /matlab/fiff_start_file.m: -------------------------------------------------------------------------------- 1 | function [fid] = fiff_start_file(name) 2 | % 3 | % [fid] = fiff_start_file(name) 4 | % 5 | % Opens a fif file for writing and writes the compulsory header tags 6 | % 7 | % name The name of the file to open. It is recommended 8 | % that the name ends with .fif 9 | % 10 | % 11 | 12 | % 13 | % 14 | % Author : Matti Hamalainen, MGH Martinos Center 15 | % License : BSD 3-clause 16 | % 17 | % Revision 1.3 2006/04/23 15:29:40 msh 18 | % Added MGH to the copyright 19 | % 20 | % Revision 1.2 2006/04/10 23:26:54 msh 21 | % Added fiff reading routines 22 | % 23 | % Revision 1.1 2005/12/05 16:01:04 msh 24 | % Added an initial set of fiff writing routines. 25 | % 26 | % 27 | 28 | me='MNE:fiff_start_file'; 29 | if nargin ~= 1 30 | error(me,'File name required as an argument'); 31 | end 32 | 33 | [fid,message] = fopen(name,'w+','ieee-be'); 34 | if fid < 0 35 | error(me,message); 36 | end 37 | % 38 | % Write the compulsory items 39 | % 40 | FIFF_FILE_ID=100; 41 | FIFF_DIR_POINTER=101; 42 | FIFF_FREE_LIST=106; 43 | 44 | fiff_write_id(fid,FIFF_FILE_ID); 45 | fiff_write_int(fid,FIFF_DIR_POINTER,-1); 46 | fiff_write_int(fid,FIFF_FREE_LIST,-1); 47 | % 48 | % Ready for more 49 | % 50 | return; 51 | 52 | -------------------------------------------------------------------------------- /matlab/fiff_transform_eeg_chs.m: -------------------------------------------------------------------------------- 1 | function [res, count] = fiff_transform_eeg_chs(chs,trans) 2 | % 3 | % [res, count] = fiff_transform_eeg_chs(chs,trans) 4 | % 5 | % Move to another coordinate system in EEG channel channel info 6 | % Count gives the number of channels transformed 7 | % 8 | % NOTE: Only the eeg_loc field is modified by this routine, not 9 | % loc which remains to reflect the original data read from the fif file 10 | % 11 | % 12 | 13 | % 14 | % Author : Matti Hamalainen, MGH Martinos Center 15 | % License : BSD 3-clause 16 | % 17 | % 18 | % Revision 1.7 2008/11/17 21:45:56 msh 19 | % Fixed error in transforming the EEG location data to another coordinate frame 20 | % 21 | % Revision 1.6 2006/05/03 18:53:05 msh 22 | % Approaching Matlab 6.5 backward compatibility 23 | % 24 | % Revision 1.5 2006/04/23 15:29:40 msh 25 | % Added MGH to the copyright 26 | % 27 | % Revision 1.4 2006/04/21 17:31:07 msh 28 | % Added the examples. 29 | % Modified the formatting of some informative output. 30 | % 31 | % Revision 1.3 2006/04/18 20:44:46 msh 32 | % Added reading of forward solution. 33 | % Use length instead of size when appropriate 34 | % 35 | % Revision 1.2 2006/04/17 15:01:34 msh 36 | % More small improvements. 37 | % 38 | % Revision 1.1 2006/04/17 11:52:15 msh 39 | % Added coil definition stuff 40 | % 41 | % 42 | 43 | me='MNE:fiff_transform_eeg_chs'; 44 | 45 | global FIFF; 46 | if isempty(FIFF) 47 | FIFF = fiff_define_constants(); 48 | end 49 | 50 | if nargin ~= 2 51 | error(me,'Wrong number of arguments'); 52 | end 53 | 54 | res = chs; 55 | if isempty(trans) 56 | return; 57 | end 58 | 59 | count=0; 60 | % 61 | % Output unaugmented vectors from the transformation 62 | % 63 | t = trans.trans(1:3,:); 64 | for k = 1:length(res) 65 | if res(k).kind == FIFF.FIFFV_EEG_CH 66 | if res(k).coord_frame == trans.from && ~isempty(res(k).eeg_loc) 67 | % 68 | % Transform the augmented EEG location vectors 69 | % 70 | for p = 1:size(res(k).eeg_loc,2) 71 | res(k).eeg_loc(:,p) = t*[ res(k).eeg_loc(:,p) ; 1 ]; 72 | end 73 | count = count + 1; 74 | res(k).coord_frame = trans.to; 75 | end 76 | end 77 | end 78 | 79 | if count > 0 80 | fprintf(1,'\t%d EEG electrode locations transformed\n',count); 81 | end 82 | 83 | return; 84 | 85 | end 86 | -------------------------------------------------------------------------------- /matlab/fiff_transform_meg_chs.m: -------------------------------------------------------------------------------- 1 | function [res, count] = fiff_transform_meg_chs(chs,trans) 2 | % 3 | % [res, count] = fiff_transform_meg_chs(chs,trans) 4 | % 5 | % Move to another coordinate system in MEG channel channel info 6 | % Count gives the number of channels transformed 7 | % 8 | % NOTE: Only the coil_trans field is modified by this routine, not 9 | % loc which remains to reflect the original data read from the fif file 10 | % 11 | % 12 | 13 | % 14 | % Author : Matti Hamalainen, MGH Martinos Center 15 | % License : BSD 3-clause 16 | % 17 | % 18 | % Revision 1.7 2006/05/03 19:09:03 msh 19 | % Fixed even more compatibility issues. 20 | % 21 | % Revision 1.6 2006/04/23 15:29:40 msh 22 | % Added MGH to the copyright 23 | % 24 | % Revision 1.5 2006/04/21 17:31:07 msh 25 | % Added the examples. 26 | % Modified the formatting of some informative output. 27 | % 28 | % Revision 1.4 2006/04/18 20:44:46 msh 29 | % Added reading of forward solution. 30 | % Use length instead of size when appropriate 31 | % 32 | % Revision 1.3 2006/04/17 15:01:34 msh 33 | % More small improvements. 34 | % 35 | % Revision 1.2 2006/04/17 11:52:15 msh 36 | % Added coil definition stuff 37 | % 38 | % Revision 1.1 2006/04/13 22:37:03 msh 39 | % Added head_head_trans field to info. 40 | % 41 | % Revision 1.1 2006/04/13 21:20:06 msh 42 | % Added new routines for the channel information transformation. 43 | % 44 | % 45 | 46 | me='MNE:fiff_transform_meg_chs'; 47 | 48 | global FIFF; 49 | if isempty(FIFF) 50 | FIFF = fiff_define_constants(); 51 | end 52 | 53 | if nargin ~= 2 54 | error(me,'Wrong number of arguments'); 55 | end 56 | 57 | res = chs; 58 | if isempty(trans) 59 | return; 60 | end 61 | 62 | count=0; 63 | t = trans.trans; 64 | for k = 1:length(res) 65 | if res(k).kind == FIFF.FIFFV_MEG_CH || res(k).kind == FIFF.FIFFV_REF_MEG_CH 66 | if res(k).coord_frame == trans.from && ~isempty(res(k).coil_trans) 67 | res(k).coil_trans = t*res(k).coil_trans; 68 | res(k).coord_frame = trans.to; 69 | count = count + 1; 70 | end 71 | end 72 | end 73 | 74 | if count > 0 75 | fprintf(1,'\t%d MEG channel locations transformed\n',count); 76 | end 77 | 78 | return; 79 | 80 | end 81 | -------------------------------------------------------------------------------- /matlab/fiff_write_ch_infos.m: -------------------------------------------------------------------------------- 1 | function cals=fiff_write_ch_infos(fid,chs,reset_range,ch_rename) 2 | 3 | me='MNE:fiff_write_ch_infos'; 4 | 5 | if nargin == 3 6 | ch_rename = {}; 7 | elseif nargin ~= 4 8 | error(me,'Incorrect number of arguments'); 9 | end 10 | 11 | global FIFF; 12 | if isempty(FIFF) 13 | FIFF = fiff_define_constants(); 14 | end 15 | 16 | write_rename = false; 17 | for k = 1:length(chs) 18 | % 19 | % Scan numbers may have been messed up 20 | % 21 | ch = chs(k); 22 | ch.scanno = k; 23 | if reset_range 24 | ch.range = 1.0; 25 | end 26 | cals(k) = ch.cal; 27 | 28 | if length(ch_rename) 29 | idx = find(strcmp(ch_rename{:, 1}, ch.ch_name)); 30 | if length(idx) 31 | write_rename = true; 32 | ch.ch_name = ch_rename{idx(1), 2}; 33 | end 34 | end 35 | fiff_write_ch_info(fid,ch); 36 | end 37 | 38 | % add extra struct 39 | if write_rename 40 | for k=1:length(chs) 41 | ch = chs(k); 42 | fiff_start_block(fid,FIFF.FIFFB_CH_INFO); 43 | fiff_write_string(fid,FIFF.FIFF_CH_DACQ_NAME,ch.ch_name); 44 | fiff_write_int(fid, FIFF.FIFF_CH_SCAN_NO, ch.scanno); 45 | fiff_write_int(fid, FIFF.FIFF_CH_LOGICAL_NO, ch.logno); 46 | fiff_write_int(fid,FIFF.FIFF_CH_KIND,ch.kind); 47 | fiff_write_float(fid,FIFF.FIFF_CH_RANGE,ch.range); 48 | fiff_write_float(fid,FIFF.FIFF_CH_CAL,ch.cal); 49 | fiff_write_int(fid,FIFF.FIFF_CH_COIL_TYPE,ch.coil_type); 50 | fiff_write_float(fid,FIFF.FIFF_CH_LOC,ch.loc); 51 | fiff_write_int(fid,FIFF.FIFF_CH_UNIT,ch.unit); 52 | fiff_write_int(fid,FIFF.FIFF_CH_UNIT_MUL,ch.unit_mul); 53 | fiff_write_string(fid,FIFF.FIFF_CH_DACQ_NAME, ch.ch_name); 54 | fiff_write_int(fid,FIFF.FIFF_CH_COORD_FRAME,ch.coord_frame); 55 | fiff_end_block(fid, FIFF.FIFFB_CH_INFO) 56 | end 57 | end 58 | 59 | return; 60 | 61 | end 62 | -------------------------------------------------------------------------------- /matlab/fiff_write_complex.m: -------------------------------------------------------------------------------- 1 | function fiff_write_complex(fid,kind,data) 2 | % 3 | % fiff_write_complex(fid,kind,data) 4 | % 5 | % Writes a single-precision complex tag to a fif file 6 | % 7 | % fid An open fif file descriptor 8 | % kind Tag kind 9 | % data The data 10 | % 11 | 12 | % 13 | % Author : Matti Hamalainen, MGH Martinos Center 14 | % License : BSD 3-clause 15 | % 16 | % Revision 1.1 2006/09/23 14:43:56 msh 17 | % Added routines for writing complex and double complex matrices. 18 | % Added routine for writing double-precision real matrix. 19 | % 20 | % 21 | 22 | me='MNE:fiff_write_complex'; 23 | 24 | if nargin ~= 3 25 | error(me,'Incorrect number of arguments'); 26 | end 27 | 28 | FIFFT_COMPLEX_FLOAT=20; 29 | FIFFV_NEXT_SEQ=0; 30 | nel=numel(data); 31 | datasize=2*nel*4; 32 | count = fwrite(fid,int32(kind),'int32'); 33 | if count ~= 1 34 | error(me,'write failed'); 35 | end 36 | count = fwrite(fid,int32(FIFFT_COMPLEX_FLOAT),'int32'); 37 | if count ~= 1 38 | error(me,'write failed'); 39 | end 40 | count = fwrite(fid,int32(datasize),'int32'); 41 | if count ~= 1 42 | error(me,'write failed'); 43 | end 44 | count = fwrite(fid,int32(FIFFV_NEXT_SEQ),'int32'); 45 | if count ~= 1 46 | error(me,'write failed'); 47 | end 48 | data = reshape(single([real(data(:)) imag(data(:))]).', [], 1); 49 | count = fwrite(fid, data, 'single'); 50 | if count ~= nel*2 51 | error(me, 'write failed'); 52 | end 53 | return; 54 | 55 | -------------------------------------------------------------------------------- /matlab/fiff_write_complex_matrix.m: -------------------------------------------------------------------------------- 1 | function fiff_write_complex_matrix(fid,kind,mat) 2 | % 3 | % fiff_write_complex_matrix(fid,kind,mat) 4 | % 5 | % Writes a single-precision complex matrix tag 6 | % 7 | % fid An open fif file descriptor 8 | % kind The tag kind 9 | % mat The data matrix 10 | % 11 | 12 | % 13 | % 14 | % Author : Matti Hamalainen, MGH Martinos Center 15 | % License : BSD 3-clause 16 | % 17 | % Revision 1.1 2006/09/23 14:43:56 msh 18 | % Added routines for writing complex and double complex matrices. 19 | % Added routine for writing double-precision real matrix. 20 | % 21 | % 22 | 23 | me='MNE:fiff_write_complex_matrix'; 24 | 25 | if nargin ~= 3 26 | error(me,'Incorrect number of arguments'); 27 | end 28 | 29 | ndim = ndims(mat); 30 | if ndim < 2 || ndim > 3 31 | error(me,'Input should be a two-dimensional or three-dimensional matrix'); 32 | end 33 | 34 | FIFFT_COMPLEX_FLOAT = 20; 35 | FIFFT_MATRIX = bitshift(1,30); 36 | FIFFT_MATRIX_COMPLEX = bitor(FIFFT_COMPLEX_FLOAT,FIFFT_MATRIX); 37 | FIFFV_NEXT_SEQ=0; 38 | 39 | datasize = 2*4*numel(mat) + 4*(ndim+1); 40 | 41 | count = fwrite(fid,int32(kind),'int32'); 42 | if count ~= 1 43 | error(me,'write failed'); 44 | end 45 | count = fwrite(fid,int32(FIFFT_MATRIX_COMPLEX),'int32'); 46 | if count ~= 1 47 | error(me,'write failed'); 48 | end 49 | count = fwrite(fid,int32(datasize),'int32'); 50 | if count ~= 1 51 | error(me,'write failed'); 52 | end 53 | count = fwrite(fid,int32(FIFFV_NEXT_SEQ),'int32'); 54 | if count ~= 1 55 | error(me,'write failed'); 56 | end 57 | % nrow = size(mat,1); 58 | % ncol = size(mat,2); 59 | % for j = 1:nrow 60 | % for k = 1:ncol 61 | % count = fwrite(fid,real(mat(j,k)),'single'); 62 | % if count ~= 1 63 | % error(me,'write failed'); 64 | % end 65 | % count = fwrite(fid,imag(mat(j,k)),'single'); 66 | % if count ~= 1 67 | % error(me,'write failed'); 68 | % end 69 | % end 70 | % end 71 | 72 | % the matrix is written last dimension first, and then each element's real part followed by its imaginary part 73 | siz = size(mat); 74 | mat = permute(mat,ndim:-1:1); 75 | mat = [real(mat(:)) imag(mat(:))]'; 76 | count = fwrite(fid,single(mat(:)),'single'); 77 | if count ~= numel(mat) 78 | error(me,'write failed'); 79 | end 80 | if ndim==2 81 | dims(1) = siz(2); 82 | dims(2) = siz(1); 83 | dims(3) = 2; 84 | count = fwrite(fid,int32(dims),'int32'); 85 | if count ~= 3 86 | error(me,'write failed'); 87 | end 88 | elseif ndim==3 89 | dims(1) = siz(3); 90 | dims(2) = siz(2); 91 | dims(3) = siz(1); 92 | dims(4) = 3; 93 | count = fwrite(fid,int32(dims),'int32'); 94 | if count ~= 4 95 | error(me,'write failed'); 96 | end 97 | end 98 | 99 | return; 100 | 101 | -------------------------------------------------------------------------------- /matlab/fiff_write_coord_trans.m: -------------------------------------------------------------------------------- 1 | function fiff_write_coord_trans(fid,trans) 2 | % 3 | % fiff_write_coord_trans(fid,trans) 4 | % 5 | % Writes a coordinate transformation structure 6 | % 7 | % fid An open fif file descriptor 8 | % trans The coordinate transfomation structure 9 | % 10 | 11 | % 12 | % 13 | % Author : Matti Hamalainen, MGH Martinos Center 14 | % License : BSD 3-clause 15 | % 16 | % Revision 1.4 2006/04/23 15:29:40 msh 17 | % Added MGH to the copyright 18 | % 19 | % Revision 1.3 2006/04/12 10:29:02 msh 20 | % Made evoked data writing compatible with the structures returned in reading. 21 | % 22 | % Revision 1.2 2006/04/10 23:26:54 msh 23 | % Added fiff reading routines 24 | % 25 | % Revision 1.1 2005/12/05 16:01:04 msh 26 | % Added an initial set of fiff writing routines. 27 | % 28 | % 29 | 30 | me='MNE:fiff_write_coord_trans'; 31 | 32 | if nargin ~= 2 33 | error(me,'Incorrect number of arguments'); 34 | end 35 | 36 | FIFF_COORD_TRANS=222; 37 | FIFFT_COORD_TRANS_STRUCT=35; 38 | FIFFV_NEXT_SEQ=0; 39 | 40 | 41 | %?typedef struct _fiffCoordTransRec { 42 | % fiff_int_t from; /*!< Source coordinate system. */ 43 | % fiff_int_t to; /*!< Destination coordinate system. */ 44 | % fiff_float_t rot[3][3]; /*!< The forward transform (rotation part) */ 45 | % fiff_float_t move[3]; /*!< The forward transform (translation part) */ 46 | % fiff_float_t invrot[3][3]; /*!< The inverse transform (rotation part) */ 47 | % fiff_float_t invmove[3]; /*!< The inverse transform (translation part) */ 48 | %} *fiffCoordTrans, fiffCoordTransRec; /*!< Coordinate transformation descriptor */ 49 | 50 | datasize=4*2*12 + 4*2; 51 | count = fwrite(fid,int32(FIFF_COORD_TRANS),'int32'); 52 | if count ~= 1 53 | error(me,'write failed'); 54 | end 55 | count = fwrite(fid,int32(FIFFT_COORD_TRANS_STRUCT),'int32'); 56 | if count ~= 1 57 | error(me,'write failed'); 58 | end 59 | count = fwrite(fid,int32(datasize),'int32'); 60 | if count ~= 1 61 | error(me,'write failed'); 62 | end 63 | count = fwrite(fid,int32(FIFFV_NEXT_SEQ),'int32'); 64 | if count ~= 1 65 | error(me,'write failed'); 66 | end 67 | % 68 | % Start writing fiffCoordTransRec 69 | % 70 | count = fwrite(fid,int32(trans.from),'int32'); 71 | if count ~= 1 72 | error(me,'write failed'); 73 | end 74 | count = fwrite(fid,int32(trans.to),'int32'); 75 | if count ~= 1 76 | error(me,'write failed'); 77 | end 78 | % 79 | % The transform... 80 | % 81 | rot=trans.trans(1:3,1:3)'; 82 | move=trans.trans(1:3,4)'; 83 | count = fwrite(fid,single(rot),'single'); 84 | if count ~= 9 85 | error(me,'write failed'); 86 | end 87 | count = fwrite(fid,single(move),'single'); 88 | if count ~= 3 89 | error(me,'write failed'); 90 | end 91 | % 92 | % ...and its inverse 93 | % 94 | trans_inv=inv(trans.trans); 95 | rot=trans_inv(1:3,1:3)'; 96 | move=trans_inv(1:3,4)'; 97 | count = fwrite(fid,single(rot),'single'); 98 | if count ~= 9 99 | error(me,'write failed'); 100 | end 101 | count = fwrite(fid,single(move),'single'); 102 | if count ~= 3 103 | error(me,'write failed'); 104 | end 105 | -------------------------------------------------------------------------------- /matlab/fiff_write_ctf_comp.m: -------------------------------------------------------------------------------- 1 | function fiff_write_ctf_comp(fid,comps,ch_rename) 2 | % 3 | % fiff_write_ctf_comp(fid,comps,ch_rename) 4 | % 5 | % Writes the CTF compensation data into a fif file 6 | % 7 | % fid An open fif file descriptor 8 | % comps The compensation data to write 9 | % ch_rename Short-to-long channel name mapping 10 | % 11 | 12 | % 13 | % Author : Matti Hamalainen, MGH Martinos Center 14 | % License : BSD 3-clause 15 | % 16 | % Revision 1.6 2006/09/08 19:27:13 msh 17 | % Added KIT coil type to mne_load_coil_def 18 | % Allow reading of measurement info by specifying just a file name. 19 | % 20 | % Revision 1.5 2006/06/22 21:22:46 msh 21 | % Take into account the possibility of calibrated compensation matrices 22 | % 23 | % Revision 1.4 2006/05/03 19:09:03 msh 24 | % Fixed even more compatibility issues. 25 | % 26 | % Revision 1.3 2006/04/23 15:29:40 msh 27 | % Added MGH to the copyright 28 | % 29 | % Revision 1.2 2006/04/18 20:44:46 msh 30 | % Added reading of forward solution. 31 | % Use length instead of size when appropriate 32 | % 33 | % Revision 1.1 2006/04/12 10:29:02 msh 34 | % Made evoked data writing compatible with the structures returned in reading. 35 | % 36 | % 37 | 38 | me='MNE:fiff_write_ctf_comp'; 39 | 40 | if nargin == 2 41 | ch_rename = {}; 42 | elseif nargin ~= 3 43 | error(me,'Incorrect number of arguments'); 44 | end 45 | 46 | global FIFF; 47 | if isempty(FIFF) 48 | FIFF = fiff_define_constants(); 49 | end 50 | 51 | if isempty(comps) 52 | return; 53 | end 54 | % 55 | % This is very simple in fact 56 | % 57 | fiff_start_block(fid,FIFF.FIFFB_MNE_CTF_COMP); 58 | for k = 1:length(comps) 59 | comp = comps(k); 60 | comp = fiff_rename_comp(comp, ch_rename); 61 | fiff_start_block(fid,FIFF.FIFFB_MNE_CTF_COMP_DATA); 62 | % 63 | % Write the compensation kind 64 | % 65 | fiff_write_int(fid,FIFF.FIFF_MNE_CTF_COMP_KIND, ... 66 | comp.ctfkind); 67 | fiff_write_int(fid,FIFF.FIFF_MNE_CTF_COMP_CALIBRATED,comp.save_calibrated); 68 | % 69 | % Write an uncalibrated or calibrated matrix 70 | % 71 | comp.data.data = inv(diag(comp.rowcals))*comp.data.data*inv(diag(comp.colcals)); 72 | fiff_write_named_matrix(fid,FIFF.FIFF_MNE_CTF_COMP_DATA,comp.data); 73 | fiff_end_block(fid,FIFF.FIFFB_MNE_CTF_COMP_DATA); 74 | end 75 | fiff_end_block(fid,FIFF.FIFFB_MNE_CTF_COMP); 76 | 77 | return; 78 | 79 | end 80 | -------------------------------------------------------------------------------- /matlab/fiff_write_dau16.m: -------------------------------------------------------------------------------- 1 | function fiff_write_dau16(fid, kind, data) 2 | % 3 | % fiff_write_dau16(fid, kind, data) 4 | % 5 | % Writes a 16-bit integer tag to a fif file 6 | % 7 | % fid An open fif file descriptor 8 | % kind Tag kind 9 | % data The integers to use as data 10 | % 11 | 12 | % 13 | % Author : Matti Hamalainen, MGH Martinos Center 14 | % License : BSD 3-clause 15 | % 16 | % No part of this program may be photocopied, reproduced, 17 | % or translated to another program language without the 18 | % prior written consent of the author. 19 | % 20 | 21 | me = 'MNE:fiff_write_dau16'; 22 | 23 | if nargin ~= 3 24 | error(me, 'Incorrect number of arguments'); 25 | end 26 | 27 | FIFFT_DAU_PACK16 = 16; 28 | FIFFV_NEXT_SEQ = 0; 29 | nel = numel(data); 30 | datasize = nel * 2; 31 | count = fwrite(fid, int32(kind), 'int32'); 32 | if count ~= 1 33 | error(me, 'write failed'); 34 | end 35 | count = fwrite(fid, int32(FIFFT_DAU_PACK16), 'int32'); 36 | if count ~= 1 37 | error(me, 'write failed'); 38 | end 39 | count = fwrite(fid, int32(datasize), 'int32'); 40 | if count ~= 1 41 | error(me, 'write failed'); 42 | end 43 | count = fwrite(fid, int32(FIFFV_NEXT_SEQ), 'int32'); 44 | if count ~= 1 45 | error(me, 'write failed'); 46 | end 47 | count = fwrite(fid, int16(data), 'int16'); 48 | if count ~= nel 49 | error(me, 'write failed'); 50 | end 51 | 52 | return 53 | -------------------------------------------------------------------------------- /matlab/fiff_write_dig_point.m: -------------------------------------------------------------------------------- 1 | function fiff_write_dig_point(fid,dig) 2 | % 3 | % fiff_write_dig_point(fid,dig) 4 | % 5 | % Writes a digitizer data point into a fif file 6 | % 7 | % fid An open fif file descriptor 8 | % dig The point to write 9 | % 10 | 11 | % 12 | % Author : Matti Hamalainen, MGH Martinos Center 13 | % License : BSD 3-clause 14 | % 15 | % Revision 1.4 2006/04/23 15:29:40 msh 16 | % Added MGH to the copyright 17 | % 18 | % Revision 1.3 2006/04/12 10:29:02 msh 19 | % Made evoked data writing compatible with the structures returned in reading. 20 | % 21 | % Revision 1.2 2006/04/10 23:26:54 msh 22 | % Added fiff reading routines 23 | % 24 | % Revision 1.1 2005/12/05 16:01:04 msh 25 | % Added an initial set of fiff writing routines. 26 | % 27 | % 28 | 29 | me='MNE:fiff_write_dig_point'; 30 | 31 | if nargin ~= 2 32 | error(me,'Incorrect number of arguments'); 33 | end 34 | 35 | FIFF_DIG_POINT=213; 36 | FIFFT_DIG_POINT_STRUCT=33; 37 | FIFFV_NEXT_SEQ=0; 38 | 39 | %?typedef struct _fiffDigPointRec { 40 | % fiff_int_t kind; /*!< FIFF_POINT_CARDINAL, 41 | % * FIFF_POINT_HPI, or 42 | % * FIFF_POINT_EEG */ 43 | % fiff_int_t ident; /*!< Number identifying this point */ 44 | % fiff_float_t r[3]; /*!< Point location */ 45 | %} *fiffDigPoint,fiffDigPointRec; /*!< Digitization point description */ 46 | 47 | datasize=5*4; 48 | count = fwrite(fid,int32(FIFF_DIG_POINT),'int32'); 49 | if count ~= 1 50 | error(me,'write failed'); 51 | end 52 | count = fwrite(fid,int32(FIFFT_DIG_POINT_STRUCT),'int32'); 53 | if count ~= 1 54 | error(me,'write failed'); 55 | end 56 | count = fwrite(fid,int32(datasize),'int32'); 57 | if count ~= 1 58 | error(me,'write failed'); 59 | end 60 | count = fwrite(fid,int32(FIFFV_NEXT_SEQ),'int32'); 61 | if count ~= 1 62 | error(me,'write failed'); 63 | end 64 | % 65 | % Start writing fiffDigPointRec 66 | % 67 | count = fwrite(fid,int32(dig.kind),'int32'); 68 | if count ~= 1 69 | error(me,'write failed'); 70 | end 71 | count = fwrite(fid,int32(dig.ident),'int32'); 72 | if count ~= 1 73 | error(me,'write failed'); 74 | end 75 | count = fwrite(fid,single(dig.r(1:3)),'single'); 76 | if count ~= 3 77 | error(me,'write failed'); 78 | end 79 | 80 | return; 81 | 82 | 83 | -------------------------------------------------------------------------------- /matlab/fiff_write_double.m: -------------------------------------------------------------------------------- 1 | function fiff_write_double(fid,kind,data) 2 | % 3 | % fiff_write_int(fid,kind,data) 4 | % 5 | % Writes a double-precision floating point tag to a fif file 6 | % 7 | % fid An open fif file descriptor 8 | % kind Tag kind 9 | % data The data 10 | % 11 | 12 | % 13 | % Author : Matti Hamalainen, MGH Martinos Center 14 | % License : BSD 3-clause 15 | % 16 | % Revision 1.2 2006/05/26 07:17:17 msh 17 | % The me variable was set incorrectly 18 | % 19 | % Revision 1.1 2006/04/27 22:38:37 msh 20 | % Splitting an empty list now results in an empty output. 21 | % Added fiff_write_double and fiff_write_short 22 | % Write an array of floats, ints, and doubles instead of just one value. 23 | % Fixed help text of fiff_pick_channels. 24 | % 25 | % 26 | 27 | me='MNE:fiff_write_double'; 28 | 29 | if nargin ~= 3 30 | error(me,'Incorrect number of arguments'); 31 | end 32 | 33 | FIFFT_DOUBLE=5; 34 | FIFFV_NEXT_SEQ=0; 35 | nel=numel(data); 36 | datasize=nel*8; 37 | count = fwrite(fid,int32(kind),'int32'); 38 | if count ~= 1 39 | error(me,'write failed'); 40 | end 41 | count = fwrite(fid,int32(FIFFT_DOUBLE),'int32'); 42 | if count ~= 1 43 | error(me,'write failed'); 44 | end 45 | count = fwrite(fid,int32(datasize),'int32'); 46 | if count ~= 1 47 | error(me,'write failed'); 48 | end 49 | count = fwrite(fid,int32(FIFFV_NEXT_SEQ),'int32'); 50 | if count ~= 1 51 | error(me,'write failed'); 52 | end 53 | count = fwrite(fid,double(data),'double'); 54 | if count ~= nel 55 | error(me,'write failed'); 56 | end 57 | 58 | return; 59 | -------------------------------------------------------------------------------- /matlab/fiff_write_double_complex.m: -------------------------------------------------------------------------------- 1 | function fiff_write_double_complex(fid,kind,data) 2 | % 3 | % fiff_write_double_complex(fid,kind,data) 4 | % 5 | % Writes a double-precision complex tag to a fif file 6 | % 7 | % fid An open fif file descriptor 8 | % kind Tag kind 9 | % data The data 10 | % 11 | 12 | % 13 | % Author : Matti Hamalainen, MGH Martinos Center 14 | % License : BSD 3-clause 15 | % 16 | % Revision 1.1 2006/09/23 14:43:56 msh 17 | % Added routines for writing complex and double complex matrices. 18 | % Added routine for writing double-precision real matrix. 19 | % 20 | % 21 | 22 | me='MNE:fiff_write_double_complex'; 23 | 24 | if nargin ~= 3 25 | error(me,'Incorrect number of arguments'); 26 | end 27 | 28 | FIFFT_COMPLEX_DOUBLE=21; 29 | FIFFV_NEXT_SEQ=0; 30 | nel=numel(data); 31 | datasize=2*nel*8; 32 | count = fwrite(fid,int32(kind),'int32'); 33 | if count ~= 1 34 | error(me,'write failed'); 35 | end 36 | count = fwrite(fid,int32(FIFFT_COMPLEX_DOUBLE),'int32'); 37 | if count ~= 1 38 | error(me,'write failed'); 39 | end 40 | count = fwrite(fid,int32(datasize),'int32'); 41 | if count ~= 1 42 | error(me,'write failed'); 43 | end 44 | count = fwrite(fid,int32(FIFFV_NEXT_SEQ),'int32'); 45 | if count ~= 1 46 | error(me,'write failed'); 47 | end 48 | data = reshape([real(data(:)) imag(data(:))].', [], 1); 49 | count = fwrite(fid, data, 'double'); 50 | if count ~= nel*2 51 | error(me, 'write failed'); 52 | end 53 | return; 54 | 55 | -------------------------------------------------------------------------------- /matlab/fiff_write_double_complex_matrix.m: -------------------------------------------------------------------------------- 1 | function fiff_write_double_complex_matrix(fid,kind,mat) 2 | % 3 | % fiff_write_double_complex_matrix(fid,kind,mat) 4 | % 5 | % Writes a double-precision complex matrix tag 6 | % 7 | % fid An open fif file descriptor 8 | % kind The tag kind 9 | % mat The data matrix 10 | % 11 | 12 | % 13 | % 14 | % Author : Matti Hamalainen, MGH Martinos Center 15 | % License : BSD 3-clause 16 | % 17 | % Revision 1.1 2006/09/23 14:43:56 msh 18 | % Added routines for writing complex and double complex matrices. 19 | % Added routine for writing double-precision real matrix. 20 | % 21 | % 22 | 23 | me='MNE:fiff_write_double_complex_matrix'; 24 | 25 | if nargin ~= 3 26 | error(me,'Incorrect number of arguments'); 27 | end 28 | 29 | ndim = ndims(mat); 30 | if ndim < 2 || ndim > 3 31 | error(me,'Input should be a two-dimensional or three-dimensional matrix'); 32 | end 33 | 34 | FIFFT_COMPLEX_DOUBLE = 21; 35 | FIFFT_MATRIX = bitshift(1,30); 36 | FIFFT_MATRIX_COMPLEX_DOUBLE = bitor(FIFFT_COMPLEX_DOUBLE,FIFFT_MATRIX); 37 | FIFFV_NEXT_SEQ=0; 38 | 39 | datasize = 2*8*numel(mat) + 4*(ndim+1); 40 | 41 | count = fwrite(fid,int32(kind),'int32'); 42 | if count ~= 1 43 | error(me,'write failed'); 44 | end 45 | count = fwrite(fid,int32(FIFFT_MATRIX_COMPLEX_DOUBLE),'int32'); 46 | if count ~= 1 47 | error(me,'write failed'); 48 | end 49 | count = fwrite(fid,int32(datasize),'int32'); 50 | if count ~= 1 51 | error(me,'write failed'); 52 | end 53 | count = fwrite(fid,int32(FIFFV_NEXT_SEQ),'int32'); 54 | if count ~= 1 55 | error(me,'write failed'); 56 | end 57 | % nrow = size(mat,1); 58 | % ncol = size(mat,2); 59 | % for j = 1:nrow 60 | % for k = 1:ncol 61 | % count = fwrite(fid,real(mat(j,k)),'double'); 62 | % if count ~= 1 63 | % error(me,'write failed'); 64 | % end 65 | % count = fwrite(fid,imag(mat(j,k)),'double'); 66 | % if count ~= 1 67 | % error(me,'write failed'); 68 | % end 69 | % end 70 | % end 71 | 72 | % the matrix is written last dimension first, and then each element's real part followed by its imaginary part 73 | siz = size(mat); 74 | mat = permute(mat,ndim:-1:1); 75 | mat = [real(mat(:)) imag(mat(:))]'; 76 | count = fwrite(fid,double(mat(:)),'double'); 77 | if count ~= numel(mat) 78 | error(me,'write failed'); 79 | end 80 | if ndim==2 81 | dims(1) = siz(2); 82 | dims(2) = siz(1); 83 | dims(3) = 2; 84 | count = fwrite(fid,int32(dims),'int32'); 85 | if count ~= 3 86 | error(me,'write failed'); 87 | end 88 | elseif ndim==3 89 | dims(1) = siz(3); 90 | dims(2) = siz(2); 91 | dims(3) = siz(1); 92 | dims(4) = 3; 93 | count = fwrite(fid,int32(dims),'int32'); 94 | if count ~= 4 95 | error(me,'write failed'); 96 | end 97 | end 98 | 99 | return; 100 | 101 | -------------------------------------------------------------------------------- /matlab/fiff_write_double_matrix.m: -------------------------------------------------------------------------------- 1 | function fiff_write_double_matrix(fid,kind,mat) 2 | % 3 | % fiff_write_double_matrix(fid,kind,mat) 4 | % 5 | % Writes a double-precision floating-point matrix tag 6 | % 7 | % fid An open fif file descriptor 8 | % kind The tag kind 9 | % mat The data matrix 10 | % 11 | 12 | % 13 | % 14 | % Author : Matti Hamalainen, MGH Martinos Center 15 | % License : BSD 3-clause 16 | % 17 | % Revision 1.1 2006/09/23 14:43:56 msh 18 | % Added routines for writing complex and double complex matrices. 19 | % Added routine for writing double-precision real matrix. 20 | % 21 | % 22 | 23 | me='MNE:fiff_write_double_matrix'; 24 | 25 | if nargin ~= 3 26 | error(me,'Incorrect number of arguments'); 27 | end 28 | 29 | ndim = ndims(mat); 30 | if ndim < 2 || ndim > 3 31 | error(me,'Input should be a two-dimensional or three-dimensional matrix'); 32 | end 33 | 34 | FIFFT_DOUBLE = 5; 35 | FIFFT_MATRIX = bitshift(1,30); 36 | FIFFT_MATRIX_DOUBLE = bitor(FIFFT_DOUBLE,FIFFT_MATRIX); 37 | FIFFV_NEXT_SEQ=0; 38 | 39 | datasize = 8*numel(mat) + 4*(ndim+1); 40 | 41 | count = fwrite(fid,int32(kind),'int32'); 42 | if count ~= 1 43 | error(me,'write failed'); 44 | end 45 | count = fwrite(fid,int32(FIFFT_MATRIX_DOUBLE),'int32'); 46 | if count ~= 1 47 | error(me,'write failed'); 48 | end 49 | count = fwrite(fid,int32(datasize),'int32'); 50 | if count ~= 1 51 | error(me,'write failed'); 52 | end 53 | count = fwrite(fid,int32(FIFFV_NEXT_SEQ),'int32'); 54 | if count ~= 1 55 | error(me,'write failed'); 56 | end 57 | count = fwrite(fid,double(permute(mat,ndim:-1:1)),'double'); 58 | if count ~= numel(mat) 59 | error(me,'write failed'); 60 | end 61 | if ndim==2 62 | dims(1) = size(mat,2); 63 | dims(2) = size(mat,1); 64 | dims(3) = 2; 65 | count = fwrite(fid,int32(dims),'int32'); 66 | if count ~= 3 67 | error(me,'write failed'); 68 | end 69 | elseif ndim==3 70 | dims(1) = size(mat,3); 71 | dims(2) = size(mat,2); 72 | dims(3) = size(mat,1); 73 | dims(4) = 3; 74 | count = fwrite(fid,int32(dims),'int32'); 75 | if count ~= 4 76 | error(me,'write failed'); 77 | end 78 | end 79 | 80 | return; 81 | 82 | -------------------------------------------------------------------------------- /matlab/fiff_write_events.m: -------------------------------------------------------------------------------- 1 | function fiff_write_events(filename,eventlist,mappings) 2 | % 3 | % fiff_write_events(filename,eventlist,mappings) 4 | % 5 | % Write an event list into a fif file, and include an optional description 6 | % of the event ids. This function has been adjusted by Jan Mathijs Schoffelen 7 | % from mne_write_events, with the intention to make a writing function that 8 | % is symmetric in its behavior w.r.t. fiff_read_events (which can read the 9 | % mappings). The filename argument can be a string, or a file identifier to 10 | % an open (for writing) fif-file. 11 | % 12 | 13 | % 14 | % Author : Matti Hamalainen, MGH Martinos Center 15 | % License : BSD 3-clause 16 | % 17 | % Revision 1.2 2008/10/31 13:07:05 msh 18 | % Added mne_make_combined_event_file function 19 | % 20 | % Revision 1.1 2008/06/16 17:27:50 msh 21 | % Added mne_read_events and mne_write_events functions 22 | % 23 | 24 | global FIFF; 25 | if isempty(FIFF) 26 | FIFF = fiff_define_constants(); 27 | end 28 | 29 | me = 'MNE:fiff_write_events'; 30 | 31 | if nargin < 3 32 | mappings = ''; 33 | end 34 | 35 | eventlist = reshape(eventlist',numel(eventlist),1); 36 | % 37 | % Start writing... 38 | % 39 | if ischar(filename) 40 | fid = fiff_start_file(filename); 41 | else 42 | % assume the supplied filename to be a file identifier to an open file 43 | fid = filename; 44 | end 45 | 46 | fiff_start_block(fid,FIFF.FIFFB_MNE_EVENTS); 47 | 48 | fiff_write_int(fid,FIFF.FIFF_MNE_EVENT_LIST,eventlist); 49 | if ~isempty(mappings) 50 | % write a string that describes the mappings for numbers to a possibly 51 | % more informative string 52 | fiff_write_string(fid,FIFF.FIFF_DESCRIPTION,mappings); 53 | end 54 | 55 | fiff_end_block(fid,FIFF.FIFFB_MNE_EVENTS); 56 | 57 | if ischar(filename) 58 | fiff_end_file(fid); 59 | end 60 | -------------------------------------------------------------------------------- /matlab/fiff_write_float.m: -------------------------------------------------------------------------------- 1 | function fiff_write_float(fid,kind,data) 2 | % 3 | % fiff_write_float(fid,kind,data) 4 | % 5 | % Writes a single-precision floating point tag to a fif file 6 | % 7 | % fid An open fif file descriptor 8 | % kind Tag kind 9 | % data The data 10 | % 11 | 12 | % 13 | % Author : Matti Hamalainen, MGH Martinos Center 14 | % License : BSD 3-clause 15 | % 16 | % Revision 1.3 2006/04/27 22:38:37 msh 17 | % Splitting an empty list now results in an empty output. 18 | % Added fiff_write_double and fiff_write_short 19 | % Write an array of floats, ints, and doubles instead of just one value. 20 | % Fixed help text of fiff_pick_channels. 21 | % 22 | % Revision 1.2 2006/04/23 15:29:40 msh 23 | % Added MGH to the copyright 24 | % 25 | % Revision 1.1 2006/04/10 23:26:54 msh 26 | % Added fiff reading routines 27 | % 28 | % 29 | 30 | me='MNE:fiff_write_float'; 31 | 32 | if nargin ~= 3 33 | error(me,'Incorrect number of arguments'); 34 | end 35 | 36 | FIFFT_FLOAT=4; 37 | FIFFV_NEXT_SEQ=0; 38 | nel=numel(data); 39 | datasize=nel*4; 40 | count = fwrite(fid,int32(kind),'int32'); 41 | if count ~= 1 42 | error(me,'write failed'); 43 | end 44 | count = fwrite(fid,int32(FIFFT_FLOAT),'int32'); 45 | if count ~= 1 46 | error(me,'write failed'); 47 | end 48 | count = fwrite(fid,int32(datasize),'int32'); 49 | if count ~= 1 50 | error(me,'write failed'); 51 | end 52 | count = fwrite(fid,int32(FIFFV_NEXT_SEQ),'int32'); 53 | if count ~= 1 54 | error(me,'write failed'); 55 | end 56 | count = fwrite(fid,single(data),'single'); 57 | if count ~= nel 58 | error(me,'write failed'); 59 | end 60 | 61 | return; 62 | 63 | -------------------------------------------------------------------------------- /matlab/fiff_write_float_matrix.m: -------------------------------------------------------------------------------- 1 | function fiff_write_float_matrix(fid,kind,mat) 2 | % 3 | % fiff_write_float_matrix(fid,kind,mat) 4 | % 5 | % Writes a single-precision floating-point matrix tag 6 | % 7 | % fid An open fif file descriptor 8 | % kind The tag kind 9 | % mat The data matrix 10 | % 11 | 12 | % 13 | % 14 | % Author : Matti Hamalainen, MGH Martinos Center 15 | % License : BSD 3-clause 16 | % 17 | % Revision 1.3 2006/04/23 15:29:40 msh 18 | % Added MGH to the copyright 19 | % 20 | % Revision 1.2 2006/04/10 23:26:54 msh 21 | % Added fiff reading routines 22 | % 23 | % Revision 1.1 2005/12/05 16:01:04 msh 24 | % Added an initial set of fiff writing routines. 25 | % 26 | % 27 | 28 | me='MNE:fiff_write_float_matrix'; 29 | 30 | if nargin ~= 3 31 | error(me,'Incorrect number of arguments'); 32 | end 33 | 34 | ndim = ndims(mat); 35 | if ndim < 2 || ndim > 3 36 | error(me,'Input should be a two-dimensional or three-dimensional matrix'); 37 | end 38 | 39 | FIFFT_FLOAT = 4; 40 | FIFFT_MATRIX = bitshift(1,30); 41 | FIFFT_MATRIX_FLOAT = bitor(FIFFT_FLOAT,FIFFT_MATRIX); 42 | FIFFV_NEXT_SEQ=0; 43 | 44 | datasize = 4*numel(mat) + 4*(ndim+1); 45 | 46 | count = fwrite(fid,int32(kind),'int32'); 47 | if count ~= 1 48 | error(me,'write failed'); 49 | end 50 | count = fwrite(fid,int32(FIFFT_MATRIX_FLOAT),'int32'); 51 | if count ~= 1 52 | error(me,'write failed'); 53 | end 54 | count = fwrite(fid,int32(datasize),'int32'); 55 | if count ~= 1 56 | error(me,'write failed'); 57 | end 58 | count = fwrite(fid,int32(FIFFV_NEXT_SEQ),'int32'); 59 | if count ~= 1 60 | error(me,'write failed'); 61 | end 62 | count = fwrite(fid,single(permute(mat,ndim:-1:1)),'single'); 63 | if count ~= numel(mat) 64 | error(me,'write failed'); 65 | end 66 | if ndim==2 67 | dims(1) = size(mat,2); 68 | dims(2) = size(mat,1); 69 | dims(3) = 2; 70 | count = fwrite(fid,int32(dims),'int32'); 71 | if count ~= 3 72 | error(me,'write failed'); 73 | end 74 | elseif ndim==3 75 | dims(1) = size(mat,3); 76 | dims(2) = size(mat,2); 77 | dims(3) = size(mat,1); 78 | dims(4) = 3; 79 | count = fwrite(fid,int32(dims),'int32'); 80 | if count ~= 4 81 | error(me,'write failed'); 82 | end 83 | end 84 | 85 | return; 86 | 87 | -------------------------------------------------------------------------------- /matlab/fiff_write_float_sparse_ccs.m: -------------------------------------------------------------------------------- 1 | function fiff_write_float_sparse_ccs(fid,kind,mat) 2 | % 3 | % fiff_write_float_sparsce_ccs(fid,kind,mat) 4 | % 5 | % Writes a single-precision sparse (ccs) floating-point matrix tag 6 | % 7 | % fid An open fif file descriptor 8 | % kind The tag kind 9 | % mat The data matrix 10 | % 11 | 12 | % 13 | % Author : Matti Hamalainen, MGH Martinos Center 14 | % License : BSD 3-clause 15 | % 16 | 17 | me='MNE:fiff_write_float_sparse_ccs'; 18 | 19 | if nargin ~= 3 20 | error(me,'Incorrect number of arguments'); 21 | end 22 | 23 | if ~issparse(mat) 24 | error(me,'Input should be a sparse matrix'); 25 | end 26 | 27 | if length(size(mat)) ~= 2 28 | error(me,'Input should be a two-dimensional matrix'); 29 | end 30 | 31 | FIFFT_FLOAT = 4; 32 | FIFFT_MATRIX = bitshift(16400,16); % 4010 33 | FIFFT_MATRIX_FLOAT_CCS = bitor(FIFFT_FLOAT,FIFFT_MATRIX); 34 | FIFFV_NEXT_SEQ=0; 35 | % 36 | % nnz values 37 | % nnz row indices 38 | % ncol+1 pointers 39 | % dims 40 | % nnz 41 | % ndim 42 | % 43 | nnzm = nnz(mat); 44 | ncol = size(mat,2); 45 | datasize = 4*nnzm + 4*nnzm + 4*(ncol+1) + 4*4; 46 | % 47 | % Nonzero entries 48 | % 49 | [ s(:,1), s(:,2), s(:,3) ] = find(mat); 50 | s = sortrows(s,2); 51 | [ cols, starts ] = unique(s(:,2),'first'); 52 | 53 | count = fwrite(fid,int32(kind),'int32'); 54 | if count ~= 1 55 | error(me,'write failed'); 56 | end 57 | count = fwrite(fid,int32(FIFFT_MATRIX_FLOAT_CCS),'int32'); 58 | if count ~= 1 59 | error(me,'write failed'); 60 | end 61 | count = fwrite(fid,int32(datasize),'int32'); 62 | if count ~= 1 63 | error(me,'write failed'); 64 | end 65 | count = fwrite(fid,int32(FIFFV_NEXT_SEQ),'int32'); 66 | if count ~= 1 67 | error(me,'write failed'); 68 | end 69 | % 70 | % The data values 71 | % 72 | count = fwrite(fid,single(s(:,3)),'single'); 73 | if count ~= nnzm 74 | error(me,'write failed'); 75 | end 76 | % 77 | % Row indices 78 | % 79 | count = fwrite(fid,int32(s(:,1)-1),'int32'); 80 | if count ~= nnzm 81 | error(me,'write failed'); 82 | end 83 | % 84 | % Pointers 85 | % 86 | ptrs = -ones(1,ncol+1); 87 | for k = 1:length(cols) 88 | ptrs(cols(k)) = starts(k) - 1; 89 | end 90 | ptrs(ncol+1) = nnzm; 91 | % 92 | % Fill in pointers for empty columns 93 | % 94 | for k = ncol:-1:1 95 | if ptrs(k) < 0 96 | ptrs(k) = ptrs(k+1); 97 | end 98 | end 99 | % 100 | count = fwrite(fid,int32(ptrs),'int32'); 101 | if count ~= ncol+1 102 | error(me,'write failed'); 103 | end 104 | % 105 | % Dimensions 106 | % 107 | dims(1) = nnz(mat); 108 | dims(2) = size(mat,1); 109 | dims(3) = size(mat,2); 110 | dims(4) = 2; 111 | count = fwrite(fid,int32(dims),'int32'); 112 | if count ~= 4 113 | error(me,'write failed'); 114 | end 115 | 116 | return; 117 | 118 | -------------------------------------------------------------------------------- /matlab/fiff_write_float_sparse_rcs.m: -------------------------------------------------------------------------------- 1 | function fiff_write_float_sparse_rcs(fid,kind,mat) 2 | % 3 | % fiff_write_float_sparsce_rcs(fid,kind,mat) 4 | % 5 | % Writes a single-precision sparse (RCS) floating-point matrix tag 6 | % 7 | % fid An open fif file descriptor 8 | % kind The tag kind 9 | % mat The data matrix 10 | % 11 | 12 | % 13 | % Author : Matti Hamalainen, MGH Martinos Center 14 | % License : BSD 3-clause 15 | % 16 | 17 | me='MNE:fiff_write_float_sparse_rcs'; 18 | 19 | if nargin ~= 3 20 | error(me,'Incorrect number of arguments'); 21 | end 22 | 23 | if ~issparse(mat) 24 | error(me,'Input should be a sparse matrix'); 25 | end 26 | 27 | if length(size(mat)) ~= 2 28 | error(me,'Input should be a two-dimensional matrix'); 29 | end 30 | 31 | FIFFT_FLOAT = 4; 32 | FIFFT_MATRIX = bitshift(16416,16); % 4020 33 | FIFFT_MATRIX_FLOAT_RCS = bitor(FIFFT_FLOAT,FIFFT_MATRIX); 34 | FIFFV_NEXT_SEQ=0; 35 | % 36 | % nnz values 37 | % nnz column indices 38 | % nrow+1 pointers 39 | % dims 40 | % nnz 41 | % ndim 42 | % 43 | nnzm = nnz(mat); 44 | nrow = size(mat,1); 45 | datasize = 4*nnzm + 4*nnzm + 4*(nrow+1) + 4*4; 46 | % 47 | % Nonzero entries 48 | % 49 | [ s(:,1), s(:,2), s(:,3) ] = find(mat); 50 | s = sortrows(s,1); 51 | [ rows, starts ] = unique(s(:,1),'first'); 52 | 53 | count = fwrite(fid,int32(kind),'int32'); 54 | if count ~= 1 55 | error(me,'write failed'); 56 | end 57 | count = fwrite(fid,int32(FIFFT_MATRIX_FLOAT_RCS),'int32'); 58 | if count ~= 1 59 | error(me,'write failed'); 60 | end 61 | count = fwrite(fid,int32(datasize),'int32'); 62 | if count ~= 1 63 | error(me,'write failed'); 64 | end 65 | count = fwrite(fid,int32(FIFFV_NEXT_SEQ),'int32'); 66 | if count ~= 1 67 | error(me,'write failed'); 68 | end 69 | % 70 | % The data values 71 | % 72 | count = fwrite(fid,single(s(:,3)),'single'); 73 | if count ~= nnzm 74 | error(me,'write failed'); 75 | end 76 | % 77 | % Column indices 78 | % 79 | count = fwrite(fid,int32(s(:,2)-1),'int32'); 80 | if count ~= nnzm 81 | error(me,'write failed'); 82 | end 83 | % 84 | % Pointers 85 | % 86 | ptrs = -ones(1,nrow+1); 87 | for k = 1:length(rows) 88 | ptrs(rows(k)) = starts(k) - 1; 89 | end 90 | ptrs(nrow+1) = nnzm; 91 | % 92 | % Fill in pointers for empty rows 93 | % 94 | for k = nrow:-1:1 95 | if ptrs(k) < 0 96 | ptrs(k) = ptrs(k+1); 97 | end 98 | end 99 | % 100 | count = fwrite(fid,int32(ptrs),'int32'); 101 | if count ~= nrow+1 102 | error(me,'write failed'); 103 | end 104 | % 105 | % Dimensions 106 | % 107 | dims(1) = nnz(mat); 108 | dims(2) = size(mat,1); 109 | dims(3) = size(mat,2); 110 | dims(4) = 2; 111 | count = fwrite(fid,int32(dims),'int32'); 112 | if count ~= 4 113 | error(me,'write failed'); 114 | end 115 | 116 | return; 117 | 118 | -------------------------------------------------------------------------------- /matlab/fiff_write_id.m: -------------------------------------------------------------------------------- 1 | function fiff_write_id(fid,kind,id) 2 | % 3 | % fiff_write_id(fid,kind,id) 4 | % 5 | % Writes fiff id 6 | % 7 | % fid An open fif file descriptor 8 | % kind The tag kind 9 | % id The id to write 10 | % 11 | % If the id argument is missing it will be generated here 12 | % 13 | 14 | % 15 | % Author : Matti Hamalainen, MGH Martinos Center 16 | % License : BSD 3-clause 17 | % 18 | % Revision 1.6 2009/03/30 11:37:37 msh 19 | % Added copying of measurement info blocks from the original like in mne_browse_raw 20 | % 21 | % Revision 1.5 2008/06/16 17:27:11 msh 22 | % Incremented file version number to 1.2 23 | % 24 | % Revision 1.4 2006/04/23 15:29:40 msh 25 | % Added MGH to the copyright 26 | % 27 | % Revision 1.3 2006/04/14 11:03:57 msh 28 | % Changed fiff_write_id write a given id. 29 | % Added parent id writing. 30 | % 31 | % Revision 1.2 2006/04/10 23:26:54 msh 32 | % Added fiff reading routines 33 | % 34 | % Revision 1.1 2005/12/05 16:01:04 msh 35 | % Added an initial set of fiff writing routines. 36 | % 37 | % 38 | me='MNE:fiff_write_id'; 39 | 40 | if nargin == 2 41 | timezone=5; % Matlab does not know the timezone 42 | id.version = bitor(bitshift(1,16),2); % Version (1 << 16) | 2 43 | id.machid(1) = 65536*rand(1); % Machine id is random for now 44 | id.machid(2) = 65536*rand(1); % Machine id is random for now 45 | id.secs = 3600*(24*(now-datenum(1970,1,1,0,0,0))+timezone); 46 | id.usecs = 0; % Do not know how we could get this 47 | elseif nargin ~= 3 48 | error(me,'Incorrect number of arguments'); 49 | end 50 | 51 | FIFFT_ID_STRUCT=31; 52 | FIFFV_NEXT_SEQ=0; 53 | % 54 | % 55 | datasize=5*4; % The id comprises five integers 56 | count = fwrite(fid,int32(kind),'int32'); 57 | if count ~= 1 58 | error(me,'write failed'); 59 | end 60 | count = fwrite(fid,int32(FIFFT_ID_STRUCT),'int32'); 61 | if count ~= 1 62 | error(me,'write failed'); 63 | end 64 | count = fwrite(fid,int32(datasize),'int32'); 65 | if count ~= 1 66 | error(me,'write failed'); 67 | end 68 | count = fwrite(fid,int32(FIFFV_NEXT_SEQ),'int32'); 69 | if count ~= 1 70 | error(me,'write failed'); 71 | end 72 | % 73 | % Collect the bits together for one write 74 | % 75 | data(1) = id.version; 76 | data(2) = id.machid(1); 77 | data(3) = id.machid(2); 78 | data(4) = id.secs; 79 | data(5) = id.usecs; 80 | count = fwrite(fid,int32(data),'int32'); 81 | if count ~= 5 82 | error(me,'write failed'); 83 | end 84 | return; 85 | 86 | -------------------------------------------------------------------------------- /matlab/fiff_write_int.m: -------------------------------------------------------------------------------- 1 | function fiff_write_int(fid,kind,data) 2 | % 3 | % fiff_write_int(fid,kind,data) 4 | % 5 | % Writes a 32-bit integer tag to a fif file 6 | % 7 | % fid An open fif file descriptor 8 | % kind Tag kind 9 | % data The integers to use as data 10 | % 11 | 12 | % 13 | % Author : Matti Hamalainen, MGH Martinos Center 14 | % License : BSD 3-clause 15 | % 16 | % Revision 1.3 2006/04/27 22:38:37 msh 17 | % Splitting an empty list now results in an empty output. 18 | % Added fiff_write_double and fiff_write_short 19 | % Write an array of floats, ints, and doubles instead of just one value. 20 | % Fixed help text of fiff_pick_channels. 21 | % 22 | % Revision 1.2 2006/04/23 15:29:40 msh 23 | % Added MGH to the copyright 24 | % 25 | % Revision 1.1 2006/04/10 23:26:54 msh 26 | % Added fiff reading routines 27 | % 28 | % 29 | 30 | me='MNE:fiff_write_int'; 31 | 32 | if nargin ~= 3 33 | error(me,'Incorrect number of arguments'); 34 | end 35 | 36 | FIFFT_INT=3; 37 | FIFFV_NEXT_SEQ=0; 38 | nel=numel(data); 39 | datasize=nel*4; 40 | count = fwrite(fid,int32(kind),'int32'); 41 | if count ~= 1 42 | error(me,'write failed'); 43 | end 44 | count = fwrite(fid,int32(FIFFT_INT),'int32'); 45 | if count ~= 1 46 | error(me,'write failed'); 47 | end 48 | count = fwrite(fid,int32(datasize),'int32'); 49 | if count ~= 1 50 | error(me,'write failed'); 51 | end 52 | count = fwrite(fid,int32(FIFFV_NEXT_SEQ),'int32'); 53 | if count ~= 1 54 | error(me,'write failed'); 55 | end 56 | count = fwrite(fid,int32(data),'int32'); 57 | if count ~= nel 58 | error(me,'write failed'); 59 | end 60 | 61 | return; 62 | -------------------------------------------------------------------------------- /matlab/fiff_write_int_matrix.m: -------------------------------------------------------------------------------- 1 | function fiff_write_int_matrix(fid,kind,mat) 2 | % 3 | % fiff_write_int_matrix(fid,kind,mat) 4 | % 5 | % Writes a integer matrix tag 6 | % 7 | % fid An open fif file descriptor 8 | % kind The tag kind 9 | % mat The data matrix 10 | % 11 | 12 | % 13 | % 14 | % Author : Matti Hamalainen, MGH Martinos Center 15 | % License : BSD 3-clause 16 | % 17 | % 18 | % $Id$ 19 | 20 | % 21 | % 22 | 23 | me='MNE:fiff_write_int_matrix'; 24 | 25 | if nargin ~= 3 26 | error(me,'Incorrect number of arguments'); 27 | end 28 | 29 | if length(size(mat)) ~= 2 30 | error(me,'Input should be a two-dimensional matrix'); 31 | end 32 | 33 | FIFFT_INT = 3; 34 | FIFFT_MATRIX = bitshift(1,30); 35 | FIFFT_MATRIX_INT = bitor(FIFFT_INT,FIFFT_MATRIX); 36 | FIFFV_NEXT_SEQ=0; 37 | 38 | datasize = 4*numel(mat) + 4*3; 39 | 40 | count = fwrite(fid,int32(kind),'int32'); 41 | if count ~= 1 42 | error(me,'write failed'); 43 | end 44 | count = fwrite(fid,int32(FIFFT_MATRIX_INT),'int32'); 45 | if count ~= 1 46 | error(me,'write failed'); 47 | end 48 | count = fwrite(fid,int32(datasize),'int32'); 49 | if count ~= 1 50 | error(me,'write failed'); 51 | end 52 | count = fwrite(fid,int32(FIFFV_NEXT_SEQ),'int32'); 53 | if count ~= 1 54 | error(me,'write failed'); 55 | end 56 | count = fwrite(fid,int32(mat'),'int32'); 57 | if count ~= numel(mat) 58 | error(me,'write failed'); 59 | end 60 | dims(1) = size(mat,2); 61 | dims(2) = size(mat,1); 62 | dims(3) = 2; 63 | count = fwrite(fid,int32(dims),'int32'); 64 | if count ~= 3 65 | error(me,'write failed'); 66 | end 67 | 68 | return; 69 | 70 | -------------------------------------------------------------------------------- /matlab/fiff_write_name_list.m: -------------------------------------------------------------------------------- 1 | function fiff_write_name_list(fid,kind,data) 2 | % 3 | % fiff_write_name_list(fid,kind,mat) 4 | % 5 | % Writes a colon-separated list of names 6 | % 7 | % fid An open fif file descriptor 8 | % kind The tag kind 9 | % data An array of names to create the list from 10 | % 11 | 12 | % 13 | % Author : Matti Hamalainen, MGH Martinos Center 14 | % License : BSD 3-clause 15 | % 16 | % Revision 1.4 2006/04/23 15:29:40 msh 17 | % Added MGH to the copyright 18 | % 19 | % Revision 1.3 2006/04/12 10:29:02 msh 20 | % Made evoked data writing compatible with the structures returned in reading. 21 | % 22 | % Revision 1.2 2006/04/10 23:26:54 msh 23 | % Added fiff reading routines 24 | % 25 | % Revision 1.1 2005/12/05 16:01:04 msh 26 | % Added an initial set of fiff writing routines. 27 | % 28 | % 29 | 30 | 31 | me='MNE:fiff_write_name_list'; 32 | 33 | if nargin ~= 3 34 | error(me,'Incorrect number of arguments'); 35 | end 36 | 37 | all=data{1}; 38 | for k = 2:size(data,2) 39 | all=strcat(all,':',data{k}); 40 | end 41 | fiff_write_string(fid,kind,all); 42 | 43 | 44 | -------------------------------------------------------------------------------- /matlab/fiff_write_named_matrix.m: -------------------------------------------------------------------------------- 1 | function fiff_write_named_matrix(fid,kind,mat) 2 | % 3 | % fiff_write_named_matrix(fid,kind,mat) 4 | % 5 | % Writes a named single-precision floating-point matrix 6 | % 7 | % fid An open fif file descriptor 8 | % kind The tag kind to use for the data 9 | % mat The data matrix 10 | % 11 | 12 | % 13 | % 14 | % Author : Matti Hamalainen, MGH Martinos Center 15 | % License : BSD 3-clause 16 | % 17 | % Revision 1.5 2006/04/23 15:29:40 msh 18 | % Added MGH to the copyright 19 | % 20 | % Revision 1.4 2006/04/12 10:29:03 msh 21 | % Made evoked data writing compatible with the structures returned in reading. 22 | % 23 | % Revision 1.3 2006/04/10 23:26:54 msh 24 | % Added fiff reading routines 25 | % 26 | % Revision 1.2 2005/12/05 20:23:21 msh 27 | % Added fiff_save_evoked. Improved error handling. 28 | % 29 | % Revision 1.1 2005/12/05 16:01:04 msh 30 | % Added an initial set of fiff writing routines. 31 | % 32 | % 33 | 34 | global FIFF; 35 | if isempty(FIFF) 36 | FIFF = fiff_define_constants(); 37 | end 38 | 39 | me='MNE:fiff_write_named_matrix'; 40 | 41 | if nargin ~= 3 42 | error(me,'Incorrect number of arguments'); 43 | end 44 | 45 | fiff_start_block(fid,FIFF.FIFFB_MNE_NAMED_MATRIX); 46 | fiff_write_int(fid,FIFF.FIFF_MNE_NROW,mat.nrow); 47 | fiff_write_int(fid,FIFF.FIFF_MNE_NCOL,mat.ncol); 48 | if size(mat.row_names,2) > 0 49 | fiff_write_name_list(fid,FIFF.FIFF_MNE_ROW_NAMES,mat.row_names); 50 | end 51 | if size(mat.col_names,2) > 0 52 | fiff_write_name_list(fid,FIFF.FIFF_MNE_COL_NAMES,mat.col_names); 53 | end 54 | fiff_write_float_matrix(fid,kind,mat.data); 55 | fiff_end_block(fid,FIFF.FIFFB_MNE_NAMED_MATRIX); 56 | 57 | return; 58 | 59 | -------------------------------------------------------------------------------- /matlab/fiff_write_proj.m: -------------------------------------------------------------------------------- 1 | function fiff_write_proj(fid,projs,ch_rename) 2 | % 3 | % fiff_write_proj(fid,projs,ch_rename) 4 | % 5 | % Writes the projection data into a fif file 6 | % 7 | % fid An open fif file descriptor 8 | % projs The compensation data to write 9 | % ch_rename Short-to-long channel name mapping 10 | % 11 | 12 | % 13 | % Author : Matti Hamalainen, MGH Martinos Center 14 | % License : BSD 3-clause 15 | % 16 | % Revision 1.9 2008/08/06 17:31:11 msh 17 | % Removed some debug output 18 | % 19 | % Revision 1.8 2008/08/06 17:29:55 msh 20 | % Fixed missing end in writing the projection time. 21 | % 22 | % Revision 1.7 2008/05/09 11:02:09 msh 23 | % Added FIFF_PROJ_ITEM_TIME for Neuromag compatibility 24 | % 25 | % Revision 1.6 2008/05/06 20:40:56 msh 26 | % Fixed ordering of output for compatibility with maxfilter averager 27 | % 28 | % Revision 1.5 2006/05/03 18:53:05 msh 29 | % Approaching Matlab 6.5 backward compatibility 30 | % 31 | % Revision 1.4 2006/04/23 15:29:40 msh 32 | % Added MGH to the copyright 33 | % 34 | % Revision 1.3 2006/04/18 20:44:46 msh 35 | % Added reading of forward solution. 36 | % Use length instead of size when appropriate 37 | % 38 | % Revision 1.2 2006/04/12 10:51:18 msh 39 | % Added projection writing and compensation routines 40 | % 41 | % Revision 1.1 2006/04/12 10:29:03 msh 42 | % Made evoked data writing compatible with the structures returned in reading. 43 | % 44 | % 45 | 46 | me='MNE:fiff_write_proj'; 47 | 48 | if nargin == 2 49 | ch_rename = {}; 50 | elseif nargin ~= 3 51 | error(me,'Incorrect number of arguments'); 52 | end 53 | 54 | global FIFF; 55 | if isempty(FIFF) 56 | FIFF = fiff_define_constants(); 57 | end 58 | 59 | if isempty(projs) 60 | return; 61 | end 62 | 63 | fiff_start_block(fid,FIFF.FIFFB_PROJ); 64 | for k = 1:length(projs) 65 | fiff_start_block(fid,FIFF.FIFFB_PROJ_ITEM); 66 | fiff_write_string(fid,FIFF.FIFF_NAME,projs(k).desc); 67 | fiff_write_int(fid,FIFF.FIFF_PROJ_ITEM_KIND,projs(k).kind); 68 | if projs(k).kind == FIFF.FIFFV_PROJ_ITEM_FIELD 69 | fiff_write_float(fid,FIFF.FIFF_PROJ_ITEM_TIME,0.0); 70 | end 71 | fiff_write_int(fid,FIFF.FIFF_NCHAN,projs(k).data.ncol); 72 | fiff_write_int(fid,FIFF.FIFF_PROJ_ITEM_NVEC, ... 73 | projs(k).data.nrow); 74 | fiff_write_int(fid,FIFF.FIFF_MNE_PROJ_ITEM_ACTIVE, ... 75 | projs(k).active); 76 | names = fiff_rename_list(projs(k).data.col_names, ch_rename); 77 | fiff_write_name_list(fid, ... 78 | FIFF.FIFF_PROJ_ITEM_CH_NAME_LIST, ... 79 | names); 80 | fiff_write_float_matrix(fid,FIFF.FIFF_PROJ_ITEM_VECTORS,projs(k).data.data); 81 | fiff_end_block(fid,FIFF.FIFFB_PROJ_ITEM); 82 | end 83 | fiff_end_block(fid,FIFF.FIFFB_PROJ); 84 | 85 | return; 86 | 87 | 88 | end 89 | -------------------------------------------------------------------------------- /matlab/fiff_write_raw_buffer.m: -------------------------------------------------------------------------------- 1 | function fiff_write_raw_buffer(fid,buf,cals,datatype) 2 | % 3 | % function fiff_write_raw_buffer(fid,buf,cals,datatype) 4 | % 5 | % fid of an open raw data file 6 | % buf the buffer to write 7 | % cals calibration factors 8 | % datatype (optional) datatype to write, default float 9 | % 10 | % 11 | 12 | % 13 | % Author : Matti Hamalainen, MGH Martinos Center 14 | % License : BSD 3-clause 15 | % 16 | % Revision 1.1 2007/11/07 16:05:05 msh 17 | % New routines for writing raw files 18 | % 19 | 20 | me='MNE:fiff_write_raw_buffer'; 21 | if nargin < 3 22 | error(me,'Incorrect number of arguments'); 23 | end 24 | 25 | if size(buf,1) ~= length(cals) 26 | error(me,'buffer and calibration sizes do not match'); 27 | end 28 | 29 | global FIFF; 30 | if isempty(FIFF) 31 | FIFF = fiff_define_constants(); 32 | end 33 | 34 | if nargin < 4 35 | datatype = FIFF.FIFFT_FLOAT; 36 | end 37 | 38 | if datatype ~= FIFF.FIFFT_FLOAT 39 | warning(me, 'reading and writing of data in numeric precision ~= float is only supported in FieldTrip and MNE-Python'); 40 | end 41 | 42 | switch datatype 43 | case FIFF.FIFFT_FLOAT 44 | fiff_write_float(fid,FIFF.FIFF_DATA_BUFFER,diag(1./cals)*buf); 45 | case FIFF.FIFFT_DOUBLE 46 | fiff_write_double(fid,FIFF.FIFF_DATA_BUFFER,diag(1./cals)*buf); 47 | case FIFF.FIFFT_COMPLEX_FLOAT 48 | fiff_write_complex(fid,FIFF.FIFF_DATA_BUFFER,diag(1./cals)*buf); 49 | case FIFF.FIFFT_COMPLEX_DOUBLE 50 | fiff_write_double_complex(fid,FIFF.FIFF_DATA_BUFFER,diag(1./cals)*buf); 51 | otherwise 52 | error(me,'unsupported datatype requested for writing of the buffer'); 53 | end 54 | 55 | return; 56 | -------------------------------------------------------------------------------- /matlab/fiff_write_raw_segment.m: -------------------------------------------------------------------------------- 1 | function fiff_write_raw_segment(fname, raw, from, to, sel, drop_small_buffer, buffer_size) 2 | % FIFF_WRITE_RAW_SEGMENT Write chunck of raw data to disk 3 | % [] = FIFF_WRITE_RAW_SEGMENT(FNAME, RAW, FROM, TO, SEL) 4 | % 5 | % The functions reads data from a file specified by raw 6 | % which is obtained with fiff_setup_read_raw 7 | % 8 | % fname - the name of the file where to write 9 | % raw - structure returned by fiff_setup_read_raw 10 | % from - first sample to include. If omitted, defaults to the 11 | % first sample in data 12 | % to - last sample to include. If omitted, defaults to the last 13 | % sample in data 14 | % sel - optional channel selection vector 15 | % drop_small_buffer - optional bool to say if the last data buffer is dropped 16 | % to make sure all buffers have the same size 17 | % (required by maxfilter) 18 | % buffer_size - float (size of data buffers) 19 | 20 | % 21 | % Author : Alexandre Gramfort, MGH Martinos Center 22 | % License : BSD 3 - clause 23 | % 24 | 25 | global FIFF; 26 | if isempty(FIFF) 27 | FIFF = fiff_define_constants(); 28 | end 29 | % 30 | me = 'MNE:fiff_write_raw_segment'; 31 | % 32 | if nargin < 2 33 | error(me, 'Incorrect number of arguments'); 34 | end 35 | if nargin < 3 | isempty(from) 36 | from = raw.first_samp; 37 | end 38 | if nargin < 4 | isempty(to) 39 | to = raw.last_samp; 40 | end 41 | if nargin < 5 | isempty(sel) 42 | sel = 1:raw.info.nchan; 43 | end 44 | if nargin < 6 45 | drop_small_buffer = false; 46 | end 47 | if nargin < 7 48 | buffer_size_sec = 25; % read by chunks of 30 seconds 49 | buffer_size = ceil(buffer_size_sec * raw.info.sfreq); 50 | end 51 | % 52 | [outfid, cals] = fiff_start_writing_raw(fname, raw.info, sel); 53 | % 54 | first_buffer = true; 55 | for first = from:buffer_size:to 56 | last = first + buffer_size - 1; 57 | if last > to 58 | last = to; 59 | end 60 | try 61 | [ data, times ] = fiff_read_raw_segment(raw, first, last, sel); 62 | catch 63 | fclose(raw.fid); 64 | fclose(outfid); 65 | error(me, '%s', mne_omit_first_line(lasterr)); 66 | end 67 | if drop_small_buffer && first_buffer == false && length(times) < buffer_size 68 | fprintf(1, 'Skipping due to small buffer ... [done]\n'); 69 | break 70 | end 71 | % 72 | % You can add your own miracle here 73 | % 74 | fprintf(1, 'Writing...'); 75 | if first_buffer 76 | if first > 0 77 | fiff_write_int(outfid, FIFF.FIFF_FIRST_SAMPLE, first); 78 | end 79 | first_buffer = false; 80 | end 81 | fiff_write_raw_buffer(outfid, data, cals); 82 | fprintf(1, '[done]\n'); 83 | end 84 | 85 | fiff_finish_writing_raw(outfid); 86 | -------------------------------------------------------------------------------- /matlab/fiff_write_raw_segment_times.m: -------------------------------------------------------------------------------- 1 | function fiff_write_raw_segment_times(fname, raw, from, to, sel, drop_small_buffer, buffer_size_sec) 2 | % FIFF_WRITE_RAW_SEGMENT_TIMES Write chunck of raw data to disk 3 | % [] = FIFF_WRITE_RAW_SEGMENT_TIMES(FNAME, RAW, FROM, TO, SEL) 4 | % 5 | % The functions reads data from a file specified by raw 6 | % which is obtained with fiff_setup_read_raw 7 | % 8 | % fname - the name of the file where to write 9 | % raw - structure returned by fiff_setup_read_raw 10 | % from - starting time of the segment in seconds 11 | % to - end time of the segment in seconds 12 | % sel - optional channel selection vector 13 | % drop_small_buffer - optional bool to say if the last data buffer is dropped 14 | % to make sure all buffers have the same size 15 | % (required by maxfilter) 16 | % buffer_size_sec - float (size of data buffers in seconds) 17 | 18 | % 19 | % Author : Alexandre Gramfort, MGH Martinos Center 20 | % License : BSD 3 - clause 21 | % 22 | 23 | global FIFF; 24 | if isempty(FIFF) 25 | FIFF = fiff_define_constants(); 26 | end 27 | % 28 | me = 'MNE:fiff_write_raw_segment'; 29 | % 30 | if nargin < 2 31 | error(me, 'Incorrect number of arguments'); 32 | end 33 | if nargin < 3 | isempty(from) 34 | from = raw.first_samp / raw.info.sfreq; 35 | end 36 | if nargin < 4 | isempty(to) 37 | to = raw.last_samp / raw.info.sfreq; 38 | end 39 | if nargin < 5 40 | sel = 1:raw.info.nchan; 41 | end 42 | if nargin < 6 43 | drop_small_buffer = false; 44 | end 45 | if nargin < 7 46 | buffer_size_sec = 25; 47 | end 48 | 49 | buffer_size = ceil(buffer_size_sec * raw.info.sfreq); 50 | 51 | % 52 | % 53 | % Convert to samples 54 | % 55 | from = floor(from * raw.info.sfreq); 56 | to = ceil(to * raw.info.sfreq); 57 | 58 | fiff_write_raw_segment(fname, raw, from, to, sel, drop_small_buffer, buffer_size); 59 | -------------------------------------------------------------------------------- /matlab/fiff_write_short.m: -------------------------------------------------------------------------------- 1 | function fiff_write_short(fid, kind, data) 2 | % 3 | % fiff_write_short(fid, kind, data) 4 | % 5 | % Writes a 16-bit integer tag to a fif file 6 | % 7 | % fid An open fif file descriptor 8 | % kind Tag kind 9 | % data The integers to use as data 10 | % 11 | 12 | % 13 | % Author : Matti Hamalainen, MGH Martinos Center 14 | % License : BSD 3-clause 15 | % 16 | % Revision 1.1 2006/04/27 22:38:37 msh 17 | % Splitting an empty list now results in an empty output. 18 | % Added fiff_write_double and fiff_write_short 19 | % Write an array of floats, ints, and doubles instead of just one value. 20 | % Fixed help text of fiff_pick_channels. 21 | % 22 | % 23 | 24 | me = 'MNE:fiff_write_short'; 25 | 26 | if nargin ~= 3 27 | error(me, 'Incorrect number of arguments'); 28 | end 29 | 30 | FIFFT_SHORT = 2; 31 | FIFFV_NEXT_SEQ = 0; 32 | nel = numel(data); 33 | datasize = nel * 2; 34 | count = fwrite(fid, int32(kind), 'int32'); 35 | if count ~= 1 36 | error(me, 'write failed'); 37 | end 38 | count = fwrite(fid, int32(FIFFT_SHORT), 'int32'); 39 | if count ~= 1 40 | error(me, 'write failed'); 41 | end 42 | count = fwrite(fid, int32(datasize), 'int32'); 43 | if count ~= 1 44 | error(me, 'write failed'); 45 | end 46 | count = fwrite(fid, int32(FIFFV_NEXT_SEQ), 'int32'); 47 | if count ~= 1 48 | error(me, 'write failed'); 49 | end 50 | count = fwrite(fid, int16(data), 'int16'); 51 | if count ~= nel 52 | error(me, 'write failed'); 53 | end 54 | 55 | return 56 | -------------------------------------------------------------------------------- /matlab/fiff_write_string.m: -------------------------------------------------------------------------------- 1 | function fiff_write_string(fid,kind,data) 2 | % 3 | % fiff_write_string(fid,kind,data) 4 | % 5 | % Writes a string tag 6 | % 7 | % fid An open fif file descriptor 8 | % kind The tag kind 9 | % data The string data to write 10 | % 11 | 12 | % 13 | % Author : Matti Hamalainen, MGH Martinos Center 14 | % License : BSD 3-clause 15 | % 16 | % Revision 1.2 2006/04/23 15:29:40 msh 17 | % Added MGH to the copyright 18 | % 19 | % Revision 1.1 2006/04/10 23:26:54 msh 20 | % Added fiff reading routines 21 | % 22 | % 23 | 24 | me='MNE:fiff_write_string'; 25 | 26 | if nargin ~= 3 27 | error(me,'Incorrect number of arguments'); 28 | end 29 | 30 | FIFFT_STRING=10; 31 | FIFFV_NEXT_SEQ=0; 32 | datasize=size(data,2); 33 | count = fwrite(fid,int32(kind),'int32'); 34 | if count ~= 1 35 | error(me,'write failed'); 36 | end 37 | count = fwrite(fid,int32(FIFFT_STRING),'int32'); 38 | if count ~= 1 39 | error(me,'write failed'); 40 | end 41 | count = fwrite(fid,int32(datasize),'int32'); 42 | if count ~= 1 43 | error(me,'write failed'); 44 | end 45 | count = fwrite(fid,int32(FIFFV_NEXT_SEQ),'int32'); 46 | if count ~= 1 47 | error(me,'write failed'); 48 | end 49 | count = fwrite(fid,data,'uchar'); 50 | if count ~= datasize 51 | error(me,'write failed'); 52 | end 53 | -------------------------------------------------------------------------------- /matlab/mne_combine_xyz.m: -------------------------------------------------------------------------------- 1 | function [comb] = mne_combine_xyz(vec) 2 | % 3 | % function [comb] = mne_combine_xyz(vec) 4 | % 5 | % Compute the three Cartesian components of a vector together 6 | % 7 | % 8 | % vec - Input row or column vector [ x1 y1 z1 ... x_n y_n z_n ] 9 | % comb - Output vector [x1^2+y1^2+z1^2 ... x_n^2+y_n^2+z_n^2 ] 10 | % 11 | 12 | % 13 | % 14 | % Author : Matti Hamalainen, MGH Martinos Center 15 | % License : BSD 3-clause 16 | % 17 | % Revision 1.1 2006/05/05 03:50:40 msh 18 | % Added routines to compute L2-norm inverse solutions. 19 | % Added mne_write_inverse_sol_stc to write them in stc files 20 | % Several bug fixes in other files 21 | % 22 | % 23 | 24 | me = 'MNE:mne_combine_xyz'; 25 | if nargin ~= 1 26 | error(me,'Wrong number of arguments'); 27 | end 28 | if size(vec,1) > size(vec,2) 29 | vec = vec'; 30 | end 31 | if size(vec,1) ~= 1 || mod(size(vec,2),3) ~= 0 32 | error(me,'Input must be a row or a column vector with 3N components'); 33 | end 34 | 35 | s = mne_block_diag(vec,3); 36 | comb = full(diag(s*s')); 37 | if size(vec,1) > size(vec,2) 38 | comb = comb'; 39 | end 40 | 41 | return; 42 | 43 | -------------------------------------------------------------------------------- /matlab/mne_compensate_to.m: -------------------------------------------------------------------------------- 1 | function [newdata] = mne_compensate_to(data,to) 2 | % 3 | % [newdata] = mne_compensate_to(data,to) 4 | % 5 | % Apply compensation to the data as desired 6 | % 7 | 8 | 9 | % 10 | % Author : Matti Hamalainen, MGH Martinos Center 11 | % License : BSD 3-clause 12 | % 13 | % 14 | % Revision 1.4 2006/04/23 15:29:40 msh 15 | % Added MGH to the copyright 16 | % 17 | % Revision 1.3 2006/04/18 20:44:46 msh 18 | % Added reading of forward solution. 19 | % Use length instead of size when appropriate 20 | % 21 | % Revision 1.2 2006/04/14 15:49:49 msh 22 | % Improved the channel selection code and added ch_names to measurement info. 23 | % 24 | % Revision 1.1 2006/04/12 10:51:19 msh 25 | % Added projection writing and compensation routines 26 | % 27 | % 28 | 29 | me='MNE:mne_compensate_to'; 30 | 31 | newdata = data; 32 | now = mne_get_current_comp(newdata.info); 33 | % 34 | % Are we there already? 35 | % 36 | if now == to 37 | fprintf(1,'Data are already compensated as desired\n'); 38 | end 39 | % 40 | % Make the compensator and apply it to all data sets 41 | % 42 | comp = mne_make_compensator(newdata.info,now,to); 43 | for k = 1:length(newdata.evoked) 44 | newdata.evoked(k).epochs = comp*newdata.evoked(k).epochs; 45 | end 46 | % 47 | % Update the compensation info in the channel descriptors 48 | % 49 | newdata.info.chs = mne_set_current_comp(newdata.info.chs,to); 50 | return; 51 | 52 | -------------------------------------------------------------------------------- /matlab/mne_ex_cancel_noise.m: -------------------------------------------------------------------------------- 1 | function [res,proj,comp] = mne_ex_cancel_noise(data,dest_comp) 2 | % 3 | % Do projection and compensation as needed 4 | % Return the appropriate operators 5 | % 6 | % [res,proj,comp] = mne_ex_cancel_noise(data,dest_comp) 7 | % 8 | % res - Data after noise cancellation 9 | % proj - The projection operator applied 10 | % comp - The compensator which brings uncompensated data to the 11 | % desired compensation grade (will be useful in forward 12 | % calculations) 13 | % 14 | 15 | % 16 | % Author : Matti Hamalainen, MGH Martinos Center 17 | % License : BSD 3-clause 18 | % 19 | % Revision 1.4 2006/05/05 03:50:40 msh 20 | % Added routines to compute L2-norm inverse solutions. 21 | % Added mne_write_inverse_sol_stc to write them in stc files 22 | % Several bug fixes in other files 23 | % 24 | % Revision 1.3 2006/04/27 20:57:41 msh 25 | % Fixed typo on line 76 of mne_ex_cancel_noise 26 | % 27 | % Revision 1.2 2006/04/23 15:29:40 msh 28 | % Added MGH to the copyright 29 | % 30 | % Revision 1.1 2006/04/21 17:31:07 msh 31 | % Added the examples. 32 | % Modified the formatting of some informative output. 33 | % 34 | % 35 | 36 | me='MNE:mne_ex_cancel_noise'; 37 | 38 | if nargin == 1 39 | dest_comp = 0; 40 | elseif nargin ~= 2 41 | error(me,'Incorrect number of arguments'); 42 | end 43 | % 44 | % Compensate the data and make a compensator for forward modelling 45 | % 46 | comp = []; 47 | proj = []; 48 | comp_now = mne_get_current_comp(data.info); 49 | if comp_now == dest_comp 50 | res = data; 51 | else 52 | try 53 | res = mne_compensate_to(data,dest_comp); 54 | fprintf(1,'The data are now compensated to grade %d.\n',dest_comp); 55 | catch 56 | error(me,'%s',mne_omit_first_line(lasterr)); 57 | end 58 | end 59 | if dest_comp > 0 60 | comp = mne_make_compensator(res.info,0,dest_comp); 61 | fprintf(1,'Appropriate forward operator compensator created.\n'); 62 | else 63 | fprintf(1,'No forward operator compensator needed.\n'); 64 | end 65 | % 66 | % Do the projection 67 | % 68 | if isempty(data.info.projs) 69 | fprintf(1,'No projector included with these data\n'); 70 | else 71 | % 72 | % Activate the projection items 73 | % 74 | for k = 1:length(res.info.projs) 75 | res.info.projs(k).active = true; 76 | end 77 | % 78 | % Create the projector 79 | % 80 | [proj,nproj] = mne_make_projector_info(res.info); 81 | if nproj == 0 82 | fprintf(1,'The projection vectors do not apply to these channels\n'); 83 | proj = []; 84 | else 85 | fprintf(1,'Created an SSP operator (subspace dimension = %d)\n',nproj); 86 | res.evoked.epochs = proj*res.evoked.epochs; 87 | fprintf(1,'Projector applied to the data\n'); 88 | end 89 | end 90 | 91 | return; 92 | 93 | end 94 | -------------------------------------------------------------------------------- /matlab/mne_ex_data_sets.m: -------------------------------------------------------------------------------- 1 | function [res] = mne_ex_data_sets(fname) 2 | % 3 | % Find all evoked response data from a given file 4 | % 5 | % [res] = mne_ex_data_sets(fname) 6 | % 7 | % fname - Name of the file to look at 8 | % res - Structure containing the result 9 | % 10 | 11 | % 12 | % Author : Matti Hamalainen, MGH Martinos Center 13 | % License : BSD 3-clause 14 | % 15 | % Revision 1.2 2006/04/23 15:29:40 msh 16 | % Added MGH to the copyright 17 | % 18 | % Revision 1.1 2006/04/21 17:31:07 msh 19 | % Added the examples. 20 | % Modified the formatting of some informative output. 21 | % 22 | % 23 | me='MNE:mne_ex_data_sets'; 24 | 25 | if nargin ~= 1 26 | error(me,'Wrong number of arguments'); 27 | end 28 | 29 | try 30 | res = fiff_find_evoked(fname); 31 | catch 32 | error(me,'%s',mne_omit_first_line(lasterr)); 33 | end 34 | 35 | if isempty(res) 36 | fprintf(1,'No evoked response data sets in %s\n',fname); 37 | else 38 | fprintf(1,'Found %d evoked response data sets in %s :\n',length(res),fname); 39 | for k = 1:length(res) 40 | fprintf(1,'\t%s (%s)\n',res(k).comment,res(k).aspect_name); 41 | end 42 | end 43 | 44 | 45 | return; 46 | 47 | end 48 | 49 | -------------------------------------------------------------------------------- /matlab/mne_ex_read_write_raw.m: -------------------------------------------------------------------------------- 1 | function mne_ex_read_write_raw(infile,outfile) 2 | % 3 | % function mne_ex_read_write_raw(infile,outfile); 4 | % 5 | % Read and write raw data in 60-sec blocks 6 | % 7 | 8 | % 9 | % Author : Matti Hamalainen, MGH Martinos Center 10 | % License : BSD 3-clause 11 | % 12 | 13 | 14 | global FIFF; 15 | if isempty(FIFF) 16 | FIFF = fiff_define_constants(); 17 | end 18 | % 19 | me = 'MNE:mne_ex_read_write_raw'; 20 | % 21 | if nargin ~= 2 22 | error(me,'Incorrect number of arguments'); 23 | end 24 | % 25 | % Setup for reading the raw data 26 | % 27 | try 28 | raw = fiff_setup_read_raw(infile); 29 | catch 30 | error(me,'%s',mne_omit_first_line(lasterr)); 31 | end 32 | %raw.info.projs = []; 33 | % 34 | % Set up pick list: MEG + STI 014 - bad channels 35 | % 36 | % 37 | want_meg = true; 38 | want_eeg = false; 39 | want_stim = false; 40 | include{1} = 'STI 014'; 41 | try 42 | picks = fiff_pick_types(raw.info,want_meg,want_eeg,want_stim,include,raw.info.bads); 43 | catch 44 | % 45 | % Failure: Try MEG + STI101 + STI201 + STI301 - bad channels instead 46 | % 47 | include{1} = 'STI101'; 48 | include{2} = 'STI201'; 49 | include{3} = 'STI301'; 50 | try 51 | picks = fiff_pick_types(raw.info,want_meg,want_eeg,want_stim,include,raw.info.bads); 52 | catch 53 | error(me,'%s (channel list may need modification)',mne_omit_first_line(lasterr)); 54 | end 55 | end 56 | 57 | % 58 | [outfid,cals] = fiff_start_writing_raw(outfile,raw.info,picks); 59 | % 60 | % Set up the reading parameters 61 | % 62 | from = raw.first_samp; 63 | to = raw.last_samp; 64 | quantum_sec = 10; 65 | quantum = ceil(quantum_sec*raw.info.sfreq); 66 | % 67 | % To read the whole file at once set 68 | % 69 | %quantum = to - from + 1; 70 | % 71 | % 72 | % Read and write all the data 73 | % 74 | first_buffer = true; 75 | for first = from:quantum:to 76 | last = first+quantum-1; 77 | if last > to 78 | last = to; 79 | end 80 | try 81 | [ data, times ] = fiff_read_raw_segment(raw,first,last,picks); 82 | catch 83 | fclose(raw.fid); 84 | fclose(outfid); 85 | error(me,'%s',mne_omit_first_line(lasterr)); 86 | end 87 | % 88 | % You can add your own miracle here 89 | % 90 | fprintf(1,'Writing...'); 91 | if first_buffer 92 | if first > 0 93 | fiff_write_int(outfid,FIFF.FIFF_FIRST_SAMPLE,first); 94 | end 95 | first_buffer = false; 96 | end 97 | fiff_write_raw_buffer(outfid,data,cals); 98 | fprintf(1,'[done]\n'); 99 | end 100 | 101 | fiff_finish_writing_raw(outfid); 102 | fclose(raw.fid); 103 | -------------------------------------------------------------------------------- /matlab/mne_ex_rt.m: -------------------------------------------------------------------------------- 1 | function [info] = mne_ex_rt(mne_rt_server_ip, mne_rt_server_commandPort, mne_rt_server_fiffDataPort, p_nBuffers) 2 | % 3 | % An example of a mne_rt_server real-time connection 4 | % 5 | % function mne_ex_rt(mne_rt_server_ip, mne_rt_server_commandPort ,mne_rt_server_fiffDataPort) 6 | % 7 | % mne_rt_server_ip - IP of the running mne_rt_server 8 | % mne_rt_server_commandPort - Command port of the mne_rt_server 9 | % mne_rt_server_fiffDataPort - Fiff data port of the mne_rt_server 10 | % 11 | % Returns the measurement info 12 | % 13 | % 14 | 15 | % 16 | % Author : Christoph Dinh, Matti Hamalainen, MGH Martinos Center 17 | % License : BSD 3-clause 18 | % 19 | 20 | if nargin == 3 21 | p_nBuffers = 100; 22 | elseif nargin ~= 4 23 | error(me,'Incorrect number of arguments'); 24 | end 25 | 26 | %% add dynamic java path 27 | javaaddpath(fileparts(mfilename('fullpath'))); 28 | 29 | %% create command client 30 | t_cmdClient = mne_rt_cmd_client(mne_rt_server_ip, mne_rt_server_commandPort); 31 | 32 | %% create data client 33 | t_dataClient = mne_rt_data_client(mne_rt_server_ip, mne_rt_server_fiffDataPort); 34 | 35 | %% set data client alias -> for convinience (optional) 36 | t_dataClient.setClientAlias('mne_ex_matlab'); % used in option 2 later on 37 | 38 | %% example commands 39 | t_helpInfo = t_cmdClient.sendCommand('help'); 40 | fprintf('### Help ###\n%s',t_helpInfo); 41 | t_clistInfo = t_cmdClient.sendCommand('clist'); 42 | fprintf('### Client List ###\n%s',t_clistInfo); 43 | t_conInfo = t_cmdClient.sendCommand('conlist'); 44 | fprintf('### Connector List ###\n%s',t_conInfo); 45 | 46 | %% read meas info 47 | % Option 1 48 | t_aliasOrId = t_dataClient.getClientId(); 49 | % Option 2 50 | %t_aliasOrId = 'mne_ex_matlab'; 51 | t_cmdClient.requestMeasInfo(t_aliasOrId); 52 | t_measInfo = t_dataClient.readInfo(); 53 | 54 | %% start measurement 55 | t_cmdClient.requestMeas(t_aliasOrId); 56 | 57 | global FIFF; 58 | if isempty(FIFF) 59 | FIFF = fiff_define_constants(); 60 | end 61 | 62 | figure; 63 | hold on; 64 | h_old=plot(0,0); 65 | 66 | t_bIsRunning = true; 67 | t_iCount = 0; 68 | while (t_bIsRunning) 69 | fprintf('read buffer...'); 70 | [kind, t_matRawBuffer] = t_dataClient.readRawBuffer(t_measInfo.nchan); 71 | % 72 | % You can add your own miracle here 73 | % 74 | if(kind == FIFF.FIFF_DATA_BUFFER) 75 | fprintf('(%d channels x %d samples) [done]\r\n', size(t_matRawBuffer,1), size(t_matRawBuffer,2)); 76 | h = plot(t_matRawBuffer'); 77 | delete(h_old); 78 | h_old = h; 79 | drawnow; 80 | elseif (kind == FIFF.FIFF_BLOCK_END && t_matRawBuffer == FIFF.FIFFB_RAW_DATA) 81 | t_bIsRunning = false; 82 | end 83 | 84 | t_iCount = t_iCount+1; 85 | if(t_iCount >= p_nBuffers) 86 | t_cmdClient.stopAll(); 87 | t_bIsRunning = false; 88 | end 89 | end 90 | 91 | %% close the sockets 92 | t_cmdClient.close(); 93 | t_dataClient.close(); 94 | 95 | end -------------------------------------------------------------------------------- /matlab/mne_file_name.m: -------------------------------------------------------------------------------- 1 | function [res] = mne_file_name(dir,name) 2 | % 3 | % [name] = mne_file_name(dir,name) 4 | % 5 | % Compose a file name under MNE_ROOT 6 | % 7 | % dir - Name of the directory containing the file name 8 | % name - Name of the file under that directory 9 | % 10 | 11 | % 12 | % Author : Matti Hamalainen, MGH Martinos Center 13 | % License : BSD 3-clause 14 | % 15 | % 16 | 17 | me='MNE:mne_file_name'; 18 | 19 | if ~ispref('MNE','MNE_ROOT') 20 | mne_root = getenv('MNE_ROOT'); 21 | if isempty(mne_root) 22 | error(me,'MNE_ROOT not defined'); 23 | end 24 | else 25 | mne_root=getpref('MNE','MNE_ROOT'); 26 | end 27 | 28 | if nargin == 2 29 | res = sprintf('%s/%s/%s',mne_root,dir,name); 30 | elseif nargin == 1 31 | res = sprintf('%s/%s',mne_root,dir); 32 | else 33 | error(me,'incorrect number of arguments'); 34 | end 35 | 36 | return; 37 | 38 | end 39 | 40 | -------------------------------------------------------------------------------- /matlab/mne_find_channel.m: -------------------------------------------------------------------------------- 1 | function [which] = mne_find_channel(info,name) 2 | % 3 | % [which] = mne_find_channel(info,name) 4 | % 5 | % Find a channel by name employing the info structure 6 | % output by mne_raw2mat or mne_epochs2mat 7 | % 8 | % epoch - The data structure containing the channel information 9 | % name - name of the channel to look for 10 | % 11 | % Returns index of the channel in the data 12 | % If the channel is not found, returns -1 13 | % 14 | 15 | % 16 | % 17 | % Author : Matti Hamalainen, MGH Martinos Center 18 | % License : BSD 3-clause 19 | % 20 | % 21 | % $Id$ 22 | % 23 | % Revision 1.4 2006/04/23 15:29:40 msh 24 | % Added MGH to the copyright 25 | % 26 | % Revision 1.3 2006/04/14 15:49:49 msh 27 | % Improved the channel selection code and added ch_names to measurement info. 28 | % 29 | % Revision 1.2 2006/04/10 23:26:54 msh 30 | % Added fiff reading routines 31 | % 32 | % Revision 1.1 2006/02/20 15:45:05 msh 33 | % Added mne_find_channel.m and mne_read_epoch.m 34 | % 35 | % 36 | me='MNE:mne_find_channel'; 37 | if(nargin ~= 2) 38 | error(me,'Usage : [which] = mne_find_channel(info,which)'); 39 | end 40 | 41 | which = strmatch(name,info.ch_names,'exact'); 42 | 43 | return; 44 | -------------------------------------------------------------------------------- /matlab/mne_find_source_space_hemi.m: -------------------------------------------------------------------------------- 1 | function [hemi] = mne_find_source_space_hemi(src) 2 | % 3 | % function mne_find_source_space_hemi(src) 4 | % 5 | % Return the hemisphere id for a source space 6 | % 7 | % src - The source space to investigate 8 | % hemi - Deduced hemisphere id 9 | % 10 | 11 | % 12 | % 13 | % Author : Matti Hamalainen, MGH Martinos Center 14 | % License : BSD 3-clause 15 | % 16 | % Revision 1.2 2006/09/25 19:48:16 msh 17 | % Added projection item kinds to fiff_define_constants 18 | % Changed some fields to int32 in surface structures 19 | % 20 | % Revision 1.1 2006/05/05 03:50:40 msh 21 | % Added routines to compute L2-norm inverse solutions. 22 | % Added mne_write_inverse_sol_stc to write them in stc files 23 | % Several bug fixes in other files 24 | % 25 | % 26 | 27 | me='MNE:mne_find_source_space_hemi'; 28 | 29 | global FIFF; 30 | if isempty(FIFF) 31 | FIFF = fiff_define_constants(); 32 | end 33 | 34 | if nargin ~= 1 35 | error(me,'Incorrect number of arguments'); 36 | end 37 | 38 | xave = sum(src.rr(:,1)); 39 | 40 | if xave < 0 41 | hemi = int32(FIFF.FIFFV_MNE_SURF_LEFT_HEMI); 42 | else 43 | hemi = int32(FIFF.FIFFV_MNE_SURF_RIGHT_HEMI); 44 | end 45 | 46 | return; 47 | 48 | -------------------------------------------------------------------------------- /matlab/mne_fread3.m: -------------------------------------------------------------------------------- 1 | function [retval] = mne_fread3(fid) 2 | % 3 | % [retval] = mne_fread3(fid) 4 | % read a 3 byte integer out of a file 5 | % 6 | 7 | % 8 | % Author : Matti Hamalainen, MGH Martinos Center 9 | % License : BSD 3-clause 10 | % 11 | % Revision 1.3 2006/04/23 15:29:40 msh 12 | % Added MGH to the copyright 13 | % 14 | % Revision 1.2 2006/04/10 23:26:54 msh 15 | % Added fiff reading routines 16 | % 17 | % Revision 1.1 2005/11/21 02:15:51 msh 18 | % Added more routines 19 | % 20 | % 21 | 22 | b1 = fread(fid, 1, 'uchar') ; 23 | b2 = fread(fid, 1, 'uchar') ; 24 | b3 = fread(fid, 1, 'uchar') ; 25 | retval = bitshift(b1, 16) + bitshift(b2,8) + b3 ; 26 | 27 | -------------------------------------------------------------------------------- /matlab/mne_fwrite3.m: -------------------------------------------------------------------------------- 1 | function mne_fwrite3(fid, val) 2 | % 3 | % mne_fwrite(fid, val) 4 | % write a 3 byte integer to a file 5 | % 6 | 7 | % 8 | % 9 | % Author : Matti Hamalainen, MGH Martinos Center 10 | % License : BSD 3-clause 11 | % 12 | % Revision 1.3 2006/04/23 15:29:40 msh 13 | % Added MGH to the copyright 14 | % 15 | % Revision 1.2 2006/04/10 23:26:54 msh 16 | % Added fiff reading routines 17 | % 18 | % Revision 1.1 2005/11/21 02:15:51 msh 19 | % Added more routines 20 | % 21 | % 22 | 23 | b1 = bitand(bitshift(val, -16), 255) ; 24 | b2 = bitand(bitshift(val, -8), 255) ; 25 | b3 = bitand(val, 255) ; 26 | fwrite(fid, b1, 'uchar') ; 27 | fwrite(fid, b2, 'uchar') ; 28 | fwrite(fid, b3, 'uchar') ; 29 | 30 | 31 | -------------------------------------------------------------------------------- /matlab/mne_get_current_comp.m: -------------------------------------------------------------------------------- 1 | function [comp] = mne_get_current_comp(info) 2 | % 3 | % [comp] = mne_get_current_comp(info) 4 | % 5 | % Get the current compensation in effect in the data 6 | % 7 | 8 | 9 | % 10 | % Author : Matti Hamalainen, MGH Martinos Center 11 | % License : BSD 3-clause 12 | % 13 | % 14 | % Revision 1.2 2006/04/23 15:29:40 msh 15 | % Added MGH to the copyright 16 | % 17 | % Revision 1.1 2006/04/12 10:51:19 msh 18 | % Added projection writing and compensation routines 19 | % 20 | % 21 | 22 | me='MNE:mne_get_current_comp'; 23 | 24 | global FIFF; 25 | if isempty(FIFF) 26 | FIFF = fiff_define_constants(); 27 | end 28 | 29 | comp = 0; 30 | first_comp = -1; 31 | for k = 1:info.nchan 32 | if info.chs(k).kind == FIFF.FIFFV_MEG_CH 33 | comp = bitshift(double(info.chs(k).coil_type),-16); 34 | if first_comp < 0 35 | first_comp = comp; 36 | elseif comp ~= first_comp 37 | error(me,'Compensation is not set equally on all MEG channels'); 38 | end 39 | end 40 | end 41 | 42 | return; 43 | 44 | -------------------------------------------------------------------------------- /matlab/mne_label_time_courses.m: -------------------------------------------------------------------------------- 1 | function [ values, times, vertices ] = mne_label_time_courses(labelfile,stcfile) 2 | % 3 | % function [ values, times ] = mne_label_time_courses(labelfile,stcfile) 4 | % 5 | % Extract the time courses corresponding to a label file from an stc file 6 | % 7 | % labelfile - The name of the label file 8 | % stcfile - The name of the stc file (must be on the same subject and 9 | % hemisphere as the stc file 10 | % 11 | % values - The time courses 12 | % times - The time points 13 | % vertices - The vertices corresponding to the time points 14 | % 15 | 16 | % 17 | % 18 | % Author : Matti Hamalainen, MGH Martinos Center 19 | % License : BSD 3-clause 20 | % 21 | % 22 | % 23 | me='MNE:mne_label_time_courses'; 24 | 25 | if nargin ~= 2 26 | error(me,'Incorrect number of arguments'); 27 | end 28 | try 29 | stc = mne_read_stc_file(stcfile); 30 | catch 31 | error(me,'%s',mne_omit_first_line(lasterr)); 32 | end 33 | 34 | try 35 | lab = mne_read_label_file(labelfile); 36 | catch 37 | error(me,'%s',mne_omit_first_line(lasterr)); 38 | end 39 | 40 | [vertices,ia,ib] = intersect(double(stc.vertices),double(lab.vertices)); 41 | if length(vertices) == 0 42 | error(me,'No vertices match the label in the stc file'); 43 | end 44 | 45 | values = stc.data(ia,:); 46 | times = zeros(1,size(stc.data,2)); 47 | for k = 0:length(times)-1 48 | times(k+1) = stc.tmin + k*stc.tstep; 49 | end 50 | 51 | end 52 | 53 | -------------------------------------------------------------------------------- /matlab/mne_make_projector_info.m: -------------------------------------------------------------------------------- 1 | function [proj,nproj] = mne_make_projector_info(info) 2 | % 3 | % [proj,nproj] = mne_make_projector_info(info) 4 | % 5 | % Make an SSP operator using the meas info 6 | % 7 | 8 | % 9 | % Author : Matti Hamalainen, MGH Martinos Center 10 | % License : BSD 3-clause 11 | % 12 | % 13 | % Revision 1.1 2006/05/05 10:52:33 msh 14 | % Added missing file. 15 | % 16 | % 17 | 18 | me='MNE:mne_make_projector_info'; 19 | 20 | if nargin ~= 1 21 | error(me,'Incorrect number of arguments'); 22 | end 23 | 24 | [ proj, nproj ] = mne_make_projector(info.projs,info.ch_names,info.bads); 25 | 26 | return; 27 | 28 | end 29 | -------------------------------------------------------------------------------- /matlab/mne_mesh_edges.m: -------------------------------------------------------------------------------- 1 | function [edges] = mne_mesh_edges(faces) 2 | 3 | % MESH_EDGES Returns sparse matrix with edges number 4 | % 5 | % SYNTAX 6 | % [EDGES] = MESH_EDGES(FACES) 7 | % 8 | % faces : matrix of size [n_trianges, 3] 9 | % edges : sparse matrix of size [n_vertices, n_vertices] 10 | 11 | % 12 | % Author : Alexandre Gramfort, MGH Martinos Center 13 | % License : BSD 3-clause 14 | % 15 | 16 | me = 'MESH_EDGES'; 17 | 18 | if nargin == 0 19 | eval(['help ',lower(me)]) 20 | return 21 | end 22 | 23 | faces = double(faces); 24 | npoints = max(faces(:)); 25 | nfaces = size(faces,1); 26 | edges = sparse(faces(:,1),faces(:,2),ones(1,nfaces),npoints,npoints); 27 | edges = edges + sparse(faces(:,2),faces(:,3),ones(1,nfaces),npoints,npoints); 28 | edges = edges + sparse(faces(:,3),faces(:,1),ones(1,nfaces),npoints,npoints); 29 | 30 | edges = edges + edges'; 31 | 32 | end % function 33 | -------------------------------------------------------------------------------- /matlab/mne_morph_data.m: -------------------------------------------------------------------------------- 1 | function [stcs] = mne_morph_data(from,to,stcs,grade) 2 | % MNE_MORPH_DATA Returns data morphed to a new subject. 3 | % 4 | % SYNTAX 5 | % [STCS] = MNE_MORPH_DATA(FROM, TO, STCS, GRADE) 6 | % 7 | % from : name of origin subject 8 | % to : name of destination subject 9 | % stcs : stc data to morph 10 | % grade : (optional) resolution of the icosahedral mesh (typically 5) 11 | % 12 | % Note : The functions requires to set MNE_ROOT and SUBJECTS_DIR variables. 13 | % 14 | % Example: 15 | % from = 'sample'; 16 | % to = 'fsaverage'; 17 | % stcs_morph = mne_morph_data(from,to,stcs,5); 18 | % 19 | % 20 | 21 | % 22 | % Author : Alexandre Gramfort, MGH Martinos Center 23 | % Matti Hamalainen 24 | % License : BSD 3-clause 25 | % 26 | 27 | if nargin < 5 28 | grade = 5; 29 | end 30 | 31 | sphere_from = [getenv('SUBJECTS_DIR'),'/',from,'/surf/lh.sphere.reg']; 32 | [tmp, tris{1}] = mne_read_surface(sphere_from); 33 | sphere_from = [getenv('SUBJECTS_DIR'),'/',from,'/surf/rh.sphere.reg']; 34 | [tmp, tris{2}] = mne_read_surface(sphere_from); 35 | 36 | [map{1}, map{2}] = mne_read_morph_map(from,to); 37 | 38 | for hemi = 1:2 39 | e = mne_mesh_edges(tris{hemi}); 40 | e = e==2; 41 | n_vertices = length(e); 42 | e = e + speye(n_vertices, n_vertices); 43 | idx_use = stcs{hemi}.vertices + 1; 44 | n_iter = 100; % nb of smoothing iterations 45 | for k = 1:n_iter 46 | data1 = e(:,idx_use) * ones(length(idx_use),1); 47 | stcs{hemi}.data = e(:,idx_use)*stcs{hemi}.data; 48 | idx_use = find(data1); 49 | fprintf(1,'%d/%d ',k,length(idx_use)); 50 | if ( k == n_iter ) || ( length(idx_use) >= n_vertices ) 51 | stcs{hemi}.data(idx_use,:) = bsxfun(@rdivide, stcs{hemi}.data(idx_use,:), data1(idx_use)); 52 | break; 53 | else 54 | stcs{hemi}.data = bsxfun(@rdivide, stcs{hemi}.data(idx_use,:), data1(idx_use)); 55 | end 56 | end 57 | fprintf(1,'\n'); 58 | stcs{hemi}.data = map{hemi}*stcs{hemi}.data; 59 | end 60 | 61 | ico_file_name = [getenv('MNE_ROOT'),'/share/mne/icos.fif']; 62 | 63 | s = mne_read_bem_surfaces(ico_file_name); 64 | 65 | for k = 1:length(s) 66 | if (s(k).id == 9000 + grade) 67 | ico = s(k); 68 | break; 69 | end 70 | end 71 | 72 | sphere = [getenv('SUBJECTS_DIR'),'/',to,'/surf/lh.sphere.reg']; 73 | lhs = mne_read_surface(sphere); 74 | sphere = [getenv('SUBJECTS_DIR'),'/',to,'/surf/rh.sphere.reg']; 75 | rhs = mne_read_surface(sphere); 76 | 77 | nearest{1} = zeros(ico.np, 1); 78 | nearest{2} = zeros(ico.np, 1); 79 | 80 | lhs = bsxfun(@rdivide, lhs, sqrt(sum(lhs.^2, 2))); 81 | rhs = bsxfun(@rdivide, rhs, sqrt(sum(rhs.^2, 2))); 82 | 83 | for k = 1:size(ico.rr,1) 84 | dots = ico.rr(k,:)*lhs'; 85 | [tmp, nearest{1}(k)] = max(dots); 86 | dots = ico.rr(k,:)*rhs'; 87 | [tmp, nearest{2}(k)] = max(dots); 88 | end 89 | 90 | stcs{1}.data = stcs{1}.data(nearest{1},:); 91 | stcs{2}.data = stcs{2}.data(nearest{2},:); 92 | stcs{1}.vertices = nearest{1}-1; 93 | stcs{2}.vertices = nearest{2}-1; 94 | 95 | fprintf(1, '\n'); 96 | -------------------------------------------------------------------------------- /matlab/mne_omit_first_line.m: -------------------------------------------------------------------------------- 1 | function [rest] = mne_omit_first_line(str) 2 | % 3 | % [rest] = mne_omit_first_line(str) 4 | % 5 | % Omit the first line in a multi-line string (useful for handling 6 | % error messages) 7 | % 8 | 9 | % 10 | % Author : Matti Hamalainen, MGH Martinos Center 11 | % License : BSD 3-clause 12 | % 13 | % 14 | % Revision 1.2 2006/04/23 15:29:40 msh 15 | % Added MGH to the copyright 16 | % 17 | % Revision 1.1 2006/04/17 11:52:15 msh 18 | % Added coil definition stuff 19 | % 20 | % 21 | me='MNE:mne_omit_first_line'; 22 | rest = str; 23 | if exist('OCTAVE_VERSION', 'builtin') ~= 0 24 | return; 25 | end 26 | 27 | lf = findstr(10,str); 28 | if ~isempty(lf) 29 | rest = str(lf+1:size(str,2)); 30 | end 31 | 32 | return; 33 | 34 | end 35 | -------------------------------------------------------------------------------- /matlab/mne_patch_info.m: -------------------------------------------------------------------------------- 1 | function [pinfo] = mne_patch_info(nearest) 2 | % 3 | % [pinfo] = mne_patch_info(nearest) 4 | % 5 | % Generate the patch information from the 'nearest' vector in a source space 6 | % 7 | 8 | % 9 | % 10 | % Author : Matti Hamalainen, MGH Martinos Center 11 | % License : BSD 3-clause 12 | % 13 | % 14 | 15 | me='MNE:mne_patch_info'; 16 | 17 | if nargin ~= 1 18 | error(me,'Incorrect number of arguments'); 19 | end 20 | 21 | if isempty(nearest) 22 | pinfo = []; 23 | return; 24 | end 25 | 26 | [ sorted, indn ] = sort(nearest); 27 | 28 | [uniq,firsti,dum] = unique(sorted,'first'); 29 | [uniq,lasti,dum] = unique(sorted,'last'); 30 | 31 | for k = 1:length(uniq) 32 | pinfo{k} = indn(firsti(k):lasti(k)); 33 | end 34 | 35 | return; 36 | 37 | -------------------------------------------------------------------------------- /matlab/mne_pick_channels_cov.m: -------------------------------------------------------------------------------- 1 | function [cov] = mne_pick_channels_cov(orig,include,exclude) 2 | % 3 | % [cov] = mne_pick_channels_cov(orig,include,exclude) 4 | % 5 | % Pick desired channels from a covariance matrix 6 | % 7 | % orig - The original covariance matrix 8 | % include - Channels to include (if empty, include all available) 9 | % exclude - Channels to exclude (if empty, do not exclude any) 10 | % 11 | % 12 | 13 | % 14 | % 15 | % Author : Matti Hamalainen, MGH Martinos Center 16 | % License : BSD 3-clause 17 | % 18 | % Revision 1.9 2006/11/21 12:53:49 msh 19 | % Fixed error in picking 20 | % 21 | % Revision 1.8 2006/06/29 22:12:28 msh 22 | % Fixed errors in channel picking 23 | % 24 | % Revision 1.7 2006/05/03 18:53:05 msh 25 | % Approaching Matlab 6.5 backward compatibility 26 | % 27 | % Revision 1.6 2006/04/23 15:29:40 msh 28 | % Added MGH to the copyright 29 | % 30 | % Revision 1.5 2006/04/18 20:44:46 msh 31 | % Added reading of forward solution. 32 | % Use length instead of size when appropriate 33 | % 34 | % Revision 1.4 2006/04/15 12:21:00 msh 35 | % Several small improvements 36 | % 37 | % Revision 1.3 2006/04/14 15:49:49 msh 38 | % Improved the channel selection code and added ch_names to measurement info. 39 | % 40 | % Revision 1.2 2006/04/14 00:45:42 msh 41 | % Added channel picking and fiff_invert_transform 42 | % 43 | % Revision 1.1 2006/04/13 17:05:45 msh 44 | % Added reading of bad channels to fiff_read_meas_info.m 45 | % Added mne_pick_channels_cov.m 46 | % 47 | % 48 | 49 | me='MNE:mne_pick_channels_cov'; 50 | 51 | if nargin == 1 52 | cov = orig; 53 | if isempty(cov.eig) || isempty(cov.eigvec) 54 | decompose_eigen; 55 | end 56 | return; 57 | elseif nargin == 2 58 | exclude = []; 59 | elseif nargin ~= 3 60 | error(me,'Incorrect number of arguments'); 61 | end 62 | 63 | if isempty(include) && isempty(exclude) 64 | cov = orig; 65 | if isempty(cov.eig) || isempty(cov.eigvec) 66 | decompose_eigen; 67 | end 68 | return; 69 | end 70 | 71 | if isempty(orig.names) 72 | error(me,'Cannot pick from a covariance matrix without channel names'); 73 | end 74 | 75 | cov = orig; 76 | % 77 | % First do the channels to be included 78 | % 79 | sel = fiff_pick_channels(cov.names,include,exclude); 80 | if isempty(sel) 81 | error(me,'Nothing remains after picking'); 82 | end 83 | % 84 | % Select the desired stuff 85 | % 86 | if cov.diag 87 | cov.data = cov.data(sel); 88 | else 89 | cov.data = cov.data(:,sel); 90 | cov.data = cov.data(sel,:); 91 | end 92 | for p = 1:size(sel,2) 93 | names{p} = cov.names{sel(p)}; 94 | end 95 | cov.names = names; 96 | cov.dim = length(cov.names); 97 | % 98 | % Eigenvalues and vectors are no longer valid 99 | % 100 | decompose_eigen; 101 | % 102 | return; 103 | 104 | function decompose_eigen 105 | if cov.diag 106 | cov.eig = cov.data; 107 | cov.eigvec = eye(cov.dim); 108 | else 109 | [ cov.eigvec, cov.eig ] = eig(cov.data); 110 | % 111 | % We use the convention that rows of eigvec are the 112 | % eigenvectors 113 | % 114 | cov.eigvec = cov.eigvec'; 115 | cov.eig = diag(cov.eig); 116 | end 117 | end 118 | 119 | end 120 | -------------------------------------------------------------------------------- /matlab/mne_pick_channels_forward.m: -------------------------------------------------------------------------------- 1 | function [fwd] = mne_pick_channels_forward(orig,include,exclude) 2 | % 3 | % [fwd] = mne_pick_channels_forward(orig,include,exclude) 4 | % 5 | % Pick desired channels from a forward solution 6 | % 7 | % orig - The original forward solution 8 | % include - Channels to include (if empty, include all available) 9 | % exclude - Channels to exclude (if empty, do not exclude any) 10 | % 11 | % 12 | 13 | % 14 | % 15 | % Author : Matti Hamalainen, MGH Martinos Center 16 | % License : BSD 3-clause 17 | % 18 | % Revision 1.2 2009/03/04 22:04:31 msh 19 | % Fixed typos in comments 20 | % 21 | % Revision 1.1 2009/02/11 22:04:02 msh 22 | % New routine to pick selected channels from the forward solution 23 | % 24 | % 25 | 26 | me='MNE:mne_pick_channels_forward'; 27 | 28 | if nargin == 1 29 | fwd = orig; 30 | return; 31 | elseif nargin == 2 32 | exclude = []; 33 | elseif nargin ~= 3 34 | error(me,'Incorrect number of arguments'); 35 | end 36 | 37 | if isempty(include) && isempty(exclude) 38 | fwd = orig; 39 | return; 40 | end 41 | 42 | if isempty(orig.sol.row_names) 43 | error(me,'Cannot pick from a forward solution without channel names'); 44 | end 45 | 46 | fwd = orig; 47 | % 48 | % First do the channels to be included 49 | % 50 | sel = fiff_pick_channels(orig.sol.row_names, include, exclude); 51 | if isempty(sel) 52 | error(me, 'Nothing remains after picking'); 53 | end 54 | fwd.sol.data = fwd.sol.data(sel,:); 55 | fwd.chs = fwd.chs(sel); 56 | % 57 | % Select the desired stuff 58 | % 59 | for p = 1:length(sel) 60 | names{p} = fwd.sol.row_names{sel(p)}; 61 | end 62 | fwd.sol.row_names = names; 63 | fwd.sol.nrow = length(sel); 64 | fwd.nchan = length(sel); 65 | % 66 | return; 67 | -------------------------------------------------------------------------------- /matlab/mne_read_curvature.m: -------------------------------------------------------------------------------- 1 | function [curv] = mne_read_curvature(fname) 2 | % 3 | % [curf] = mne_read_surface(fname) 4 | % 5 | % Reads a FreeSurfer curvature file 6 | % 7 | % fname - The file to read 8 | % curv - The curvature values 9 | % 10 | 11 | % 12 | % 13 | % Author : Matti Hamalainen, MGH Martinos Center 14 | % License : BSD 3-clause 15 | % 16 | % 17 | % Revision 1.4 2006/07/31 05:12:39 msh 18 | % fread3 was still called instead of mne_fread3 19 | % 20 | % Revision 1.3 2006/05/22 11:01:47 msh 21 | % Deleted superfluous text from the comments. 22 | % 23 | % Revision 1.2 2006/05/22 10:55:02 msh 24 | % Fixed help text. 25 | % 26 | % Revision 1.1 2006/05/22 10:44:44 msh 27 | % Added surface and curvature reading routines. 28 | % Fixed error in mne_read_source_spaces: triangle normals were not normalized 29 | % 30 | 31 | me='MNE:mne_read_curvature'; 32 | 33 | % 34 | % The input file is big endian 35 | % 36 | fid = fopen(fname,'rb','ieee-be'); 37 | 38 | if (fid < 0) 39 | error(me,'Cannot open file %s', fname); 40 | end; 41 | 42 | magic = mne_fread3(fid) ; 43 | NEW_VERSION_MAGIC_NUMBER = 16777215; 44 | if (magic == NEW_VERSION_MAGIC_NUMBER) 45 | nvert = fread(fid, 1, 'int32'); 46 | nface = fread(fid, 1, 'int32'); 47 | val_per_vertex = fread(fid, 1, 'int32'); 48 | if val_per_vertex ~= 1 49 | fclose(fid); 50 | error(me,'Values per vertex not equal to one'); 51 | end 52 | curv = fread(fid, nvert, 'float') ; 53 | fprintf(1,'\t%d values read from a new style curvature file.\n',nvert); 54 | else 55 | nvert = magic; 56 | nface = mne_fread3(fid); 57 | curv = fread(fid, nvert, 'int16') ./ 100 ; 58 | fprintf(1,'\t%d values read from an old style curvature file.\n',nvert); 59 | end 60 | 61 | fclose(fid); 62 | 63 | return; 64 | -------------------------------------------------------------------------------- /matlab/mne_read_epoch.m: -------------------------------------------------------------------------------- 1 | function [data,fid] = mne_read_epoch(epoch_info,which,prev_fid) 2 | % 3 | % [data,fid] = mne_read_epoch(epoch_info,which,prev_fid) 4 | % 5 | % Reads an epoch from a binary file produced by mne_epochs2mat 6 | % 7 | % epoch_info - The data structure read from the epoch data description file 8 | % which - Which epoch to read 9 | % prev_fid - Open file id from previous call 10 | % if prev_fid < 0 or missing, the file will be opened 11 | % The the current file id will be returned in the 12 | % output argument fid, if present. If this argument is 13 | % missing, file will be close upon exit from this function. 14 | % 15 | % The data will contain nchan x ntimes calibrated values 16 | % 17 | 18 | % 19 | % 20 | % Author : Matti Hamalainen, MGH Martinos Center 21 | % License : BSD 3-clause 22 | % 23 | % 24 | % $Id$ 25 | % 26 | % Revision 1.6 2006/05/03 19:03:19 msh 27 | % Eliminated the use of cast function for Matlab 6.5 compatibility 28 | % 29 | % Revision 1.5 2006/04/23 15:29:41 msh 30 | % Added MGH to the copyright 31 | % 32 | % Revision 1.4 2006/04/10 23:26:54 msh 33 | % Added fiff reading routines 34 | % 35 | % Revision 1.3 2006/02/21 03:35:35 msh 36 | % Improved help text of mne_read_epoch.m 37 | % 38 | % Revision 1.2 2006/02/21 03:26:02 msh 39 | % Improved mne_read_epoch.m 40 | % 41 | % Revision 1.1 2006/02/20 15:45:05 msh 42 | % Added mne_find_channel.m and mne_read_epoch.m 43 | % 44 | % 45 | me='MNE:mne_read_epoch'; 46 | if(nargin ~= 2 && nargin ~= 3) 47 | error(me,'Usage : [data,fid] = mne_read_epoch(epoch_info,which,prev_fid)'); 48 | end 49 | 50 | if (which < 0) 51 | error(me,'Epoch number must be positive'); 52 | end 53 | 54 | if (which > epoch_info.nepoch) 55 | error(me,'Epoch number too large'); 56 | end 57 | 58 | if (epoch_info.epochs(which,1) ~= 4) 59 | error(me,'Epoch is not in float format'); 60 | end 61 | 62 | % open it as a big-endian file 63 | if (nargin < 3 || prev_fid < 0) 64 | [fid,message] = fopen(epoch_info.epoch_file, 'rb', 'b') ; 65 | if (fid < 0) 66 | error(me,message); 67 | end 68 | else 69 | fid = prev_fid; 70 | end 71 | 72 | pos = double(epoch_info.epochs(which,2)); 73 | nsamp = double(epoch_info.epochs(which,5)); 74 | nchan = double(epoch_info.nchan); 75 | 76 | if (fseek(fid,pos,'bof') < 0) 77 | error(me,'Could not position file to the epoch'); 78 | end 79 | 80 | data = fread(fid, [nchan,nsamp], 'float','ieee-be'); 81 | cal = diag(epoch_info.ch_cals(:,1))*diag(epoch_info.ch_cals(:,2)); 82 | data = cal*data; 83 | 84 | if (nargout == 1) 85 | fclose(fid); 86 | end 87 | 88 | return; 89 | 90 | 91 | 92 | 93 | 94 | 95 | -------------------------------------------------------------------------------- /matlab/mne_read_events.m: -------------------------------------------------------------------------------- 1 | function [eventlist] = mne_read_events(filename) 2 | % 3 | % [eventlist] = mne_read_events(filename) 4 | % 5 | % Read an event list from a fif file 6 | % 7 | 8 | % 9 | % Author : Matti Hamalainen, MGH Martinos Center 10 | % License : BSD 3-clause 11 | % 12 | % 13 | % Revision 1.2 2008/08/21 18:03:16 msh 14 | % Fixed possibility for closing the file twice 15 | % 16 | % Revision 1.1 2008/06/16 17:27:50 msh 17 | % Added mne_read_events and mne_write_events functions 18 | % 19 | % 20 | global FIFF; 21 | if isempty(FIFF) 22 | FIFF = fiff_define_constants(); 23 | end 24 | 25 | me='MNE:mne_read_events'; 26 | 27 | 28 | % 29 | % Open file 30 | % 31 | [ fid, tree ] = fiff_open(filename); 32 | % 33 | % Find the desired block 34 | % 35 | events = fiff_dir_tree_find(tree,FIFF.FIFFB_MNE_EVENTS); 36 | if isempty(events) 37 | fclose(fid); 38 | error(me,'Could not find event data'); 39 | end 40 | 41 | eventlist = []; 42 | for k = 1:events.nent 43 | kind = events.dir(k).kind; 44 | pos = events.dir(k).pos; 45 | if kind == FIFF.FIFF_MNE_EVENT_LIST 46 | tag = fiff_read_tag(fid,pos); 47 | eventlist = tag.data; 48 | break; 49 | end 50 | end 51 | fclose(fid); 52 | if isempty(eventlist) 53 | error(me,'Could not find any events'); 54 | else 55 | eventlist = reshape(eventlist',3,size(eventlist,1)/3)'; 56 | end 57 | 58 | return; 59 | 60 | -------------------------------------------------------------------------------- /matlab/mne_read_label_file.m: -------------------------------------------------------------------------------- 1 | function [label] = mne_read_label_file(filename) 2 | % 3 | % [label] = mne_read_label_file(filename) 4 | % 5 | % Reads a label file. The returned structure has the following fields 6 | % 7 | % comment comment from the first line of the label file 8 | % vertices vertex indices (0 based, column 1) 9 | % pos locations in meters (columns 2 - 4 divided by 1000) 10 | % values values at the vertices (column 5) 11 | % 12 | 13 | % 14 | % 15 | % Author : Matti Hamalainen, MGH Martinos Center 16 | % License : BSD 3-clause 17 | % 18 | 19 | % 20 | % This is based on the FreeSurfer read_label routine 21 | % SUBJECTS_DIR environment variable is not consulted for the standard location 22 | % 23 | 24 | me='MNE:mne_read_label_file'; 25 | if(nargin ~= 1) 26 | error(me,'usage: mne_read_label_file(filename)'); 27 | end 28 | 29 | [fid,message] = fopen(filename,'r'); 30 | if (fid < 0) 31 | error(me,'Cannot open file %s (%s)', filename,message); 32 | end 33 | 34 | comment = fgets(fid) ; 35 | line = fgets(fid) ; 36 | nv = sscanf(line, '%d') ; 37 | data = fscanf(fid, '%d %f %f %f %f\n') ; 38 | data = reshape(data, 5, nv); 39 | 40 | for k = 2:length(comment) 41 | if comment(k) ~= ' ' 42 | break; 43 | end 44 | end 45 | if comment(length(comment)) == 10 46 | comment = comment(1:end-1); 47 | end 48 | label.comment = comment(k:end); 49 | label.vertices = int32(data(1,:)); 50 | label.pos = 1e-3*data(2:4,:)'; 51 | label.values = data(5,:); 52 | fclose(fid); 53 | 54 | 55 | -------------------------------------------------------------------------------- /matlab/mne_read_morph_map.m: -------------------------------------------------------------------------------- 1 | function [leftmap,rightmap] = mne_read_morph_map(from,to,subjects_dir) 2 | % 3 | % [leftmap,rightmap] = mne_read_morph_map(from,to,subjects_dir) 4 | % 5 | % Read the morphing map from subject 'from' to subject 'to'. 6 | % If subjects_dir is not specified, the SUBJECTS_DIR environment 7 | % variable is used 8 | % 9 | 10 | % 11 | % Author : Matti Hamalainen, MGH Martinos Center 12 | % License : BSD 3-clause 13 | % 14 | % 15 | % Revision 1.1 2008/03/25 21:06:08 msh 16 | % Added mne_read_morph_map function 17 | % 18 | % 19 | me='MNE:mne_read_morph_map'; 20 | 21 | global FIFF; 22 | if isempty(FIFF) 23 | FIFF = fiff_define_constants(); 24 | end 25 | 26 | if nargin < 3 27 | subjects_dir=getenv('SUBJECTS_DIR'); 28 | if isempty(subjects_dir) 29 | error(me,'SUBJECTS_DIR not set'); 30 | end 31 | end 32 | if nargin < 2 33 | error(me,'Not enough input arguments'); 34 | end 35 | % 36 | % Does the file exist 37 | % 38 | name = sprintf('%s/morph-maps/%s-%s-morph.fif',subjects_dir,from,to); 39 | if ~exist(name,'file') 40 | name = sprintf('%s/morph-maps/%s-%s-morph.fif',subjects_dir,to,from); 41 | if ~exist(name,'file') 42 | error(me,'The requested morph map does not exist'); 43 | end 44 | end 45 | % 46 | % Open it 47 | % 48 | [fid,tree] = fiff_open(name); 49 | % 50 | % Locate all maps 51 | % 52 | maps = fiff_dir_tree_find(tree,FIFF.FIFFB_MNE_MORPH_MAP); 53 | if isempty(maps) 54 | fclose(fid); 55 | error(me,'Morphing map data not found'); 56 | end 57 | % 58 | % Find the correct ones 59 | % 60 | for k = 1:length(maps) 61 | tag = find_tag(maps(k),FIFF.FIFF_MNE_MORPH_MAP_FROM); 62 | if strcmp(tag.data,from) 63 | tag = find_tag(maps(k),FIFF.FIFF_MNE_MORPH_MAP_TO); 64 | if strcmp(tag.data,to) 65 | % 66 | % Names match: which hemishere is this? 67 | % 68 | tag = find_tag(maps(k),FIFF.FIFF_MNE_HEMI); 69 | if tag.data == FIFF.FIFFV_MNE_SURF_LEFT_HEMI 70 | tag = find_tag(maps(k),FIFF.FIFF_MNE_MORPH_MAP); 71 | leftmap = tag.data; 72 | fprintf(1,'\tLeft-hemisphere map read.\n'); 73 | elseif tag.data == FIFF.FIFFV_MNE_SURF_RIGHT_HEMI 74 | tag = find_tag(maps(k),FIFF.FIFF_MNE_MORPH_MAP); 75 | rightmap = tag.data; 76 | fprintf(1,'\tRight-hemisphere map read.\n'); 77 | end 78 | end 79 | end 80 | end 81 | fclose(fid); 82 | if ~exist('leftmap') 83 | error(me,'Left hemisphere map not found in %s',name); 84 | end 85 | if ~exist('rightmap') 86 | error(me,'Left hemisphere map not found in %s',name); 87 | end 88 | 89 | return; 90 | 91 | function [tag] = find_tag(node,findkind) 92 | 93 | for p = 1:node.nent 94 | if node.dir(p).kind == findkind 95 | tag = fiff_read_tag(fid,node.dir(p).pos); 96 | return; 97 | end 98 | end 99 | tag = []; 100 | return; 101 | end 102 | 103 | end 104 | -------------------------------------------------------------------------------- /matlab/mne_read_noise_cov.m: -------------------------------------------------------------------------------- 1 | function [cov] = mne_read_noise_cov(fname) 2 | % 3 | % [cov] = mne_read_noise_cov(fname) 4 | % 5 | % Reads a noise-covariance matrix from a fiff file 6 | % 7 | % fname - The name of the file 8 | % 9 | % 10 | 11 | % 12 | % 13 | % Author : Matti Hamalainen, MGH Martinos Center 14 | % License : BSD 3-clause 15 | % 16 | % Revision 1.5 2006/05/03 19:09:03 msh 17 | % Fixed even more compatibility issues. 18 | % 19 | % Revision 1.4 2006/04/23 15:29:41 msh 20 | % Added MGH to the copyright 21 | % 22 | % Revision 1.3 2006/04/20 21:49:38 msh 23 | % Added mne_read_inverse_operator 24 | % Changed some of the routines accordingly for more flexibility. 25 | % 26 | % Revision 1.2 2006/04/18 20:44:46 msh 27 | % Added reading of forward solution. 28 | % Use length instead of size when appropriate 29 | % 30 | % Revision 1.1 2006/04/12 17:09:28 msh 31 | % Added routines for reading noise-covariance matrices 32 | % 33 | % 34 | 35 | me='MNE:mne_read_noise_cov'; 36 | 37 | global FIFF; 38 | if isempty(FIFF) 39 | FIFF = fiff_define_constants(); 40 | end 41 | % 42 | % Open the file, create directory 43 | % 44 | [ fid, tree ] = fiff_open(fname); 45 | try 46 | cov = mne_read_cov(fid,tree,FIFF.FIFFV_MNE_NOISE_COV); 47 | catch 48 | fclose(fid); 49 | error(me,'%s',mne_omit_first_line(lasterr)); 50 | end 51 | 52 | return; 53 | 54 | end 55 | -------------------------------------------------------------------------------- /matlab/mne_read_stc_file.m: -------------------------------------------------------------------------------- 1 | function [stc] = mne_read_stc_file(filename) 2 | % 3 | % [stc] = mne_read_stc_file(filename) 4 | % 5 | % Reads an stc file. The returned structure has the following fields 6 | % 7 | % tmin The first time point of the data in seconds 8 | % tstep Time between frames in seconds 9 | % vertices vertex indices (0 based) 10 | % data The data matrix (nvert * ntime) 11 | % 12 | % 13 | 14 | % 15 | % 16 | % Author : Matti Hamalainen, MGH Martinos Center 17 | % License : BSD 3-clause 18 | % 19 | % Revision 1.7 2006/04/23 15:29:41 msh 20 | % Added MGH to the copyright 21 | % 22 | % Revision 1.6 2006/04/10 23:26:54 msh 23 | % Added fiff reading routines 24 | % 25 | % Revision 1.5 2005/12/05 20:23:21 msh 26 | % Added fiff_save_evoked. Improved error handling. 27 | % 28 | % Revision 1.4 2005/11/21 05:40:24 msh 29 | % Required number of arguments was wrong. 30 | % 31 | % Revision 1.3 2005/11/21 03:19:12 msh 32 | % Improved error handling 33 | % 34 | % Revision 1.2 2005/11/21 02:15:51 msh 35 | % Added more routines 36 | % 37 | % Revision 1.1 2005/11/21 01:41:57 msh 38 | % Introduced structures and start all function names with mne_ 39 | % 40 | % 41 | me='MNE:mne_read_stc_file'; 42 | if(nargin ~= 1) 43 | error(me,'usage: [stc] = mne_read_stc_file(filename)'); 44 | end 45 | 46 | [fid,message] = fopen(filename,'r','ieee-be'); 47 | if fid == -1 48 | error(me,message) 49 | end 50 | 51 | STATUS = fseek(fid, 0, 'eof'); 52 | filelen= ftell(fid); 53 | STATUS = fseek(fid, 0, 'bof'); 54 | 55 | % read tmin in ms 56 | [stc.tmin count]=fread(fid,1,'float32'); 57 | stc.tmin=stc.tmin/1000.0; 58 | % read sampling rate in ms 59 | [stc.tstep count]=fread(fid,1,'float32'); 60 | stc.tstep=stc.tstep/1000.0; 61 | % read number of vertices/sources 62 | [vertices_n count]=fread(fid,1,'uint32'); 63 | % read the source vector 64 | [stc.vertices count]=fread(fid,vertices_n,'uint32'); 65 | stc.vertices=uint32(stc.vertices); 66 | % read the number of timepts 67 | [data_n count]=fread(fid,1,'uint32'); 68 | 69 | if (mod((filelen/4-4-vertices_n),data_n*vertices_n) ~= 0) 70 | error(me,'incorrect stc file size') 71 | end 72 | 73 | 74 | % read the data matrix 75 | [stc.data count]=fread(fid,vertices_n*data_n,'float32'); 76 | stc.data=squeeze(reshape(stc.data,vertices_n,data_n)); 77 | % close the file 78 | fclose(fid); 79 | return; 80 | 81 | -------------------------------------------------------------------------------- /matlab/mne_read_stc_file1.m: -------------------------------------------------------------------------------- 1 | function [stc] = mne_read_stc_file1(filename) 2 | % 3 | % [stc] = mne_read_stc_file1(filename) 4 | % 5 | % Reads an stc file. The returned structure has the following fields 6 | % 7 | % tmin The first time point of the data in seconds 8 | % tstep Time between frames in seconds 9 | % vertices vertex indices (1 based) 10 | % data The data matrix (nvert * ntime) 11 | % 12 | % 13 | 14 | % 15 | % 16 | % Author : Matti Hamalainen, MGH Martinos Center 17 | % License : BSD 3-clause 18 | % 19 | me='MNE:mne_read_stc_file1'; 20 | if(nargin ~= 1) 21 | error(me,'usage: [stc] = mne_read_stc_file1(filename)'); 22 | end 23 | 24 | try 25 | stc = mne_read_stc_file(filename); 26 | stc.vertices = stc.vertices + 1; 27 | catch 28 | error(me,'%s',mne_omit_first_line(lasterr)); 29 | end 30 | 31 | return; 32 | 33 | 34 | -------------------------------------------------------------------------------- /matlab/mne_read_w_file.m: -------------------------------------------------------------------------------- 1 | function [w] = mne_read_w_file(filename) 2 | % 3 | % [w] = mne_read_w_file(filename) 4 | % 5 | % Reads a binary w file into the structure w with the following fields 6 | % 7 | % vertices - vector of vertex indices (0-based) 8 | % data - vector of data values 9 | % 10 | 11 | % 12 | % Author : Matti Hamalainen, MGH Martinos Center 13 | % License : BSD 3-clause 14 | % 15 | % 16 | % $Id$ 17 | % 18 | % Revision 1.6 2006/04/23 15:29:41 msh 19 | % Added MGH to the copyright 20 | % 21 | % Revision 1.5 2006/04/10 23:26:54 msh 22 | % Added fiff reading routines 23 | % 24 | % Revision 1.4 2005/12/05 20:23:21 msh 25 | % Added fiff_save_evoked. Improved error handling. 26 | % 27 | % Revision 1.3 2005/11/21 03:19:12 msh 28 | % Improved error handling 29 | % 30 | % Revision 1.2 2005/11/21 02:15:51 msh 31 | % Added more routines 32 | % 33 | % Revision 1.1 2005/11/21 01:41:57 msh 34 | % Introduced structures and start all function names with mne_ 35 | % 36 | % 37 | me='MNE:mne_read_w_file'; 38 | if(nargin ~= 1) 39 | error(me,'usage: [w] = mne_read_w_file(filename)'); 40 | end 41 | 42 | % open it as a big-endian file 43 | [fid,message] = fopen(filename, 'rb', 'b') ; 44 | if (fid < 0) 45 | error(me,message); 46 | end 47 | 48 | fread(fid, 1, 'int16') ; 49 | vnum = mne_fread3(fid) ; 50 | w.data = zeros(vnum,1) ; 51 | w.vertices = zeros(vnum,1) ; 52 | for i=1:vnum 53 | w.vertices(i) = mne_fread3(fid) ; % vertex number (0-based) 54 | w.data(i) = fread(fid, 1, 'float') ; % vertex value 55 | end 56 | w.vertices = uint32(w.vertices); 57 | 58 | fclose(fid) ; 59 | return; 60 | 61 | 62 | 63 | 64 | 65 | 66 | -------------------------------------------------------------------------------- /matlab/mne_read_w_file1.m: -------------------------------------------------------------------------------- 1 | function [w] = mne_read_w_file1(filename) 2 | % 3 | % [w] = mne_read_w_file(filename) 4 | % 5 | % Reads a binary w file into the structure w with the following fields 6 | % 7 | % vertices - vector of vertex indices (1-based) 8 | % data - vector of data values 9 | % 10 | 11 | % 12 | % Author : Matti Hamalainen, MGH Martinos Center 13 | % License : BSD 3-clause 14 | % 15 | 16 | me='MNE:mne_read_w_file1'; 17 | if(nargin ~= 1) 18 | error(me,'usage: [w] = mne_read_w_file1(filename)'); 19 | end 20 | 21 | try 22 | w = mne_read_w_file(filename); 23 | w.vertices = w.vertices + 1; 24 | catch 25 | error(me,'%s',mne_omit_first_line(lasterr)); 26 | end 27 | 28 | return; 29 | -------------------------------------------------------------------------------- /matlab/mne_reduce_surface.m: -------------------------------------------------------------------------------- 1 | function [verts, faces] = mne_reduce_surface(surfin,desired_ntri,surfout) 2 | % 3 | % [verts, faces] = mne_reduce_surface(surfin,desired_ntri,surfout) 4 | % 5 | % verts - Vertex coordinates in meters 6 | % faces - The triangulation information 7 | % 8 | % surfin - Name of a surface file to read 9 | % desired_nri - Desired number of triangles after reduction 10 | % surfout - Name of a surface file to hold the reduce surface (optional) 11 | % 12 | 13 | % 14 | % Author : Matti Hamalainen, MGH Martinos Center 15 | % License : BSD 3-clause 16 | % 17 | % 18 | % Revision 1.2 2009/02/03 10:26:13 msh 19 | % Added reading of 'new' quadrangle files 20 | % 21 | % Revision 1.1 2009/01/28 20:43:52 msh 22 | % Added mne_reduce_surface 23 | % 24 | % 25 | me='MNE:mne_reduce_surface'; 26 | 27 | if nargin < 2 28 | error(me,'Incorrect number of input arguments'); 29 | end 30 | 31 | fprintf(1,'Reading %s...\n',surfin); 32 | [surf.vertices,surf.faces] = mne_read_surface(surfin); 33 | 34 | ratio = desired_ntri/size(surf.faces,1); 35 | 36 | fprintf(1,'Reducing the number of triangles...'); 37 | surf2 = reducepatch(surf,ratio); 38 | fprintf(1,'[done]\n'); 39 | 40 | fprintf(1,'After reduction: %d vertices and %d triangles\n',size(surf2.vertices,1),size(surf2.faces,1)); 41 | 42 | if nargin == 3 43 | mne_write_surface(surfout,surf2.vertices,surf2.faces); 44 | end 45 | 46 | verts = surf2.vertices; 47 | faces = surf2.faces; 48 | 49 | return 50 | -------------------------------------------------------------------------------- /matlab/mne_rt_buffer_reader.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mne-tools/mne-matlab/e5a26779313d2d7b4cba2b0b69d0e32f48a84d3f/matlab/mne_rt_buffer_reader.class -------------------------------------------------------------------------------- /matlab/mne_rt_buffer_reader.java: -------------------------------------------------------------------------------- 1 | //============================================================================================================= 2 | /** 3 | * @file mne_rt_buffer_reader.java 4 | * @author Christoph Dinh ; 5 | * Matti Hamalainen 6 | * @version 1.0 7 | * @date July, 2012 8 | * 9 | * @section LICENSE 10 | * 11 | * Copyright (C) 2012, Christoph Dinh and Matti Hamalainen. All rights reserved. 12 | * 13 | * Redistribution and use in source and binary forms, with or without modification, are permitted provided that 14 | * the following conditions are met: 15 | * * Redistributions of source code must retain the above copyright notice, this list of conditions and the 16 | * following disclaimer. 17 | * * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and 18 | * the following disclaimer in the documentation and/or other materials provided with the distribution. 19 | * * Neither the name of the Massachusetts General Hospital nor the names of its contributors may be used 20 | * to endorse or promote products derived from this software without specific prior written permission. 21 | * 22 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED 23 | * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A 24 | * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MASSACHUSETTS GENERAL HOSPITAL BE LIABLE FOR ANY DIRECT, 25 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 26 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 27 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 28 | * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29 | * POSSIBILITY OF SUCH DAMAGE. 30 | * 31 | * 32 | * @brief Contains the implementation of the MNERTBufferReader Class, to realize fast array reading. 33 | * 34 | */ 35 | 36 | import java.io.*; 37 | 38 | class mne_rt_buffer_reader 39 | { 40 | public mne_rt_buffer_reader(DataInput p_DataInputStream) 41 | { 42 | m_DataInputStream = p_DataInputStream; 43 | } 44 | 45 | public byte[] readBuffer(int p_iLength) 46 | { 47 | byte[] t_byteBuffer = new byte[p_iLength]; 48 | 49 | try 50 | { 51 | m_DataInputStream.readFully(t_byteBuffer, 0, p_iLength); 52 | } 53 | 54 | catch (StreamCorruptedException e) 55 | { 56 | System.out.println("Stream Corrupted Exception Occured"); 57 | t_byteBuffer = new byte[0]; 58 | } 59 | catch (EOFException e) 60 | { 61 | System.out.println("EOF Reached"); 62 | t_byteBuffer = new byte[0]; 63 | } 64 | catch (IOException e) 65 | { 66 | System.out.println("IO Exception Occured"); 67 | t_byteBuffer = new byte[0]; 68 | } 69 | 70 | return t_byteBuffer; 71 | } 72 | 73 | private DataInput m_DataInputStream; 74 | } 75 | -------------------------------------------------------------------------------- /matlab/mne_rt_define_commands.m: -------------------------------------------------------------------------------- 1 | function [ MNE_RT ] = mne_rt_define_commands() 2 | % 3 | % [ FIFF ] = mne_rt_define_commands() 4 | % 5 | % Defines structure containing the MNE_RT constants 6 | % 7 | 8 | % 9 | % Author : Christoph Dinh, Matti Hamalainen, MGH Martinos Center 10 | % License : BSD 3-clause 11 | 12 | % 13 | % MNE RT Communication Constants 14 | % 15 | MNE_RT.MNE_RT_GET_CLIENT_ID = 1; % Request client id at mne_rt_server 16 | MNE_RT.MNE_RT_SET_CLIENT_ALIAS = 2; % Set client alias at mne_rt_server 17 | 18 | return 19 | 20 | end 21 | -------------------------------------------------------------------------------- /matlab/mne_set_current_comp.m: -------------------------------------------------------------------------------- 1 | function new_chs = mne_set_current_comp(chs,value) 2 | % 3 | % mne_set_current_comp(chs,value) 4 | % 5 | % Set the current compensation value in the channel info structures 6 | % 7 | 8 | 9 | % 10 | % Author : Matti Hamalainen, MGH Martinos Center 11 | % License : BSD 3-clause 12 | % 13 | % 14 | % Revision 1.5 2008/11/18 02:38:51 msh 15 | % Modified mne_ex_read_evoked to apply projection and compensation 16 | % Modified mne_ex_read_raw to call mne_set_current_comp 17 | % 18 | % Revision 1.4 2006/04/23 15:29:41 msh 19 | % Added MGH to the copyright 20 | % 21 | % Revision 1.3 2006/04/18 20:44:46 msh 22 | % Added reading of forward solution. 23 | % Use length instead of size when appropriate 24 | % 25 | % Revision 1.2 2006/04/14 15:49:49 msh 26 | % Improved the channel selection code and added ch_names to measurement info. 27 | % 28 | % Revision 1.1 2006/04/12 10:51:19 msh 29 | % Added projection writing and compensation routines 30 | % 31 | % 32 | 33 | me='MNE:mne_set_current_comp'; 34 | 35 | global FIFF; 36 | if isempty(FIFF) 37 | FIFF = fiff_define_constants(); 38 | end 39 | 40 | new_chs = chs; 41 | 42 | lower_half = hex2dec('FFFF'); 43 | for k = 1:length(chs) 44 | if chs(k).kind == FIFF.FIFFV_MEG_CH 45 | coil_type = bitand(double(chs(k).coil_type),lower_half); 46 | new_chs(k).coil_type = int32(bitor(coil_type,bitshift(value,16))); 47 | end 48 | end 49 | 50 | return; 51 | 52 | -------------------------------------------------------------------------------- /matlab/mne_transform_source_space_to.m: -------------------------------------------------------------------------------- 1 | function [res] = mne_transform_source_space_to(src,dest,trans) 2 | % 3 | % [res] = mne_transform_source_space_to(src,dest,trans) 4 | % 5 | % Transform source space data to the desired coordinate system 6 | % 7 | % src - The source space to transform 8 | % dest - The id of the destination coordinate system (FIFFV_COORD_...) 9 | % trans - The coordinate transformation structure to use 10 | % 11 | 12 | % 13 | % 14 | % Author : Matti Hamalainen, MGH Martinos Center 15 | % License : BSD 3-clause 16 | % 17 | % Revision 1.4 2008/04/04 16:00:28 msh 18 | % Forgot to set the coordinate frame of the result correctly 19 | % 20 | % Revision 1.3 2006/05/03 18:53:06 msh 21 | % Approaching Matlab 6.5 backward compatibility 22 | % 23 | % Revision 1.2 2006/04/23 15:29:41 msh 24 | % Added MGH to the copyright 25 | % 26 | % Revision 1.1 2006/04/18 23:21:22 msh 27 | % Added mne_transform_source_space_to and mne_transpose_named_matrix 28 | % 29 | % 30 | 31 | me='MNE:mne_transform_source_space_to'; 32 | 33 | global FIFF; 34 | if isempty(FIFF) 35 | FIFF = fiff_define_constants(); 36 | end 37 | 38 | if nargin ~= 3 39 | error(me,'Incorrect number of arguments'); 40 | end 41 | 42 | if src.coord_frame == dest 43 | res = src; 44 | return; 45 | end 46 | 47 | if trans.to == src.coord_frame && trans.from == dest 48 | trans = fiff_invert_transform(trans); 49 | elseif trans.from ~= src.coord_frame || trans.to ~= dest 50 | error(me,'Cannot transform the source space using this coordinate transformation'); 51 | end 52 | 53 | t = trans.trans(1:3,:); 54 | res = src; 55 | res.coord_frame = dest; 56 | res.rr = (t*[ res.rr ones(res.np,1) ]')'; 57 | res.nn = (t*[ res.nn zeros(res.np,1) ]')'; 58 | 59 | return; 60 | 61 | end 62 | 63 | 64 | -------------------------------------------------------------------------------- /matlab/mne_transpose_named_matrix.m: -------------------------------------------------------------------------------- 1 | function [res] = mne_transpose_named_matrix(mat) 2 | % 3 | % [res] = mne_transpose_named_matrix(mat) 4 | % 5 | % Transpose a named matrix 6 | % 7 | 8 | % 9 | % 10 | % Author : Matti Hamalainen, MGH Martinos Center 11 | % License : BSD 3-clause 12 | % 13 | % Revision 1.2 2006/04/23 15:29:41 msh 14 | % Added MGH to the copyright 15 | % 16 | % Revision 1.1 2006/04/18 23:21:22 msh 17 | % Added mne_transform_source_space_to and mne_transpose_named_matrix 18 | % 19 | % 20 | 21 | res.nrow = mat.ncol; 22 | res.ncol = mat.nrow; 23 | res.row_names = mat.col_names; 24 | res.col_names = mat.row_names; 25 | res.data = mat.data'; 26 | 27 | return; 28 | 29 | end 30 | 31 | -------------------------------------------------------------------------------- /matlab/mne_write_cov.m: -------------------------------------------------------------------------------- 1 | function mne_write_cov(fid,cov) 2 | % 3 | % 4 | % mne_write_cov(fid,cov) 5 | % 6 | % Write a covariance matrix to an open file 7 | % 8 | % fid - an open file id 9 | % cov - the covariance matrix to write 10 | % 11 | 12 | % 13 | % Author : Matti Hamalainen, MGH Martinos Center 14 | % License : BSD 3-clause 15 | % 16 | % Revision 1.4 2007/12/10 01:02:55 msh 17 | % Fixed writing of a diagonal covariance matrix 18 | % 19 | % Revision 1.3 2006/05/05 03:50:40 msh 20 | % Added routines to compute L2-norm inverse solutions. 21 | % Added mne_write_inverse_sol_stc to write them in stc files 22 | % Several bug fixes in other files 23 | % 24 | % Revision 1.2 2006/05/03 18:53:06 msh 25 | % Approaching Matlab 6.5 backward compatibility 26 | % 27 | % Revision 1.1 2006/04/29 12:44:10 msh 28 | % Added covariance matrix writing routines. 29 | % 30 | % 31 | 32 | me='MNE:mne_write_cov'; 33 | 34 | global FIFF; 35 | if isempty(FIFF) 36 | FIFF = fiff_define_constants(); 37 | end 38 | 39 | fiff_start_block(fid,FIFF.FIFFB_MNE_COV); 40 | % 41 | % Dimensions etc. 42 | % 43 | fiff_write_int(fid,FIFF.FIFF_MNE_COV_KIND,cov.kind); 44 | fiff_write_int(fid,FIFF.FIFF_MNE_COV_DIM,cov.dim); 45 | if cov.nfree > 0 46 | fiff_write_int(fid,FIFF.FIFF_MNE_COV_NFREE,cov.nfree); 47 | end 48 | % 49 | % Channel names 50 | % 51 | if ~isempty(cov.names) 52 | fiff_write_name_list(fid,FIFF.FIFF_MNE_ROW_NAMES,cov.names); 53 | end 54 | % 55 | % Data 56 | % 57 | if cov.diag 58 | fiff_write_double(fid,FIFF.FIFF_MNE_COV_DIAG,cov.data); 59 | else 60 | if issparse(cov.data) 61 | fiff_write_float_sparse_rcs(fid,FIFF.FIFF_MNE_COV,cov.data); 62 | else 63 | q = 1; 64 | vals = zeros(cov.dim*(cov.dim+1)/2,1); 65 | for j = 1:cov.dim 66 | for k = 1:j 67 | vals(q) = cov.data(j,k); 68 | q = q + 1; 69 | end 70 | end 71 | fiff_write_double(fid,FIFF.FIFF_MNE_COV,vals); 72 | end 73 | end 74 | % 75 | % Eigenvalues and vectors if present 76 | % 77 | if ~isempty(cov.eig) && ~isempty(cov.eigvec) 78 | fiff_write_float_matrix(fid,FIFF.FIFF_MNE_COV_EIGENVECTORS,cov.eigvec); 79 | fiff_write_double(fid,FIFF.FIFF_MNE_COV_EIGENVALUES,cov.eig); 80 | end 81 | % 82 | % Projection operator 83 | % 84 | fiff_write_proj(fid,cov.projs); 85 | % 86 | % Bad channels 87 | % 88 | if ~isempty(cov.bads) 89 | fiff_start_block(fid,FIFF.FIFFB_MNE_BAD_CHANNELS); 90 | fiff_write_name_list(fid,FIFF.FIFF_MNE_CH_NAME_LIST,cov.bads); 91 | fiff_end_block(fid,FIFF.FIFFB_MNE_BAD_CHANNELS); 92 | end 93 | % 94 | % Done! 95 | % 96 | fiff_end_block(fid,FIFF.FIFFB_MNE_COV); 97 | -------------------------------------------------------------------------------- /matlab/mne_write_cov_file.m: -------------------------------------------------------------------------------- 1 | function mne_write_cov_file(fname,cov) 2 | % 3 | % function mne_write_cov_file(name,cov) 4 | % 5 | % Write a complete fif file containing a covariance matrix 6 | % 7 | % fname filename 8 | % cov the covariance matrix to write 9 | % 10 | % 11 | 12 | % 13 | % Author : Matti Hamalainen, MGH Martinos Center 14 | % License : BSD 3-clause 15 | % 16 | % Revision 1.3 2008/10/10 16:13:57 msh 17 | % Added mne_ex_read_epochs. Fixed help text of mne_write_cov_file.m 18 | % 19 | % Revision 1.2 2006/05/03 18:53:06 msh 20 | % Approaching Matlab 6.5 backward compatibility 21 | % 22 | % Revision 1.1 2006/04/29 12:44:10 msh 23 | % Added covariance matrix writing routines. 24 | % 25 | % 26 | 27 | me='MNE:mne_write_cov_file'; 28 | 29 | global FIFF; 30 | if isempty(FIFF) 31 | FIFF = fiff_define_constants(); 32 | end 33 | 34 | fid = fiff_start_file(fname); 35 | 36 | try 37 | mne_write_cov(fid,cov); 38 | catch 39 | delete(fname); 40 | error(me,'%s',mne_omit_first_line(lasterr)); 41 | end 42 | 43 | 44 | fiff_end_file(fid); 45 | 46 | return; 47 | 48 | 49 | end 50 | -------------------------------------------------------------------------------- /matlab/mne_write_events.m: -------------------------------------------------------------------------------- 1 | function mne_write_events(filename,eventlist) 2 | % 3 | % mne_write_events(filename,eventlist) 4 | % 5 | % Write an event list into a fif file 6 | % 7 | 8 | % 9 | % Author : Matti Hamalainen, MGH Martinos Center 10 | % License : BSD 3-clause 11 | % 12 | % Revision 1.2 2008/10/31 13:07:05 msh 13 | % Added mne_make_combined_event_file function 14 | % 15 | % Revision 1.1 2008/06/16 17:27:50 msh 16 | % Added mne_read_events and mne_write_events functions 17 | % 18 | 19 | global FIFF; 20 | if isempty(FIFF) 21 | FIFF = fiff_define_constants(); 22 | end 23 | 24 | me='MNE:mne_write_events'; 25 | 26 | eventlist = reshape(eventlist',numel(eventlist),1); 27 | % 28 | % Start writing... 29 | % 30 | fid = fiff_start_file(filename); 31 | 32 | fiff_start_block(fid,FIFF.FIFFB_MNE_EVENTS); 33 | fiff_write_int(fid,FIFF.FIFF_MNE_EVENT_LIST,eventlist); 34 | fiff_end_block(fid,FIFF.FIFFB_MNE_EVENTS); 35 | 36 | fiff_end_file(fid); 37 | -------------------------------------------------------------------------------- /matlab/mne_write_inverse_sol_stc.m: -------------------------------------------------------------------------------- 1 | function mne_write_inverse_sol_stc(stem,inv,sol,tmin,tstep) 2 | % 3 | % function mne_write_inverse_sol_stc(stem,inv,sol,tmin,tstep) 4 | % 5 | % Save dynamic inverse solution data into stc files 6 | % 7 | % stem - Stem for the stc files 8 | % inv - The inverse operator structure (can be the forward operator as well) 9 | % sol - A solution matrix (locations x time) 10 | % tmin - Time of the first data point in seconds 11 | % tstep - Time between data points in seconds 12 | % 13 | 14 | % 15 | % 16 | % Author : Matti Hamalainen, MGH Martinos Center 17 | % License : BSD 3-clause 18 | % 19 | % Revision 1.3 2009/03/04 03:47:15 msh 20 | % Fixed comments and added a note that the source spaces can come from a forward operator as well 21 | % 22 | % Revision 1.2 2006/09/14 22:12:48 msh 23 | % Added output of the files written. 24 | % 25 | % Revision 1.1 2006/05/05 03:50:40 msh 26 | % Added routines to compute L2-norm inverse solutions. 27 | % Added mne_write_inverse_sol_stc to write them in stc files 28 | % Several bug fixes in other files 29 | % 30 | % 31 | 32 | me='MNE:mne_write_inverse_sol_stc'; 33 | global FIFF; 34 | if isempty(FIFF) 35 | FIFF = fiff_define_constants(); 36 | end 37 | 38 | if nargin ~= 5 39 | error(me,'Incorrect number of arguments'); 40 | end 41 | 42 | if size(sol,1) ~= inv.nsource 43 | error(me,'The solution matrix cannot correspond to these source spaces'); 44 | end 45 | 46 | off = 0; 47 | for k = 1:length(inv.src) 48 | off = off + inv.src(k).nuse; 49 | if (inv.src(k).id ~= FIFF.FIFFV_MNE_SURF_LEFT_HEMI && ... 50 | inv.src(k).id ~= FIFF.FIFFV_MNE_SURF_RIGHT_HEMI) 51 | error(me,'Source space hemispheres not properly assigned.'); 52 | end 53 | end 54 | if off ~= inv.nsource 55 | error(me,'The source spaces are inconsistent with other inverse/forward operator data'); 56 | end 57 | % 58 | % Write a separate stc file for each source space 59 | % 60 | off = 0; 61 | for k = 1:length(inv.src) 62 | if (inv.src(k).id == FIFF.FIFFV_MNE_SURF_LEFT_HEMI) 63 | outname = sprintf('%s-lh.stc',stem); 64 | elseif (inv.src(k).id == FIFF.FIFFV_MNE_SURF_RIGHT_HEMI) 65 | outname = sprintf('%s-rh.stc',stem); 66 | end 67 | stc.tmin = tmin; 68 | stc.tstep = tstep; 69 | stc.vertices = inv.src(k).vertno - 1; 70 | stc.data = sol(off+1:off+inv.src(k).nuse,:); 71 | mne_write_stc_file(outname,stc); 72 | off = off + inv.src(k).nuse; 73 | fprintf(1,'Wrote %s\n',outname); 74 | end 75 | 76 | return; 77 | 78 | 79 | 80 | -------------------------------------------------------------------------------- /matlab/mne_write_inverse_sol_w.m: -------------------------------------------------------------------------------- 1 | function mne_write_inverse_sol_w(stem,inv,sol) 2 | % 3 | % function mne_write_inverse_sol_w(stem,inv,sol) 4 | % 5 | % Save static inverse solution data into stc files 6 | % 7 | % stem - Stem for the w files 8 | % inv - The inverse operator structure (can be the forward operator as well) 9 | % sol - The solution matrix (number of locations) 10 | % 11 | 12 | % 13 | % 14 | % Author : Matti Hamalainen, MGH Martinos Center 15 | % License : BSD 3-clause 16 | % 17 | % Revision 1.2 2009/03/04 22:04:31 msh 18 | % Fixed typos in comments 19 | % 20 | % Revision 1.1 2009/03/04 03:50:15 msh 21 | % Added mne_write_inverse_sol_w 22 | % 23 | % Revision 1.2 2006/09/14 22:12:48 msh 24 | % Added output of the files written. 25 | % 26 | % Revision 1.1 2006/05/05 03:50:40 msh 27 | % Added routines to compute L2-norm inverse solutions. 28 | % Added mne_write_inverse_sol_stc to write them in stc files 29 | % Several bug fixes in other files 30 | % 31 | % 32 | 33 | me='MNE:mne_write_inverse_sol_w'; 34 | global FIFF; 35 | if isempty(FIFF) 36 | FIFF = fiff_define_constants(); 37 | end 38 | 39 | if nargin ~= 3 40 | error(me,'Incorrect number of arguments'); 41 | end 42 | 43 | if length(sol) ~= inv.nsource 44 | error(me,'The solution data cannot correspond to these source spaces'); 45 | end 46 | 47 | off = 0; 48 | for k = 1:length(inv.src) 49 | off = off + inv.src(k).nuse; 50 | if (inv.src(k).id ~= FIFF.FIFFV_MNE_SURF_LEFT_HEMI && ... 51 | inv.src(k).id ~= FIFF.FIFFV_MNE_SURF_RIGHT_HEMI) 52 | error(me,'Source space hemispheres not properly assigned.'); 53 | end 54 | end 55 | if off ~= inv.nsource 56 | error(me,'The source spaces are inconsistent with other inverse/forward operator data'); 57 | end 58 | % 59 | % Write a separate stc file for each source space 60 | % 61 | off = 0; 62 | for k = 1:length(inv.src) 63 | if (inv.src(k).id == FIFF.FIFFV_MNE_SURF_LEFT_HEMI) 64 | outname = sprintf('%s-lh.w',stem); 65 | elseif (inv.src(k).id == FIFF.FIFFV_MNE_SURF_RIGHT_HEMI) 66 | outname = sprintf('%s-rh.w',stem); 67 | end 68 | w.vertices = double(inv.src(k).vertno - 1); 69 | w.data = sol(off+1:off+inv.src(k).nuse); 70 | mne_write_w_file(outname,w); 71 | off = off + inv.src(k).nuse; 72 | fprintf(1,'Wrote %s\n',outname); 73 | end 74 | 75 | return; 76 | 77 | 78 | 79 | -------------------------------------------------------------------------------- /matlab/mne_write_label_file.m: -------------------------------------------------------------------------------- 1 | function mne_write_label_file(filename,label) 2 | % 3 | % write_read_label_file(filename,label) 4 | % 5 | % Writes label file. The returned structure has the following fields 6 | % 7 | % filename output file 8 | % label a stucture containing the stc data with fields: 9 | % 10 | % comment comment for the first line of the label file 11 | % vertices vertex indices (0 based, column 1) 12 | % pos locations in meters (columns 2 - 4 divided by 1000) 13 | % values values at the vertices (column 5) 14 | % 15 | 16 | % 17 | % 18 | % Author : Matti Hamalainen, MGH Martinos Center 19 | % License : BSD 3-clause 20 | % 21 | 22 | % 23 | % This is based on the FreeSurfer read_label routine 24 | % SUBJECTS_DIR environment variable is not consulted for the standard location 25 | % 26 | 27 | me='MNE:mne_write_label_file'; 28 | if(nargin ~= 2) 29 | error(me,'usage: mne_read_label_file(filename, label)'); 30 | end 31 | 32 | if length(label.vertices) ~= length(label.values) || length(label.vertices) ~= size(label.pos,1) || size(label.pos,2) ~= 3 33 | error(me,'fields of the label structure have conflicting or incorrect dimensions'); 34 | end 35 | 36 | [fid,message] = fopen(filename,'w'); 37 | if (fid < 0) 38 | error(me,'Cannot open file %s (%s)', filename,message); 39 | end; 40 | 41 | if ~isempty(label.comment) 42 | fprintf(fid,'# %s\n',label.comment); 43 | else 44 | fprintf(fid,'# label file written with mne_write_label\n'); 45 | end 46 | fprintf(fid,'%d\n',length(label.vertices)); 47 | for k = 1:length(label.vertices) 48 | fprintf(fid,'%d %.2f %.2f %.2f %f\n',label.vertices(k),1000*label.pos(k,:),label.values(k)); 49 | end 50 | fclose(fid); 51 | 52 | 53 | -------------------------------------------------------------------------------- /matlab/mne_write_stc_file.m: -------------------------------------------------------------------------------- 1 | function mne_write_stc_file(filename,stc) 2 | % 3 | % mne_write_stc_file(filename,stc) 4 | % 5 | % writes an stc file 6 | % 7 | % filename output file 8 | % stc a stucture containing the stc data with fields: 9 | % 10 | % tmin The time of the first frame in seconds 11 | % tstep Time between frames in seconds 12 | % vertices Vertex indices (0 based) 13 | % data The data matrix nvert * ntime 14 | % 15 | 16 | % 17 | % 18 | % Author : Matti Hamalainen, MGH Martinos Center 19 | % License : BSD 3-clause 20 | % 21 | % 22 | % $Id$ 23 | % 24 | % Revision 1.7 2006/05/05 03:50:40 msh 25 | % Added routines to compute L2-norm inverse solutions. 26 | % Added mne_write_inverse_sol_stc to write them in stc files 27 | % Several bug fixes in other files 28 | % 29 | % Revision 1.6 2006/04/23 15:29:41 msh 30 | % Added MGH to the copyright 31 | % 32 | % Revision 1.5 2006/04/10 23:26:54 msh 33 | % Added fiff reading routines 34 | % 35 | % Revision 1.4 2005/12/05 20:23:21 msh 36 | % Added fiff_save_evoked. Improved error handling. 37 | % 38 | % Revision 1.3 2005/11/21 03:19:12 msh 39 | % Improved error handling 40 | % 41 | % Revision 1.2 2005/11/21 02:15:51 msh 42 | % Added more routines 43 | % 44 | % Revision 1.1 2005/11/21 01:41:57 msh 45 | % Introduced structures and start all function names with mne_ 46 | % 47 | % 48 | me='MNE:mne_write_stc_file'; 49 | if(nargin ~= 2) 50 | error(me,'usage: mne_write_stc_file(filename, stc)'); 51 | end 52 | 53 | [fid,message] = fopen(filename,'w','ieee-be'); 54 | if fid == -1 55 | error(me,message); 56 | end 57 | 58 | % write starttime in ms 59 | fwrite(fid,1000*stc.tmin,'float32'); 60 | % write sampling rate in ms 61 | fwrite(fid,1000*stc.tstep,'float32'); 62 | % write number of vertices 63 | fwrite(fid,length(stc.vertices),'uint32'); 64 | % write the vertex indices 65 | for i=1:length(stc.vertices) 66 | fwrite(fid,stc.vertices(i),'uint32'); 67 | end 68 | 69 | % write the number of timepts 70 | fwrite(fid,size(stc.data,2),'uint32'); 71 | % 72 | % write the data 73 | % 74 | fwrite(fid,stc.data,'float32'); 75 | 76 | % close the file 77 | fclose(fid); 78 | -------------------------------------------------------------------------------- /matlab/mne_write_stc_file1.m: -------------------------------------------------------------------------------- 1 | function mne_write_stc_file1(filename,stc) 2 | % 3 | % mne_write_stc_file1(filename,stc) 4 | % 5 | % writes an stc file 6 | % 7 | % filename output file 8 | % stc a stucture containing the stc data with fields: 9 | % 10 | % tmin The time of the first frame in seconds 11 | % tstep Time between frames in seconds 12 | % vertices Vertex indices (1 based) 13 | % data The data matrix nvert * ntime 14 | % 15 | 16 | % 17 | % 18 | % Author : Matti Hamalainen, MGH Martinos Center 19 | % License : BSD 3-clause 20 | % 21 | % 22 | % $Id$ 23 | % 24 | me='MNE:mne_write_stc_file1'; 25 | if(nargin ~= 2) 26 | error(me,'usage: mne_write_stc_file1(filename, stc)'); 27 | end 28 | 29 | stc.vertices = stc.vertices - 1; 30 | try 31 | mne_write_stc_file(filename,stc); 32 | stc.vertices = stc.vertices + 1; 33 | catch 34 | stc.vertices = stc.vertices - 1; 35 | error(me,'%s',mne_omit_first_line(lasterr)); 36 | end 37 | 38 | return; 39 | 40 | 41 | -------------------------------------------------------------------------------- /matlab/mne_write_surface.m: -------------------------------------------------------------------------------- 1 | function mne_write_surface(fname,verts,faces,comment) 2 | % 3 | % mne_write_surface(fname,verts,faces) 4 | % 5 | % Writes a FreeSurfer surface file 6 | % 7 | % fname - The file to write 8 | % verts - Vertex coordinates in meters 9 | % faces - The triangle descriptions 10 | % comment - Optional comment to include 11 | % 12 | 13 | % 14 | % Author : Matti Hamalainen, MGH Martinos Center 15 | % License : BSD 3-clause 16 | % 17 | % 18 | % Revision 1.1 2009/01/28 19:27:59 msh 19 | % Added mne_write_surface 20 | % 21 | 22 | me='MNE:mne_write_surface'; 23 | 24 | if nargin < 4 25 | comment = 'Triangle file written with MNE Matlab tools'; 26 | end 27 | % 28 | % The input file will be big endian 29 | % 30 | fid = fopen(fname,'wb','ieee-be'); 31 | 32 | if (fid < 0) 33 | error(me,'Cannot open file %s', fname); 34 | end 35 | % 36 | % Magic number to identify a TRIANGLE file 37 | % 38 | % 39 | TRIANGLE_FILE_MAGIC_NUMBER = 16777214 ; 40 | 41 | mne_fwrite3(fid,TRIANGLE_FILE_MAGIC_NUMBER) ; 42 | fprintf(fid,'%s\n\n',comment); 43 | fwrite(fid, size(verts,1), 'int32') ; 44 | fwrite(fid, size(faces,1), 'int32') ; 45 | fwrite(fid, 1000.0*reshape(verts',1,numel(verts)), 'float32') ; 46 | fwrite(fid, reshape(faces',1,numel(faces)) - 1, 'int32') ; 47 | fclose(fid) ; 48 | fprintf(1,'\tWrote the surface file %s with %d vertices and %d triangles\n',fname,size(verts,1),size(faces,1)); 49 | 50 | return; 51 | -------------------------------------------------------------------------------- /matlab/mne_write_w_file.m: -------------------------------------------------------------------------------- 1 | function err = mne_write_w_file(filename, w) 2 | % mne_write_w_file(filename, w) 3 | % 4 | % writes a binary 'w' file 5 | % 6 | % filename - name of file to write to 7 | % w - a structure with the following fields: 8 | % 9 | % vertices - vector of vertex indices (0-based) 10 | % data - vector of data values 11 | % 12 | 13 | % 14 | % Author : Matti Hamalainen, MGH Martinos Center 15 | % License : BSD 3-clause 16 | % 17 | % Revision 1.5 2006/04/23 15:29:41 msh 18 | % Added MGH to the copyright 19 | % 20 | % Revision 1.4 2006/04/10 23:26:54 msh 21 | % Added fiff reading routines 22 | % 23 | % Revision 1.3 2005/12/05 20:23:21 msh 24 | % Added fiff_save_evoked. Improved error handling. 25 | % 26 | % Revision 1.2 2005/11/21 03:19:12 msh 27 | % Improved error handling 28 | % 29 | % Revision 1.1 2005/11/21 02:15:51 msh 30 | % Added more routines 31 | % 32 | 33 | me='MNE:mne_write_w_file'; 34 | if(nargin ~= 2) 35 | error(me,'usage: mne_write_w_file(filename, w)'); 36 | return; 37 | end 38 | 39 | vnum = length(w.vertices) ; 40 | 41 | % open it as a big-endian file 42 | [fid,message] = fopen(filename, 'wb', 'b') ; 43 | if(fid == -1) 44 | error(me,message); 45 | end 46 | 47 | fwrite(fid, 0, 'int16') ; 48 | mne_fwrite3(fid, uint32(vnum)) ; 49 | for i=1:vnum 50 | mne_fwrite3(fid, w.vertices(i)) ; % vertex number (0-based) 51 | fwrite(fid, w.data(i), 'float') ; % vertex value 52 | end 53 | 54 | fclose(fid) ; 55 | 56 | return; 57 | -------------------------------------------------------------------------------- /matlab/mne_write_w_file1.m: -------------------------------------------------------------------------------- 1 | function err = mne_write_w_file1(filename, w) 2 | % mne_write_w_file1(filename, w) 3 | % 4 | % writes a binary 'w' file 5 | % 6 | % filename - name of file to write to 7 | % w - a structure with the following fields: 8 | % 9 | % vertices - vector of vertex indices (1-based) 10 | % data - vector of data values 11 | % 12 | 13 | % 14 | % Author : Matti Hamalainen, MGH Martinos Center 15 | % License : BSD 3-clause 16 | % 17 | % Revision 1.5 2006/04/23 15:29:41 msh 18 | % Added MGH to the copyright 19 | % 20 | % Revision 1.4 2006/04/10 23:26:54 msh 21 | % Added fiff reading routines 22 | % 23 | % Revision 1.3 2005/12/05 20:23:21 msh 24 | % Added fiff_save_evoked. Improved error handling. 25 | % 26 | % Revision 1.2 2005/11/21 03:19:12 msh 27 | % Improved error handling 28 | % 29 | % Revision 1.1 2005/11/21 02:15:51 msh 30 | % Added more routines 31 | % 32 | 33 | me='MNE:mne_write_w_file1'; 34 | if(nargin ~= 2) 35 | error(me,'usage: mne_write_w_file1(filename, w)'); 36 | return; 37 | end 38 | 39 | try 40 | w.vertices = w.vertices - 1; 41 | mne_write_w_file(filename,w); 42 | catch 43 | w.vertices = w.vertices - 1; 44 | error(me,'%s',mne_omit_first_line(lasterr)); 45 | end 46 | 47 | return; 48 | -------------------------------------------------------------------------------- /matlab/test/data/process_raw.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | # Generate events 4 | mne_process_raw --raw test_raw.fif \ 5 | --eventsout test-eve.fif 6 | 7 | # Averaging 8 | mne_process_raw --raw test_raw.fif --lowpass 40 --projoff \ 9 | --saveavetag -ave --ave test.ave 10 | 11 | # Compute the noise covariance matrix 12 | mne_process_raw --raw test_raw.fif --lowpass 40 --projoff \ 13 | --savecovtag -cov --cov test.cov 14 | -------------------------------------------------------------------------------- /matlab/test/data/test-ave.fif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mne-tools/mne-matlab/e5a26779313d2d7b4cba2b0b69d0e32f48a84d3f/matlab/test/data/test-ave.fif -------------------------------------------------------------------------------- /matlab/test/data/test-cov.fif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mne-tools/mne-matlab/e5a26779313d2d7b4cba2b0b69d0e32f48a84d3f/matlab/test/data/test-cov.fif -------------------------------------------------------------------------------- /matlab/test/data/test-eve.fif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mne-tools/mne-matlab/e5a26779313d2d7b4cba2b0b69d0e32f48a84d3f/matlab/test/data/test-eve.fif -------------------------------------------------------------------------------- /matlab/test/data/test.ave: -------------------------------------------------------------------------------- 1 | # 2 | # These are comments 3 | # 4 | average { 5 | # 6 | # Output files 7 | # 8 | # outfile test-ave.fif 9 | # logfile test-ave.log 10 | # eventfile test.eve 11 | # 12 | # Rejection values 13 | # 14 | gradReject 4000e-13 15 | magReject 4e-12 16 | eegReject 0e-6 17 | eogReject 150e-6 18 | # 19 | # Category specifications 20 | # 21 | category { 22 | name "Left Auditory" 23 | event 1 24 | tmin -0.2 25 | tmax 0.5 26 | color 1 1 0 27 | } 28 | category { 29 | name "Right Auditory" 30 | event 2 31 | tmin -0.2 32 | tmax 0.5 33 | color 1 0 0 34 | } 35 | category { 36 | name "Left visual" 37 | event 3 38 | tmin -0.2 39 | tmax 0.5 40 | color 0 1 0 41 | } 42 | category { 43 | name "Right visual" 44 | event 4 45 | tmin -0.2 46 | tmax 0.5 47 | color 0.5 0.8 1 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /matlab/test/data/test.cov: -------------------------------------------------------------------------------- 1 | # 2 | # These are comments 3 | # 4 | cov { 5 | # 6 | # Output files 7 | # 8 | outfile sample_audvis-cov.fif 9 | logfile sample_audvis-cov.log 10 | # 11 | # Rejection values 12 | # 13 | gradReject 4000e-13 14 | magReject 4e-12 15 | eegReject 40e-6 16 | eogReject 150e-6 17 | # 18 | # What to include in the covariance matrix? 19 | # 20 | def { 21 | name "Left Auditory" 22 | event 1 23 | tmin -0.2 24 | tmax 0.0 25 | basemin -0.2 26 | basemax 0 27 | } 28 | def { 29 | name "Right Auditory" 30 | event 2 31 | ignore 0 32 | tmin -0.2 33 | tmax 0.0 34 | basemin -0.2 35 | basemax 0 36 | } 37 | def { 38 | name "Left visual" 39 | event 3 40 | ignore 0 41 | tmin -0.2 42 | tmax 0.0 43 | basemin -0.2 44 | basemax 0 45 | } 46 | def { 47 | name "Right visual" 48 | event 4 49 | ignore 0 50 | tmin -0.2 51 | tmax 0.0 52 | basemin -0.2 53 | basemax 0 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /matlab/test/data/test_fiftrees_ref.mat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mne-tools/mne-matlab/e5a26779313d2d7b4cba2b0b69d0e32f48a84d3f/matlab/test/data/test_fiftrees_ref.mat -------------------------------------------------------------------------------- /matlab/test/data/test_long_raw.fif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mne-tools/mne-matlab/e5a26779313d2d7b4cba2b0b69d0e32f48a84d3f/matlab/test/data/test_long_raw.fif -------------------------------------------------------------------------------- /matlab/test/data/test_raw-eve.fif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mne-tools/mne-matlab/e5a26779313d2d7b4cba2b0b69d0e32f48a84d3f/matlab/test/data/test_raw-eve.fif -------------------------------------------------------------------------------- /matlab/test/data/test_raw.fif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mne-tools/mne-matlab/e5a26779313d2d7b4cba2b0b69d0e32f48a84d3f/matlab/test/data/test_raw.fif -------------------------------------------------------------------------------- /matlab/test/data/test_setup_raw_ref.mat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mne-tools/mne-matlab/e5a26779313d2d7b4cba2b0b69d0e32f48a84d3f/matlab/test/data/test_setup_raw_ref.mat -------------------------------------------------------------------------------- /matlab/test/test_ex_compute_inverse.m: -------------------------------------------------------------------------------- 1 | function test_ex_compute_inverse() 2 | % Test MNE dSPM inverse computation 3 | 4 | data_path = getenv('MNE_SAMPLE_DATASET_PATH'); 5 | if isempty(data_path) 6 | error('MNE_SAMPLE_DATASET_PATH environment variable no set.') 7 | end 8 | 9 | fname_inv = [data_path filesep 'MEG' filesep 'sample' filesep 'sample_audvis-meg-oct-6-meg-inv.fif']; 10 | fname_data = [data_path filesep 'MEG' filesep 'sample' filesep 'sample_audvis-ave.fif']; 11 | 12 | setno = 1; 13 | nave = -1; 14 | dSPM = true; 15 | lambda2 = 1/9; 16 | 17 | [res] = mne_ex_compute_inverse(fname_data,setno,fname_inv,nave,lambda2,dSPM); 18 | 19 | -------------------------------------------------------------------------------- /matlab/test/test_ex_read_epochs.m: -------------------------------------------------------------------------------- 1 | function test_suite=test_ex_read_epochs 2 | try test_functions=localfunctions(); catch 3 | end 4 | initTestSuite; 5 | 6 | 7 | function test_ex_read_epochs_() 8 | % Test IO with FIF epochs 9 | 10 | pathstr = fileparts(mfilename('fullpath')); 11 | fname = [pathstr filesep 'data' filesep 'test_raw.fif']; 12 | eventname = [pathstr filesep 'data' filesep 'test-eve.fif']; 13 | 14 | event = 1; 15 | tmin = -0.2; 16 | tmax = 0.5; 17 | 18 | [data, times, ch_names] = mne_ex_read_epochs(fname, event, eventname, tmin, tmax); 19 | 20 | assert(376 == length(ch_names)) 21 | assert(7 == length(data)) 22 | assert(376 == size(data(1).epoch, 1)) 23 | % assert(length(times) == size(data(1).epoch, 2)) # XXX should not fail 24 | -------------------------------------------------------------------------------- /matlab/test/test_ex_read_raw.m: -------------------------------------------------------------------------------- 1 | function test_suite=test_ex_read_raw 2 | try test_functions=localfunctions(); catch 3 | end 4 | initTestSuite; 5 | 6 | function test_ex_read_raw_() 7 | % Test IO with FIF raw files 8 | 9 | pathstr = fileparts(mfilename('fullpath')); 10 | fname = [pathstr filesep 'data' filesep 'test_raw.fif']; 11 | 12 | from = 50; 13 | to = 55; 14 | in_samples = false; 15 | [data, times] = mne_ex_read_raw(fname, from, to, in_samples); 16 | 17 | assert(size(data, 2) == length(times)) 18 | assertTrue(all(times < 56)) 19 | assertTrue(all(times > 49)) 20 | 21 | raw = fiff_setup_read_raw(fname); 22 | fiff_write_raw_segment_times('foo.fif', raw, 51, 53); 23 | 24 | raw = fiff_setup_read_raw('foo.fif'); 25 | [data, times] = fiff_read_raw_segment(raw); 26 | assert(size(data, 2) == length(times)) 27 | assertTrue(all(times < 53.01)) 28 | assertTrue(all(times > 50.99)) 29 | 30 | % import mne, numpy as np 31 | % data = np.random.RandomState(0).randn(3, 2) 32 | % info = mne.create_info(['MEG1', 'MEG2 with a very long name', 'STI 014'], 1000., ['grad', 'grad', 'stim']) 33 | % mne.io.RawArray(data, info).save('matlab/test/data/test_long_raw.fif') 34 | want_data = [[1.76405235, 0.40015721]; [0.97873798, 2.2408932]; [1.86755799, -0.97727788]]; 35 | want_times = [0., 0.001] 36 | want_names = {'MEG1', 'MEG2 with a very long name', 'STI 014'} 37 | 38 | fname = [pathstr filesep 'data' filesep 'test_long_raw.fif']; 39 | [data, times] = mne_ex_read_raw(fname, 0, 1, in_samples); 40 | assertTrue(all(data(:) - want_data(:) < 1e-6)); 41 | assertTrue(all(times == want_times)); 42 | 43 | raw = fiff_setup_read_raw(fname); 44 | assertTrue(all(strcmp(raw.info.ch_names, want_names))); 45 | fiff_write_raw_segment_times('foo.fif', raw, 0, 1); 46 | 47 | raw = fiff_setup_read_raw('foo.fif'); 48 | [data, times] = fiff_read_raw_segment(raw); 49 | assertTrue(all(data(:) - want_data(:) < 1e-6)); 50 | assertTrue(all(times == want_times)); 51 | assertTrue(all(strcmp(raw.info.ch_names, want_names))); 52 | -------------------------------------------------------------------------------- /matlab/test/test_fiff_define_constants.m: -------------------------------------------------------------------------------- 1 | function test_suite=test_fiff_define_constants 2 | try test_functions=localfunctions(); catch 3 | end 4 | initTestSuite; 5 | 6 | function test_fiff_define_constants_() 7 | % Test consistency of the fiff_define_constants.m w.r.t. the required 8 | % constants in the code base 9 | 10 | if ispc 11 | % I don't know what happens with the unit testing actually, if the 12 | % function returns without actually testing anything 13 | return; 14 | end 15 | 16 | p = fileparts(mfilename('fullpath')); 17 | if ~isempty(p) 18 | % this is assumed to end with '/test'; 19 | if numel(p)>4 && strcmp(p(end-3:end), 'test') 20 | p = strrep(p, [filesep 'test'], ''); 21 | end 22 | cd(p); 23 | end 24 | 25 | % create temporary files that holds the fiff constants from all m-files 26 | % together, and one that holds the fiff constants from the definition file 27 | system('grep -o "\\.FIFF[A-Z_]*" fiff_define_constants.m | sort -u > c1'); 28 | system('grep -oh "\\.FIFF[A-Z_]*" *.m --exclude fiff_define_constants.m | sort -u > c2'); 29 | 30 | % make the diff 31 | system('diff c1 c2 > delta'); 32 | 33 | % check which constants are accessed from the m-files, but which are not 34 | % present inthe definitions file 35 | [st, count] = system('grep ">" delta | wc -l'); 36 | 37 | % clean up 38 | delete('c1'); 39 | delete('c2'); 40 | delete('delta'); 41 | 42 | assertEqual(deblank(count), '0'); 43 | -------------------------------------------------------------------------------- /matlab/test/test_fiff_finish_writing_raw.m: -------------------------------------------------------------------------------- 1 | function test_suite=test_fiff_finish_writing_raw 2 | try test_functions=localfunctions(); catch 3 | end 4 | initTestSuite; 5 | 6 | function test_fiff_readwrite_raw_() 7 | % Test a round trip of reading and writing raw data 8 | % this function calls the following relevant m-files: 9 | % - fiff_read_meas_info 10 | % - fiff_setup_read_raw 11 | % - fiff_read_raw_segment 12 | % - fiff_start_writing_raw 13 | % - fiff_write_int 14 | % - fiff_write_raw_buffer 15 | % - fiff_finish_writing_raw 16 | 17 | FIFF = fiff_define_constants; 18 | 19 | % read 20 | pathstr = fileparts(mfilename('fullpath')); 21 | fname = fullfile(pathstr, 'data', 'test_raw.fif'); 22 | info = fiff_read_meas_info(fname); 23 | raw = fiff_setup_read_raw(fname); 24 | [data, times] = fiff_read_raw_segment(raw); 25 | 26 | % write -> note that the original data was in 24 chunks of 600 samples, the 27 | % data is saved out as a single chunk of 14400 samples 28 | fnamenew = strrep(fname, 'test_', 'testout_'); 29 | [outfid, cals] = fiff_start_writing_raw(fnamenew, info); 30 | fiff_write_int(outfid, FIFF.FIFF_FIRST_SAMPLE, raw.first_samp); 31 | fiff_write_int(outfid, FIFF.FIFF_DATA_SKIP, 0); 32 | fiff_write_raw_buffer(outfid, data, cals); 33 | fiff_finish_writing_raw(outfid); 34 | 35 | % read the new file 36 | rawnew = fiff_setup_read_raw(fnamenew); 37 | [datanew, timesnew] = fiff_read_raw_segment(rawnew); 38 | 39 | % compare 40 | assertEqual(data, datanew); 41 | assertEqual(times, timesnew); 42 | 43 | % clean up 44 | delete(fnamenew); 45 | 46 | -------------------------------------------------------------------------------- /matlab/test/test_fiff_make_dir_tree.m: -------------------------------------------------------------------------------- 1 | function test_suite=test_fiff_make_dir_tree 2 | try test_functions=localfunctions(); catch 3 | end 4 | initTestSuite; 5 | 6 | function test_fiff_make_dir_tree_() 7 | % Test fiff_make_dir_tree.m 8 | % 9 | % This actually the same functionality as test_fiff_open 10 | 11 | if ispc 12 | % I don't know what happens with the unit testing actually, if the 13 | % function returns without actually testing anything 14 | return; 15 | end 16 | 17 | p = fileparts(mfilename('fullpath')); 18 | 19 | % load the reference trees 20 | load(fullfile(p, 'data', 'test_fiftrees_ref.mat')); 21 | 22 | fname = fullfile(p, 'data', 'test_raw.fif'); 23 | [fid, tree, dir] = fiff_open(fname); 24 | fclose(fid); 25 | assert(isequal(tree, trees.raw)); 26 | 27 | fname = fullfile(p, 'data', 'test_long_raw.fif'); 28 | [fid, tree, dir] = fiff_open(fname); 29 | fclose(fid); 30 | assert(isequal(tree, trees.longraw)); 31 | 32 | fname = fullfile(p, 'data', 'test_raw-eve.fif'); 33 | [fid, tree, dir] = fiff_open(fname); 34 | fclose(fid); 35 | assert(isequal(tree, trees.raweve)); 36 | 37 | fname = fullfile(p, 'data', 'test-eve.fif'); 38 | [fid, tree, dir] = fiff_open(fname); 39 | fclose(fid); 40 | assert(isequal(tree, trees.eve)); 41 | 42 | fname = fullfile(p, 'data', 'test-ave.fif'); 43 | [fid, tree, dir] = fiff_open(fname); 44 | fclose(fid); 45 | assert(isequal(tree, trees.ave)); 46 | 47 | fname = fullfile(p, 'data', 'test-cov.fif'); 48 | [fid, tree, dir] = fiff_open(fname); 49 | fclose(fid); 50 | assert(isequal(tree, trees.cov)); 51 | 52 | -------------------------------------------------------------------------------- /matlab/test/test_fiff_open.m: -------------------------------------------------------------------------------- 1 | function test_suite=test_fiff_open 2 | try test_functions=localfunctions(); catch 3 | end 4 | initTestSuite; 5 | 6 | function test_fiff_open_() 7 | % Test fiff_open.m 8 | 9 | if ispc 10 | % I don't know what happens with the unit testing actually, if the 11 | % function returns without actually testing anything 12 | return; 13 | end 14 | 15 | p = fileparts(mfilename('fullpath')); 16 | 17 | % load the reference trees 18 | load(fullfile(p, 'data', 'test_fiftrees_ref.mat')); 19 | 20 | fname = fullfile(p, 'data', 'test_raw.fif'); 21 | [fid, tree, dir] = fiff_open(fname); 22 | fclose(fid); 23 | assert(isequal(tree, trees.raw)); 24 | 25 | fname = fullfile(p, 'data', 'test_long_raw.fif'); 26 | [fid, tree, dir] = fiff_open(fname); 27 | fclose(fid); 28 | assert(isequal(tree, trees.longraw)); 29 | 30 | fname = fullfile(p, 'data', 'test_raw-eve.fif'); 31 | [fid, tree, dir] = fiff_open(fname); 32 | fclose(fid); 33 | assert(isequal(tree, trees.raweve)); 34 | 35 | fname = fullfile(p, 'data', 'test-eve.fif'); 36 | [fid, tree, dir] = fiff_open(fname); 37 | fclose(fid); 38 | assert(isequal(tree, trees.eve)); 39 | 40 | fname = fullfile(p, 'data', 'test-ave.fif'); 41 | [fid, tree, dir] = fiff_open(fname); 42 | fclose(fid); 43 | assert(isequal(tree, trees.ave)); 44 | 45 | fname = fullfile(p, 'data', 'test-cov.fif'); 46 | [fid, tree, dir] = fiff_open(fname); 47 | fclose(fid); 48 | assert(isequal(tree, trees.cov)); 49 | -------------------------------------------------------------------------------- /matlab/test/test_fiff_read_meas_info.m: -------------------------------------------------------------------------------- 1 | function test_suite=test_fiff_read_meas_info 2 | try test_functions=localfunctions(); catch 3 | end 4 | initTestSuite; 5 | 6 | function test_fiff_readwrite_raw_() 7 | % Test a round trip of reading and writing raw data 8 | % this function calls the following relevant m-files: 9 | % - fiff_read_meas_info 10 | % - fiff_setup_read_raw 11 | % - fiff_read_raw_segment 12 | % - fiff_start_writing_raw 13 | % - fiff_write_int 14 | % - fiff_write_raw_buffer 15 | % - fiff_finish_writing_raw 16 | 17 | FIFF = fiff_define_constants; 18 | 19 | % read 20 | pathstr = fileparts(mfilename('fullpath')); 21 | fname = fullfile(pathstr, 'data', 'test_raw.fif'); 22 | info = fiff_read_meas_info(fname); 23 | raw = fiff_setup_read_raw(fname); 24 | [data, times] = fiff_read_raw_segment(raw); 25 | 26 | % write -> note that the original data was in 24 chunks of 600 samples, the 27 | % data is saved out as a single chunk of 14400 samples 28 | fnamenew = strrep(fname, 'test_', 'testout_'); 29 | [outfid, cals] = fiff_start_writing_raw(fnamenew, info); 30 | fiff_write_int(outfid, FIFF.FIFF_FIRST_SAMPLE, raw.first_samp); 31 | fiff_write_int(outfid, FIFF.FIFF_DATA_SKIP, 0); 32 | fiff_write_raw_buffer(outfid, data, cals); 33 | fiff_finish_writing_raw(outfid); 34 | 35 | % read the new file 36 | rawnew = fiff_setup_read_raw(fnamenew); 37 | [datanew, timesnew] = fiff_read_raw_segment(rawnew); 38 | 39 | % compare 40 | assertEqual(data, datanew); 41 | assertEqual(times, timesnew); 42 | 43 | % clean up 44 | delete(fnamenew); 45 | 46 | -------------------------------------------------------------------------------- /matlab/test/test_fiff_read_raw_segment.m: -------------------------------------------------------------------------------- 1 | function test_suite=test_fiff_read_raw_segment 2 | try test_functions=localfunctions(); catch 3 | end 4 | initTestSuite; 5 | 6 | function test_fiff_read_raw_segment_() 7 | % Test fiff_read_raw_segment.m 8 | 9 | if ispc 10 | % I don't know what happens with the unit testing actually, if the 11 | % function returns without actually testing anything 12 | return; 13 | end 14 | 15 | p = fileparts(mfilename('fullpath')); 16 | fname = fullfile(p, 'data', 'test_raw.fif'); 17 | [raw] = fiff_setup_read_raw(fname); 18 | [dat,times] = fiff_read_raw_segment(raw); 19 | 20 | % grab the first 100 samples 21 | indx = double(raw.first_samp) + [0 99]; 22 | [dat2,times2] = fiff_read_raw_segment(raw, indx(1), indx(2)); 23 | assert(isequal(dat(:,1:100),dat2)); 24 | assert(isequal(times(:,1:100),times2)); 25 | 26 | % grab the last 100 samples 27 | indx = double(raw.last_samp) - [99 0]; 28 | [dat2,times2] = fiff_read_raw_segment(raw, indx(1), indx(2)); 29 | assert(isequal(dat(:,(end-99):end),dat2)); 30 | assert(isequal(times(:,(end-99):end),times2)); 31 | 32 | % grab somewhere in between, going over a block boundary 33 | indx = double(raw.first_samp) + 500 + [0 99]; 34 | [dat2,times2] = fiff_read_raw_segment(raw, indx(1), indx(2)); 35 | assert(isequal(dat(:,501:600),dat2)); 36 | assert(isequal(times(:,501:600),times2)); 37 | 38 | % grab a subset of channels 39 | [dat2,times2] = fiff_read_raw_segment(raw, raw.first_samp, raw.last_samp, 2:2:size(dat,1)); 40 | assert(isequal(dat(2:2:end,:), dat2)); 41 | -------------------------------------------------------------------------------- /matlab/test/test_fiff_read_tag.m: -------------------------------------------------------------------------------- 1 | function test_suite=test_fiff_read_tag 2 | try test_functions=localfunctions(); catch 3 | end 4 | initTestSuite; 5 | 6 | function test_fiff_read_tag_() 7 | % Test fiff_open.m 8 | 9 | if ispc 10 | % I don't know what happens with the unit testing actually, if the 11 | % function returns without actually testing anything 12 | return; 13 | end 14 | 15 | p = fileparts(mfilename('fullpath')); 16 | 17 | % load the reference trees 18 | load(fullfile(p, 'data', 'test_fiftrees_ref.mat')); 19 | 20 | fname = fullfile(p, 'data', 'test_raw.fif'); 21 | [fid, tree, dir] = fiff_open(fname); 22 | fclose(fid); 23 | assert(isequal(tree, trees.raw)); 24 | 25 | fname = fullfile(p, 'data', 'test_long_raw.fif'); 26 | [fid, tree, dir] = fiff_open(fname); 27 | fclose(fid); 28 | assert(isequal(tree, trees.longraw)); 29 | 30 | fname = fullfile(p, 'data', 'test_raw-eve.fif'); 31 | [fid, tree, dir] = fiff_open(fname); 32 | fclose(fid); 33 | assert(isequal(tree, trees.raweve)); 34 | 35 | fname = fullfile(p, 'data', 'test-eve.fif'); 36 | [fid, tree, dir] = fiff_open(fname); 37 | fclose(fid); 38 | assert(isequal(tree, trees.eve)); 39 | 40 | fname = fullfile(p, 'data', 'test-ave.fif'); 41 | [fid, tree, dir] = fiff_open(fname); 42 | fclose(fid); 43 | assert(isequal(tree, trees.ave)); 44 | 45 | fname = fullfile(p, 'data', 'test-cov.fif'); 46 | [fid, tree, dir] = fiff_open(fname); 47 | fclose(fid); 48 | assert(isequal(tree, trees.cov)); 49 | -------------------------------------------------------------------------------- /matlab/test/test_fiff_read_tag_info.m: -------------------------------------------------------------------------------- 1 | function test_suite=test_fiff_read_tag_info 2 | try test_functions=localfunctions(); catch 3 | end 4 | initTestSuite; 5 | 6 | function test_fiff_read_tag_info_() 7 | % Test fiff_read_tag_info.m 8 | % 9 | % This actually the same functionality as test_fiff_open 10 | 11 | if ispc 12 | % I don't know what happens with the unit testing actually, if the 13 | % function returns without actually testing anything 14 | return; 15 | end 16 | 17 | p = fileparts(mfilename('fullpath')); 18 | 19 | % load the reference trees 20 | load(fullfile(p, 'data', 'test_fiftrees_ref.mat')); 21 | 22 | fname = fullfile(p, 'data', 'test_raw.fif'); 23 | [fid, tree, dir] = fiff_open(fname); 24 | fclose(fid); 25 | assert(isequal(tree, trees.raw)); 26 | 27 | fname = fullfile(p, 'data', 'test_long_raw.fif'); 28 | [fid, tree, dir] = fiff_open(fname); 29 | fclose(fid); 30 | assert(isequal(tree, trees.longraw)); 31 | 32 | fname = fullfile(p, 'data', 'test_raw-eve.fif'); 33 | [fid, tree, dir] = fiff_open(fname); 34 | fclose(fid); 35 | assert(isequal(tree, trees.raweve)); 36 | 37 | fname = fullfile(p, 'data', 'test-eve.fif'); 38 | [fid, tree, dir] = fiff_open(fname); 39 | fclose(fid); 40 | assert(isequal(tree, trees.eve)); 41 | 42 | fname = fullfile(p, 'data', 'test-ave.fif'); 43 | [fid, tree, dir] = fiff_open(fname); 44 | fclose(fid); 45 | assert(isequal(tree, trees.ave)); 46 | 47 | fname = fullfile(p, 'data', 'test-cov.fif'); 48 | [fid, tree, dir] = fiff_open(fname); 49 | fclose(fid); 50 | assert(isequal(tree, trees.cov)); 51 | -------------------------------------------------------------------------------- /matlab/test/test_fiff_setup_read_raw.m: -------------------------------------------------------------------------------- 1 | function test_suite=test_fiff_setup_read_raw 2 | try test_functions=localfunctions(); catch 3 | end 4 | initTestSuite; 5 | 6 | function test_fiff_setup_read_raw_() 7 | % Test fiff_setup_read_raw.m 8 | 9 | if ispc 10 | % I don't know what happens with the unit testing actually, if the 11 | % function returns without actually testing anything 12 | return; 13 | end 14 | 15 | p = fileparts(mfilename('fullpath')); 16 | 17 | % load the reference trees 18 | load(fullfile(p, 'data', 'test_setup_raw_ref.mat')); 19 | 20 | fname = fullfile(p, 'data', 'test_raw.fif'); 21 | [raw] = fiff_setup_read_raw(fname); 22 | if ~isfield(raws, 'raw') 23 | raws(1).raw = raw; 24 | end 25 | try, raw.info = rmfield(raw.info, 'filename'); end 26 | try, raws.raw.info = rmfield(raws.raw.info, 'filename'); end 27 | assert(isequaln(raw, raws.raw)); 28 | 29 | fname = fullfile(p, 'data', 'test_long_raw.fif'); 30 | [raw] = fiff_setup_read_raw(fname); 31 | if ~isfield(raws, 'longraw') 32 | raws(1).longraw = raw; 33 | end 34 | try, raw.info = rmfield(raw.info, 'filename'); end 35 | try, raws.longraw.info = rmfield(raws.longraw.info, 'filename'); end 36 | 37 | if isfield(raw, 'fastread') && raw.fastread~=0 38 | raw = rmfield(raw, 'fastread'); 39 | 40 | % the version stored on disk is based on the representation that allows 41 | % for fast reading, i.e. the former has an a struct-array 'ent' with 42 | % metadata for each of the data blocks, the latter has metadata that is 43 | % relevant for all data blocks at once, also the data type of the samples 44 | % may be different (int32 vs. double), this is not relevant for most 45 | % conventional use of thos quantities downstream in matlab 46 | raw.first_samp = double(raw.first_samp); 47 | raw.last_samp = double(raw.last_samp); 48 | assert(isequaln(rmfield(raw, 'rawdir'), rmfield(raws.longraw, 'rawdir'))); 49 | 50 | elseif isfield(raw, 'fastread') 51 | raw = rmfield(raw, 'fastread'); 52 | assert(isequaln(raw, raws.longraw)); 53 | end 54 | 55 | -------------------------------------------------------------------------------- /matlab/test/test_fiff_start_writing_raw.m: -------------------------------------------------------------------------------- 1 | function test_suite=test_fiff_start_writing_raw 2 | try test_functions=localfunctions(); catch 3 | end 4 | initTestSuite; 5 | 6 | function test_fiff_readwrite_raw_() 7 | % Test a round trip of reading and writing raw data 8 | % this function calls the following relevant m-files: 9 | % - fiff_read_meas_info 10 | % - fiff_setup_read_raw 11 | % - fiff_read_raw_segment 12 | % - fiff_start_writing_raw 13 | % - fiff_write_int 14 | % - fiff_write_raw_buffer 15 | % - fiff_finish_writing_raw 16 | 17 | FIFF = fiff_define_constants; 18 | 19 | % read 20 | pathstr = fileparts(mfilename('fullpath')); 21 | fname = fullfile(pathstr, 'data', 'test_raw.fif'); 22 | info = fiff_read_meas_info(fname); 23 | raw = fiff_setup_read_raw(fname); 24 | [data, times] = fiff_read_raw_segment(raw); 25 | 26 | % write -> note that the original data was in 24 chunks of 600 samples, the 27 | % data is saved out as a single chunk of 14400 samples 28 | fnamenew = strrep(fname, 'test_', 'testout_'); 29 | [outfid, cals] = fiff_start_writing_raw(fnamenew, info); 30 | fiff_write_int(outfid, FIFF.FIFF_FIRST_SAMPLE, raw.first_samp); 31 | fiff_write_int(outfid, FIFF.FIFF_DATA_SKIP, 0); 32 | fiff_write_raw_buffer(outfid, data, cals); 33 | fiff_finish_writing_raw(outfid); 34 | 35 | % read the new file 36 | rawnew = fiff_setup_read_raw(fnamenew); 37 | [datanew, timesnew] = fiff_read_raw_segment(rawnew); 38 | 39 | % compare 40 | assertEqual(data, datanew); 41 | assertEqual(times, timesnew); 42 | 43 | % clean up 44 | delete(fnamenew); 45 | 46 | -------------------------------------------------------------------------------- /matlab/test/test_fiff_write_int.m: -------------------------------------------------------------------------------- 1 | function test_suite=test_fiff_write_int 2 | try test_functions=localfunctions(); catch 3 | end 4 | initTestSuite; 5 | 6 | function test_fiff_readwrite_raw_() 7 | % Test a round trip of reading and writing raw data 8 | % this function calls the following relevant m-files: 9 | % - fiff_read_meas_info 10 | % - fiff_setup_read_raw 11 | % - fiff_read_raw_segment 12 | % - fiff_start_writing_raw 13 | % - fiff_write_int 14 | % - fiff_write_raw_buffer 15 | % - fiff_finish_writing_raw 16 | 17 | FIFF = fiff_define_constants; 18 | 19 | % read 20 | pathstr = fileparts(mfilename('fullpath')); 21 | fname = fullfile(pathstr, 'data', 'test_raw.fif'); 22 | info = fiff_read_meas_info(fname); 23 | raw = fiff_setup_read_raw(fname); 24 | [data, times] = fiff_read_raw_segment(raw); 25 | 26 | % write -> note that the original data was in 24 chunks of 600 samples, the 27 | % data is saved out as a single chunk of 14400 samples 28 | fnamenew = strrep(fname, 'test_', 'testout_'); 29 | [outfid, cals] = fiff_start_writing_raw(fnamenew, info); 30 | fiff_write_int(outfid, FIFF.FIFF_FIRST_SAMPLE, raw.first_samp); 31 | fiff_write_int(outfid, FIFF.FIFF_DATA_SKIP, 0); 32 | fiff_write_raw_buffer(outfid, data, cals); 33 | fiff_finish_writing_raw(outfid); 34 | 35 | % read the new file 36 | rawnew = fiff_setup_read_raw(fnamenew); 37 | [datanew, timesnew] = fiff_read_raw_segment(rawnew); 38 | 39 | % compare 40 | assertEqual(data, datanew); 41 | assertEqual(times, timesnew); 42 | 43 | % clean up 44 | delete(fnamenew); 45 | 46 | -------------------------------------------------------------------------------- /matlab/test/test_fiff_write_raw_buffer.m: -------------------------------------------------------------------------------- 1 | function test_suite=test_fiff_write_raw_buffer 2 | try test_functions=localfunctions(); catch 3 | end 4 | initTestSuite; 5 | 6 | function test_fiff_readwrite_raw_() 7 | % Test a round trip of reading and writing raw data 8 | % this function calls the following relevant m-files: 9 | % - fiff_read_meas_info 10 | % - fiff_setup_read_raw 11 | % - fiff_read_raw_segment 12 | % - fiff_start_writing_raw 13 | % - fiff_write_int 14 | % - fiff_write_raw_buffer 15 | % - fiff_finish_writing_raw 16 | 17 | FIFF = fiff_define_constants; 18 | 19 | % read 20 | pathstr = fileparts(mfilename('fullpath')); 21 | fname = fullfile(pathstr, 'data', 'test_raw.fif'); 22 | info = fiff_read_meas_info(fname); 23 | raw = fiff_setup_read_raw(fname); 24 | [data, times] = fiff_read_raw_segment(raw); 25 | 26 | % write -> note that the original data was in 24 chunks of 600 samples, the 27 | % data is saved out as a single chunk of 14400 samples 28 | fnamenew = strrep(fname, 'test_', 'testout_'); 29 | [outfid, cals] = fiff_start_writing_raw(fnamenew, info); 30 | fiff_write_int(outfid, FIFF.FIFF_FIRST_SAMPLE, raw.first_samp); 31 | fiff_write_int(outfid, FIFF.FIFF_DATA_SKIP, 0); 32 | fiff_write_raw_buffer(outfid, data, cals); 33 | fiff_finish_writing_raw(outfid); 34 | 35 | % read the new file 36 | rawnew = fiff_setup_read_raw(fnamenew); 37 | [datanew, timesnew] = fiff_read_raw_segment(rawnew); 38 | 39 | % compare 40 | assertEqual(data, datanew); 41 | assertEqual(times, timesnew); 42 | 43 | % clean up 44 | delete(fnamenew); 45 | 46 | -------------------------------------------------------------------------------- /matlab/test/test_mne_source_spectral_analysis.m: -------------------------------------------------------------------------------- 1 | function test_mne_source_spectral_analysis() 2 | % Test PSD computation for sources 3 | 4 | data_path = getenv('MNE_SAMPLE_DATASET_PATH'); 5 | if isempty(data_path) 6 | error('MNE_SAMPLE_DATASET_PATH environment variable no set.') 7 | end 8 | 9 | fname_data = [data_path filesep 'MEG' filesep 'sample' filesep 'sample_audvis_raw.fif']; 10 | 11 | clear cfg 12 | cfg.inv = [data_path filesep 'MEG' filesep 'sample' filesep 'sample_audvis-meg-oct-6-meg-inv.fif']; 13 | cfg.first_samp = 0; 14 | cfg.last_samp = 40000; 15 | cfg.outfile = 'source_psd' 16 | 17 | [res] = mne_source_spectral_analysis(fname_data,cfg); 18 | -------------------------------------------------------------------------------- /matlab/test/test_read_forward.m: -------------------------------------------------------------------------------- 1 | function test_read_forward() 2 | % Test IO with FIF forward files 3 | 4 | data_path = getenv('MNE_SAMPLE_DATASET_PATH'); 5 | if isempty(data_path) 6 | error('MNE_SAMPLE_DATASET_PATH environment variable no set.') 7 | end 8 | 9 | fname = [data_path filesep 'MEG' filesep 'sample' filesep 'sample_audvis-meg-eeg-oct-6-fwd.fif']; 10 | fwd = mne_read_forward_solution(fname); --------------------------------------------------------------------------------