Rendering drifting gratings

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 (3.0 KB)

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 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 and dest_blend_factor to add the top grating’s color to the bottom grating’s color, as demonstrated below:

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

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

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 (2.8 KB)

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

Jaime