Week 5: Tables
1 Using Tables
So far we have been creating tables by writing to the Info window. This week we will learn how to create Tables, which is one of the many different types of Objects in Praat. One of the benefits of using Tables is that they can be reopened with Praat, as well as calculate several statistical tests from a Table object.
First, let’s analyze an existing dataset (Praat has a couple of these). Go to the main menu and then New Tables Create formant table (Peterson & Barney 1952). In more recent versions, this option is found under New > Tables > Data sets from the literature. Or, you can simply write a script with:
data = Create formant table (Peterson & Barney 1952)
This will create a new object in your Objects window called Table pb. Select it and then try View & Edit. You will get a pop-up window like this:
This table has the F0 and formant values from 10 monophthongal vowels produced by 76 native speakers of American English (women, men, and children). The Vowel column has the vowel notation in the American sociolinguistic tradition, while the IPA column has the corresponding symbol.
A Table can be queried for specific values. For instance, we can check how many rows the Table has. If you select the Table object, you can then try Query Get number of rows. You could make your previous script line grow by querying the number of rows and printing the answers to the Info window with something like this:
data= Create formant table (Peterson & Barney 1952)
selectObject: data
nrows= Get number of rows
appendInfoLine: "This table contains ", nrows, " rows."
Likewise, you can calculate some descriptive stats, such as the mean: Query Get mean... and then enter the column label where the data to be calculated is (e.g. F1). Similarly, by using the options under the Query button on the Objects list when you select a Table you can calculate a quartile, minimum/maximum values, or the standard deviation. Let's make the script above grow with this info as well:
data= Create formant table (Peterson & Barney 1952)
selectObject: data
nrows= Get number of rows
meanf0=Get mean: "F0"
median= Get quantile: "F0", 0.5
min= Get minimum: "F0"
max= Get maximum: "F0"
appendInfoLine: "This table contains ", nrows, " rows, the mean F0 is ", meanf0, ", the minimum value is ", min, ", the maximum is ", max, ", and the median is ", median
2 More elaborate queries
2.1 Calculate a group mean
However, calculating the mean for the entire dataset might not be of much use, especially if we are trying to find differences among groups. Let’s now try to calculate the mean of a given group in the pb dataset.
For instance, we will find the mean F0 in females. We go to Query Get group mean.... In Column label we put the name of the column where the values are, in this case, F0. Then, in Group column label, we put the name of the column where the groups are, that is, Sex. Finally, in Group we put the label used for the observations, in this case, f.
If you paste the history on a Praat script, you’ll get this:
Get group mean: "F0", "Sex", "f"
As usual, you can store this as a numeric variable by adding the name of your variable followed by = right before the line above, such as
femMeanf0= Get group mean: "F0", "Sex", "f"
2.2 Calculate difference in means
Now we can do something a bit more complex: we can ask Praat to perform a Student t-test on these values. Let’s calculate the differences in F0 in males and females. The question that we ask ourselves is: are these means different? and if so, is this difference statistically significant?
So, we go to Query Report group difference (Student t)... and we fill in the parameters. In Column we put F0, then in Group column we put again Sex. Group 1 will be the males (hence we put m), and Group 2 the females (f). Which one is group 1 and which one is Group 2, is irrelevant. We leave the un-confidence level with the default value. The script would look like this:
Report group difference (Student t): "F0", "Sex", "f", "m", 0.025
You will get a report on the Info window with the results of your t-test, including the p-value, the degrees of freedom, and the confidence intervals.
2.3 Subsetting your Table
But what if we wanted to look into the values of the ɛ vowel only? We can subset our Table by using an Extract command. We go to Extract Extract rows where column (text)... and fill in the parameters: we put first IPA, we leave the drop-down menu as ...is equal to, and then we use the Praat way to code ɛ: \ef. That will create a second table in the Objects window, called Table pb__ef.
The script line looks like this:
Extract rows where column (text): "IPA", "is equal to", "\ef"
And then you can, for instance, see whether there is a difference in F1 between males and females, but only for this vowel.
3 Create your own table
For this part, we will use the files excerpt.wav
(that you can download here) and excerpt.TextGrid
(which you can get here).
#Opening files
dir$= "/media/fernanda/1A46-9D0A/praatscripting/week08/"
name$= "excerpt"
tg= Read from file: dir$ + name$ + ".TextGrid"
sound= Read from file: dir$ + name$ + ".wav"
Inspect the Objects: you will see that there is a Sound file with a sentence and a Textgrid with two Tiers: Tier 1 has the words, and Tier 2 has only the intervals with the vowels (whatever interval is between two vowels is empty, i.e. has no label). The goal is to obtain formant values and duration for each one of the vowels in the sentence. The following lines will extract all the non-empty intervals from Tier 2 in the TextGrid.
#Extract
selectObject: tg
plusObject: sound
Extract non-empty intervals: 2, "no"
Now, to the Table. First, we will create a table with zero rows (we will start appending them later) and the column headers Sound, F1, F2 and Duration. Then, we will select all the extracted sounds and leave the Table, plus the TextGrid and the original Sound unselected.
#Create table
mytable= Create Table with column names: "table", 0, "Sound F1 F2 Duration"
#Select everything, EXCEPT: original sound, tg, and table
select all
minusObject: mytable, tg, sound
Now that we have extracted all the vowels, we will create a vector, which is an array of numbers that are grouped as a single object. A vector is created by using the hash symbol (#). In the following lines we created a vector called sounds# which has the ID numbers of every selected object. Since the step above selected the extracted vowel sounds, the vector will contain the IDs of each extracted vowel.
#Create a vector containing the ID numbers of all extracted vowels
sounds# = selected# ("Sound")
The following chunk creates a for-loop that iterates through all the extracted vowel sounds and gets info such as formant values, duration, and the name of the vowel sound. Then, the for-loop appends a row to the Table that we created.
#Create a for-loop that retrieves the name of the vowel, creates a formant, and gets F1, F2, and duration
for i to size (sounds#)
selectObject: sounds# [i]
name$ = selected$ ("Sound")
dur= Get total duration
To Formant (burg): 0, 5, 5000, 0.025, 50
f1= Get mean: 1, 0, 0, "hertz"
f2= Get mean: 2, 0, 0, "hertz"
#This part of the for-loop appends a row for each vowel, and sets values for each cell.
selectObject: mytable
Append row
Set string value: i, "Sound", name$
Set numeric value: i, "F1", f1
Set numeric value: i, "F2", f2
Set numeric value: i, "Duration", dur
endfor
Let's see what each line does:
- Line 2 opens the loop.
- Line 3 selects one of the extracted sounds which are now part of the vector called sounds#.
- Line 4 stores the name of the sound as a string variable.
- Line 5 gets the total duration of the sound.
- Line 6 creates a formant element.
- Line 7 queries the Formant element, and obtains the mean F1 of the sound, which is stored as a numeric variable called f1 .
- Line 8 does the same for F2.
- Line 10 selects the Table object and line 11 appends a new row to the Table.
- Line 12 adds a string value to the table, which is the name of the sound (stored as name$), under the column called ”Sound”.
- Lines 13 to 15 add numeric values to the Table (F1, F2, and duration) under the other columns.
PRO TIP: Access the magic of Praat's history
As we have mentioned several times, scripting is mostly about telling Praat where to click. HOWEVER, this doesn't work if you want to create variables and/or run for-loops: that why we need scripting. Still, you can obtain the history of all your previous clicks since you opened Praat. So, basically, if you were to perform operations only once, you could get away with pasting the history as a script; but again, this does not work if you want to perform the same operation many times, as we do with for-loops. That being said, accessing the history might be very useful for debugging: if for some reason you script is not working, you can check whether there is a typo, a wrong command, a problem with the number of parameters, missing commas or quotation marks, etc. by following these steps:
- Step 1: Go to the Praat script window, and then to Edit > Clear history.
- Step 2: Perform the desired operation by clicking. For instance, if you wanted to get the table with the vowels of American English, you can go to the Objects window and then New > Tables > Data sets from the literature > Create formant table (Peterson & Barney 1952).
- Step 3: Go back to the script window, place the cursor at the end of the script, and then go to Edit > Clear history. You can cut this line and paste it on your script where you want it to be.
As an example, I was having trouble with my script because I wrote the following line:
femMeanf0= Get mean by group: "F0", "Sex", "f"
...Which won't work: Praat gave me an error message and I couldn't figure out why. So I did this by clicking and only then I noticed that the command is Get group mean and NOT Get mean by group, which is what I had typed.
4 Homework
Put together all the code lines under section 3 to create a script. Now modify it so that you get the mean values for each vowel's F1 and F2 (so, mean F1 and F2 of all the /a/'s, mean F1 and F2 of all /e/'s, etc), and print it to the Info window. Upload your script to ILIAS as hw8_yourlastname.txt.