/* ------------------------------------------------------------------------
          __
         / /_______________  ____  ___
        / __/ ___/ ___/ __ \/ __ \/ _ \
       / /_(__  ) /__/ /_/ / /_/ /  __/
       \__/____/\___/\____/ .___/\___/
                         /_/

    lexdec.c
    basic lexical decision experiment

------------------------------------------------------------------------ */

#include <tscope.h>

// definitions that control which main function is used
// only one should be used, all others should be commented out

//#define TESTSTIM
//#define TESTRND
#define REALEXP

/* ------------------------------------------------------------------------

    part 1: reading the stimulus files

------------------------------------------------------------------------ */

#define NWORDS 10
char word[NWORDS][20];
char nonword[NWORDS][20];

// puts all words and nonwords in 2 arrays
void readstim()
{
    FILE *fp;

    // open word file
    fp = fopen("words.txt", "r");

    // check whether file was found
    if (fp == NULL)
        ts_fatal("error opening words.txt");

    // put the words in an array
    int i;
    for (i = 0; i < NWORDS; i++)
        fscanf(fp, "%s", word[i]);

    // close wordfile
    fclose(fp);

    // open nonword file
    fp = fopen("nonwords.txt", "r");

    // check whether file was found
    if (fp == NULL)
        ts_fatal("error opening nonwords.txt");

    // put the nonwords in an array
    for (i = 0; i < NWORDS; i++)
        fscanf(fp, "%s", nonword[i]);

    // nietwoordfile sluiten                    
    fclose(fp);
}

#ifdef TESTSTIM
int main()
{
    // read the stimuli & print them out as a test
    readstim();

    int i;
    for (i = 0; i < NWORDS; i++)
        printf("%s %s\n", word[i], nonword[i]);

    return 0;
}

END_OF_MAIN();
#endif

/* ------------------------------------------------------------------------

    part 2: randomization of the trials

------------------------------------------------------------------------ */


// definitions of the number of cells and replications in the design
#define WN      2               // word or not
#define NSTIM   NWORDS          // number of stimuli per category

#define NTRIALS (WN*NSTIM)

// data structure containing all information about each trial
struct {
    int subj;                   // subject number
    int rmap;                   // response mapping
    int wn;                     // word or not
    int stimnr;                 // number of the stimulus
    char stim[20];              // the stimulus itself
    int xr;                     // expected response
    int r;                      // the response
    int corr;                   // correctness of the response
    int rt;                     // reaction time
    int re;                     // timing error
} data[NTRIALS];

#define TESTLIST    FALSE

void randomize(int subjnr)
{
    int i, tmp[NTRIALS];

    // run trials randomly or in a fixed sequence
    if (TESTLIST) {
        for (i = 0; i < NTRIALS; i++)
            tmp[i] = i;
    } else
        ts_rlist(NTRIALS, 1, tmp);

    for (i = 0; i < NTRIALS; i++) {

        // fill in subject number and determine response mapping
        data[i].subj = subjnr;
        data[i].rmap = subjnr % 2;

        // determine stimulus number and category
        data[i].wn = tmp[i] % WN;
        data[i].stimnr = tmp[i] / WN;

        // copy the actual stimulus
        if (data[i].wn == 1)
            sprintf(data[i].stim, "%s", word[data[i].stimnr]);
        else
            sprintf(data[i].stim, "%s", nonword[data[i].stimnr]);

        // determine the expected response
        // based on response mapping and stimulus category
        if (data[i].rmap == 0) {
            if (data[i].wn == 0)
                data[i].xr = 2;
            else
                data[i].xr = 1;
        } else {
            if (data[i].wn == 0)
                data[i].xr = 1;
            else
                data[i].xr = 2;
        }

        // initialize all other variables
        data[i].r = 0;
        data[i].corr = 0;
        data[i].rt = 0;
        data[i].re = 0;
    }
}


