# rotating grating

**URL:** <https://psychtoolbox.discourse.group/t/rotating-grating/1932>\
**Category:** Uncategorized\
**Created:** [July 10, 2003, 4:55pm UTC](https://psychtoolbox.discourse.group/t/rotating-grating/1932 "2003-07-10T16:55:01Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Allen\_Ingling](https://avatars.discourse-cdn.com/v4/letter/a/a183cd/32.png) [@Allen\_Ingling](https://psychtoolbox.discourse.group/u/Allen_Ingling)\
**Post date:** [July 10, 2003, 4:55pm UTC](https://psychtoolbox.discourse.group/t/rotating-grating/1932/1 "2003-07-10T16:55:01Z")

</div>

Hi,

&nbsp;

I'm trying to modify 'DriftDemo' to allow for rotation of the stimulus through 360 degrees, at a rotation rate much slower than the drift rate. To save computational complexity and speed of calculation, I was considering&nbsp;generating a few slides of the plain drifting grating as given in the demo and then just rotating the display when showing the movie.   
  
Has this been done before? Or is there some easy way to go about this?

&nbsp;

thanks in advance

&nbsp;

Ronen

---

<div class="post-metadata">

**Author:** ![Allen\_Ingling](https://avatars.discourse-cdn.com/v4/letter/a/a183cd/32.png) [@Allen\_Ingling](https://psychtoolbox.discourse.group/u/Allen_Ingling)\
**Post date:** [December 19, 2006, 8:05am UTC](https://psychtoolbox.discourse.group/t/rotating-grating/1932/2 "2006-12-19T08:05:03Z")

</div>

hi   
i am new with this toolbox. i am trying to do drifting and rotating   
grating. i modified the driftdemo (reomved the mask) and iserted a loop   
that changing the angle in the drawtexture line but the result is   
drifting rotating and jumping every time it changes orientation it   
jumps back. does anyone has an idea how to fix it?  
thanks

---

<div class="post-metadata">

**Author:** ![taz\_liraz](https://avatars.discourse-cdn.com/v4/letter/t/8e8cbc/32.png) [@taz\_liraz](https://psychtoolbox.discourse.group/u/taz_liraz)\
**Post date:** [December 24, 2006, 6:33am UTC](https://psychtoolbox.discourse.group/t/rotating-grating/1932/3 "2006-12-24T06:33:42Z")

</div>

if nargin\<2  
 ifis=2;  
end;  
  
if nargin\<1  
 numFrames=1  
end;  
  
try  
AssertOpenGL;  
screens=Screen('Screens');  
screenNumber=max(screens);  
w=Screen('OpenWindow',screenNumber);  
 [widthp, heightp]=Screen('WindowSize', w);  
 [width, height]=Screen('DisplaySize', screenNumber);  
 pixelratio=widthp/width;  
 spatial=2;   
white=WhiteIndex(screenNumber);  
 black=BlackIndex(screenNumber);  
 gray=(white+black)/2;  
 if round(gray)==white  
 gray=black;  
 end  
 inc=white-gray;  
 s=min(widthp, heightp) /1;  
 [x,y]=meshgrid(-s:s-1, -s:s-1);  
 angle=0\*pi/180; % 30 deg orientation.  
 f=0.2/34\*2\*pi; % cycles/pixel  
 a=cos(angle)\*f;  
 b=sin(angle)\*f;  
for i=1:numFrames  
 phase=(i/numFrames)\*2\*pi;  
 m=sin(a\*x+b\*y+phase);  
 gratingtex=(gray+inc\*m);  
 tex(i)=Screen('MakeTexture', w, gratingtex);  
 end  
  
 tavg=0;  
 ifi\_duration = Screen('GetFlipInterval', w)  
 movieDurationSecs=0.5;  
frameRate=Screen('FrameRate',screenNumber);  
 if(frameRate==0)   
 frameRate=60; % 60 Hz is a good guess for flat-panels...  
 end   
 movieDurationFrames=round(movieDurationSecs \* frameRate / ifis);  
movieFrameIndices=mod(0:(movieDurationFrames-1), numFrames) + 1;  
priorityLevel=MaxPriority(w);  
 Priority(priorityLevel)  
Screen('FillRect',w, gray);  
 vbl = Screen('Flip', w);  
 tic  
 for j=0:1:30  
 for i=1:movieDurationFrames  
 t1=GetSecs;  
Screen('DrawTexture', w, tex(movieFrameIndices(i)), [], [], j);  
 vbl=Screen('Flip', w, vbl + (ifis - 0.5) \* ifi\_duration);  
  
 t1=GetSecs - t1;  
 if (i\>numFrames)  
 tavg=tavg+t1;  
 end;  
  
 % We also abort on keypress...  
 if KbCheck  
 break;  
 end;  
 end;  
 end  
toc  
 Priority(0);  
  
 tavg=tavg / (movieDurationFrames - numFrames)  
  
 % We're done: Close all windows and textures:  
 Screen('CloseAll');  
  
catch  
 %this "catch" section executes in case of an error in the "try"   
section  
 %above. Importantly, it closes the onscreen window if its open.  
 Priority(0);  
 Screen('CloseAll');  
 psychrethrow(psychlasterror);  
end %try..catch..  
  
  
  
  
--- In [psychtoolbox@yahoogroups.com](mailto:psychtoolbox@yahoogroups.com), "Mario Kleiner"   
\<mario.kleiner@...\> wrote:  

> \>  
> \> I still don't get it, this should simply work by providing a proper  
> \> rotation angle to the Screen('DrawTexture'...) command - the angle

is  

> \> in degress ie 0-360 degrees. Did you try DriftDemo2 for a more  
> \> efficient way of doing drifting gratings?  
> \>   
> \> You could describe your hardware specs and display settings, post

the  

> \> snippet of code that you use and/or provide a much more detailed  
> \> explanation of what you mean with "jumps".  
> \>   
> \> Your jumps could be jumps in the graphics, due to overloading the  
> \> machine by bad coding or due to a coding bug. It also could be  
> \> temporal aliasing if you happen to hit a unlucky combination of  
> \> spatial wavelength of your grating, drifting speed of the grating

and  

> \> monitor refresh / redraw rate, but this shouldn't be related to  
> \> rotation...  
> \>   
> \> -mario  
> \>   
> \> --- In [psychtoolbox@yahoogroups.com](mailto:psychtoolbox@yahoogroups.com), Li-Raz R \<taz\_liraz@\> wrote:  
> \> \>  
> \> \> hi  
> \> \> i am trying to create a stimulus that has 2 degrees of fridom.

drift  

> \> and angular rotation. when i tried to do the grating when it changed  
> \> orientation (1 degree) it allways jumped. i trying to do a continues  
> \> grating stimulus that drift and rotate. but i only succed partly

cause  

> \> it has "jumps" in the rotation. i think the reason is the phase of

the  

> \> grating  
> \> \> thanks   
> \> \>   
> \> \>   
> \> \> ----- Original Message ----  
> \> \> From: Mario Kleiner \<mario.kleiner@\>  
> \> \> To: [psychtoolbox@yahoogroups.com](mailto:psychtoolbox@yahoogroups.com)  
> \> \> Sent: Wednesday, December 20, 2006 9:16:55 PM  
> \> \> Subject: [psychtoolbox] Re: rotating grating  
> \> \>   
> \> \> hmm,  
> \> \>   
> \> \> could you explain in more detail or in a different way what you

want  

> \> \> to do and what goes wrong? I don't understand the problem from

your  

> \> \> posting...  
> \> \>   
> \> \> -mario  
> \> \>   
> \> \> --- In psychtoolbox@ yahoogroups. com, "taz\_liraz" \<taz\_liraz@ ..\>  
> \> wrote:  
> \> \> \>  
> \> \> \> hi   
> \> \> \> i am new with this toolbox. i am trying to do drifting and

rotating   

> \> \> \> grating. i modified the driftdemo (reomved the mask) and

iserted a  

> \> loop   
> \> \> \> that changing the angle in the drawtexture line but the result

is   

> \> \> \> drifting rotating and jumping every time it changes orientation

it   

> \> \> \> jumps back. does anyone has an idea how to fix it?  
> \> \> \> thanks  
> \> \> \>  
> \> \>   
> \> \>   
> \> \>   
> \> \>   
> \> \> \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_  
> \> \> Do You Yahoo!?  
> \> \> Tired of spam? Yahoo! Mail has the best spam protection around   
> \> \> [http://mail.yahoo.com](http://mail.yahoo.com)  
> \> \>  
> \>

---

<div class="post-metadata">

**Author:** ![franswcornelissen](https://avatars.discourse-cdn.com/v4/letter/f/df705f/32.png) [@franswcornelissen](https://psychtoolbox.discourse.group/u/franswcornelissen)\
**Post date:** [December 27, 2006, 7:40pm UTC](https://psychtoolbox.discourse.group/t/rotating-grating/1932/4 "2006-12-27T19:40:23Z")

</div>

"taz\_liraz",  
I think there were some conceptual/programming errors in your code. The code below   
seems to produce smoothly rotating (and phase shifting) gratings.  
Gr.  
Frans  
  
function better\_rotating\_grating  
  
if nargin\<2  
 ifis=2;  
end;  
  
if nargin\<1  
 numFrames=16;  
end;  
  
try  
 commandwindow;  
 Screen('Preference','SkipSyncTests', 1);  
 AssertOpenGL;  
 screens=Screen('Screens');  
 screenNumber=max(screens);  
 w=Screen('OpenWindow',screenNumber);  
 [widthp, heightp]=Screen('WindowSize', w);  
 [width, height]=Screen('DisplaySize', screenNumber);  
 pixelratio=widthp/width;  
 spatial=2;  
 white=WhiteIndex(screenNumber);  
 black=BlackIndex(screenNumber);  
 gray=(white+black)/2;  
 if round(gray)==white  
 gray=black;  
 end  
 inc=white-gray;  
 s=min(widthp, heightp) /1;  
 [x,y]=meshgrid(-s:s-1, -s:s-1);  
 angle=0\*pi/180; % 30 deg orientation.  
 f=0.2/34\*2\*pi; % cycles/pixel  
 a=cos(angle)\*f;  
 b=sin(angle)\*f;  
 for i=1:numFrames  
 phase=(i/numFrames)\*2\*pi  
 m=sin(a\*x+b\*y+phase);  
 gratingtex=(gray+inc\*m);  
 tex(i)=Screen('MakeTexture', w, gratingtex);  
 end  
 i  
 tavg=0;  
 ifi\_duration = Screen('GetFlipInterval', w)  
 movieDurationSecs=2.5;  
 frameRate=Screen('FrameRate',screenNumber);  
 if(frameRate==0)  
 frameRate=60; % 60 Hz is a good guess for flat-panels...  
 end  
 movieDurationFrames=round(movieDurationSecs \* frameRate / ifis)  
 movieFrameIndices=mod(0:(movieDurationFrames-1), numFrames) + 1  
 priorityLevel=MaxPriority(w);  
 Priority(priorityLevel)  
 Screen('FillRect',w, gray);  
 vbl = Screen('Flip', w);  
 tic  
 rotSp=60; %( deg/s)  
 movieFrameAngle=(0:(length(movieFrameIndices)-1))\*(rotSp/frameRate);  
 lengthMovieFrameAngle=length(movieFrameAngle)  
 lengthMovieFrameIndices=length(movieFrameIndices)  
  
 if length(movieFrameAngle)~=length(movieFrameIndices)  
 error('Error: Different length of movie index and angle vectors');  
 end  
 i=0; j=0;  
 Screen('Flip', w);  
 t1=Getsecs;  
 tx=GetSecs;  
 if 0 % do not use movieFrameAngle vector, but calculate angle based on time passed  
 while 1  
 i=i+1;  
 if i\>length(tex)  
 i=1;  
 end  
 tx=GetSecs;  
 angle=rotSp\*(GetSecs-t1);  
 Screen('DrawTexture', w, tex(movieFrameIndices(i)), [], [], angle);  
 vbl=Screen('Flip', w);  
  
 tavg=tavg+(GetSecs - tx);  
 j=j+1;  
 tx=GetSecs;  
  
 % We also abort on keypress...  
 if KbCheck  
 break;  
 end;  
 end  
 else  
 while 1  
 for i=1:length(movieFrameIndices)  
 Screen('DrawTexture', w, tex(movieFrameIndices(i)), [], [], movieFrameAngle(i));  
 vbl=Screen('Flip', w);  
  
 tavg=tavg+(GetSecs - tx);  
 j=j+1;  
 tx=GetSecs;  
  
 % We also abort on keypress...  
 if KbCheck  
 break;  
 end;  
 end  
 if KbCheck  
 break;  
 end;  
 end  
 end  
 toc  
 Priority(0);  
  
 tavg=tavg / j % average frameduration  
  
 % We're done: Close all windows and textures:  
 Screen('CloseAll');  
  
catch  
 %this "catch" section executes in case of an error in the "try" section  
 %above. Importantly, it closes the onscreen window if its open.  
 Priority(0);  
 Screen('CloseAll');  
 psychrethrow(psychlasterror);  
end %try..catch..  
  
  
  
  
  

> --- In [psychtoolbox@yahoogroups.com](mailto:psychtoolbox@yahoogroups.com), "taz\_liraz" \<taz\_liraz@...\> wrote:  
> \>  
> \> if nargin\<2  
> \> ifis=2;  
> \> end;  
> \>   
> \> if nargin\<1  
> \> numFrames=1  
> \> end;  
> \>   
> \> try  
> \> AssertOpenGL;  
> \> screens=Screen('Screens');  
> \> screenNumber=max(screens);  
> \> w=Screen('OpenWindow',screenNumber);  
> \> [widthp, heightp]=Screen('WindowSize', w);  
> \> [width, height]=Screen('DisplaySize', screenNumber);  
> \> pixelratio=widthp/width;  
> \> spatial=2;   
> \> white=WhiteIndex(screenNumber);  
> \> black=BlackIndex(screenNumber);  
> \> gray=(white+black)/2;  
> \> if round(gray)==white  
> \> gray=black;  
> \> end  
> \> inc=white-gray;  
> \> s=min(widthp, heightp) /1;  
> \> [x,y]=meshgrid(-s:s-1, -s:s-1);  
> \> angle=0\*pi/180; % 30 deg orientation.  
> \> f=0.2/34\*2\*pi; % cycles/pixel  
> \> a=cos(angle)\*f;  
> \> b=sin(angle)\*f;  
> \> for i=1:numFrames  
> \> phase=(i/numFrames)\*2\*pi;  
> \> m=sin(a\*x+b\*y+phase);  
> \> gratingtex=(gray+inc\*m);  
> \> tex(i)=Screen('MakeTexture', w, gratingtex);  
> \> end  
> \>   
> \> tavg=0;  
> \> ifi\_duration = Screen('GetFlipInterval', w)  
> \> movieDurationSecs=0.5;  
> \> frameRate=Screen('FrameRate',screenNumber);  
> \> if(frameRate==0)   
> \> frameRate=60; % 60 Hz is a good guess for flat-panels...  
> \> end   
> \> movieDurationFrames=round(movieDurationSecs \* frameRate / ifis);  
> \> movieFrameIndices=mod(0:(movieDurationFrames-1), numFrames) + 1;  
> \> priorityLevel=MaxPriority(w);  
> \> Priority(priorityLevel)  
> \> Screen('FillRect',w, gray);  
> \> vbl = Screen('Flip', w);  
> \> tic  
> \> for j=0:1:30  
> \> for i=1:movieDurationFrames  
> \> t1=GetSecs;  
> \> Screen('DrawTexture', w, tex(movieFrameIndices(i)), [], [], j);  
> \> vbl=Screen('Flip', w, vbl + (ifis - 0.5) \* ifi\_duration);  
> \>   
> \> t1=GetSecs - t1;  
> \> if (i\>numFrames)  
> \> tavg=tavg+t1;  
> \> end;  
> \>   
> \> % We also abort on keypress...  
> \> if KbCheck  
> \> break;  
> \> end;  
> \> end;  
> \> end  
> \> toc  
> \> Priority(0);  
> \>   
> \> tavg=tavg / (movieDurationFrames - numFrames)  
> \>   
> \> % We're done: Close all windows and textures:  
> \> Screen('CloseAll');  
> \>   
> \> catch  
> \> %this "catch" section executes in case of an error in the "try"   
> \> section  
> \> %above. Importantly, it closes the onscreen window if its open.  
> \> Priority(0);  
> \> Screen('CloseAll');  
> \> psychrethrow(psychlasterror);  
> \> end %try..catch..  
> \>   
> \>   
> \>   
> \>   
> \> --- In [psychtoolbox@yahoogroups.com](mailto:psychtoolbox@yahoogroups.com), "Mario Kleiner"   
> \> \<mario.kleiner@\> wrote:  
> \> \>  
> \> \> I still don't get it, this should simply work by providing a proper  
> \> \> rotation angle to the Screen('DrawTexture'...) command - the angle   
> \> is  
> \> \> in degress ie 0-360 degrees. Did you try DriftDemo2 for a more  
> \> \> efficient way of doing drifting gratings?  
> \> \>   
> \> \> You could describe your hardware specs and display settings, post   
> \> the  
> \> \> snippet of code that you use and/or provide a much more detailed  
> \> \> explanation of what you mean with "jumps".  
> \> \>   
> \> \> Your jumps could be jumps in the graphics, due to overloading the  
> \> \> machine by bad coding or due to a coding bug. It also could be  
> \> \> temporal aliasing if you happen to hit a unlucky combination of  
> \> \> spatial wavelength of your grating, drifting speed of the grating   
> \> and  
> \> \> monitor refresh / redraw rate, but this shouldn't be related to  
> \> \> rotation...  
> \> \>   
> \> \> -mario  
> \> \>   
> \> \> --- In [psychtoolbox@yahoogroups.com](mailto:psychtoolbox@yahoogroups.com), Li-Raz R \<taz\_liraz@\> wrote:  
> \> \> \>  
> \> \> \> hi  
> \> \> \> i am trying to create a stimulus that has 2 degrees of fridom.   
> \> drift  
> \> \> and angular rotation. when i tried to do the grating when it changed  
> \> \> orientation (1 degree) it allways jumped. i trying to do a continues  
> \> \> grating stimulus that drift and rotate. but i only succed partly   
> \> cause  
> \> \> it has "jumps" in the rotation. i think the reason is the phase of   
> \> the  
> \> \> grating  
> \> \> \> thanks   
> \> \> \>   
> \> \> \>   
> \> \> \> ----- Original Message ----  
> \> \> \> From: Mario Kleiner \<mario.kleiner@\>  
> \> \> \> To: [psychtoolbox@yahoogroups.com](mailto:psychtoolbox@yahoogroups.com)  
> \> \> \> Sent: Wednesday, December 20, 2006 9:16:55 PM  
> \> \> \> Subject: [psychtoolbox] Re: rotating grating  
> \> \> \>   
> \> \> \> hmm,  
> \> \> \>   
> \> \> \> could you explain in more detail or in a different way what you   
> \> want  
> \> \> \> to do and what goes wrong? I don't understand the problem from   
> \> your  
> \> \> \> posting...  
> \> \> \>   
> \> \> \> -mario  
> \> \> \>   
> \> \> \> --- In psychtoolbox@ yahoogroups. com, "taz\_liraz" \<taz\_liraz@ ..\>  
> \> \> wrote:  
> \> \> \> \>  
> \> \> \> \> hi   
> \> \> \> \> i am new with this toolbox. i am trying to do drifting and   
> \> rotating   
> \> \> \> \> grating. i modified the driftdemo (reomved the mask) and   
> \> iserted a  
> \> \> loop   
> \> \> \> \> that changing the angle in the drawtexture line but the result   
> \> is   
> \> \> \> \> drifting rotating and jumping every time it changes orientation   
> \> it   
> \> \> \> \> jumps back. does anyone has an idea how to fix it?  
> \> \> \> \> thanks  
> \> \> \> \>  
> \> \> \>   
> \> \> \>   
> \> \> \>   
> \> \> \>   
> \> \> \> \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_  
> \> \> \> Do You Yahoo!?  
> \> \> \> Tired of spam? Yahoo! Mail has the best spam protection around   
> \> \> \> [http://mail.yahoo.com](http://mail.yahoo.com)  
> \> \> \>  
> \> \>  
> \>
