Square waves in spiral texture in Matlab (PTB)

2 views (last 30 days)
Hello!
I would like to use **square waves (instead of sine waves) in a spiral texture in Matlab** (PTB). As of now, the spiral is in sino wave form (without this being predefined).
This is the code to draw the spiral texture - now I would like the texture to have/ be in square wave frequencies instead of sino waves.
The general function for sino waves is the following: Z = amplitude * sin((2*3.1415*frequency.*X)+(phase));

But how would I integrate that into the mesh grid and to then draw the spiral texture?
%% Spiral texture demo
% Clear the workspace and the screen
% Issue: square waves (instead of sine waves) in a spiral texture
sca;
close all;
clear;
Screen('Preference', 'SkipSyncTests', 1);
% Here we call some default settings for setting up Psychtoolbox
PsychDefaultSetup(2);
screens = Screen('Screens');
screenNumber = max(screens);
% Define black and white
white = WhiteIndex(screenNumber);
black = BlackIndex(screenNumber);
grey = white / 2;
inc = white - grey;
% Open an on screen window
[window, windowRect] = PsychImaging('OpenWindow', screenNumber, grey);
% Get the size of the on screen window
[screenXpixels, screenYpixels] = Screen('WindowSize', window);
ifi = Screen('GetFlipInterval', window);
[xCenter, yCenter] = RectCenter(windowRect);
Screen('BlendFunction', window, 'GL_SRC_ALPHA', 'GL_ONE_MINUS_SRC_ALPHA');
% Define a simple spiral texture by defining X and Y coordinates with the
% meshgrid command, converting these to polar coordinates and finally
% defining the spiral texture
[x, y] = meshgrid(-720:1:720, -720:1:720); % define size of spiral
[th, r] = cart2pol(x, y);
spiral = grey + inc .* cos(r / 5 + th * 5);
% Make our sprial texure into a screen texture for drawing
spiralTexture = Screen('MakeTexture', window, spiral);
% We are going to draw four textures to show how a black and white texture
% can be color modulated upon drawing.
yPos = yCenter;
xPos = xCenter;
% Define the destination rectangles for our spiral textures. For this demo
% these will be the same size as out actualy texture, but this doesn't have
% to be the case. See: ScaleSpiralTextureDemo and CheckerboardTextureDemo.
[s1, s2] = size(x);
baseRect = [0 0 s1 s2];
% Color Modulation
colorMod = [0 1 0]';
% Our will fade in and out with a sine wave function
amplitude = 0.5;
frequency = 0.2;
angFreq = 2 * pi * frequency;
startPhase = 0;
time = 0;
%Z = amplitude*sin((2*3.1415*frequency.*X)+(phase)); % this is the actual
% code for square
% waves to sine waves
% BUT where would I
% integrate the Z?
% Presentation loop (press any key to exit)
while ~KbCheck
% Position of the square on this frame
thisContrast = amplitude * sin(angFreq * time + startPhase) + amplitude;
% Batch Draw all of the texures to screen
Screen('DrawTexture', window, spiralTexture, baseRect );
% Increment the time
time = time + ifi;
% Flip to the screen
Screen('Flip', window);
end
% Wait for a key press
KbStrokeWait;
% Clear the screen
sca;
Many thanks in advance :)

Answers (1)

Raag
Raag on 27 Apr 2025
Hi Marianne,
To generate a spiral texture with square waves instead of sine waves in MATLAB using Psych toolbox, you can replace the ‘cos’ or ‘sin’ function in your spiral texture definition with the ‘sign’ function. The sign function converts a sine or cosine wave into a square wave by outputting +1 or -1 depending on the sign of the input. This change will create sharp, high-contrast transitions in your spiral pattern, matching the appearance of a square wave.
Currently, your spiral texture is defined as follows:
spiral = grey + inc .* cos(r / 5 + th * 5)
This produces a smooth, sinusoidal spiral. To create a square wave spiral, simply use the ‘sign’ function:
spiral = grey + inc .* sign(cos(r / 5 + th * 5));
This modification will result in a spiral pattern with hard-edged, alternating bands (square wave), instead of smooth transitions as shown below:
For a better understanding of the above solution, refer to the following MATLAB documentation:

Products


Release

R2020b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!