<p>The <code>size</code> parameter in your code simply means the size of the patch, i. e. the interval in x and y over which you sample your Gabor function.  What you are thinking of is the <em>support</em> of the function, i. e. the interval where it is noticeably different from 0. This is determined by the <code>sigma</code> parameter, which is the bandwidth of the Gaussian.</p>

<p>So if you just increase <code>size</code>, you will get a bigger image back, but the area where you can see the ripples will stay the same.  If you increase <code>sigma</code>, the area with the ripples will get larger, and then you may have to increase <code>size</code> as well to see all of it.</p>

<p>By the way, IMHO, the right place to ask this question is on <a href="https://dsp.stackexchange.com/">dsp.stackexchange.com</a>.</p>

CC BY-SA 3.0
2013-10-02T19:57:28.367Z
2013-10-02T19:57:28.367Z
2017-04-13T12:47:30.073Z
Dima
<p>I'm trying to create a Gabor patch of variable size for an upcoming experiment.</p>

<p>Below is the python function used to generate the code.  The code was ported from <a href="http://www.icn.ucl.ac.uk/courses/MATLAB-Tutorials/Elliot_Freeman/html/gabor_tutorial.html" rel="noreferrer">this tutorial</a> for Matlab (assume <code>import numpy as np</code>):</p>

<pre><code>def gabor_patch(size, lambda_, theta, sigma, phase, trim=.005):
    """Create a Gabor Patch

    size : int
        Image size (n x n)

    lambda_ : int
        Spatial frequency (px per cycle)

    theta : int or float
        Grating orientation in degrees

    sigma : int or float
        gaussian standard deviation (in pixels)

    phase : float
        0 to 1 inclusive
    """
    # make linear ramp
    X0 = (np.linspace(1, size, size) / size) - .5

    # Set wavelength and phase
    freq = size / float(lambda_)
    phaseRad = phase * 2 * np.pi

    # Make 2D grating
    Xm, Ym = np.meshgrid(X0, X0)

    # Change orientation by adding Xm and Ym together in different proportions
    thetaRad = (theta / 360.) * 2 * np.pi
    Xt = Xm * np.cos(thetaRad)
    Yt = Ym * np.sin(thetaRad)
    grating = np.sin(((Xt + Yt) * freq * 2 * np.pi) + phaseRad)

    # 2D Gaussian distribution
    gauss = np.exp(-((Xm ** 2) + (Ym ** 2)) / (2 * (sigma / float(size)) ** 2))

    # Trim
    gauss[gauss &lt; trim] = 0

    return grating * gauss
</code></pre>

<p>I would like the size of the Gabor patch to increase proportionally to the <code>size</code> parameter.  In other words, I would like the dimensions of the bounding box to dictate the diameter of the patch.  The problem is that this function does not behave in this way.  Instead, the bounding box increases in size while the patch retains the same dimensions.</p>

<p><strong>Example 1:  size = 100</strong></p>

<p><img src="https://i.sstatic.net/XLobY.png" alt="enter image description here"></p>

<p><strong>Example 2:  size = 500</strong></p>

<p><img src="https://i.sstatic.net/wKb5k.png" alt="enter image description here"></p>

<p>It's not at all obvious to me what I'm doing incorrectly.  Could somebody please point me in the right direction?</p>

<p>Please let me know if I can provide further information.  Thank you!</p>

CC BY-SA 3.0
2013-09-26T22:26:07.760Z
This question appears to be off-topic because it is about a coding problem.
Krysta
CC BY-SA 3.0
2013-09-27T00:28:53.310Z
I assume this question is about programming a psychophysics experiment.  There are varying views about when a question about programming a psychological experiment is on topic. This meta question discusses the topic: http://meta.cogsci.stackexchange.com/questions/471/are-questions-about-coding-psychology-and-cognitive-science-experiments-simulat/472#472 Thus, I'd like to think that  such questions wouldn't be closed as off topic. That said, I think the OP would get better answers on stack overflow.
Jeromy Anglim
CC BY-SA 3.0
2013-09-27T17:15:20.753Z
@JeromyAnglim, I'll certainly participate in the meta discussion, but I would just like to point out that this question isn't *really* about programming *per se*.  It's more about the math behind the Gabor patch than anything else -- it just so happens that I can better express my problem using code as opposed to mathematical notation.

