Skip to content

Instantly share code, notes, and snippets.

@Hio-Been
Created February 22, 2020 07:12
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Hio-Been/bfc0b9ee0b1909d84783baedafb19ae7 to your computer and use it in GitHub Desktop.
Save Hio-Been/bfc0b9ee0b1909d84783baedafb19ae7 to your computer and use it in GitHub Desktop.
MATLAB function for drifting grating (2D sine wave)
function imgs = get_drifting_grating( n_frames, rec, tilt, sf, drift_speed, plot_option )
%% MATLAB function for drifting grating (2D sine wave)
% Usage:
% imgs = get_drifting_grating( n_frames, rec, tilt, sf, drift_speed, plot_option )
% ex) imgs = get_drifting_grating( 60, 500, 30, .01, 5, 1 );
% ex) imgs = get_drifting_grating();
%
% written by Hio-Been Han, 2020-02-22
% hiobeen.han@kaist.ac.kr
%
%% (1) Check arg-in
if nargin < 6, plot_option = 1;
elseif nargin < 5, drift_speed = 1;
elseif nargin < 4, sf = .005;
elseif nargin < 3, tilt = 30;
elseif nargin < 2, rec = 100;
elseif nargin < 1, n_frames = 1;
end
%% (2) Set phase vector
initial_phase = rand*360; % set random initial phase
phases = mod( initial_phase: drift_speed: n_frames*drift_speed+initial_phase, 360);
%% (3) Calculate
x=rec/2; y=rec/2;
[mesh_x, mesh_y] = meshgrid(0:(rec-1), 0:(rec-1));
a=cos(deg2rad(tilt))*sf*360;
b=sin(deg2rad(tilt))*sf*360;
imgs = [];
counter = 0;
for phase = phases(1:end-1)
counter = counter+1;
imgs(:,:,counter ) = sin(deg2rad(a*(mesh_x - x) + b*(mesh_y - y)+phase))/2+.5 ;
end
%% (4) Visualization (if specified)
if plot_option
figure(100); clf;
colormap gray
for frameIdx = 1:n_frames
imagesc(imgs(:,:,frameIdx));
set(gca,'XTick',[1,rec],'YTick',[1,rec],'LineWidth',2,'Box','off');
if n_frames>1, title(frameIdx); end
drawnow;
end
end
return
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment