@@ -210,13 +210,17 @@ def destripe(x, fs, tr_sel=None, neuropixel_version=1, butter_kwargs=None, k_kwa
210210 return x_
211211
212212
213- def decompress_destripe_cbin (sr , output_file = None , h = None ):
213+ def decompress_destripe_cbin (sr , output_file = None , h = None , wrot = None , append = False , nc_out = None ):
214214 """
215- From a spikeglx Reader object, decompresses and apply ADC
215+ From a spikeglx Reader object, decompresses and apply ADC.
216+ Saves output as a flat binary file in int16
216217 Production version with optimized FFTs - requires pyfftw
217218 :param sr: seismic reader object (spikeglx.Reader)
218219 :param output_file: (optional, defaults to .bin extension of the compressed bin file)
219220 :param h: (optional)
221+ :param wrot: (optional) whitening matrix [nc x nc] or amplitude scalar to apply to the output
222+ :param append: (optional, False) for chronic recordings, append to end of file
223+ :param nc_out: (optional, True) saves non selected channels (synchronisation trace) in output
220224 :return:
221225 """
222226 import pyfftw
@@ -235,6 +239,7 @@ def decompress_destripe_cbin(sr, output_file=None, h=None):
235239 taper = np .r_ [0 , scipy .signal .windows .cosine ((SAMPLES_TAPER - 1 ) * 2 ), 0 ]
236240 # create the FFT stencils
237241 ncv = h ['x' ].size # number of channels
242+ nc_out = nc_out or sr .nc
238243 # compute LP filter coefficients
239244 sos = scipy .signal .butter (** butter_kwargs , output = 'sos' )
240245 # compute fft stencil for batchsize
@@ -247,7 +252,7 @@ def decompress_destripe_cbin(sr, output_file=None, h=None):
247252 DEPHAS = np .exp (1j * np .angle (fft_object (dephas )) * h ['sample_shift' ][:, np .newaxis ])
248253
249254 pbar = tqdm (total = sr .ns / sr .fs )
250- with open (output_file , 'wb' ) as fid :
255+ with open (output_file , 'ab' if append else ' wb' ) as fid :
251256 first_s = 0
252257 while True :
253258 last_s = np .minimum (NBATCH + first_s , sr .ns )
@@ -273,10 +278,13 @@ def decompress_destripe_cbin(sr, output_file=None, h=None):
273278 chunk = kfilt (chunk , ** k_kwargs )
274279 # add back sync trace and save
275280 chunk = np .r_ [chunk , sr [first_s :last_s , ncv :].T ].T
276- (chunk [slice (* ind2save ), :] / sr .channel_conversion_sample2v ['ap' ]
277- ).astype (np .int16 ).tofile (fid )
281+ chunk = chunk [slice (* ind2save ), :] / sr .channel_conversion_sample2v ['ap' ]
282+ if wrot is not None :
283+ chunk [:, :ncv ] = np .dot (chunk [:, :ncv ], wrot )
284+ chunk [:, :nc_out ].astype (np .int16 ).tofile (fid )
278285 first_s += NBATCH - SAMPLES_TAPER * 2
279286 pbar .update (NBATCH / sr .fs )
280287 if last_s == sr .ns :
288+ # pad with last sample
281289 break
282290 pbar .close ()
0 commit comments