/external/fieldtrip/trunk/fileio/private/write_neuralynx_ncs.m

http://open-realtime-fmri.googlecode.com/ · MATLAB · 95 lines · 42 code · 10 blank · 43 comment · 6 complexity · fa44eb5e586057d487884b6043e29e67 MD5 · raw file

  1. function write_neuralynx_ncs(filename, ncs);
  2. % WRITE_NEURALYNX_NCS writes continuous data to a NCS file
  3. % The input data should be scaled in uV.
  4. %
  5. % Use as
  6. % write_neuralynx_ncs(filename, ncs)
  7. %
  8. % See also READ_NEURALYNX_NCS
  9. % Copyright (C) 2005-2007, Robert Oostenveld
  10. %
  11. % This file is part of FieldTrip, see http://www.ru.nl/neuroimaging/fieldtrip
  12. % for the documentation and details.
  13. %
  14. % FieldTrip is free software: you can redistribute it and/or modify
  15. % it under the terms of the GNU General Public License as published by
  16. % the Free Software Foundation, either version 3 of the License, or
  17. % (at your option) any later version.
  18. %
  19. % FieldTrip is distributed in the hope that it will be useful,
  20. % but WITHOUT ANY WARRANTY; without even the implied warranty of
  21. % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  22. % GNU General Public License for more details.
  23. %
  24. % You should have received a copy of the GNU General Public License
  25. % along with FieldTrip. If not, see <http://www.gnu.org/licenses/>.
  26. %
  27. % $Id: write_neuralynx_ncs.m 2885 2011-02-16 09:41:58Z roboos $
  28. if ~isa(ncs.TimeStamp, 'uint64')
  29. error('timestamps should be uint64');
  30. end
  31. % convert the data from uV into V
  32. ncs.dat = ncs.dat * 1e-6;
  33. % scale the data and convert to 16 bits,
  34. % this has to be done prior to writing the header
  35. ADMaxValue = double(intmax('int16'));
  36. ADMaxVolts = max(abs(ncs.dat(:)));
  37. if ADMaxVolts>0
  38. ADBitVolts = ADMaxVolts / ADMaxValue;
  39. else
  40. ADBitVolts = 1;
  41. end
  42. ncs.dat = int16(ncs.dat / ADBitVolts);
  43. % update the header with the calibration values
  44. ncs.hdr.ADBitVolts = ADBitVolts;
  45. ncs.hdr.ADMaxValue = ADMaxValue;
  46. % construct the header
  47. buf = [];
  48. buf = [buf sprintf('######## Neuralynx Data File Header\r\n')];
  49. buf = [buf sprintf('## File Name: %s\r\n', filename)];
  50. buf = [buf sprintf('## Time Opened: (m/d/y): %s At Time: %s\r\n', datestr(clock, 'mm/dd/yy'), datestr(clock, 'HH:MM:SS'))];
  51. f = fieldnames(ncs.hdr);
  52. for i=1:length(f)
  53. v = getfield(ncs.hdr, f{i});
  54. switch class(v)
  55. case 'char'
  56. buf = [buf sprintf('-%s\t%s\r\n', f{i}, v)];
  57. case 'double'
  58. buf = [buf sprintf('-%s\t%s\r\n', f{i}, num2str(v))];
  59. otherwise
  60. error('unknown class in writing header');
  61. end
  62. end
  63. % pad the rest of the header with zeros
  64. buf((end+1):16384) = 0;
  65. % open the file and write the header
  66. fid = fopen(filename, 'wb', 'ieee-le');
  67. fwrite(fid, buf);
  68. % The format of a continuous sampled record is
  69. % int64 TimeStamp
  70. % int32 ChanNumber
  71. % int32 SampFreq
  72. % int32 NumValidSamp
  73. % int16 Samp[0] ... int16 Samp[511]
  74. % Note that if NumValidSamp < 512, Samp[n], where n >= NumValidSamp, will
  75. % contain random data.
  76. for i=1:size(ncs.dat,2)
  77. % write a single continuous data record
  78. fwrite(fid, ncs.TimeStamp(i) , 'uint64');
  79. fwrite(fid, ncs.ChanNumber(i) , 'int32');
  80. fwrite(fid, ncs.SampFreq(i) , 'int32');
  81. fwrite(fid, ncs.NumValidSamp(i), 'int32');
  82. fwrite(fid, ncs.dat(:,i) , 'int16');
  83. end
  84. % close the file
  85. fclose(fid);