recreate_spatiotemporal_white_noise_stimulus.m 4.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. % This shows how to load data for a single cell and recreate the binary
  2. % spatio-temporal stimulus.
  3. % The data is organized as as a matrix of spike-counts versus stimulus
  4. % frames and separated into trials. A trial here is 50 sec (1500 frames)
  5. % for running noise (i.e. non-repeated sequences) and 10 sec (300 frames)
  6. % for frozen noise.
  7. % To recreate the stimulus, we use a mex file that implements the ran1
  8. % random number generator from the Numerical Recipes library. (This is what
  9. % we use in our stimulus generator program for the experiments.)
  10. % The code below then provides the stimulus (as a 3D matrix, see below) for
  11. % chunks of data (several trials combined in a block), and then you have to
  12. % insert code to use these stimulus chunks. For us, working with the
  13. % stimulus in chunks works better than trying to get the entire stimulus
  14. % into memory.
  15. % by J.K. Liu and T. Gollisch; 2017/05/17
  16. clear;
  17. close all;
  18. RefreshRate = 30; % Hz; stimulus update rate
  19. pStim.xPix = 800; % pixels on the monitor
  20. pStim.yPix = 600;
  21. pStim.stixelwidth = 4; % monitor pixels per stimulus pixel
  22. pStim.stixelheight = 4;
  23. pStim.Nx = ceil(pStim.xPix/pStim.stixelwidth);
  24. pStim.Ny = ceil(pStim.yPix/pStim.stixelheight);
  25. % The stimulus here is separated into sections of running noise (different
  26. % for each trial) and frozen noise (same sequence repeated).
  27. % The numbers below give the numbers of stimulus frames in each section
  28. pStim.RunningFrames = 1500;
  29. pStim.FrozenFrames = 300;
  30. pStim.seed1 = -10000; % the random generator seed for running noise
  31. pStim.seed2 = -20000; % the random generator seed for frozen noise
  32. % As a convenient way to find out the number of trials for which you need
  33. % to recreate the stimulus, you can read in the spike count data and
  34. % determine the number of trials as below.
  35. % Otherwise, Ntrial can also be set by hand, e.g., to a lower number to
  36. % start by analyzing only the first few trials, e.g. "Ntrial = 3;".
  37. load('cell_data_01_NC.mat'); % loads one of the data sets
  38. Ntrial = size(spk1,3); % number of trials for non-repeated noise
  39. % regenerate running noise by chunks
  40. for nn = 1:Ntrial
  41. fprintf(1, 'Runing ... %d/%d \n', nn, Ntrial );
  42. if nn == 1
  43. seed = pStim.seed1;
  44. end;
  45. % CB1: CheckerBoard Stimulus in black and white
  46. % ran1: the random number generator
  47. [CB1 seed] = ran1(seed, pStim.Nx*pStim.Ny*pStim.RunningFrames);
  48. % We generate random numbers of the stimulus through the Numerical
  49. % Recipes routine "ran1". We have a mex file for this that is called
  50. % above, which is fairly fast for generating sequences of the random
  51. % numbers (rather than returning just 1 random number for each call).
  52. % The parameters of the ran1 function are the seed and the number of
  53. % random numbers to be obtained.
  54. % Below, the random numbers in the inverval (0,1) are turned into
  55. % contrast values of -1 and 1 just like we do in the stimulation
  56. % program.
  57. CB1(CB1>=0.5) = 1;
  58. CB1(CB1<0.5) = 0;
  59. CB1 = 2*(CB1 - 0.5);
  60. CB1 = reshape(CB1,pStim.Ny,pStim.Nx,[]);
  61. % CB1 now contains a 3D matrix with values of -1 and 1.
  62. % The dimensions are spatial-y versus spatial-x versus time (in
  63. % frames). The number of frames depends on the trials per block.
  64. % Here now is the place to do something with the recreated stimulus,
  65. % e.g. use it for computing the receptive field or (after having
  66. % determined the receptive field center and size and thereby the
  67. % spatial region of interest) extracting the spike-triggered stimulus
  68. % ensemble. Or maybe save the recreated stimulus chunk to disk for
  69. % later use.
  70. % For memory reasons, we generally don't recreate the entire
  71. % spatio-temporal stimulus and save it to disk.
  72. % E.g. use the following to save the stimulus in successive .mat files
  73. filename = sprintf( 'nonrepeatedStim%03d.mat', nn );
  74. save( filename, 'CB1' );
  75. end
  76. % For recreating the frozen noise:
  77. [CB2] = ran1(pStim.seed2, pStim.Nx*pStim.Ny*pStim.FrozenFrames);
  78. CB2(CB2>=0.5) = 1;
  79. CB2(CB2<0.5) = 0;
  80. CB2 = 2*(CB2 - 0.5);
  81. CB2 = reshape(CB2,pStim.Ny,pStim.Nx,[]);
  82. % Again, probably you just want to save the stimulus to file:
  83. save( 'frozenStim.mat', 'CB2' );