Workshop
Variables

Introduction

In the last workshop (Generating Output) you practiced writing literal numbers and strings to Praat Info. Now, you'll widen your output skills using variables. And of course, you'll practice variable usage in other contexts.

Preparation

Start Praat and open a new Praat script. Later, we'll need a sound object containing at least one vowel.

Exercise

Warm up: Invent a valid variable name and assign a number. Generate the following output:
  • clear Praat Info and write out the name and the content of the variable
  • add a line and write out the content of the variable multiplied by 2
  • assign the content of the variable divided by 2 to a new variable and write out the content of the new variable
  • assign a string to a new variable and write out it's content, followed by both numeric variables, separated by spaces
  • attach the content of your first numeric variable to the string variable as a string and write it out
  • Solution
    myNumber = 70 writeInfoLine: "myNumber = ", myNumber appendInfoLine: myNumber * 2 myNewNumber = myNumber / 2 appendInfoLine: myNewNumber myString$ = "Some string" appendInfoLine: myString$, " ", myNumber, " ", myNewNumber myString$ = myString$ + string$ (myNumber) appendInfoLine: myString$

    The resulting output:

    myNumber = 70
    140
    35
    Some string 70 35
    Some string70

Now, let's try something useful: A script that measures—within an editor selection—the mean and standard deviation of a formant and moves the curor to the formant maximum. We need a sound object in the sound editor with activated formant analysis (Formant > Show formants). Since we'll start with a selection in the sound editor, we implement this as an editor script. Here's a step-by-step description of what the script will do:

  1. Starting point: a selected vowel in the sound editor
  2. Extract selection (windowed)
  3. In the shell, run formant analysis of extracted vowel
  4. Query formant object for
    1. mean
    2. standard deviation
    3. and time of formant maximum
  5. Back in the editor, move cursor to time of formant maximum
  6. Write out query results

To get started, open a new editor script (File > New editor script) and clear the history. (1.) Select some vowel. (2.) Extract the selection using a Hamming window and preserve times (File > Extract selected sound (windowed)...); paste and clear history. This is how your script should look:

  • Listing
    Extract selected sound (windowed): "slice", "Hamming", 1, "yes"

(3.) In the shell, run a formant analysis of the extracted sound slice (Analyse spectrum > To Formant (burg)...). Use standard settings but adapt 'Maximum formant' to your speaker. Test the script and inspect the resulting formant object. When you are satisfied paste and clear history. The following listing suits a male speaker:

  • Listing
    Extract selected sound (windowed): "slice", "Hamming", 1, "yes" endeditor To Formant (burg): 0, 5, 5000, 0.025, 50

To be able to quickly switch between male and female speakers, assign 'Maximum formant' to a variable at the beginning of the script and apply the variable in the fromant analysis.

  • Listing
    # set to 5000 (male) or 5500 (female) maxFormant = 5000 Extract selected sound (windowed): "slice", "Hamming", 1, "yes" endeditor To Formant (burg): 0, 5, maxFormant, 0.025, 50

(4.) With the formant object selected, explore the Query menu. Query the first formant for the (a.) mean, (b.) standard deviation, and (c.) time of maximum, then paste and clear history. Edit the script to assign the query results to variables.

  • Listing
    # set to 5000 (male) or 5500 (female) maxFormant = 5000 Extract selected sound (windowed): "slice", "Hamming", 1, "yes" endeditor To Formant (burg): 0, 5, maxFormant, 0.025, 50 mean = Get mean: 1, 0, 0, "Hertz" sd = Get standard deviation: 1, 0, 0, "Hertz" tmax = Get time of maximum: 1, 0, 0, "Hertz", "Parabolic"

If you are interested in the second formant instead of the first, you would have to edit 3 query commands. To correct this, invent a variable at the beginning to specify the formant and apply this variable in the query commands.

  • Listing
    formant = 1 # set to 5000 (male) or 5500 (female) maxFormant = 5000 Extract selected sound (windowed): "slice", "Hamming", 1, "yes" endeditor To Formant (burg): 0, 5, maxFormant, 0.025, 50 mean = Get mean: formant, 0, 0, "Hertz" sd = Get standard deviation: formant, 0, 0, "Hertz" tmax = Get time of maximum: formant, 0, 0, "Hertz", "Parabolic"

(5.) Back in the editor, use your time-of-maximum variable (mine is called tmax) to move the cursor to the time of the specified formant's maximum (if you never moved the cursor without the mouse check Select > Move cursor to...).

  • Listing
    formant = 1 # set to 5000 (male) or 5500 (female) maxFormant = 5000 Extract selected sound (windowed): "slice", "Hamming", 1, "yes" endeditor To Formant (burg): 0, 5, maxFormant, 0.025, 50 mean = Get mean: formant, 0, 0, "Hertz" sd = Get standard deviation: formant, 0, 0, "Hertz" tmax = Get time of maximum: formant, 0, 0, "Hertz", "Parabolic" editor Move cursor to: tmax

(6.) At the end of the script, write the query results to Praat Info. Try to replicate the following output (heading informs about specified formant; round mean and sd to 2 decimal places):

Results for F1
mean = 455.54
standard deviation = 2.99
time of maximum: 16.000498865517002
Results for F2
mean = 1645.34
standard deviation = 3.94
time of maximum: 15.999669746744102
  • Listing
    formant = 1 # set to 5000 (male) or 5500 (female) maxFormant = 5000 Extract selected sound (windowed): "slice", "Hamming", 1, "yes" endeditor To Formant (burg): 0, 5, maxFormant, 0.025, 50 mean = Get mean: formant, 0, 0, "Hertz" sd = Get standard deviation: formant, 0, 0, "Hertz" tmax = Get time of maximum: formant, 0, 0, "Hertz", "Parabolic" editor Move cursor to: tmax writeInfoLine: "Results for F", formant appendInfoLine: "mean = ", fixed$ (mean, 2) appendInfoLine: "standard deviation = ", fixed$ (sd, 2) appendInfoLine: "time of maximum: ", tmax

That's it, the script fits the job description formulated above. But certainly, there's room for improvements. First, the script is messy. Each time you run it, it generates two new objects which are of no interest to you. After the next session on object selection you'll know how to address this problem. Second, if you are interested in more than one formant, you have to edit the formant variable each time and run the script for each formant separately. This could be enhanced with the help of a loop (session 9). Third, if you need this (or a similar) analysis very often, you may want to list the script in the Formant menu of the sound editor. Then, at the latest, you'll need a more convenient method to set the formant and maxFormant parameter. That's implemented with an input form (session 11).

© Jörg Mayer

Last modified: