# Rendering drifting gratings

**URL:** <https://mworks.discourse.group/t/rendering-drifting-gratings/947>\
**Category:** Support\
**Created:** [January 24, 2024, 1:31pm UTC](https://mworks.discourse.group/t/rendering-drifting-gratings/947 "2024-01-24T13:31:15Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![jcadena](https://avatars.discourse-cdn.com/v4/letter/j/76d3ee/32.png) [@jcadena](https://mworks.discourse.group/u/jcadena)\
**Post date:** [January 24, 2024, 1:31pm UTC](https://mworks.discourse.group/t/rendering-drifting-gratings/947/1 "2024-01-24T13:31:15Z")

</div>

Hi Chris,  
hope you are doing fine, this is Jaime from Fribourg University.

I want to display some drifting gratings with a Gaussian mask. The negative and positive parts of the drifting gratings should have different colours. So far, I’ve found that I can control this in MWorks, but the negative will always be black. Therefore, I’ve written the following code to render the stimuli I intend to present. I wanted to ask you if there’s a more efficient way of doing this.  
Thanks in advance!

Jaime  
[Drifting\_grating\_object\_composite.mwel](https://mworks.discourse.group/uploads/short-url/9G6vLb8Lq5auVLDSjtzuzigp3Q3.mwel) (3.0 KB)

---

<div class="post-metadata">

**Author:** ![cstawarz](https://yyz1.discourse-cdn.com/flex031/user_avatar/mworks.discourse.group/cstawarz/32/3_2.png) [@cstawarz](https://mworks.discourse.group/u/cstawarz)\
**Post date:** [January 26, 2024, 2:45pm UTC](https://mworks.discourse.group/t/rendering-drifting-gratings/947/2 "2024-01-26T14:45:12Z")

</div>

Made this topic public on January 26, 2024, 2:45pm UTC



---

<div class="post-metadata">

**Author:** ![cstawarz](https://yyz1.discourse-cdn.com/flex031/user_avatar/mworks.discourse.group/cstawarz/32/3_2.png) [@cstawarz](https://mworks.discourse.group/u/cstawarz)\
**Post date:** [January 26, 2024, 5:02pm UTC](https://mworks.discourse.group/t/rendering-drifting-gratings/947/3 "2024-01-26T17:02:52Z")

</div>

Hi Jaime,

Apologies for the delayed response. I had to think about this a bit.

The first thing to note is that the technique you’re using only really works for square gratings. You can see this by replacing the two colors with black and white, and comparing your composite grating with a standard grating. With any [grating type](https://mworks.github.io/documentation/latest/components/drifting_grating_stimulus.html#grating-type) other than square, the luminance pattern of the composite grating is noticeably different from the standard one. Perhaps you’re already aware of this, but I didn’t realize it at first, so I thought I should point it out.

If you only want to use square gratings, then you could simplify things by having a single layer with two non-mask gratings and using [source\_blend\_factor](https://mworks.github.io/documentation/latest/components/drifting_grating_stimulus.html#source-blend-factor) and [dest\_blend\_factor](https://mworks.github.io/documentation/latest/components/drifting_grating_stimulus.html#dest-blend-factor) to add the top grating’s color to the bottom grating’s color, as demonstrated below:

```auto
layer drifting_grating_simpler {
    drifting_grating (
        spatial_frequency = drift_grating_spatial_freq_main
        speed = drift_grating_speed_main
        grating_type = square
        direction =0
        rotation = drift_grating_orientation_main
        autoplay = true
        starting_phase=0

        color = drift_grating_color_h_main[0], drift_grating_color_h_main[1], drift_grating_color_h_main[2]
        x_size = drift_grating_x_size_main
        y_size = drift_grating_x_size_main

        x_position = drift_grating_x_position_main
        y_position = drift_grating_y_position_main

        )

    drifting_grating (
        spatial_frequency = drift_grating_spatial_freq_main
        speed = drift_grating_speed_main
        grating_type = square
        direction =0
        rotation = drift_grating_orientation_main
        autoplay = true
        starting_phase=180

        color = drift_grating_color_l_main[0], drift_grating_color_l_main[1], drift_grating_color_l_main[2]
        source_blend_factor = one
        dest_blend_factor = one
        x_size = drift_grating_x_size_main
        y_size = drift_grating_x_size_main

        x_position = drift_grating_x_position_main
        y_position = drift_grating_y_position_main        
        )

    mask (
        mask = gaussian
        std_dev=drift_grating_std_main

        x_size = drift_grating_x_size_main
        y_size = drift_grating_x_size_main

        x_position = drift_grating_x_position_main
        y_position = drift_grating_y_position_main
        )     
}

```

That would definitely be more efficient, since you’d only need one layer instead of two.

Cheers,  
Chris

---

<div class="post-metadata">

**Author:** ![jcadena](https://avatars.discourse-cdn.com/v4/letter/j/76d3ee/32.png) [@jcadena](https://mworks.discourse.group/u/jcadena)\
**Post date:** [January 29, 2024, 11:41am UTC](https://mworks.discourse.group/t/rendering-drifting-gratings/947/4 "2024-01-29T11:41:33Z")

</div>

Thanks a lot for your answer.  
I have a follow up question: this solution would work as well if we use a grating different from square one?

Cheers,

Jaime

---

<div class="post-metadata">

**Author:** ![cstawarz](https://yyz1.discourse-cdn.com/flex031/user_avatar/mworks.discourse.group/cstawarz/32/3_2.png) [@cstawarz](https://mworks.discourse.group/u/cstawarz)\
**Post date:** [January 29, 2024, 7:06pm UTC](https://mworks.discourse.group/t/rendering-drifting-gratings/947/5 "2024-01-29T19:06:52Z")

</div>

Hi Jaime,

> I have a follow up question: this solution would work as well if we use a grating different from square one?

No, my previous suggestion only works for square gratings. However, after some additional thought, I came up with another solution that _does_ seem to work correctly for all grating types.

The attached experiment demonstrates this new approach. The composite grating consists of three parts. From top to bottom, these are

1. a rectangle of the “low” color,
2. a layer containing a rectangle of the “high” color with a mask grating on top of it, and
3. a second layer containing a 50% gray rectangle with an inverted mask on top of it.

The experiment alternately displays the composite grating and a standard grating at one-second intervals. If you set `color_low` to `[0,0,0]` (i.e. black), you can see that the two gratings are identical, even when you change the grating type or the mask.

I think this version will do what you want. If you have any questions or run in to any issues, please let me know!

Cheers,  
Chris  
[two\_color\_grating.mwel](https://mworks.discourse.group/uploads/short-url/4pmTyEggIpp2kRRVNyDpSRcoNqR.mwel) (2.8 KB)

---

<div class="post-metadata">

**Author:** ![jcadena](https://avatars.discourse-cdn.com/v4/letter/j/76d3ee/32.png) [@jcadena](https://mworks.discourse.group/u/jcadena)\
**Post date:** [February 5, 2024, 11:44am UTC](https://mworks.discourse.group/t/rendering-drifting-gratings/947/6 "2024-02-05T11:44:42Z")

</div>

Thanks Chris! I checked the stimulus and seems to be working nicely  
Have a good one,

Jaime

---

<div class="post-metadata">

**Author:** ![cstawarz](https://yyz1.discourse-cdn.com/flex031/user_avatar/mworks.discourse.group/cstawarz/32/3_2.png) [@cstawarz](https://mworks.discourse.group/u/cstawarz)\
**Post date:** [April 2, 2024, 1:26pm UTC](https://mworks.discourse.group/t/rendering-drifting-gratings/947/7 "2024-04-02T13:26:08Z")

</div>

Closed on April 2, 2024, 1:26pm UTC


