# Sinewave Gratings/Squarewave Gratings with different hue and contrast.

**URL:** <https://psychtoolbox.discourse.group/t/sinewave-gratings-squarewave-gratings-with-different-hue-and-contrast/31>\
**Category:** Uncategorized\
**Created:** [March 26, 2019, 4:35pm UTC](https://psychtoolbox.discourse.group/t/sinewave-gratings-squarewave-gratings-with-different-hue-and-contrast/31 "2019-03-26T16:35:45Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![debarshi\_optometry](https://avatars.discourse-cdn.com/v4/letter/d/8c91f0/32.png) [@debarshi\_optometry](https://psychtoolbox.discourse.group/u/debarshi_optometry)\
**Post date:** [March 26, 2019, 4:35pm UTC](https://psychtoolbox.discourse.group/t/sinewave-gratings-squarewave-gratings-with-different-hue-and-contrast/31/1 "2019-03-26T16:35:45Z")

</div>

Hello All,

Do you have any codes on Color Look Up Table (CLUT)? Would you mind to find why the following codes are running for me? I think that MATLAB codes are running fine but there is a problem&nbsp;on the Psychtoolbox part?

I am trying to build a sinewave&nbsp;gratings/squarewave gratings with hue and contrast change.

If anyone can help me out.

% STEP 1: Creating a sinusoidal image in a MATLAB matrix.

nPixels = 256;

cyclesPerImage = 4;

sinVector = 1+254\*(1+ sin(2\*pi\*cyclesPerImage\*(1:nPixels)/nPixels))/2;

sinImage =ones(nPixels,1)\*sinVector;

% STEP 2: Create a set of color lookup tables (cluts) for animating the grating.

nCluts = 80;

theCluts=zeros(256,3\*nCluts);

theContrasts =sin(2\*pi\*(0:nCluts-1)/nCluts);

for i = 1:nCluts

&nbsp; &nbsp; contrast = theContrasts(i);

&nbsp; &nbsp; lowVal = 127.5-contrast\*127.5;

&nbsp; &nbsp; highVal = 127.5+contrast\*127.5;

&nbsp; &nbsp; clutEntries =[round(linspace(lowVal,highVal,256)')];

&nbsp; &nbsp; theCluts(:,(i-1)\*3+1:i\*3)= clutEntries\*ones(1,3);

&nbsp; &nbsp; &nbsp; &nbsp;

end

&nbsp;

offClut = 128\*ones(256,3);

firstClutEntry=0;

framesPerClut=3;

nCycles =2;

% STEP 3: Open up a window on the main screen, initialize the clut, and draw a grating onto the screen.

whichScreen = max(Screen('Screens')); % find the main monitor

[windowPtr, rect] = PsychImaging('OpenWindow',whichScreen);

% Screen('ClutMovie',windowPtr, theCluts, firstClutEntry, framesPerClut, nCluts\*nCycles);

Screen('LoadNormalizedGammaTable',windowPtr, theCluts, firstClutEntry, framesPerClut, nCluts\*nCycles);

% Screen('LoadCLUT',windowPtr, offClut);&nbsp;

Screen('SetClut', windowPtr,offClut);

Screen('Flip',windowPtr);

Screen('CloseScreen');

It would be great if anyone could help me out this.

Thank you,

Best,

Debarshi Datta

---

<div class="post-metadata">

**Author:** ![mariokleiner](https://avatars.discourse-cdn.com/v4/letter/m/13edae/32.png) [@mariokleiner](https://psychtoolbox.discourse.group/u/mariokleiner)\
**Post date:** [March 27, 2019, 5:17pm UTC](https://psychtoolbox.discourse.group/t/sinewave-gratings-squarewave-gratings-with-different-hue-and-contrast/31/2 "2019-03-27T17:17:39Z")

</div>

Hi,

  

the code you posted looks so wrong, and suggests you didn't even read the basic intro material or tutorials, or know how to use the basic help system for functions. Therefore i think you should first learn proper basic use of Psychtoolbox via our docs or Peter Scarfe's beginner friendly tutorials. See the "Tutorial" and "Function reference" links at [http://psychtoolbox.org](http://psychtoolbox.org) and also the primer on how to use the forum (Link "Forum") as well as the intro pdf docs in the PsychDocumentation folder.  

  

best,

-mario

  

  

> On Tue, Mar 26, 2019 at 7:17 PM [debarshi\_optometry@...](mailto:debarshi_optometry@...) [PSYCHTOOLBOX] \<[PSYCHTOOLBOX@yahoogroups.com](mailto:PSYCHTOOLBOX@yahoogroups.com)\> wrote:  
> 
> > Hello All,
> > 
> > Do you have any codes on Color Look Up Table (CLUT)? Would you mind to find why the following codes are running for me? I think that MATLAB codes are running fine but there is a problem on the Psychtoolbox part?
> > 
> > I am trying to build a sinewave gratings/squarewave gratings with hue and contrast change.
> > 
> > If anyone can help me out.
> > 
> > % STEP 1: Creating a sinusoidal image in a MATLAB matrix.
> > 
> > nPixels = 256;
> > 
> > cyclesPerImage = 4;
> > 
> > sinVector = 1+254\*(1+ sin(2\*pi\*cyclesPerImage\*(1:nPixels)/nPixels))/2;
> > 
> > sinImage =ones(nPixels,1)\*sinVector;
> > 
> > % STEP 2: Create a set of color lookup tables (cluts) for animating the grating.
> > 
> > nCluts = 80;
> > 
> > theCluts=zeros(256,3\*nCluts);
> > 
> > theContrasts =sin(2\*pi\*(0:nCluts-1)/nCluts);
> > 
> > for i = 1:nCluts
> > 
> > contrast = theContrasts(i);
> > 
> > lowVal = 127.5-contrast\*127.5;
> > 
> > highVal = 127.5+contrast\*127.5;
> > 
> > clutEntries =[round(linspace(lowVal,highVal,256)')];
> > 
> > theCluts(:,(i-1)\*3+1:i\*3)= clutEntries\*ones(1,3);
> > 
> > end
> > 
> > offClut = 128\*ones(256,3);
> > 
> > firstClutEntry=0;
> > 
> > framesPerClut=3;
> > 
> > nCycles =2;
> > 
> > % STEP 3: Open up a window on the main screen, initialize the clut, and draw a grating onto the screen.
> > 
> > whichScreen = max(Screen('Screens')); % find the main monitor
> > 
> > [windowPtr, rect] = PsychImaging('OpenWindow',whichScreen);
> > 
> > % Screen('ClutMovie',windowPtr, theCluts, firstClutEntry, framesPerClut, nCluts\*nCycles);
> > 
> > Screen('LoadNormalizedGammaTable',windowPtr, theCluts, firstClutEntry, framesPerClut, nCluts\*nCycles);
> > 
> > % Screen('LoadCLUT',windowPtr, offClut);
> > 
> > Screen('SetClut', windowPtr,offClut);
> > 
> > Screen('Flip',windowPtr);
> > 
> > Screen('CloseScreen');
> > 
> > It would be great if anyone could help me out this.
> > 
> > Thank you,
> > 
> > Best,
> > 
> > Debarshi Datta

---

<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:** [March 27, 2019, 5:29pm UTC](https://psychtoolbox.discourse.group/t/sinewave-gratings-squarewave-gratings-with-different-hue-and-contrast/31/3 "2019-03-27T17:29:07Z")

</div>

Hi Mario,
Thanks for your suggestions. The code I posted MATLAB part of the codes is running very fine. But the stuck only on the Psychtoolbox syntax. I know how to use the help function. But, probably the syntax is so old that there is no much help regarding that. These codes I got from online, change a bit but it is not working. Surely, I will learn more, but if anyone knows better please send some CLUT related codes or fix the odds on my codes- the way I will have better understandings.

  

Thank you,

  

Best,

  

Debarshi

  

> On Wed, Mar 27, 2019 at 1:18 PM Mario Kleiner [mario.kleiner.de@...](mailto:mario.kleiner.de@...) [PSYCHTOOLBOX] \<[PSYCHTOOLBOX@yahoogroups.com](mailto:PSYCHTOOLBOX@yahoogroups.com)\> wrote:  
> 
> > Hi,
> > 
> >   
> > 
> > the code you posted looks so wrong, and suggests you didn't even read the basic intro material or tutorials, or know how to use the basic help system for functions. Therefore i think you should first learn proper basic use of Psychtoolbox via our docs or Peter Scarfe's beginner friendly tutorials. See the "Tutorial" and "Function reference" links at [http://psychtoolbox.org](http://psychtoolbox.org) and also the primer on how to use the forum (Link "Forum") as well as the intro pdf docs in the PsychDocumentation folder.  
> > 
> >   
> > 
> > best,
> > 
> > -mario
> > 
> >   
> > 
> >   
> > 
> > On Tue, Mar 26, 2019 at 7:17 PM [debarshi\_optometry@...](mailto:debarshi_optometry@...) [PSYCHTOOLBOX] \<[PSYCHTOOLBOX@yahoogroups.com](mailto:PSYCHTOOLBOX@yahoogroups.com)\> wrote:  
> > 
> > > Hello All,
> > > 
> > > Do you have any codes on Color Look Up Table (CLUT)? Would you mind to find why the following codes are running for me? I think that MATLAB codes are running fine but there is a problem on the Psychtoolbox part?
> > > 
> > > I am trying to build a sinewave gratings/squarewave gratings with hue and contrast change.
> > > 
> > > If anyone can help me out.
> > > 
> > > % STEP 1: Creating a sinusoidal image in a MATLAB matrix.
> > > 
> > > nPixels = 256;
> > > 
> > > cyclesPerImage = 4;
> > > 
> > > sinVector = 1+254\*(1+ sin(2\*pi\*cyclesPerImage\*(1:nPixels)/nPixels))/2;
> > > 
> > > sinImage =ones(nPixels,1)\*sinVector;
> > > 
> > > % STEP 2: Create a set of color lookup tables (cluts) for animating the grating.
> > > 
> > > nCluts = 80;
> > > 
> > > theCluts=zeros(256,3\*nCluts);
> > > 
> > > theContrasts =sin(2\*pi\*(0:nCluts-1)/nCluts);
> > > 
> > > for i = 1:nCluts
> > > 
> > > contrast = theContrasts(i);
> > > 
> > > lowVal = 127.5-contrast\*127.5;
> > > 
> > > highVal = 127.5+contrast\*127.5;
> > > 
> > > clutEntries =[round(linspace(lowVal,highVal,256)')];
> > > 
> > > theCluts(:,(i-1)\*3+1:i\*3)= clutEntries\*ones(1,3);
> > > 
> > > end
> > > 
> > > offClut = 128\*ones(256,3);
> > > 
> > > firstClutEntry=0;
> > > 
> > > framesPerClut=3;
> > > 
> > > nCycles =2;
> > > 
> > > % STEP 3: Open up a window on the main screen, initialize the clut, and draw a grating onto the screen.
> > > 
> > > whichScreen = max(Screen('Screens')); % find the main monitor
> > > 
> > > [windowPtr, rect] = PsychImaging('OpenWindow',whichScreen);
> > > 
> > > % Screen('ClutMovie',windowPtr, theCluts, firstClutEntry, framesPerClut, nCluts\*nCycles);
> > > 
> > > Screen('LoadNormalizedGammaTable',windowPtr, theCluts, firstClutEntry, framesPerClut, nCluts\*nCycles);
> > > 
> > > % Screen('LoadCLUT',windowPtr, offClut);
> > > 
> > > Screen('SetClut', windowPtr,offClut);
> > > 
> > > Screen('Flip',windowPtr);
> > > 
> > > Screen('CloseScreen');
> > > 
> > > It would be great if anyone could help me out this.
> > > 
> > > Thank you,
> > > 
> > > Best,
> > > 
> > > Debarshi Datta

---

<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:** [March 27, 2019, 6:43pm UTC](https://psychtoolbox.discourse.group/t/sinewave-gratings-squarewave-gratings-with-different-hue-and-contrast/31/4 "2019-03-27T18:43:21Z")

</div>

Hello Marion,
  

Thanks for your suggestions. I just want to do square wave gratings with different hue and contrast. For those reasons, I was thinking to use CLUT.

  

Best,

  

Debarshi

  

> On Wed, Mar 27, 2019 at 2:13 PM Mario Kleiner [mario.kleiner.de@...](mailto:mario.kleiner.de@...) [PSYCHTOOLBOX] \<[PSYCHTOOLBOX@yahoogroups.com](mailto:PSYCHTOOLBOX@yahoogroups.com)\> wrote:  
> 
> > On Wed, Mar 27, 2019 at 6:31 PM Debarshi Datta [ddatta2014@...](mailto:ddatta2014@...)  
> > [PSYCHTOOLBOX] \<[PSYCHTOOLBOX@yahoogroups.com](mailto:PSYCHTOOLBOX@yahoogroups.com)\> wrote:  
> > \> Hi Mario,  
> > \> Thanks for your suggestions. The code I posted MATLAB part of the codes is running very fine. But the stuck only on the Psychtoolbox syntax. I know how to use the help function. But, probably the syntax is so old that there is no much help regarding that. These codes I got from online, change a bit but it is not working. Surely, I will learn more, but if anyone knows better please send some CLUT related codes or fix the odds on my codes- the way I will have better understandings.  
> > \>  
> >   
> > But if you know how to use our help system, it should have been  
> > absolutely obvious within 5 minutes that that code can not possibly  
> > work, not on any PTB released within the last 15 years? So far your  
> > questions looked like one of these "i don't want to do my homework,  
> > can somebody else do it for me, please?" posts.  
> >   
> > Clut's in the conventional sense nowadays are only useful for display  
> > color/gamma calibration, nothing else. The current design of operating  
> > systems makes them a no-go for almost any other classic use case.  
> >   
> > Psychtoolbox PsychImaging() command has a special clut mode that  
> > allows to use clut's for things other than gamma correction, by  
> > emulating old behaviour from the 90's.  
> >   
> > But the thing is, it is not clear at all to me what exactly you want  
> > to achieve and what the requirements of your optimal stimulus are. It  
> > seems pretty likely that any kind of clut's would be the wrong or at  
> > least highly inefficient approach to ait.  
> >   
> > You probably should post an exact description of what you want to  
> > achieve - paradigm, mathematic formulas defining your stimulus,  
> > possibly Matlab sample code that would implement that stimulus purely  
> > in Matlab, ie. outputting a simple image matrix for display. Then  
> > somebody - and not neccessarily myself - might be able to guide you on  
> > how to solve that task efficiently and accurately in the year 2019 on  
> > hardware from this decade.  
> >   
> > -mario  
> >   
> > \> Thank you,  
> > \>  
> > \> Best,  
> > \>  
> > \> Debarshi  
> > \>  
> > \> On Wed, Mar 27, 2019 at 1:18 PM Mario Kleiner [mario.kleiner.de@...](mailto:mario.kleiner.de@...) [PSYCHTOOLBOX] \<[PSYCHTOOLBOX@yahoogroups.com](mailto:PSYCHTOOLBOX@yahoogroups.com)\> wrote:  
> > \>\>  
> > \>\>  
> > \>\>  
> > \>\> Hi,  
> > \>\>  
> > \>\> the code you posted looks so wrong, and suggests you didn't even read the basic intro material or tutorials, or know how to use the basic help system for functions. Therefore i think you should first learn proper basic use of Psychtoolbox via our docs or Peter Scarfe's beginner friendly tutorials. See the "Tutorial" and "Function reference" links at [http://psychtoolbox.org](http://psychtoolbox.org) and also the primer on how to use the forum (Link "Forum") as well as the intro pdf docs in the PsychDocumentation folder.  
> > \>\>  
> > \>\> best,  
> > \>\> -mario  
> > \>\>  
> > \>\>  
> > \>\> On Tue, Mar 26, 2019 at 7:17 PM [debarshi\_optometry@...](mailto:debarshi_optometry@...) [PSYCHTOOLBOX] \<[PSYCHTOOLBOX@yahoogroups.com](mailto:PSYCHTOOLBOX@yahoogroups.com)\> wrote:  
> > \>\>\>  
> > \>\>\>  
> > \>\>\>  
> > \>\>\> Hello All,  
> > \>\>\>  
> > \>\>\>  
> > \>\>\> Do you have any codes on Color Look Up Table (CLUT)? Would you mind to find why the following codes are running for me? I think that MATLAB codes are running fine but there is a problem on the Psychtoolbox part?  
> > \>\>\>  
> > \>\>\>  
> > \>\>\> I am trying to build a sinewave gratings/squarewave gratings with hue and contrast change.  
> > \>\>\>  
> > \>\>\>  
> > \>\>\> If anyone can help me out.  
> > \>\>\>  
> > \>\>\>  
> > \>\>\> % STEP 1: Creating a sinusoidal image in a MATLAB matrix.  
> > \>\>\>  
> > \>\>\> nPixels = 256;  
> > \>\>\>  
> > \>\>\> cyclesPerImage = 4;  
> > \>\>\>  
> > \>\>\> sinVector = 1+254\*(1+ sin(2\*pi\*cyclesPerImage\*(1:nPixels)/nPixels))/2;  
> > \>\>\>  
> > \>\>\> sinImage =ones(nPixels,1)\*sinVector;  
> > \>\>\>  
> > \>\>\>  
> > \>\>\> % STEP 2: Create a set of color lookup tables (cluts) for animating the grating.  
> > \>\>\>  
> > \>\>\> nCluts = 80;  
> > \>\>\>  
> > \>\>\> theCluts=zeros(256,3\*nCluts);  
> > \>\>\>  
> > \>\>\> theContrasts =sin(2\*pi\*(0:nCluts-1)/nCluts);  
> > \>\>\>  
> > \>\>\> for i = 1:nCluts  
> > \>\>\>  
> > \>\>\> contrast = theContrasts(i);  
> > \>\>\>  
> > \>\>\> lowVal = 127.5-contrast\*127.5;  
> > \>\>\>  
> > \>\>\> highVal = 127.5+contrast\*127.5;  
> > \>\>\>  
> > \>\>\> clutEntries =[round(linspace(lowVal,highVal,256)')];  
> > \>\>\>  
> > \>\>\> theCluts(:,(i-1)\*3+1:i\*3)= clutEntries\*ones(1,3);  
> > \>\>\>  
> > \>\>\>  
> > \>\>\>  
> > \>\>\> end  
> > \>\>\>  
> > \>\>\>  
> > \>\>\>  
> > \>\>\> offClut = 128\*ones(256,3);  
> > \>\>\>  
> > \>\>\>  
> > \>\>\> firstClutEntry=0;  
> > \>\>\>  
> > \>\>\> framesPerClut=3;  
> > \>\>\>  
> > \>\>\> nCycles =2;  
> > \>\>\>  
> > \>\>\>  
> > \>\>\> % STEP 3: Open up a window on the main screen, initialize the clut, and draw a grating onto the screen.  
> > \>\>\>  
> > \>\>\>  
> > \>\>\> whichScreen = max(Screen('Screens')); % find the main monitor  
> > \>\>\>  
> > \>\>\> [windowPtr, rect] = PsychImaging('OpenWindow',whichScreen);  
> > \>\>\>  
> > \>\>\> % Screen('ClutMovie',windowPtr, theCluts, firstClutEntry, framesPerClut, nCluts\*nCycles);  
> > \>\>\>  
> > \>\>\> Screen('LoadNormalizedGammaTable',windowPtr, theCluts, firstClutEntry, framesPerClut, nCluts\*nCycles);  
> > \>\>\>  
> > \>\>\> % Screen('LoadCLUT',windowPtr, offClut);  
> > \>\>\>  
> > \>\>\> Screen('SetClut', windowPtr,offClut);  
> > \>\>\>  
> > \>\>\> Screen('Flip',windowPtr);  
> > \>\>\>  
> > \>\>\>  
> > \>\>\> Screen('CloseScreen');  
> > \>\>\>  
> > \>\>\>  
> > \>\>\> It would be great if anyone could help me out this.  
> > \>\>\>  
> > \>\>\>  
> > \>\>\> Thank you,  
> > \>\>\>  
> > \>\>\>  
> > \>\>\> Best,  
> > \>\>\>  
> > \>\>\>  
> > \>\>\> Debarshi Datta  
> > \>\>\>  
> > \>\>\>  
> > \>

---

<div class="post-metadata">

**Author:** ![mariokleiner](https://avatars.discourse-cdn.com/v4/letter/m/13edae/32.png) [@mariokleiner](https://psychtoolbox.discourse.group/u/mariokleiner)\
**Post date:** [March 27, 2019, 7:20pm UTC](https://psychtoolbox.discourse.group/t/sinewave-gratings-squarewave-gratings-with-different-hue-and-contrast/31/5 "2019-03-27T19:20:04Z")

</div>

> On Wed, Mar 27, 2019 at 7:43 PM Debarshi Datta [ddatta2014@...](mailto:ddatta2014@...) [PSYCHTOOLBOX] \<[PSYCHTOOLBOX@yahoogroups.com](mailto:PSYCHTOOLBOX@yahoogroups.com)\> wrote:  
> 
> > Hello Marion,
> >   
> > 
> > Thanks for your suggestions. I just want to do square wave gratings with different hue and contrast. For those reasons, I was thinking to use CLUT.
> 
>   
> 
> "help PsychDemos" or that section of our online reference would have pointed you to multiple demos on how to do that. Every into text of ours and on how to use the forum tells you to have a good look at the provided tutorials and PsychDemos first, before asking questions. E.g.,
> 
>   
> 
> ProceduralSquareWaveDemo.m for efficient square wave gratings, or DriftDemo4.m for efficient sine gratings, or ProceduralGarboriumDemo.m for other gratings like gaussian blobs or gabor patches. Obviously you have to read and understand the help for the functions used inside those demos on how to use and control these procedural stimuli properly. And maybe our Tutorial pdf slides on how to use alpha-blending efficiently with these.
> 
>   
> 
> This definitely allows fast online control of frequency, phase, contrast, and color. The way we define contrast may not be exactly the way you want it, because in vision science and engineering there seem to be multiple slightly conflicting definitions, so read everything about contrast and signal amplitude in the help texts very carefully. How the possible color manipulations relate to hue, and if they control hue in the way you want to control them, i don't know, because you still don't give detailed explanations.
> 
>   
> 
> -mario  
> 
>   
> 
> > Best,
> > 
> >   
> > 
> > Debarshi
> > 
> >   
> > 
> > On Wed, Mar 27, 2019 at 2:13 PM Mario Kleiner [mario.kleiner.de@...](mailto:mario.kleiner.de@...) [PSYCHTOOLBOX] \<[PSYCHTOOLBOX@yahoogroups.com](mailto:PSYCHTOOLBOX@yahoogroups.com)\> wrote:  
> > 
> > > On Wed, Mar 27, 2019 at 6:31 PM Debarshi Datta [ddatta2014@...](mailto:ddatta2014@...)  
> > > [PSYCHTOOLBOX] \<[PSYCHTOOLBOX@yahoogroups.com](mailto:PSYCHTOOLBOX@yahoogroups.com)\> wrote:  
> > > \> Hi Mario,  
> > > \> Thanks for your suggestions. The code I posted MATLAB part of the codes is running very fine. But the stuck only on the Psychtoolbox syntax. I know how to use the help function. But, probably the syntax is so old that there is no much help regarding that. These codes I got from online, change a bit but it is not working. Surely, I will learn more, but if anyone knows better please send some CLUT related codes or fix the odds on my codes- the way I will have better understandings.  
> > > \>  
> > >   
> > > But if you know how to use our help system, it should have been  
> > > absolutely obvious within 5 minutes that that code can not possibly  
> > > work, not on any PTB released within the last 15 years? So far your  
> > > questions looked like one of these "i don't want to do my homework,  
> > > can somebody else do it for me, please?" posts.  
> > >   
> > > Clut's in the conventional sense nowadays are only useful for display  
> > > color/gamma calibration, nothing else. The current design of operating  
> > > systems makes them a no-go for almost any other classic use case.  
> > >   
> > > Psychtoolbox PsychImaging() command has a special clut mode that  
> > > allows to use clut's for things other than gamma correction, by  
> > > emulating old behaviour from the 90's.  
> > >   
> > > But the thing is, it is not clear at all to me what exactly you want  
> > > to achieve and what the requirements of your optimal stimulus are. It  
> > > seems pretty likely that any kind of clut's would be the wrong or at  
> > > least highly inefficient approach to ait.  
> > >   
> > > You probably should post an exact description of what you want to  
> > > achieve - paradigm, mathematic formulas defining your stimulus,  
> > > possibly Matlab sample code that would implement that stimulus purely  
> > > in Matlab, ie. outputting a simple image matrix for display. Then  
> > > somebody - and not neccessarily myself - might be able to guide you on  
> > > how to solve that task efficiently and accurately in the year 2019 on  
> > > hardware from this decade.  
> > >   
> > > -mario  
> > >   
> > > \> Thank you,  
> > > \>  
> > > \> Best,  
> > > \>  
> > > \> Debarshi  
> > > \>  
> > > \> On Wed, Mar 27, 2019 at 1:18 PM Mario Kleiner [mario.kleiner.de@...](mailto:mario.kleiner.de@...) [PSYCHTOOLBOX] \<[PSYCHTOOLBOX@yahoogroups.com](mailto:PSYCHTOOLBOX@yahoogroups.com)\> wrote:  
> > > \>\>  
> > > \>\>  
> > > \>\>  
> > > \>\> Hi,  
> > > \>\>  
> > > \>\> the code you posted looks so wrong, and suggests you didn't even read the basic intro material or tutorials, or know how to use the basic help system for functions. Therefore i think you should first learn proper basic use of Psychtoolbox via our docs or Peter Scarfe's beginner friendly tutorials. See the "Tutorial" and "Function reference" links at [http://psychtoolbox.org](http://psychtoolbox.org) and also the primer on how to use the forum (Link "Forum") as well as the intro pdf docs in the PsychDocumentation folder.  
> > > \>\>  
> > > \>\> best,  
> > > \>\> -mario  
> > > \>\>  
> > > \>\>  
> > > \>\> On Tue, Mar 26, 2019 at 7:17 PM [debarshi\_optometry@...](mailto:debarshi_optometry@...) [PSYCHTOOLBOX] \<[PSYCHTOOLBOX@yahoogroups.com](mailto:PSYCHTOOLBOX@yahoogroups.com)\> wrote:  
> > > \>\>\>  
> > > \>\>\>  
> > > \>\>\>  
> > > \>\>\> Hello All,  
> > > \>\>\>  
> > > \>\>\>  
> > > \>\>\> Do you have any codes on Color Look Up Table (CLUT)? Would you mind to find why the following codes are running for me? I think that MATLAB codes are running fine but there is a problem on the Psychtoolbox part?  
> > > \>\>\>  
> > > \>\>\>  
> > > \>\>\> I am trying to build a sinewave gratings/squarewave gratings with hue and contrast change.  
> > > \>\>\>  
> > > \>\>\>  
> > > \>\>\> If anyone can help me out.  
> > > \>\>\>  
> > > \>\>\>  
> > > \>\>\> % STEP 1: Creating a sinusoidal image in a MATLAB matrix.  
> > > \>\>\>  
> > > \>\>\> nPixels = 256;  
> > > \>\>\>  
> > > \>\>\> cyclesPerImage = 4;  
> > > \>\>\>  
> > > \>\>\> sinVector = 1+254\*(1+ sin(2\*pi\*cyclesPerImage\*(1:nPixels)/nPixels))/2;  
> > > \>\>\>  
> > > \>\>\> sinImage =ones(nPixels,1)\*sinVector;  
> > > \>\>\>  
> > > \>\>\>  
> > > \>\>\> % STEP 2: Create a set of color lookup tables (cluts) for animating the grating.  
> > > \>\>\>  
> > > \>\>\> nCluts = 80;  
> > > \>\>\>  
> > > \>\>\> theCluts=zeros(256,3\*nCluts);  
> > > \>\>\>  
> > > \>\>\> theContrasts =sin(2\*pi\*(0:nCluts-1)/nCluts);  
> > > \>\>\>  
> > > \>\>\> for i = 1:nCluts  
> > > \>\>\>  
> > > \>\>\> contrast = theContrasts(i);  
> > > \>\>\>  
> > > \>\>\> lowVal = 127.5-contrast\*127.5;  
> > > \>\>\>  
> > > \>\>\> highVal = 127.5+contrast\*127.5;  
> > > \>\>\>  
> > > \>\>\> clutEntries =[round(linspace(lowVal,highVal,256)')];  
> > > \>\>\>  
> > > \>\>\> theCluts(:,(i-1)\*3+1:i\*3)= clutEntries\*ones(1,3);  
> > > \>\>\>  
> > > \>\>\>  
> > > \>\>\>  
> > > \>\>\> end  
> > > \>\>\>  
> > > \>\>\>  
> > > \>\>\>  
> > > \>\>\> offClut = 128\*ones(256,3);  
> > > \>\>\>  
> > > \>\>\>  
> > > \>\>\> firstClutEntry=0;  
> > > \>\>\>  
> > > \>\>\> framesPerClut=3;  
> > > \>\>\>  
> > > \>\>\> nCycles =2;  
> > > \>\>\>  
> > > \>\>\>  
> > > \>\>\> % STEP 3: Open up a window on the main screen, initialize the clut, and draw a grating onto the screen.  
> > > \>\>\>  
> > > \>\>\>  
> > > \>\>\> whichScreen = max(Screen('Screens')); % find the main monitor  
> > > \>\>\>  
> > > \>\>\> [windowPtr, rect] = PsychImaging('OpenWindow',whichScreen);  
> > > \>\>\>  
> > > \>\>\> % Screen('ClutMovie',windowPtr, theCluts, firstClutEntry, framesPerClut, nCluts\*nCycles);  
> > > \>\>\>  
> > > \>\>\> Screen('LoadNormalizedGammaTable',windowPtr, theCluts, firstClutEntry, framesPerClut, nCluts\*nCycles);  
> > > \>\>\>  
> > > \>\>\> % Screen('LoadCLUT',windowPtr, offClut);  
> > > \>\>\>  
> > > \>\>\> Screen('SetClut', windowPtr,offClut);  
> > > \>\>\>  
> > > \>\>\> Screen('Flip',windowPtr);  
> > > \>\>\>  
> > > \>\>\>  
> > > \>\>\> Screen('CloseScreen');  
> > > \>\>\>  
> > > \>\>\>  
> > > \>\>\> It would be great if anyone could help me out this.  
> > > \>\>\>  
> > > \>\>\>  
> > > \>\>\> Thank you,  
> > > \>\>\>  
> > > \>\>\>  
> > > \>\>\> Best,  
> > > \>\>\>  
> > > \>\>\>  
> > > \>\>\> Debarshi Datta  
> > > \>\>\>  
> > > \>\>\>  
> > > \>

---

<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:** [March 28, 2019, 4:32pm UTC](https://psychtoolbox.discourse.group/t/sinewave-gratings-squarewave-gratings-with-different-hue-and-contrast/31/6 "2019-03-28T16:32:28Z")

</div>

Hello Mario,
  

Thanks for your post. I made the gratings with different hue by using only Matlab coding. The output of the function I used there was an image. So I got the square wave grating on the image. I want that on screen by using psych tool box. Please see the codes I attached (
[GratingImageFinal.m](https://mail.google.com/mail/u/1?ui=2&ik=5a5f27ea55&attid=0.1&permmsgid=msg-a:r3770458238249794075&view=att&disp=safe&realattid=f_jtsusuk90)
), it runs fine. I used this: [x,y]=meshgrid(linspace(0, wDeg, nxPix+1), linspace(0, wDeg, nyPix+1));

  

But, when I try to run the code on psychtoolbox the colormap I used before does not run in psychtoolbox. Please see the attachment (
[GratingsPsychtoolbox.m](https://mail.google.com/mail/u/1?ui=2&ik=5a5f27ea55&attid=0.2&permmsgid=msg-a:r3770458238249794075&view=att&disp=safe&realattid=f_jtsutf791))
 I sent the code, It runs also fine, but square wave gratings comes on grayscale on screen. I thought then CLUT might go here fine.

  

Please see the codes I attached.

  

Thanks for your help,

  

Best,

  

Debarshi

  

> On Wed, Mar 27, 2019 at 3:20 PM Mario Kleiner [mario.kleiner.de@...](mailto:mario.kleiner.de@...) [PSYCHTOOLBOX] \<[PSYCHTOOLBOX@yahoogroups.com](mailto:PSYCHTOOLBOX@yahoogroups.com)\> wrote:  
> 
> > On Wed, Mar 27, 2019 at 7:43 PM Debarshi Datta [ddatta2014@...](mailto:ddatta2014@...) [PSYCHTOOLBOX] \<[PSYCHTOOLBOX@yahoogroups.com](mailto:PSYCHTOOLBOX@yahoogroups.com)\> wrote:  
> > 
> > > Hello Marion,
> > >   
> > > 
> > > Thanks for your suggestions. I just want to do square wave gratings with different hue and contrast. For those reasons, I was thinking to use CLUT.
> > 
> >   
> > 
> > "help PsychDemos" or that section of our online reference would have pointed you to multiple demos on how to do that. Every into text of ours and on how to use the forum tells you to have a good look at the provided tutorials and PsychDemos first, before asking questions. E.g.,
> > 
> >   
> > 
> > ProceduralSquareWaveDemo.m for efficient square wave gratings, or DriftDemo4.m for efficient sine gratings, or ProceduralGarboriumDemo.m for other gratings like gaussian blobs or gabor patches. Obviously you have to read and understand the help for the functions used inside those demos on how to use and control these procedural stimuli properly. And maybe our Tutorial pdf slides on how to use alpha-blending efficiently with these.
> > 
> >   
> > 
> > This definitely allows fast online control of frequency, phase, contrast, and color. The way we define contrast may not be exactly the way you want it, because in vision science and engineering there seem to be multiple slightly conflicting definitions, so read everything about contrast and signal amplitude in the help texts very carefully. How the possible color manipulations relate to hue, and if they control hue in the way you want to control them, i don't know, because you still don't give detailed explanations.
> > 
> >   
> > 
> > -mario  
> > 
> >   
> > 
> > > Best,
> > > 
> > >   
> > > 
> > > Debarshi
> > > 
> > >   
> > > 
> > > On Wed, Mar 27, 2019 at 2:13 PM Mario Kleiner [mario.kleiner.de@...](mailto:mario.kleiner.de@...) [PSYCHTOOLBOX] \<[PSYCHTOOLBOX@yahoogroups.com](mailto:PSYCHTOOLBOX@yahoogroups.com)\> wrote:  
> > > 
> > > > On Wed, Mar 27, 2019 at 6:31 PM Debarshi Datta [ddatta2014@...](mailto:ddatta2014@...)  
> > > > [PSYCHTOOLBOX] \<[PSYCHTOOLBOX@yahoogroups.com](mailto:PSYCHTOOLBOX@yahoogroups.com)\> wrote:  
> > > > \> Hi Mario,  
> > > > \> Thanks for your suggestions. The code I posted MATLAB part of the codes is running very fine. But the stuck only on the Psychtoolbox syntax. I know how to use the help function. But, probably the syntax is so old that there is no much help regarding that. These codes I got from online, change a bit but it is not working. Surely, I will learn more, but if anyone knows better please send some CLUT related codes or fix the odds on my codes- the way I will have better understandings.  
> > > > \>  
> > > >   
> > > > But if you know how to use our help system, it should have been  
> > > > absolutely obvious within 5 minutes that that code can not possibly  
> > > > work, not on any PTB released within the last 15 years? So far your  
> > > > questions looked like one of these "i don't want to do my homework,  
> > > > can somebody else do it for me, please?" posts.  
> > > >   
> > > > Clut's in the conventional sense nowadays are only useful for display  
> > > > color/gamma calibration, nothing else. The current design of operating  
> > > > systems makes them a no-go for almost any other classic use case.  
> > > >   
> > > > Psychtoolbox PsychImaging() command has a special clut mode that  
> > > > allows to use clut's for things other than gamma correction, by  
> > > > emulating old behaviour from the 90's.  
> > > >   
> > > > But the thing is, it is not clear at all to me what exactly you want  
> > > > to achieve and what the requirements of your optimal stimulus are. It  
> > > > seems pretty likely that any kind of clut's would be the wrong or at  
> > > > least highly inefficient approach to ait.  
> > > >   
> > > > You probably should post an exact description of what you want to  
> > > > achieve - paradigm, mathematic formulas defining your stimulus,  
> > > > possibly Matlab sample code that would implement that stimulus purely  
> > > > in Matlab, ie. outputting a simple image matrix for display. Then  
> > > > somebody - and not neccessarily myself - might be able to guide you on  
> > > > how to solve that task efficiently and accurately in the year 2019 on  
> > > > hardware from this decade.  
> > > >   
> > > > -mario  
> > > >   
> > > > \> Thank you,  
> > > > \>  
> > > > \> Best,  
> > > > \>  
> > > > \> Debarshi  
> > > > \>  
> > > > \> On Wed, Mar 27, 2019 at 1:18 PM Mario Kleiner [mario.kleiner.de@...](mailto:mario.kleiner.de@...) [PSYCHTOOLBOX] \<[PSYCHTOOLBOX@yahoogroups.com](mailto:PSYCHTOOLBOX@yahoogroups.com)\> wrote:  
> > > > \>\>  
> > > > \>\>  
> > > > \>\>  
> > > > \>\> Hi,  
> > > > \>\>  
> > > > \>\> the code you posted looks so wrong, and suggests you didn't even read the basic intro material or tutorials, or know how to use the basic help system for functions. Therefore i think you should first learn proper basic use of Psychtoolbox via our docs or Peter Scarfe's beginner friendly tutorials. See the "Tutorial" and "Function reference" links at [http://psychtoolbox.org](http://psychtoolbox.org) and also the primer on how to use the forum (Link "Forum") as well as the intro pdf docs in the PsychDocumentation folder.  
> > > > \>\>  
> > > > \>\> best,  
> > > > \>\> -mario  
> > > > \>\>  
> > > > \>\>  
> > > > \>\> On Tue, Mar 26, 2019 at 7:17 PM [debarshi\_optometry@...](mailto:debarshi_optometry@...) [PSYCHTOOLBOX] \<[PSYCHTOOLBOX@yahoogroups.com](mailto:PSYCHTOOLBOX@yahoogroups.com)\> wrote:  
> > > > \>\>\>  
> > > > \>\>\>  
> > > > \>\>\>  
> > > > \>\>\> Hello All,  
> > > > \>\>\>  
> > > > \>\>\>  
> > > > \>\>\> Do you have any codes on Color Look Up Table (CLUT)? Would you mind to find why the following codes are running for me? I think that MATLAB codes are running fine but there is a problem on the Psychtoolbox part?  
> > > > \>\>\>  
> > > > \>\>\>  
> > > > \>\>\> I am trying to build a sinewave gratings/squarewave gratings with hue and contrast change.  
> > > > \>\>\>  
> > > > \>\>\>  
> > > > \>\>\> If anyone can help me out.  
> > > > \>\>\>  
> > > > \>\>\>  
> > > > \>\>\> % STEP 1: Creating a sinusoidal image in a MATLAB matrix.  
> > > > \>\>\>  
> > > > \>\>\> nPixels = 256;  
> > > > \>\>\>  
> > > > \>\>\> cyclesPerImage = 4;  
> > > > \>\>\>  
> > > > \>\>\> sinVector = 1+254\*(1+ sin(2\*pi\*cyclesPerImage\*(1:nPixels)/nPixels))/2;  
> > > > \>\>\>  
> > > > \>\>\> sinImage =ones(nPixels,1)\*sinVector;  
> > > > \>\>\>  
> > > > \>\>\>  
> > > > \>\>\> % STEP 2: Create a set of color lookup tables (cluts) for animating the grating.  
> > > > \>\>\>  
> > > > \>\>\> nCluts = 80;  
> > > > \>\>\>  
> > > > \>\>\> theCluts=zeros(256,3\*nCluts);  
> > > > \>\>\>  
> > > > \>\>\> theContrasts =sin(2\*pi\*(0:nCluts-1)/nCluts);  
> > > > \>\>\>  
> > > > \>\>\> for i = 1:nCluts  
> > > > \>\>\>  
> > > > \>\>\> contrast = theContrasts(i);  
> > > > \>\>\>  
> > > > \>\>\> lowVal = 127.5-contrast\*127.5;  
> > > > \>\>\>  
> > > > \>\>\> highVal = 127.5+contrast\*127.5;  
> > > > \>\>\>  
> > > > \>\>\> clutEntries =[round(linspace(lowVal,highVal,256)')];  
> > > > \>\>\>  
> > > > \>\>\> theCluts(:,(i-1)\*3+1:i\*3)= clutEntries\*ones(1,3);  
> > > > \>\>\>  
> > > > \>\>\>  
> > > > \>\>\>  
> > > > \>\>\> end  
> > > > \>\>\>  
> > > > \>\>\>  
> > > > \>\>\>  
> > > > \>\>\> offClut = 128\*ones(256,3);  
> > > > \>\>\>  
> > > > \>\>\>  
> > > > \>\>\> firstClutEntry=0;  
> > > > \>\>\>  
> > > > \>\>\> framesPerClut=3;  
> > > > \>\>\>  
> > > > \>\>\> nCycles =2;  
> > > > \>\>\>  
> > > > \>\>\>  
> > > > \>\>\> % STEP 3: Open up a window on the main screen, initialize the clut, and draw a grating onto the screen.  
> > > > \>\>\>  
> > > > \>\>\>  
> > > > \>\>\> whichScreen = max(Screen('Screens')); % find the main monitor  
> > > > \>\>\>  
> > > > \>\>\> [windowPtr, rect] = PsychImaging('OpenWindow',whichScreen);  
> > > > \>\>\>  
> > > > \>\>\> % Screen('ClutMovie',windowPtr, theCluts, firstClutEntry, framesPerClut, nCluts\*nCycles);  
> > > > \>\>\>  
> > > > \>\>\> Screen('LoadNormalizedGammaTable',windowPtr, theCluts, firstClutEntry, framesPerClut, nCluts\*nCycles);  
> > > > \>\>\>  
> > > > \>\>\> % Screen('LoadCLUT',windowPtr, offClut);  
> > > > \>\>\>  
> > > > \>\>\> Screen('SetClut', windowPtr,offClut);  
> > > > \>\>\>  
> > > > \>\>\> Screen('Flip',windowPtr);  
> > > > \>\>\>  
> > > > \>\>\>  
> > > > \>\>\> Screen('CloseScreen');  
> > > > \>\>\>  
> > > > \>\>\>  
> > > > \>\>\> It would be great if anyone could help me out this.  
> > > > \>\>\>  
> > > > \>\>\>  
> > > > \>\>\> Thank you,  
> > > > \>\>\>  
> > > > \>\>\>  
> > > > \>\>\> Best,  
> > > > \>\>\>  
> > > > \>\>\>  
> > > > \>\>\> Debarshi Datta  
> > > > \>\>\>  
> > > > \>\>\>  
> > > > \>