Incidentally, I figured out what the problem was.  I need to tie the standard deviation to the size of the bounding box in order to get the behavior I expect.
Louis Thibault
CC BY-SA 3.0
2013-09-27T17:27:22.780Z
@JeromyAnglim, I'm a frequent SO poster, so my decision to post here was a conscious one.  I needed domain-specific input, which ordinary programmers most likely could not provide.  I'd much rather this question **not** be migrated, if it's all the same.
Louis Thibault
<experimental-psychology><vision>
Why is my Gabor patch not resizing as a function of the bounding box?
Louis Thibault
# Why is my Gabor patch not resizing as a function of the bounding box?

(Asked by: username_0 on 2013-09-26)

I'm trying to create a Gabor patch of variable size for an upcoming experiment.
Below is the python function used to generate the code.  The code was ported from this tutorial for Matlab (assume import numpy as np):
def gabor_patch(size, lambda_, theta, sigma, phase, trim=.005):
    """Create a Gabor Patch

    size : int
        Image size (n x n)

    lambda_ : int
        Spatial frequency (px per cycle)

    theta : int or float
        Grating orientation in degrees

    sigma : int or float
        gaussian standard deviation (in pixels)

    phase : float
        0 to 1 inclusive
    """
    # make linear ramp
    X0 = (np.linspace(1, size, size) / size) - .5

    # Set wavelength and phase
    freq = size / float(lambda_)
    phaseRad = phase * 2 * np.pi

    # Make 2D grating
    Xm, Ym = np.meshgrid(X0, X0)

    # Change orientation by adding Xm and Ym together in different proportions
    thetaRad = (theta / 360.) * 2 * np.pi
    Xt = Xm * np.cos(thetaRad)
    Yt = Ym * np.sin(thetaRad)
    grating = np.sin(((Xt + Yt) * freq * 2 * np.pi) + phaseRad)

    # 2D Gaussian distribution
    gauss = np.exp(-((Xm ** 2) + (Ym ** 2)) / (2 * (sigma / float(size)) ** 2))

    # Trim
    gauss[gauss < trim] = 0

    return grating * gauss

I would like the size of the Gabor patch to increase proportionally to the size parameter.  In other words, I would like the dimensions of the bounding box to dictate the diameter of the patch.  The problem is that this function does not behave in this way.  Instead, the bounding box increases in size while the patch retains the same dimensions.
Example 1:  size = 100

Example 2:  size = 500

It's not at all obvious to me what I'm doing incorrectly.  Could somebody please point me in the right direction?
Please let me know if I can provide further information.  Thank you!

**Tags:** experimental-psychology, vision

> **Comments:**
> **username_1**: This question appears to be off-topic because it is about a coding problem.  
> **username_2**: I assume this question is about programming a psychophysics experiment.  There are varying views about when a question about programming a psychological experiment is on topic. This meta question discusses the topic: http://meta.cogsci.stackexchange.com/questions/471/are-questions-about-coding-psychology-and-cognitive-science-experiments-simulat/472#472 Thus, I'd like to think that  such questions wouldn't be closed as off topic. That said, I think the OP would get better answers on stack overflow.  
> **username_0**: @JeromyAnglim, I'll certainly participate in the meta discussion, but I would just like to point out that this question isn't *really* about programming *per se*.  It's more about the math behind the Gabor patch than anything else -- it just so happens that I can better express my problem using code as opposed to mathematical notation.  Incidentally, I figured out what the problem was.  I need to tie the standard deviation to the size of the bounding box in order to get the behavior I expect.  
> **username_0**: @JeromyAnglim, I'm a frequent SO poster, so my decision to post here was a conscious one.  I needed domain-specific input, which ordinary programmers most likely could not provide.  I'd much rather this question **not** be migrated, if it's all the same.  

---

## 1 answers

### Answer by username_3 on 2013-10-02. Score: 5 | Accepted answer

The `size` parameter in your code simply means the size of the patch, i. e. the interval in x and y over which you sample your Gabor function. What you are thinking of is the *support* of the function, i. e. the interval where it is noticeably different from 0. This is determined by the `sigma` parameter, which is the bandwidth of the Gaussian.

So if you just increase `size`, you will get a bigger image back, but the area where you can see the ripples will stay the same. If you increase `sigma`, the area with the ripples will get larger, and then you may have to increase `size` as well to see all of it.

By the way, IMHO, the right place to ask this question is on [dsp.stackexchange.com](https://dsp.stackexchange.com/).


---

