matrixToNSx.m 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. function matrixToNSx(varargin)
  2. % matrixToNSx
  3. %
  4. % Converts a MATLAB data matrix (channels as rows, samples as columns) into
  5. % a NSx file.
  6. %
  7. % Use matrixToNSx(inputData, samplingFreq, inputUnits, savedDataPath)
  8. %
  9. % All input arguments are optional.
  10. %
  11. % inputData: data matrix (channels as rows, samples as columns) into
  12. % a NSx file.
  13. %
  14. % samplingFreq: Sampling frequency of the data. Currently only 500Hz,
  15. % 1kHz, 2kHz, 10kHz, and 30kHz sampaling frequencies are
  16. % supported.
  17. %
  18. % inputUnits: The unit for the recorded data. Most data acquisition
  19. % systems save the data in units of µV. TDT units are V.
  20. % The supported units are V, mV, uV or nV.
  21. %
  22. % savedDataPath: The full path (excluding the extension) of the saved
  23. % file.
  24. %
  25. % Example 1 (Mac):
  26. % matrixToNSx(myData, 30000, 'uv', '/Desktop/newConvertedFile');
  27. %
  28. % In the example above, the data matrix myData recorded at 30kHz will be
  29. % saved on the Desktop (Mac style path) as newConvertedFile.ns5 file.
  30. %
  31. % Example 2 (PC):
  32. % matrixToNSx(myData, 30000, 'uv', 'C:\Data\newConvertedFile');
  33. %
  34. % In the example above, the data matrix myData recorded at 30kHz will be
  35. % saved C:\Data folder as newConvertedFile.ns5 file.
  36. %
  37. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  38. % Kian Torab
  39. % kian@blackrockmicro.com
  40. % Blackrock Microsystems
  41. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  42. %
  43. % Version History
  44. %
  45. % 1.0.0.0:
  46. % - Initial release.
  47. %
  48. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  49. switch nargin
  50. case 0
  51. disp('Data is a required input.');
  52. return
  53. case 1
  54. disp('Sampling rate and input unit were not provided. Assuming 30kHz and µV.');
  55. disp('The converted file will be saved on the Desktop.');
  56. inputData = varargin{1};
  57. samplingFreq = input('What is the sampling frequency of the recorded data (e.g. ''30000'')? ');
  58. signalUnit = input('What is the input unit of the signal (V, mV, uV or nV)? ', 's');
  59. savedFilePath = input('Enter the full path for the converted file? ', 's');
  60. case 2
  61. disp('Input unit was not provided. Assuming µV.');
  62. disp('The converted file will be saved on the Desktop.');
  63. inputData = varargin{1};
  64. signalUnit = input('What is the input unit of the signal (V, mV, uV or nV)? ', 's');
  65. savedFilePath = input('Enter the full path for the converted file? ', 's');
  66. case 3
  67. disp('The converted file will be saved on the Desktop.');
  68. inputData = varargin{1};
  69. samplingFreq = varargin{2};
  70. signalUnit = varargin{3};
  71. savedFilePath = input('Enter the full path for the converted file? ', 's');
  72. case 4
  73. inputData = varargin{1};
  74. samplingFreq = varargin{2};
  75. signalUnit = varargin{3};
  76. savedFilePath = varargin{4};
  77. otherwise
  78. disp('Invalid number of arguments.');
  79. return;
  80. end
  81. %% Validate sampling frequency
  82. switch samplingFreq
  83. case 500
  84. savedFileExt = '.ns1';
  85. case 1000
  86. savedFileExt = '.ns2';
  87. case 2000
  88. savedFileExt = '.ns3';
  89. case 10000
  90. savedFileExt = '.ns4';
  91. case 30000
  92. savedFileExt = '.ns5';
  93. otherwise
  94. disp('Currently only sampling rates 500Hz, 1kHz, 2kHz, 10 kHz and 30kHz are supported.');
  95. return;
  96. end
  97. %% Finding the unit of the data.
  98. switch lower(signalUnit)
  99. case 'v'
  100. inputData = inputData * 1000000;
  101. case 'mv'
  102. inputData = inputData * 1000;
  103. case 'uv'
  104. case 'nv'
  105. inputData = inputData / 1000;
  106. otherwise
  107. disp('Invalid unit.');
  108. return;
  109. end
  110. %% Converting samplingFreq to type double
  111. samplingFreq = double(samplingFreq);
  112. % Warren from More lab @ Stanford
  113. %% Determining channel count and data length
  114. channelCount = min(size(inputData));
  115. dataLength = max(size(inputData));
  116. %% Creating a label of 16 character long.
  117. newChanLabel = [num2str(samplingFreq/1000) ' kS/s'];
  118. newChanLabel = [newChanLabel repmat(' ', [1, 16-length(newChanLabel)])];
  119. %% Converting inputData to int16
  120. inputData = int16(inputData);
  121. %% Rewriting the metatags
  122. NSx = openNSx(which('bnsx.dat'));
  123. NSx.MetaTags.SamplingLabel = newChanLabel(1:16);
  124. NSx.MetaTags.ChannelCount = channelCount;
  125. NSx.MetaTags.SamplingFreq = samplingFreq;
  126. NSx.MetaTags.TimeRes = samplingFreq;
  127. NSx.MetaTags.ChannelID = 1:NSx.MetaTags.ChannelCount;
  128. NSx.MetaTags.DateTime = datestr(now);
  129. d = datevec(date);
  130. NSx.MetaTags.DateTimeRaw = [d(1:2), weekday(date), d(3:end), 0];
  131. NSx.MetaTags.Comment = ['This data was converted into a NSx by matrixToNSx' repmat(' ',1, 207)];
  132. NSx.MetaTags.FileSpec = '2.3';
  133. NSx.MetaTags.Timestamp = 0;
  134. NSx.MetaTags.DataPoints = dataLength;
  135. NSx.MetaTags.DataDurationSec = NSx.MetaTags.DataPoints / NSx.MetaTags.SamplingFreq;
  136. NSx.Data = inputData;
  137. saveNSx(NSx, [savedFilePath savedFileExt]);