billkarsh / SpikeGLX

SpikeGLX recording system GUI [Neuropixels NI]
Other
83 stars 29 forks source link

reading a subset of data from *ap.bin #34

Closed panichem closed 8 months ago

panichem commented 8 months ago

Hi,

I'd like to read out a snippet of raw data from a particular channel at a particular time in a recording. Will the matlab code below accomplish that?

function ap = extractApFromFile(filename, numChans, idx, srtSamp, nSamps)
%extractApFromFile  Read binary data from spikeglx ap.bin 
%   ap = extractApFromFile(filename, numChans, idx, srtSamp, nSamps)
%   reads nSamps of binary data from the specified ap.bin file for channel 
%   idx starting at (*time*) sample # srtSamp. numChans = 385

fid = fopen(filename, 'r');

%skip to the timepoint of interest
offset = numChans*2*srtSamp;
fseek(fid, offset, 'bof');
% skip over the first samples of the other channels
q = fread(fid, (idx-1), 'int16=>double'); %#ok<NASGU>
% read in the channel of interest
ap = fread(fid, [1, nSamps], 'int16=>double', (numChans-1)*2); % skipping other channels

fclose(fid);
end
billkarsh commented 8 months ago

Hi Matt, First, this is not a bug but a question. Let's keep questions on the Neuropixels slack channel, and reserve github issues for SpikeGLX bug reports. Questions help educate everybody and you'd probably get better MATLAB help there. OK. I'm not a sophisticated MATLAB programmer. By my own reading of the MATLAB help files for fread() I think this should work. Since I am a performance enthusiast, I would initially fseek to: offset + (idx-1)*2. Test your code by reading the sync channel where you know how the resulting square-wave should look.

panichem commented 8 months ago

Thanks for taking a look, and the suggestion! The square wave indeed looks reasonable

billkarsh commented 8 months ago

Welcome!