psychtoolbox 是 MATLAB 中的一個工具包,對于科研人員設計實驗范式來說是不二之選,因為它可以操作計算機的底層硬件,精度可以達到幀的級別。
文章目錄
- 一、實驗目的
- 二、psychtoolbox 的下載安裝
- 三、Psychtoolbox 的基本使用
- 四、完整代碼
一、實驗目的
我們的目的是設計出一個迷宮(網格線上的一條路徑),使得小球每隔3s就移動一格,當然每次小球移動的對錯概率都是我們自己實現設定好的。
二、psychtoolbox 的下載安裝
- 首先在BING搜索,認準第一個org結尾的官網,可以進入官網,也可以直接點擊下方的“Download”,跳轉到下載界面。
- 進入到下載界面后,按照下圖中的三個步驟(第一步可不做,如果是第一次安裝)。
(1)下面是第二步,點擊這里可下載gstreamer,按照指引一步步安裝即可。
(2)下載zip,將壓縮包的子文件夾里的所有文件都拖入到一個新建的“toolbox”文件夾里。
處理完之后像下圖一樣就對了。
- 進入MATLAB,打開到剛才的toolbox->Psychtoolbox 路徑。
- 在命令行中輸入
SetupPsychtoolbox
,回車,等待即可,中間需要一路回車進行。最后出現“Success,Enjoy”等字樣即安裝成功。可以繼續在命令行使用Screen
來測試是否安裝成功。
三、Psychtoolbox 的基本使用
-
基本原理
首先它是通過一幀一幀翻轉窗口來實現所謂的動態效果,所以我們的邏輯基本就是先在畫布上繪制下一幀的畫面,然后翻轉。
可以自行到官網找到參考文檔,網絡上所有文章基本都是從那里復制出來的,并且還可以看到官網提供的Demos。 -
這里提供幾個常使用到的函數:
/* 開頭需要加的。 */
Screen('Preference', 'SkipSyncTests', 1);
Screen('Preference','SkipSyncTests',2);/* 隱藏鼠標。 */
HideCursor;/* 函數功能:新建一個窗口。* 返回值:w,窗口的句柄;* 返回值:wrect,主要用來獲取屏幕的長寬。* 參數:命令,可在命令行“help Screen”獲取幫助文檔或者直接到官網。* 參數:第n個屏幕,一般無擴展屏的話直接默認0.* 參數:顏色,窗口的RGB數組。這里是黑色。*/
[w, wrect] = Screen('OpenWindow', 0, [0, 0, 0]);/* 寫文本。一般進行心理實驗之前需要一段引導語。* 參數:imread后面為圖片的路徑。*/
instruction=Screen('MakeTexture',w,imread('xxx\xxx\xx.tif'));
Screen('DrawTexture',w,instruction,[]);%顯示提示語/* 函數功能:畫直線。* 返回值:無* 參數:命令,畫直線、畫圓等等。* 參數:窗口的句柄,按之前來講就是w。* 參數:顏色。* 參數:后面四個參數依次為x軸起點坐標、y軸起點坐標、x軸終點坐標、y軸終點坐標。* 參數:線寬。*/
Screen(‘DrawLine’, windowPtr [,color], fromH, fromV, toH, toV [,penWidth]);/* 函數功能:畫實心圓。* 返回值:無* 參數:命令。* 參數:窗口的句柄。* 參數:顏色。* 參數:后面四個參數依次為圓的左、上、右、下邊界坐標。*/
Screen(‘FillOval’, windowPtr [,color] [,rect] [,perfectUpToMaxDiameter]);/* 翻轉。每次繪制完畫布都要進行一次翻轉,這樣新繪制的場景才能顯示出來。重要!!! */
Screen('Flip',w)/* 延時函數,單位:秒。*/
WaitSecs();/* 按鍵檢測。以下是一個退出機制,按下Esc鍵退出。27對應Esc鍵的ASCII值。*/
while true[keyIsDown,~,keyCode]=KbCheck;if keyCode(27)break;end
end/* 關閉窗口,搭配上面的退出機制使用。*/
Screen('CloseAll');/* 常用的顏色RGB值。*/
color_red = [255,0,0];
color_white = [255, 255, 255];
color_black = [0, 0, 0];
color_yellow = [255, 255, 0];
四、完整代碼
- 功能邏輯通過一個哈希表來一一映射小球每一步的正確、錯誤情況。
decision_hashmap = zeros(1, 100);
decision_hashmap(1, [3,7,13,18,23,27,32,37, 42,46,50, 54,58, 63,67, 72,76, 79, 83]) = 1; % 第i個元素置一表示第i步為小球錯誤運動方向
step_up_right = 8 + 2 * 2; % 由up轉向right的第x步step_right_down = step_up_right + 8 + 2 * 3;step_down_left = step_right_down + 8 + 2 * 3;step_left_up = step_down_left + 6 + 2 * 3;step_up_right_2 = step_left_up + 6 + 2 * 2;step_right_down_2 = step_up_right_2 + 4 + 2 * 2;step_down_left_2 = step_right_down_2 + 4 + 2 * 2;step_left_up_2 = step_down_left_2 + 2 + 2 * 1; step_up_end = step_left_up_2 + 2 + 2 * 1;
在for循環里實現小球的轉向和正誤判斷,下面只張貼正確情況的轉向邏輯代碼,完整代碼可移步gitee倉庫Psychtoolbox繪制小球走迷宮下載。
for step = 1 : step_up_end% 通過中間變量,記錄上一步小球的位置if(step == 1)previous_y_index_up = xy_4_trace_start(2);previous_y_index_down = xy_4_trace_start(4);previous_x_index_left = xy_4_trace_start(1);previous_x_index_right = xy_4_trace_start(3);elseprevious_y_index_up = current_y_index_up;previous_y_index_down = current_y_index_down;previous_x_index_left = current_x_index_left;previous_x_index_right = current_x_index_right;end% 小球動態路徑if ~decision_hashmap(step) % 正確if step <= step_up_right current_y_index_up = previous_y_index_up - length_chessboard;current_y_index_down = previous_y_index_down - length_chessboard;current_x_index_left = previous_x_index_left;current_x_index_right = previous_x_index_right;Screen('FillOval', w, color_ball, [current_x_index_left current_y_index_up ...current_x_index_right current_y_index_down]);elseif step <= step_right_down current_y_index_up = previous_y_index_up ;current_y_index_down = previous_y_index_down ;current_x_index_left = previous_x_index_left + length_chessboard;current_x_index_right = previous_x_index_right + length_chessboard;Screen('FillOval', w, color_ball, [current_x_index_left current_y_index_up ...current_x_index_right current_y_index_down]);...
end