BlackrockNEVLoadingEngine.m.svn-base 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. function [timeStamps, waveForms] = BlackrockNEVLoadingEngine(fn, recordToGet, recordUnits)
  2. %%
  3. % Blackrock Microsystems NEV loading engine for MClust 3.0.
  4. %
  5. % Kian Torab
  6. % support@blackrockmicro.com
  7. % Blackrock Microsystems
  8. %
  9. % Version 1.1.1.0
  10. if ~exist(fn, 'file')
  11. disp('File does not exist.');
  12. timeStamps = 0;
  13. waveForms = 0;
  14. return;
  15. end
  16. %% Reading the entire data if recordUnits is not defined
  17. if ~exist('recordUnits', 'var')
  18. NEV = loadNEV(fn, 'read');
  19. %Saving all timestamps from the file into the timeStamp output variable
  20. timeStamps = NEV.Data.Spikes.TimeStamp;
  21. timeStamps = timeStampsToTimestampSeconds(timeStamps,NEV.MetaTags.SampleRes);
  22. %Saving all saveforms from the file into the waveForm output variable
  23. waveForms = initializeWaveform(length(timeStamps));
  24. waveForms(:,1,:) = NEV.Data.Spikes.Waveform(1:32,:)';
  25. else
  26. %% Reading the file depending on what recordUnits is defining
  27. switch recordUnits
  28. case 1 %Returning timestamp and waveforms for given timestamps
  29. %Loading the NEV file
  30. NEV = loadNEV(fn, 'read');
  31. %Saving all timestamps in the timeStamps output variable
  32. timeStamps = NEV.Data.Spikes.TimeStamp;
  33. %Converting seconds to NEV timestamps
  34. recordToGet = timeStampsSecondsToTimestamps(recordToGet,NEV.MetaTags.SampleRes);
  35. %Calculating the index of timestamps to get
  36. for idx = 1:length(recordToGet)
  37. IDXtoGet(idx) = find(timeStamps == recordToGet(idx),1);
  38. end
  39. %Saving all timestamps requested by the timestamps
  40. timeStamps = timeStamps(IDXtoGet);
  41. timeStamps = timeStampsToTimestampSeconds(timeStamps,NEV.MetaTags.SampleRes);
  42. %Calculating the number of timestamps
  43. numOfTimestamps = length(timeStamps);
  44. %Preallocating a 3D waveforms matrix
  45. waveForms = initializeWaveform(numOfTimestamps);
  46. %Saving all waveforms in the waveForms output variable
  47. waveForms(:,1,1:32) = NEV.Data.Spikes.Waveform(1:32,IDXtoGet)';
  48. case 2 %Returning timestamp and waveforms for given records
  49. %Loading the NEV file
  50. NEV = loadNEV(fn, 'read');
  51. %Saving the timestamps for the requested records
  52. timeStamps = NEV.Data.Spikes.TimeStamp(1,recordToGet);
  53. timeStamps = timeStampsToTimestampSeconds(timeStamps,NEV.MetaTags.SampleRes);
  54. %Calculating the number of timestamps
  55. numOfTimestamps = length(timeStamps);
  56. %Saving the waveforms for the requested records
  57. waveForms = initializeWaveform(numOfTimestamps);
  58. %Saving the waveforms for the requested records
  59. waveForms(:,1,1:32) = NEV.Data.Spikes.Waveform(1:32,recordToGet)';
  60. case 3 %Returning timestamp and waveforms for range of given timestamps
  61. %Loading the NEV file
  62. NEV = loadNEV(fn, 'read');
  63. %Converting timestamps in seconds to timestamps in samples
  64. recordToGet = timeStampsSecondsToTimestamps(recordToGet,NEV.MetaTags.SampleRes);
  65. %Parsing out the timestamp range
  66. tInitial = recordToGet(1);
  67. tEnd = recordToGet(2);
  68. %Saving all timestamps in the timeStamps output variable
  69. timeStamps = NEV.Data.Spikes.TimeStamp;
  70. %Finding timestamps that fall within the range
  71. IDX = find(timeStamps <= tEnd & timeStamps >= tInitial);
  72. %Saving all timestamps that fall within the range
  73. timeStamps = NEV.Data.Spikes.TimeStamp(1,IDX);
  74. timeStamps = timeStampsToTimestampSeconds(timeStamps,NEV.MetaTags.SampleRes);
  75. %Calculating the number of timestamps
  76. numOfTimestamps = length(timeStamps);
  77. %Preallocating a 3D waveforms matrix
  78. waveForms = initializeWaveform(numOfTimestamps);
  79. %Saving all waveforms in the waveForms output variable
  80. waveForms(:,1,1:32) = NEV.Data.Spikes.Waveform(1:32,IDX)';
  81. case 4 %Returning timestamp and waveforms for the range of given records
  82. %Loading the NEV file
  83. NEV = loadNEV(fn, 'read');
  84. %Creating a range out of the record list
  85. recordToGet = [recordToGet(1):recordToGet(end)];
  86. %Saving the timestamps for the requested records
  87. timeStamps = NEV.Data.Spikes.TimeStamp(1,recordToGet);
  88. timeStamps = timeStampsToTimestampSeconds(timeStamps,NEV.MetaTags.SampleRes);
  89. %Calculating the number of timestamps
  90. numOfTimestamps = length(timeStamps);
  91. %Saving the waveforms for the requested records
  92. waveForms = initializeWaveform(numOfTimestamps);
  93. %Saving the waveforms for the requested records
  94. waveForms(:,1,1:32) = NEV.Data.Spikes.Waveform(1:32,recordToGet)';
  95. case 5 %Returning the number of records in the file
  96. %Loading the NEV file
  97. NEV = loadNEV(fn);
  98. % Returning the number of spikes
  99. timeStamps = size(NEV.Data.Spikes.TimeStamp, 2);
  100. waveForms = [];
  101. otherwise
  102. disp('Invalid input for recordUnit');
  103. disp('Values from 1 to 5 are valid only.');
  104. end
  105. end
  106. timeStamps = timeStamps';
  107. %% Uses openNEV (a part of NPMK package: http://bit.ly/brnpmkkit) to open the NEV file
  108. function NEV = loadNEV(fn, input)
  109. if ~exist('input', 'var')
  110. NEV = openNEV(fn);
  111. elseif strcmpi(input, 'noread')
  112. NEV = openNEV(fn);
  113. elseif ~strcmpi(input, 'read')
  114. disp('Invalid input.');
  115. else
  116. NEV = openNEV(fn, 'read');
  117. end
  118. %% Initializes the waveform variable depending on the number of spikes in the file
  119. function waveForms = initializeWaveform(numOfTimestamps)
  120. waveForms = zeros(numOfTimestamps, 4, 32);
  121. %% Converts the timestamp samples to timestamp values in seconds
  122. function timeStampsSec = timeStampsToTimestampSeconds(timeStamps, samplingRate)
  123. timeStampsSec = double(timeStamps) / double(samplingRate);
  124. %% Converts the timestamp seconds to timestamp values in samples
  125. function timeStamps = timeStampsSecondsToTimestamps(timeStampsSeconds, samplingRate)
  126. timeStamps = int32(timeStampsSeconds * double(samplingRate));