Date
1  3 of 3
Converting table to a spectrum object
vkharlamov@...
Dear all, is there a way to import a table with spectral values into Praat and then convert it to a spectrum object that can be analyzed in Praat? In other words, Praat has an option to take a spectrum object and save it as a table but is it also possible to go the other way around? The table in question has two columns  frequency and power (the values come from a multitaper spectrum that was originally generated in R and then saved in a table format). The top rows of the table look like this:
frequency power 0 49.49533045
3.90625 49.44149401
7.8125 49.63077962
11.71875 49.71858303
The spectrum object in Praat uses real and imaginary values, not frequencies and dB levels. Is there a way to convert the values above into something that Praat will be able to read in as a spectral object? Any advice you may have would be very much appreciated! Best, Viktor 

Christian DiCanio
Dear Viktor,
This is essentially what we do with the time averaging for fricatives script on my website (https://www.acsu.buffalo.edu/~cdicanio/scripts/Time_averaging_for_fricatives_4.0.praat). The code follows Forrest et al 1988 (JASA). Spectral averages are taken for the matrix and then a composite matrix/table is created which is then reconverted into a spectrum. Though, Praat does not allow any simple function to change the sampling interval or xmax in a matrix, so I believe that the reconstituted spectrum requires some additional multiplication by the sampling interval size for it to be interpretable. Christian 

Boersma Paul
There are many ways in Praat to accomplish this. I mention two:
1. Into a Spectrum.
Your data fit probably most easily into an Ltas object (it is already in dB), but since you were asking for a Spectrum object, let's have that.
Suppose that your data is in a Table object called "hello", and that it is selected.
You start out by creating a Matrix object with explicit xmin, xmax, nx, dx and x1, perhaps derived from your table (nx = number of rows, and so on):
xmin = 0 ; the lowest logical frequency for any Spectrum object, in Hz
xmax = 20000 ; the highest logical frequency, in Hz (not necessarily the highest frequency of your list)
ncol = Get number of rows
dx = Get value: 2, "frequency" ; the frequency step, i.e. 3.90625 Hz in your case
dx = object["Table hello", 2, "frequency"] ; an alternative way to get dx
x1 = 0 ; the centre of the first frequency bin (0 Hz for most spectra, including yours)
ymin = 1
ymax = 2
nrow = 2 ; namely one row for the real values, and one row for the imaginary values
dy = 1
y1 = 1
Create Matrix: "hello", xmin, xmax, ncol, dx, x1, ymin, ymax, nrow, dy, y1, ~ 0
This creates a Matrix of the right shape, which you can check. Note that if you know all the values (by looking into the Table), you can do all of this by hand in the "Create Matrix..." window (xmin, xmax, dx and x1 can always be changed later via "Inspect";
Christian's remark about this being difficult probably refers to scripting)
You can then do "To Spectrum". You now have a Spectrum object of the right shape.
To put the values into the Spectrum, do
Formula: if row = 1 then 10^(object ["Table hello", col, "power"]/20) else 0 fi
This looked up each value (the nth column of the Spectrum gets the nth row of your table, in column "power") and converted from dB to amplitude. The complex values in the Spectrum are now all real.
2. Into an Ltas object.
If you just want to construct an Ltas object instead, then save only the "power" column to a text file (e.g. write the table after deleting the "frequency" column), and include a header like this:
"ooTextFile"
"Ltas 2"
0
20000
5400 ; or whatever the number of frequencies is
3.90625
0
49.49533045
49.44149401
... (5398 more rows with power data)
and this can be read directly into Praat with "Read from file..."
best wishes,
Paul
_____
Paul Boersma
Professor of Phonetic Sciences
University of Amsterdam
Spuistraat 134, room 632 1012VB Amsterdam, The Netherlands http://www.fon.hum.uva.nl/paul/ 
