splitNEVResets.m 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. function splitNSx(splitCount)
  2. % splitNSx
  3. %
  4. % Opens and splits an NSx file in smaller pieces, timewise.
  5. %
  6. % Use splitNSx(splitCount)
  7. %
  8. % All input arguments are optional. Input arguments can be in any order.
  9. %
  10. % splitCount: Defines the number of splits.
  11. % DEFAULT: Splits the file in 2 pieces.
  12. %
  13. % Example 1:
  14. % splitNSx(4);
  15. %
  16. % In the example above, the user will be prompted to select a file. The
  17. % loaded file will be split in 4 samller files. For example, if the file
  18. % is 1 hour long then it will be split into four 15-minute files.
  19. %
  20. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  21. % Kian Torab
  22. % support@blackrockmicro.com
  23. % Blackrock Microsystems
  24. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  25. % Version History
  26. %
  27. % 1.0.0.0:
  28. % - Initial release.
  29. %
  30. % 1.1.0.0:
  31. % - Fixed a bug related to a case where initial timestamp of the first
  32. % data segment was not 0.
  33. %
  34. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  35. %
  36. % Validating input parameter
  37. if ~exist('splitCount', 'var')
  38. splitCount = 2;
  39. end
  40. % Getting the file name
  41. [fname, path] = getFile('*.nev', 'Choose an NSx file...');
  42. if fname == 0
  43. disp('No file was selected.');
  44. if nargout
  45. clear variables;
  46. end
  47. return;
  48. end
  49. fext = fname(end-3:end);
  50. % Loading the file
  51. %% Reading Basic Header from file into NSx structure.
  52. FID = fopen([path fname], 'r', 'ieee-le');
  53. BasicHeader = fread(FID, 336, '*uint8');
  54. Trackers.fExtendedHeader = double(typecast(BasicHeader(13:16), 'uint32'));
  55. Trackers.countPacketBytes = double(typecast(BasicHeader(17:20), 'uint32'));
  56. %% Doing Trackers
  57. fseek(FID, 0, 'eof');
  58. Trackers.fData = ftell(FID);
  59. Trackers.countDataPacket = (Trackers.fData - Trackers.fExtendedHeader)/Trackers.countPacketBytes;
  60. fseek(FID, Trackers.fExtendedHeader, 'bof');
  61. tRawData = fread(FID, [10 Trackers.countDataPacket], '10*uint8=>uint8', Trackers.countPacketBytes - 10);
  62. Timestamp = tRawData(1:4,:);
  63. Timestamp = typecast(Timestamp(:), 'uint32').';
  64. splitPacketStarts = find(diff(Timestamp)<0);
  65. splitPacketBytes = Trackers.countPacketBytes * splitPacketStarts;
  66. % Reading headers and seeking to beginning of data
  67. fseek(FID, 0, 'bof');
  68. fileHeader = fread(FID, Trackers.fExtendedHeader, '*uint8');
  69. for idx = 1:length(splitPacketStarts)
  70. % Opening a file for saving
  71. FIDw = fopen([path fname(1:end-4) '-s' sprintf('%03d', idx) fname(end-3:end)], 'w+', 'ieee-le');
  72. fprintf('\nReading segment %d... ', idx);
  73. % Reading the segment
  74. dataSegment = fread(FID, splitPacketBytes(idx), 'char');
  75. fprintf('Writing segment %d... ', idx);
  76. % Writing the segmented data into file
  77. fwrite(FIDw, fileHeader, 'char');
  78. fwrite(FIDw, dataSegment, 'char');
  79. % Clearing variables and closing file
  80. clear dataSegment;
  81. fclose(FIDw);
  82. end