removeNSxData.m 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. function removeNSxData(timestamps, filename)
  2. %
  3. % removeNSxData(timestamps, filename)
  4. %
  5. % This function removes chunks of data given in a 2-column timestamps
  6. % variable from a NSx file and then saves it as another NSx. This is
  7. % useful when removing useless data from the file to decrease the size of
  8. % the NSx file without modifying the file timestamps. The file size only
  9. % changes when the NSx file is compressed.
  10. %
  11. % timestamps: This variable contains beginning and ending timestamps of
  12. % the data that need to be removed from the NSx file. The
  13. % format is:
  14. % [BegTimeStamp1 EndTimeStamp1
  15. % BegTimeStamp2 EndTimeStamp2
  16. % BegTimeStamp3 EndTimeStamp3
  17. % ... ...
  18. % BegTimeStampN EndTimeStampN]
  19. %
  20. % The data in the NSx between the pairs of timestamps will be
  21. % removed and set to 0.
  22. % (REQUIRED)
  23. %
  24. % filename: The name of the NSx file to be used. This input is also
  25. % optional. In its absense, a dialog will open and will
  26. % prompt the user to select an NSx file.
  27. % (OPTIONAL)
  28. %
  29. % Example: removeNSxData([100,600; 1000,2000], 'c:\datafile\sampleNSx.ns5')
  30. %
  31. % In the example above, the timestamps between 100:600 and
  32. % 1000:2000 will be removed from the file sampleNSx.ns5 and
  33. % the resulting NSx will be saved in sampleNSx_chunked.ns5
  34. % file.
  35. %
  36. % Kian Torab
  37. % ktorab@blackrockmicro.com
  38. % Blackrock Microsystems
  39. %
  40. % Version 1.0.1.0
  41. % Validating the timestamps variable
  42. if ~exist('timestamps', 'var')
  43. disp('The variable timestamps is required.');
  44. return;
  45. end
  46. if size(timestamps, 2) ~= 2
  47. disp('The timestamps variable should be a Nx2 variable containing beginning timestamps in the first column and ending timestamps in the second column.');
  48. return;
  49. end
  50. if ~all(timestamps(:,1) < timestamps(:,2))
  51. disp('The timestamps in the first column (beginning) have to be smaller than the ones in the second column (ending).')
  52. return;
  53. end
  54. % Figuring out the filename, if not passed on to the function
  55. if ~exist('filename', 'var')
  56. [fname, path] = getFile('*.*', 'Choose an NSx file...');
  57. filename = [path fname];
  58. end
  59. if exist(filename, 'file') ~= 2
  60. disp('The file does not exist.');
  61. return;
  62. end
  63. % Openning and reading the file
  64. FID = fopen(filename, 'r', 'ieee-le');
  65. readData = fread(FID, '*uint8');
  66. FID = fclose(FID);
  67. % Extracting some information from the NSx file
  68. fileTypeID = char(readData(1:8))';
  69. headerBytes = typecast(readData(11:14), 'uint32')+9;
  70. samplingFreq = double(typecast(readData(291:294), 'uint32'))/...
  71. double(typecast(readData(287:290), 'uint32'));
  72. channelCount = typecast(readData(311:314), 'uint32');
  73. % Converting timestamps into uint16 (2 bytes per data point)
  74. timestamps = timestamps*2;
  75. % Removing given timestamps
  76. for idx = 1:size(timestamps, 1)
  77. readData((timestamps(idx,1)-2)*channelCount+1+headerBytes:timestamps(idx,2)*channelCount+headerBytes) = NaN;
  78. end
  79. % Saving the chunked file
  80. writeFilename = [filename(1:end-4) '_chunked', filename(end-3:end)];
  81. FID = fopen(writeFilename, 'w+', 'ieee-le');
  82. fwrite(FID, readData);
  83. fclose(FID);