# Increase contrast from a gray window to the max contrast of the grating

**URL:** <https://psychtoolbox.discourse.group/t/increase-contrast-from-a-gray-window-to-the-max-contrast-of-the-grating/3564>\
**Category:** Programming Help\
**Tags:** macos, experiment-design\
**Created:** [December 6, 2020, 4:49pm UTC](https://psychtoolbox.discourse.group/t/increase-contrast-from-a-gray-window-to-the-max-contrast-of-the-grating/3564 "2020-12-06T16:49:46Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Cecilia](https://avatars.discourse-cdn.com/v4/letter/c/c5a1d2/32.png) [@Cecilia](https://psychtoolbox.discourse.group/u/Cecilia)\
**Post date:** [December 6, 2020, 4:49pm UTC](https://psychtoolbox.discourse.group/t/increase-contrast-from-a-gray-window-to-the-max-contrast-of-the-grating/3564/1 "2020-12-06T16:49:46Z")

</div>

Hello! I’m using Matlab\_R2020a on a macOS. I’d like to know how to increase the contrast from a gray window to the max contrast of the Gabori. Thank you so much!!  
I have already read all the tutorial, could you please text the code?  
(every dt of the stimulus duration, the contrast increase)

```
%% Define colors
black = 0;
white = 255 ;
gray = (white+1)/2;

%% Open a window
[window, windowRect] = Screen('OpenWindow', screenNumber, gray);

%% Get the size and the center coordinate of the window
[screenXpixels, screenYpixels] = Screen('WindowSize', window);
[xCenter, yCenter] = RectCenter(windowRect);
[width, height] = Screen('DisplaySize', screenNumber); % udm: mm

%% Set-up alpha blending
Screen('BlendFunction', window, GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

```

dim =12;

%% Grating setup  
widthOfGrid = 400;  
widthArray = (-widthOfGrid) : widthOfGrid; % widthArray is used in creating the meshgrid  
[x, y] = meshgrid(widthArray, widthArray);

```
%% Grating Frequency
fs_min = 1;
fs_max = 12; 
N_fs = 8;
fs_range_cd = logspace(log10(fs_min), log10(fs_max), N_fs);
fs_cp = fs_range_cd .* (1/conv_pix_deg);

```

%% Grating Contrast  
if gray==white  
gray=black;  
end  
absoluteDifferenceBetweenWhiteAndGray = abs(white - gray);

```
min_contrast = 0.1;
max_contrast = 0.8;
N_contr = 6;
contrast_range = logspace(log10(min_contrast), log10(max_contrast), N_contr);

absoluteDifferenceBetweenMaxAndGray = (contrast_range .* gray);

```

%% Frequency and Contrast Combination  
M = N\_fs_N\_contr;  
m = 6;  
nTrials = M_m;  
t = randperm(nTrials);

```
FreqContr = zeros(M,2);
FreqContr_tot = zeros(nTrials,2);
c = 0;
f = 0;

for a = 1:N_fs
    for b = 1:N_contr
        c = c + 1;
        FreqContr(c,:)=[fs_cp(a) absoluteDifferenceBetweenMaxAndGray(b)];
    end
end

for d = 1:m
    for e = 1:M
        f = f + 1;
        FreqContr_tot(f,:) = FreqContr(e,:);
    end
end

rand_FreqContr = FreqContr_tot(t,:);

```

%% Set structure  
p.contrasts = absoluteDifferenceBetweenMaxAndGray;  
p.SFs = fs\_range\_cd;  
p.repeats = m;

stim\_secs = 10;

%% Time start  
timeStart\_exp = GetSecs;

for k=1:dim  
g = g + 1;  
%cosd & sind (cos & sin in degrees)  
A = cosd(alpha\_angle) \* rand\_FreqContr(g,1);  
B = sind(alpha\_angle) \* rand\_FreqContr(g,1);

```
        gratingMatrix = sin(A*x+B*y); %Converts meshgrid into a sinusoidal grating

        % Gaussian function
        circularGaussianMaskMatrix = exp(-((x .^ 2) + (y .^ 2)) / (sigma ^ 2));   
        
        % Since each entry of gratingMatrix varies between minus one and one and each entry of
        % circularGaussianMaskMatrix vary between zero and one, each entry of
        % imageMatrix varies between minus one and one.
        % -1 <= imageMatrix(x0, y0) <= 1
        imageMatrix = gratingMatrix .* circularGaussianMaskMatrix;          
        
        % Since each entry of imageMatrix is a fraction between minus one and one, 
        % multiplying imageMatrix by absoluteDifferenceBetweenWhiteAndGray and adding 
        % the gray color code baseline converts each entry of imageMatrix into a shade of gray:          
        grayscaleImageMatrix = gray + absoluteDifferenceBetweenWhiteAndGray * imageMatrix; 
        

        centeredRect = CenterRectOnPointd(baseRect, rand_C(k,1), rand_C(k,2)); 
        Screen('PutImage', window, grayscaleImageMatrix, centeredRect);

      Screen('Flip', window);
        
      timeStart_stim = GetSecs;
      timeEnd_stim = timeStart_stim + stim_secs;
      T_stim = GetSecs;
        
      trial = trial + 1;

```

end
