Accelerating stimulus creation

Dear PTB community,

I am working on an experiment in which I want to present several oriented Gabors together. I want to be able to create my stimulus on the fly, so that I can change stimulus parameters according to subject behavior.

However, it turns out that even if I focus on only a subset of pixels from the screen and compute the value of the Gabors over this subset this is still quite time consuming and cannot be done within the time I was hoping to do it.

[gab_x gab_y] = meshgrid(0:(res(1)-1), 0:(res(2)-1));
a=cosd(tilt)*sf*360;
b=sind(tilt)*sf*360;
multConst=1/(sqrt(2*pi)*sc);
x_factor=-1*(gab_x-x).^2;
y_factor=-1*(gab_y-y).^2;
sinWave=sind(a*gab_x+b*gab_y+phase);
varScale=2*sc^2;
m=(multConst*exp(x_factor/varScale+y_factor/varScale).*sinWave)';

Where:
sc is the Gabor space constant
sf is the Gabor spatial frequency
tilt is the tilt of the Gabor
and res is a 1x2 matrix with the length and width of the subset of pixels on which the stimulus is computed.
The whole thing takes me (I am running it on a Mac dual core 2 GHz G5 with 2.5 GB RAM running OS 10.4.9, using Matlab 7.1 and PTB 1.0.6) about 100 msec for a 343x343 matrix. For 9 Gabors together that's almost a second of waiting. That last line of code seems to be particularly offensive and takes about 25% of the time. Does anyone have any ideas in order to accelerate this (hopefully by an order of magnitude)? Is there any way to compile all of this into a mex file? Would that make execution much faster? Does anyone have experience doing this kind of thing? Tips?

Cheers,

Ariel

------------------------------------------------------------
Ariel Rokem
Helen Wills Neuroscience Institute
University of California, Berkeley
Silver laboratory
582 Minor Hall MC #2020
Berkeley, CA 94720-2020
--
Tel: +1-510-6423134
Fax: +1-510-6435109
--
------------------------------------------------------------
"if it isn't worth doing, it isn't worth doing well." Donald Hebb


Doable - in principle, with the right equipment.

What graphics card do you have? Output of PTB to the matlab
prompt at startup. After updating to the latest PTB 3.0.8 'beta',
do the following demos work?

AdditiveBlendingForLinearSuperpositionTutorial.m
ExpandingRingsDemo.m
MandelbrotDemo.m

It stands and falls with the graphics cards capabilities...

What are your requirments (screen resolution, accuracy,
which paramters vary how much, redraw rate)?

have to go now, will check your reply later...
-mario

--- In psychtoolbox@yahoogroups.com, Ariel Rokem <arokem@...> wrote:
>
> Dear PTB community,
>
> I am working on an experiment in which I want to present several
> oriented Gabors together. I want to be able to create my stimulus on
> the fly, so that I can change stimulus parameters according to
> subject behavior.
>
> However, it turns out that even if I focus on only a subset of pixels
> from the screen and compute the value of the Gabors over this subset
> this is still quite time consuming and cannot be done within the time
> I was hoping to do it.
>
> [gab_x gab_y] = meshgrid(0:(res(1)-1), 0:(res(2)-1));
> a=cosd(tilt)*sf*360;
> b=sind(tilt)*sf*360;
> multConst=1/(sqrt(2*pi)*sc);
> x_factor=-1*(gab_x-x).^2;
> y_factor=-1*(gab_y-y).^2;
> sinWave=sind(a*gab_x+b*gab_y+phase);
> varScale=2*sc^2;
> m=(multConst*exp(x_factor/varScale+y_factor/varScale).*sinWave)';
>
> Where:
> sc is the Gabor space constant
> sf is the Gabor spatial frequency
> tilt is the tilt of the Gabor
> and res is a 1x2 matrix with the length and width of the subset of
> pixels on which the stimulus is computed.
>
> The whole thing takes me (I am running it on a Mac dual core 2 GHz G5
> with 2.5 GB RAM running OS 10.4.9, using Matlab 7.1 and PTB 1.0.6)
> about 100 msec for a 343x343 matrix. For 9 Gabors together that's
> almost a second of waiting. That last line of code seems to be
> particularly offensive and takes about 25% of the time. Does anyone
> have any ideas in order to accelerate this (hopefully by an order of
> magnitude)? Is there any way to compile all of this into a mex file?
> Would that make execution much faster? Does anyone have experience
> doing this kind of thing? Tips?
>
> Cheers,
>
> Ariel
>
> ------------------------------------------------------------
> Ariel Rokem
> Helen Wills Neuroscience Institute
> University of California, Berkeley
> Silver laboratory
> 582 Minor Hall MC #2020
> Berkeley, CA 94720-2020
> (Map: http://argentum.ucbso.berkeley.edu/contact)
> --
> Tel: +1-510-6423134
> Fax: +1-510-6435109
> --
> arokem@...
> http://argentum.ucbso.berkeley.edu/ariel
> ------------------------------------------------------------
> "if it isn't worth doing, it isn't worth doing well." Donald Hebb
>