void writedata()
{
    // use subject number to determine output filename
    char filename[20];
    sprintf(filename, "data%d.rtd", data[0].subj);

    // open file for writing, test whether it is open
    FILE *outfile;
    outfile = fopen(filename, "a+");
    if (outfile == NULL)
        ts_fatal("error opening output file");

    int i;
    for (i = 0; i < NTRIALS; i++) {
        fprintf(outfile, "%d ", data[i].subj);
        fprintf(outfile, "%d ", data[i].rmap);
        fprintf(outfile, "%d ", data[i].wn);
        fprintf(outfile, "%d ", data[i].stimnr);
        fprintf(outfile, "%d ", data[i].xr);
        fprintf(outfile, "%d ", data[i].r);
        fprintf(outfile, "%d ", data[i].corr);
        fprintf(outfile, "%4d ", data[i].rt);
        fprintf(outfile, "%4d ", data[i].re);
        fprintf(outfile, "%s ", data[i].stim);
        fprintf(outfile, "\n");
    }

    // close output file  
    fclose(outfile);
}

#ifdef TESTRND
int main()
{
    readstim();
    int i;

    // make 4 output files 
    for (i = 0; i < 4; i++) {
        randomize(i);
        writedata();
    }
    return 0;
}

END_OF_MAIN();
#endif

/* ------------------------------------------------------------------------

    part 3: trial and experiment structure

------------------------------------------------------------------------ */

// timing of the trial fields
#define FIXTM   500
#define BLANKTM 500
#define MAXTM   1500
#define FBTM    200
#define ITI     1500

// temporary timing data
struct {
    __int64 junk;               // for unused time values
    __int64 t1, e1;             // stimulus onset
    __int64 t2, e2;             // response onset
} tmp;

void trial(int i)
{
    // fixation
    ts_vsync(&tmp.junk, &tmp.junk);
    ts_printf_centre(0, 0, "+");
    ts_wait(mtt(FIXTM));

    // blank screen
    ts_vsync(&tmp.junk, &tmp.junk);
    ts_clrscr();

    // stimulus
    ts_vsync(&tmp.t1, &tmp.e1);
    ts_printf_centre(0, 0, "%s", data[i].stim);

    // response
    data[i].r = ts_resp(&tmp.t2, &tmp.e2, mtt(MAXTM));
    ts_clrscr();

    // determine reaction time and timing error
    data[i].rt = ttm(tmp.t2 - tmp.t1);
    data[i].re = ttmu(tmp.e2 + tmp.e1);

    // escape program if user pressed escape
    if (data[i].r == 3)
        ts_fatal("aborted by user");

    // determine correctness of response
    if (data[i].r == data[i].xr)
        data[i].corr = 1;
    else
        data[i].corr = 0;

    // beep if response was wrong
    if (!data[i].corr)
        ts_playstream(mtt(FBTM));

    // intertrial interval              
    ts_wait(mtt(ITI));
}

#define TRIALTEST   TRUE

#ifdef REALEXP
int main()
{
    // go fullscreen if it's a real run
    if (!TRIALTEST) {
        ts_scrsize(SIZE3);
        ts_scrmode(FULLSCREEN);
    } else
        ts_scrsize(SIZE1);
    ts_init();

    // define reponse keys
    ts_defkey(M1);
    ts_defkey(M2);
    ts_defkey(KEY_ESC);         //escape-key 

    // determine subject number
    int subjnr;
    do {
        ts_clrscr();
        int y = ts_printf(-XMAX + 20, YMAX - 20, "Participant number: ");
        ts_scanf(-XMAX + 20 + y, YMAX - 20, "%d", &subjnr);
    } while (subjnr < 0 || subjnr > 20);

    // read stimuli and randomize trials
    readstim();
    randomize(subjnr);

    // give instructions to participants
    ts_clrscr();
    if (data[0].rmap == 0) {
        ts_printf_centre(0, 10,
                         "press the left mouse button if you see a word");
        ts_printf_centre(0, 0,
                         "press the right mouse button if you see a nonword");
    } else {
        ts_printf_centre(0, 10,
                         "press the right mouse button if you see a word");
        ts_printf_centre(0, 0,
                         "press the left mouse button if you see a nonword");
    }
    ts_printf_centre(0, -10, "press escape to stop");
    ts_button(XMAX - 20, -YMAX + 20);
    ts_clrscr();
    ts_wait(mtt(1000));

    // run the trials
    int i;
    for (i = 0; i < NTRIALS; i++)
        trial(i);

    // write data and exit
    writedata();
    ts_printf_centre(0, 0, "all done, thanks!");
    ts_button(XMAX - 20, -YMAX + 20);
    return 0;
}

END_OF_MAIN();
#endif


top
Persoonlijke pagina Universiteit GentTscope
Allegro | Cygwin | Gcc
© See license.html for copyright information