| 1 | /* manual_formulas.cpp |
| 2 | * |
| 3 | * Copyright (C) 1992-2025 Paul Boersma |
| 4 | * |
| 5 | * This code is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License as published by |
| 7 | * the Free Software Foundation; either version 3 of the License, or (at |
| 8 | * your option) any later version. |
| 9 | * |
| 10 | * This code is distributed in the hope that it will be useful, but |
| 11 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| 13 | * See the GNU General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this work. If not, see <http://www.gnu.org/licenses/>. |
| 17 | */ |
| 18 | |
| 19 | #include "ManPagesM.h" |
| 20 | |
| 21 | void manual_formulas_init (ManPages me); |
| 22 | void manual_formulas_init (ManPages me) { |
| 23 | |
| 24 | MAN_BEGIN (U"Calculator", U"ppgb", 20210228) |
| 25 | INTRO (U"A window that allows you to calculate all kinds of simple or complicated mathematical and string expressions. " |
| 26 | "To show the calculator, type @@Keyboard shortcuts|Command-U@ or choose the @@Calculator...@ command. " |
| 27 | "The result will appear in the Info window.") |
| 28 | NORMAL (U"See the @Formulas tutorial for all the things that you can calculate with this command.") |
| 29 | MAN_END |
| 30 | |
| 31 | MAN_BEGIN (U"Calculator...", U"ppgb", 20050822) |
| 32 | INTRO (U"A command in the @Goodies submenu of the @@Praat menu@ of the @@Objects window@. Shortcut: @@Keyboard shortcuts|Command-U@. " |
| 33 | "Choosing this command brings up Praat's @calculator.") |
| 34 | MAN_END |
| 35 | |
| 36 | MAN_BEGIN (U"undefined", U"ppgb", 20170910) |
| 37 | INTRO (U"When you give a query command for a numeric value, Praat sometimes writes the numeric value ##--undefined--# " |
| 38 | "into the @@Info window@ (two hyphens at both sides of the word). This happens if the value you ask for is not defined, " |
| 39 | "as in the following examples:") |
| 40 | LIST_ITEM (U"\\bu You select a Sound with a finishing time of 1.0 seconds and ask for the minimum point in the wave form " |
| 41 | "between 1.5 and 2.0 seconds (with the query command ##Get minimum...#).") |
| 42 | LIST_ITEM (U"\\bu You ask for a pitch value in a voiceless part of the sound (select a #Pitch, " |
| 43 | "then choose ##Get value at time...#).") |
| 44 | LIST_ITEM (U"\\bu You type into the @Calculator the following formula: 10\\^ 400.") |
| 45 | ENTRY (U"Usage in a script") |
| 46 | NORMAL (U"In a Praat script, this value is simply represented as \"undefined\". You use it to test whether " |
| 47 | "a query command returned a valid number:") |
| 48 | CODE (U"selectObject: \"Pitch hallo\"") |
| 49 | CODE (U"meanPitch = Get mean: 0.1, 0.2, \"Hertz\", \"Parabolic\"") |
| 50 | CODE (U"if meanPitch = undefined") |
| 51 | CODE1 (U"# Take some exceptional action.") |
| 52 | CODE (U"else") |
| 53 | CODE1 (U"# Take the normal action.") |
| 54 | CODE (U"endif") |
| 55 | ENTRY (U"Details for hackers") |
| 56 | NORMAL (U"In text files, this value is written as ##--undefined--#. " |
| 57 | "In binary files, it is written as a big-endian IEEE positive infinity. " |
| 58 | "In memory, it is usually a specific \"not-a-number\" (NaN), namely the result of dividing 0 by 0, " |
| 59 | "although other NaNs, and also infinities, will equally be reported as --undefined--.") |
| 60 | MAN_END |
| 61 | |
| 62 | MAN_BEGIN (U"Formulas", U"ppgb", 20170916) |
| 63 | INTRO (U"You can use numeric expressions and string (text) expressions in many places in Praat:") |
| 64 | LIST_ITEM (U"\\bu in the @calculator in Praat’s @Goodies submenu;") |
| 65 | LIST_ITEM (U"\\bu in the numeric fields of most settings windows;") |
| 66 | LIST_ITEM (U"\\bu in a @@Praat script@.") |
| 67 | NORMAL (U"For some types of objects (mainly Sound and Matrix), you can also apply formulas to all their contents at the same time:") |
| 68 | LIST_ITEM (U"\\bu when you create a Sound or a Matrix from the @@New menu@;") |
| 69 | LIST_ITEM (U"\\bu when you choose @@Formula...@ from the @Modify menu for a selected object.") |
| 70 | NORMAL (U"You can read this tutorial sequentially with the help of the “##< 1#” and “##1 >#” buttons.") |
| 71 | LIST_ITEM (U"1. @@Formulas 1. My first formulas|My first formulas@") |
| 72 | LIST_ITEM1 (U"1.1. @@Formulas 1.1. Formulas in the calculator|Formulas in the calculator@") |
| 73 | LIST_ITEM1 (U"1.2. @@Formulas 1.2. Numeric expressions|Numeric expressions@") |
| 74 | LIST_ITEM1 (U"1.3. @@Formulas 1.3. String expressions|String expressions@") |
| 75 | LIST_ITEM1 (U"1.4. @@Formulas 1.4. Array expressions|Array expressions@") |
| 76 | LIST_ITEM1 (U"1.5. @@Formulas 1.5. Formulas in settings windows|Formulas in settings windows@") |
| 77 | LIST_ITEM1 (U"1.6. @@Formulas 1.6. Formulas for creation|Formulas for creation@") |
| 78 | LIST_ITEM1 (U"1.7. @@Formulas 1.7. Formulas for modification|Formulas for modification@") |
| 79 | LIST_ITEM1 (U"1.8. @@Formulas 1.8. Formulas in scripts|Formulas in scripts@") |
| 80 | LIST_ITEM (U"2. @@Formulas 2. Representations|Representations@") |
| 81 | LIST_ITEM1 (U"2.1. @@Formulas 2.1. Representation of numbers|Representation of numbers@") |
| 82 | LIST_ITEM1 (U"2.2. @@Formulas 2.2. Representation of strings|Representation of strings@") |
| 83 | LIST_ITEM1 (U"2.3. @@Formulas 2.3. Representation of arrays|Representation of arrays@") |
| 84 | LIST_ITEM (U"3. @@Formulas 3. Operators|Operators@ (`+`, `-`, `*`, `/`, `^`)") |
| 85 | LIST_ITEM (U"4. @@Formulas 4. Constants|Constants@ (`pi`, `e`, `undefined`)") |
| 86 | LIST_ITEM (U"5. @@Formulas 5. Mathematical functions|Mathematical functions@") |
| 87 | LIST_ITEM (U"6. @@Formulas 6. String functions|String functions@") |
| 88 | LIST_ITEM (U"7. @@Formulas 7. Control structures|Control structures@ (`if then else fi`, semicolon)") |
| 89 | LIST_ITEM (U"8. @@Formulas 8. Attributes of objects|Attributes of objects@") |
| 90 | LIST_ITEM (U"9. @@Formulas 9. Data in objects|Data in objects@") |
| 91 | MAN_END |
| 92 | |
| 93 | MAN_BEGIN (U"Formulas 1. My first formulas", U"ppgb", 20170916) |
| 94 | LIST_ITEM (U"1.1. @@Formulas 1.1. Formulas in the calculator|Formulas in the calculator@") |
| 95 | LIST_ITEM (U"1.2. @@Formulas 1.2. Numeric expressions|Numeric expressions@") |
| 96 | LIST_ITEM (U"1.3. @@Formulas 1.3. String expressions|String expressions@") |
| 97 | LIST_ITEM (U"1.4. @@Formulas 1.4. Array expressions|Array expressions@") |
| 98 | LIST_ITEM (U"1.5. @@Formulas 1.5. Formulas in settings windows|Formulas in settings windows@") |
| 99 | LIST_ITEM (U"1.6. @@Formulas 1.6. Formulas for creation|Formulas for creation@") |
| 100 | LIST_ITEM (U"1.7. @@Formulas 1.7. Formulas for modification|Formulas for modification@") |
| 101 | LIST_ITEM (U"1.8. @@Formulas 1.8. Formulas in scripts|Formulas in scripts@") |
| 102 | MAN_END |
| 103 | |
| 104 | MAN_BEGIN (U"Formulas 1.1. Formulas in the calculator", U"ppgb", 20170916) |
| 105 | INTRO (U"To use the Praat @calculator, go to the @@Praat menu@ " |
| 106 | "and choose @@Calculator...@ from the @Goodies submenu. Or simply type @@Keyboard shortcuts|Command-U@ anywhere in Praat.") |
| 107 | ENTRY (U"Calculating numbers") |
| 108 | NORMAL (U"You can do arithmetic computations. Type the formula") |
| 109 | CODE (U"8*17") |
| 110 | NORMAL (U"and click OK. The Info window will pop up and show the result:") |
| 111 | CODE (U"136") |
| 112 | ENTRY (U"Calculating strings") |
| 113 | NORMAL (U"You can also do text computations. Type the formula") |
| 114 | CODE (U"\"see\" + \"king\"") |
| 115 | NORMAL (U"and click OK. The Info window will show the result:") |
| 116 | CODE (U"seeking") |
| 117 | ENTRY (U"Calculating arrays") |
| 118 | NORMAL (U"You can perform computations on vectors (arrays of numbers). Type the formula") |
| 119 | CODE (U"{ 11, 13, 17 } + 0.5") |
| 120 | NORMAL (U"and click OK. The Info window will show the result:") |
| 121 | CODE (U"11.5") |
| 122 | CODE (U"13.5") |
| 123 | CODE (U"17.5") |
| 124 | MAN_END |
| 125 | |
| 126 | MAN_BEGIN (U"Formulas 1.2. Numeric expressions", U"ppgb", 20170916) |
| 127 | INTRO (U"All the formulas whose outcome is a number are called numeric expressions. " |
| 128 | "For the following examples, all the outcomes can be checked with the @calculator.") |
| 129 | ENTRY (U"Examples with numbers") |
| 130 | NORMAL (U"Some numeric expressions involve numbers only:") |
| 131 | TERM (U"##8*17") |
| 132 | DEFINITION (U"computes a multiplication. Outcome: 136.") |
| 133 | TERM (U"##2\\^ 10") |
| 134 | DEFINITION (U"computes the tenth power of 2. Outcome: 1024.") |
| 135 | TERM (U"##sqrt (2) / 2") |
| 136 | DEFINITION (U"computes the square root of 2, and divides the result by 2. Outcome: 0.7071067811865476.") |
| 137 | TERM (U"##sin (1/4 * pi)") |
| 138 | DEFINITION (U"computes the sine of %\\pi/4. Outcome: 0.7071067811865476 (again).") |
| 139 | ENTRY (U"Examples with strings") |
| 140 | NORMAL (U"Some numeric expressions compute numeric properties of strings:") |
| 141 | TERM (U"##length (\"internationalization\")") |
| 142 | DEFINITION (U"computes the length of the string \"internationalization\". Outcome: 20.") |
| 143 | TERM (U"##index (\"internationalization\", \"ation\")") |
| 144 | DEFINITION (U"computes the location of the first occurrence of the string \"ation\" in the string \"internationalization\". Outcome: 7, " |
| 145 | "because the first letter of \"ation\" lines up with the seventh letter of \"internationalization\". " |
| 146 | "If the substring does not occur, the outcome is 0.") |
| 147 | ENTRY (U"Examples with arrays") |
| 148 | NORMAL (U"Some numeric expressions compute numeric properties of numeric vectors:") |
| 149 | TERM (U"##size ({ 40, 70, 60, 50 })") |
| 150 | DEFINITION (U"computes the length of the vector { 40, 70, 60, 50 }, i.e. the number of its elements. Outcome: 4.") |
| 151 | TERM (U"##mean ({ 40, 70, 60, 50 })") |
| 152 | DEFINITION (U"computes the mean of the four numbers 40, 70, 60 and 50. Outcome: 55.") |
| 153 | MAN_END |
| 154 | |
| 155 | MAN_BEGIN (U"Formulas 1.3. String expressions", U"ppgb", 20170916) |
| 156 | INTRO (U"All the formulas whose outcome is a text are called string expressions. " |
| 157 | "A %string is programming jargon for a text; think of a text as a “string” of characters (letters).") |
| 158 | NORMAL (U"Again, the outcomes of the following examples can be checked with the @calculator.") |
| 159 | TERM (U"##\"see\" + \"king\"") |
| 160 | DEFINITION (U"concatenates two strings. Outcome: seeking.") |
| 161 | TERM (U"##left\\$ (\"internationalization\", 6)") |
| 162 | DEFINITION (U"computes the leftmost six letters of the string; the dollar sign is used for all functions whose result is a string. Outcome: intern.") |
| 163 | TERM (U"##mid\\$ (\"internationalization\", 6, 8)") |
| 164 | DEFINITION (U"computes the 8-letter substring that starts at the sixth letter of \"internationalization\". Outcome: national.") |
| 165 | TERM (U"##date\\$ ( )") |
| 166 | DEFINITION (U"computes the current date and time. Outcome at the time I am writing this: Mon Dec 2 02:23:45 2002.") |
| 167 | MAN_END |
| 168 | |
| 169 | MAN_BEGIN (U"Formulas 1.4. Array expressions", U"ppgb", 20170916) |
| 170 | INTRO (U"A numeric vector expression is an expression whose value is a numeric vector.") |
| 171 | NORMAL (U"You can check the outcomes of the following examples with the @calculator.") |
| 172 | TERM (U"##{ 11, 13, 17 } + 0.5") |
| 173 | DEFINITION (U"adds 0.5 to each element of a vector with three elements, giving a new vector with three elements. " |
| 174 | "Praat writes the outcome to the Info window as three lines, containing the numbers 11.5, 13.5 and 17.5:") |
| 175 | CODE (U"11.5") |
| 176 | CODE (U"13.5") |
| 177 | CODE (U"17.5") |
| 178 | TERM (U"@`zero#` (5)") |
| 179 | DEFINITION (U"creates a vector with 5 zeroes. Praat writes them on five lines:") |
| 180 | CODE (U"0") |
| 181 | CODE (U"0") |
| 182 | CODE (U"0") |
| 183 | CODE (U"0") |
| 184 | CODE (U"0") |
| 185 | TERM (U"@`repeat#` ({ 1, 5 }, 6)") |
| 186 | DEFINITION (U"creates a vector with 12 elements, in which the sequence { 1, 5 } is repeated 6 times. Outcome:") |
| 187 | CODE (U"1") |
| 188 | CODE (U"5") |
| 189 | CODE (U"1") |
| 190 | CODE (U"5") |
| 191 | CODE (U"1") |
| 192 | CODE (U"5") |
| 193 | CODE (U"1") |
| 194 | CODE (U"5") |
| 195 | CODE (U"1") |
| 196 | CODE (U"5") |
| 197 | CODE (U"1") |
| 198 | CODE (U"5") |
| 199 | MAN_END |
| 200 | |
| 201 | MAN_BEGIN (U"Formulas 1.5. Formulas in settings windows", U"ppgb", 20170916) |
| 202 | INTRO (U"Into numeric fields in settings windows you usually simply type a number. " |
| 203 | "However, you can use any numeric expression instead.") |
| 204 | NORMAL (U"For instance, suppose you want to create a Sound that contains exactly 10000 samples. " |
| 205 | "If the sampling frequency is 44100 Hz, the duration will be 10000/44100 seconds. " |
| 206 | "You can create such a Sound by choosing @@Create Sound from formula...@ from the @@New menu@, " |
| 207 | "then typing") |
| 208 | CODE (U"10000/44100") |
| 209 | NORMAL (U"into the ##End time# field.") |
| 210 | NORMAL (U"Into text fields in settings windows, you can only type text directly; there is no way " |
| 211 | "to use string expressions (except if you use scripts; see @@Formulas 1.8. Formulas in scripts@).") |
| 212 | NORMAL (U"Into numeric vector fields in settings windows, you can type any numeric vector (array) expression.") |
| 213 | MAN_END |
| 214 | |
| 215 | MAN_BEGIN (U"Formulas 1.6. Formulas for creation", U"ppgb", 20110128) |
| 216 | INTRO (U"With some commands in the @@New menu@, you can supply a formula that Praat will apply to all elements of the new object.") |
| 217 | ENTRY (U"Creating a Sound from a formula") |
| 218 | NORMAL (U"Choose @@Create Sound from formula...@ and type the following into the #%Formula field:") |
| 219 | CODE (U"1/2 * sin (2 * pi * 377 * x)") |
| 220 | NORMAL (U"When you click #OK, a new @Sound object will appear in the list. " |
| 221 | "After you click ##View & Edit# and zoom in a couple of times, you will see that the sound is a sine wave " |
| 222 | "with a frequency of 377 hertz (cycles per second). This worked because the $x in the formula represents the time, " |
| 223 | "i.e. the formula was applied to every sample separately, with a different value of $x for each sample.") |
| 224 | ENTRY (U"Creating a Matrix from a formula") |
| 225 | NORMAL (U"Choose @@Create simple Matrix...@ and type the following into the #%Formula field:") |
| 226 | CODE (U"8") |
| 227 | NORMAL (U"When you click OK, a new @Matrix object will appear in the list. When you click #Info, " |
| 228 | "you will see that it is a matrix with 10 rows and 10 columns, and that all the 100 cells contain the value 8 " |
| 229 | "(you can see this because both the minimum and the maximum are reported as being 8).") |
| 230 | NORMAL (U"A more interesting example is the formula") |
| 231 | CODE (U"row * col") |
| 232 | NORMAL (U"For the resulting Matrix, " |
| 233 | "choose @@Matrix: Paint cells...|Paint cells...@ and click #OK. The Picture window will show a 10\\xx10 " |
| 234 | "matrix whose elements are the product of the row and column numbers, i.e., they have values between " |
| 235 | "1 and 100. Beside $row and $col, you can use $x for the distance along the horizontal axis and $y for the " |
| 236 | "distance along the vertical axis; see the following page for examples.") |
| 237 | MAN_END |
| 238 | |
| 239 | MAN_BEGIN (U"Formulas 1.7. Formulas for modification", U"ppgb", 20170916) |
| 240 | INTRO (U"Analogously to the formulas that you can use for creating new objects (see the previous page), " |
| 241 | "you can use formulas for modifying existing objects. You do this with the command ##Formula...# that you " |
| 242 | "can find in the @Modify menu when you select an object.") |
| 243 | ENTRY (U"Modifying a Sound with a formula") |
| 244 | NORMAL (U"Record a sound with your microphone and talk very lowly. If you don't know how to record a sound in Praat, " |
| 245 | "consult the @Intro. Once the Sound object is in the list, click #Play. The result will sound very soft. " |
| 246 | "Then choose ##Formula...# from the Modify menu and type") |
| 247 | CODE (U"self * 3") |
| 248 | NORMAL (U"Click OK, then click #Play again. The sound is much louder now. You have multiplied the amplitude of every sample " |
| 249 | "in the sound with a factor of 3.") |
| 250 | ENTRY (U"Replacing the contents of a Sound with a formula") |
| 251 | NORMAL (U"If you don’t use `self` in your formula, the formula does not refer to the existing contents of the Sound. Hence, the formula ") |
| 252 | CODE (U"1/2 * sin (2 * pi * 377 * x)") |
| 253 | NORMAL (U"will simply replace your recorded speech with a 377-Hz sine wave.") |
| 254 | ENTRY (U"Modifying a Matrix with a formula") |
| 255 | NORMAL (U"Many objects can be thought of as matrices: " |
| 256 | "they consist of a number of rows and columns with data in every cell:") |
| 257 | LIST_ITEM (U"@Sound: one row; columns represent samples.") |
| 258 | LIST_ITEM (U"@Spectrum: two rows (first row is real part, second row is imaginary part); " |
| 259 | "columns represent frequencies.") |
| 260 | LIST_ITEM (U"@Spectrogram, @Cochleagram: rows represent frequencies; columns represent times.") |
| 261 | LIST_ITEM (U"@Excitation: one row; columns represent frequency bands.") |
| 262 | LIST_ITEM (U"@Harmonicity: one row; columns represent time frames.") |
| 263 | NORMAL (U"The formula is performed on every column of every row. The formula") |
| 264 | CODE (U"self^2") |
| 265 | NORMAL (U"will square all matrix elements.") |
| 266 | NORMAL (U"The formula first works on the first row, and in that row from the first column on; " |
| 267 | "this can work recursively. The formula") |
| 268 | CODE (U"self + self [row, col - 1]") |
| 269 | NORMAL (U"integrates each row.") |
| 270 | ENTRY (U"Referring to the current position in the object") |
| 271 | NORMAL (U"You can refer to the current position in a Matrix (or Sound, etc.) by index or by %x and %y values:") |
| 272 | TERM (U"#`row`") |
| 273 | DEFINITION (U"the current row") |
| 274 | TERM (U"#`col`") |
| 275 | DEFINITION (U"the current column") |
| 276 | TERM (U"#`x`") |
| 277 | DEFINITION (U"the %x value associated with the current column:") |
| 278 | LIST_ITEM2 (U"for a Sound, Spectrogram, Cochleagram, or Harmonicity: time, as in the 377-Hz sine wave example above") |
| 279 | LIST_ITEM2 (U"for a Spectrum: frequency (Hz)") |
| 280 | LIST_ITEM2 (U"for an Excitation: frequency (Bark)") |
| 281 | TERM (U"#`y`") |
| 282 | DEFINITION (U"the %y value associated with the current row:") |
| 283 | LIST_ITEM2 (U"for a Spectrogram: frequency (Hz)") |
| 284 | LIST_ITEM2 (U"for a Cochleagram: frequency (Bark)") |
| 285 | ENTRY (U"Referring to the contents of the object itself") |
| 286 | NORMAL (U"You can refer to values in the current Matrix (or Sound, etc.) by index.") |
| 287 | TERM (U"#`self`") |
| 288 | DEFINITION (U"refers to the value in the current Matrix at the current row and column, or to the value in the current Sound at the current sample.") |
| 289 | TERM (U"#`self` [%`column-expression`]") |
| 290 | DEFINITION (U"refers to the value in the current Sound (or Intensity etc.) at the current sample (or frame). " |
| 291 | "The %`column-expression` is rounded to the nearest integer. " |
| 292 | "If the index is out of range (less than 1 or greater than %n__%x_), the expression evaluates as 0.") |
| 293 | NORMAL (U"#Example. An integrator is") |
| 294 | CODE (U" self [col - 1] + self * dx") |
| 295 | TERM (U"#`self` [%`row-expression`, %`column-expression`]") |
| 296 | DEFINITION (U"refers to the value in the current Matrix (or Spectrogram etc.) at the specified row and column. " |
| 297 | "The expressions are rounded to the nearest integers.") |
| 298 | NORMAL (U"You can refer to values in the current Matrix (or Spectrogram, etc.) by %x and %y position:") |
| 299 | TERM (U"#`self` (%`x-expression`, %`y-expression`)") |
| 300 | DEFINITION (U"the expressions are linearly interpolated between the four nearest matrix points.") |
| 301 | NORMAL (U"You can refer to values in the current Sound (or Intensity etc.) by %x position:") |
| 302 | TERM (U"#`self` (%`x-expression`)") |
| 303 | DEFINITION (U"the expression is linearly interpolated between the two nearest samples (or frames).") |
| 304 | MAN_END |
| 305 | |
| 306 | MAN_BEGIN (U"Formulas 1.8. Formulas in scripts", U"ppgb", 20170916) |
| 307 | INTRO (U"In scripts, you can assign numeric expressions to numeric variables, " |
| 308 | "string expressions to string variables, and array expressions to array variables. " |
| 309 | "You can also use numeric, string and array variables in expressions.") |
| 310 | ENTRY (U"Example: report a square") |
| 311 | NORMAL (U"Choose @@New Praat script@ from the @@Praat menu@. A script editor window will become visible. " |
| 312 | "Type the following lines into that window:") |
| 313 | CODE (U"x = 99") |
| 314 | CODE (U"x2 = x * x") |
| 315 | CODE (U"writeInfoLine: “The square of ”, x, “ is ”, x2, “.”") |
| 316 | NORMAL (U"This is an example of a simple @@Praat script@; " |
| 317 | "it assigns the results of the numeric formulas `99` and `x * x` " |
| 318 | "to the numeric variables %`x` and %`x2`. Note that the formula `x * x` itself refers to the variable %`x`. " |
| 319 | "To run (execute) this script, type @@Keyboard shortcuts|Command-R@ or choose #Run from the Run menu. " |
| 320 | "Praat will then write the following text into the Info window:") |
| 321 | CODE (U"The square of 99 is 9801.") |
| 322 | NORMAL (U"For more information on scripts, see the @Scripting tutorial.") |
| 323 | ENTRY (U"Example: rename the city of Washington") |
| 324 | NORMAL (U"Type the following text into the script editor window:") |
| 325 | CODE (U"current$ = “Bush”") |
| 326 | CODE (U"previous$ = “Clinton”") |
| 327 | CODE (U"famous$ = “Lincoln”") |
| 328 | CODE (U"newCapital$ = current$ + mid$ (famous$, 2, 3) + right$ (previous$, 3)") |
| 329 | CODE (U"writeInfoLine: “The new capital will be ”, newCapital$, “.”") |
| 330 | NORMAL (U"This script assigns the results of four string expressions to the four string variables `current$`, " |
| 331 | "`previous$`, `famous$`, and `newCapital$`. The dollar sign is the notation for a string variable or " |
| 332 | "for a function whose result is a string (like `left$`). Note that the formula in the fourth line refers to three existing " |
| 333 | "variables.") |
| 334 | NORMAL (U"To see what the new name of the capital will be, choose #Run.") |
| 335 | ENTRY (U"Example: report five squares") |
| 336 | NORMAL (U"Type the following script:") |
| 337 | CODE (U"x# = { 1, 2, 3, 4, 5 }") |
| 338 | CODE (U"x2# = x# * x#") |
| 339 | CODE (U"writeInfoLine: “The squares of ”, x#, “ are ”, x2#, “.”") |
| 340 | NORMAL (U"Praat will then write the following text into the Info window:") |
| 341 | CODE (U"The squares of 1 2 3 4 5 are 1 4 9 16 25.") |
| 342 | ENTRY (U"Example: numeric expressions in settings in scripts") |
| 343 | NORMAL (U"As in real settings windows, you can use numeric expressions in all numeric fields. " |
| 344 | "The example of two pages back becomes:") |
| 345 | CODE (U"Create Sound from formula: “sine”, 1, 0, 10000 / 44100, 44100, ~ 0.9 * sin (2*pi*377*x)") |
| 346 | ENTRY (U"Example: string expressions in settings in scripts") |
| 347 | NORMAL (U"As in real settings windows, you can use string expressions in all text fields:") |
| 348 | CODE (U"soundName$ = “hello”") |
| 349 | CODE (U"Read from file: soundName$ + “.wav”") |
| 350 | ENTRY (U"Example: numeric expressions in creation in scripts") |
| 351 | NORMAL (U"Suppose you want to generate a sine wave whose frequency is held in a variable. This is the way:") |
| 352 | CODE (U"frequency = 377") |
| 353 | CODE (U"Create Sound from formula: “sine”, 1, 0.0, 1.0, 44100, ~ 0.9 * sin (2*pi*frequency*x)") |
| 354 | NORMAL (U"In this example, Praat will protest if `x` is a variable as well (%`x`), because that would be ambiguous " |
| 355 | "with the `x` that refers to the time in the sound (see @@Formulas 1.7. Formulas for modification@).") |
| 356 | MAN_END |
| 357 | |
| 358 | MAN_BEGIN (U"Formulas 2. Representations", U"ppgb", 20170916) |
| 359 | LIST_ITEM (U"2.1. @@Formulas 2.1. Representation of numbers|Representation of numbers@") |
| 360 | LIST_ITEM (U"2.2. @@Formulas 2.2. Representation of strings|Representation of strings@") |
| 361 | LIST_ITEM (U"2.3. @@Formulas 2.3. Representation of arrays|Representation of arrays@") |
| 362 | MAN_END |
| 363 | |
| 364 | MAN_BEGIN (U"Formulas 2.1. Representation of numbers", U"ppgb", 20170916) |
| 365 | INTRO (U"Formulas can work with integer numbers as well as with real numbers.") |
| 366 | ENTRY (U"Real numbers") |
| 367 | NORMAL (U"You can type many real numbers by using a decimal notation, for instance 3.14159, 299792.5, or -0.000123456789. " |
| 368 | "For very large or small numbers, you can use the %e-notation: 6.022\\.c10^^23^ is typed as `6.022e23` or `6.022e+23`, " |
| 369 | "and -1.6021917\\.c10^^-19^ is typed as `-1.6021917e-19`. You can use also use the percent notation: 0.157 " |
| 370 | "can be typed as `15.7%`.") |
| 371 | NORMAL (U"There are some limitations as to the values that real numbers can have in Praat. " |
| 372 | "The numbers must lie between -10^^308^ and +10^^308^. If you type") |
| 373 | CODE (U"1e200 * 1e100") |
| 374 | NORMAL (U"the outcome will be") |
| 375 | CODE (U"1e+300") |
| 376 | NORMAL (U"but if you type") |
| 377 | CODE (U"1e300 * 1e100") |
| 378 | NORMAL (U"the outcome will be") |
| 379 | CODE (U"--undefined--") |
| 380 | NORMAL (U"Another limitation is that the smallest non-zero numbers lie near -10^^-308^ and +10^^-308^. If you type") |
| 381 | CODE (U"1e-200 / 1e100") |
| 382 | NORMAL (U"the outcome will be") |
| 383 | CODE (U"1e-300") |
| 384 | NORMAL (U"but if you type") |
| 385 | CODE (U"1e-300 / 1e100") |
| 386 | NORMAL (U"the outcome will be") |
| 387 | CODE (U"0") |
| 388 | NORMAL (U"Finally, the precision of real numbers is limited by the number of bits that every real number is stored with " |
| 389 | "in the computer, namely 64. For instance, if you type") |
| 390 | CODE (U"pi") |
| 391 | NORMAL (U"the outcome will be") |
| 392 | CODE (U"3.141592653589793") |
| 393 | NORMAL (U"because only 16 digits of precision are stored. This can lead to unexpected results caused by rounding. " |
| 394 | "For instance, the formula") |
| 395 | CODE (U"0.34999999999999999 - 0.35") |
| 396 | NORMAL (U"will result in") |
| 397 | CODE (U"0") |
| 398 | NORMAL (U"rather than the correct value of 1e-17. This is because the numbers 0.34999999999999999 and 0.35 cannot " |
| 399 | "be distinguished in the computer's memory. If you simply type") |
| 400 | CODE (U"0.34999999999999999") |
| 401 | NORMAL (U"the outcome will be") |
| 402 | CODE (U"0.35") |
| 403 | NORMAL (U"(as in this example, the calculator will always come up with the minimum number of digits needed to represent the number unambiguously).") |
| 404 | NORMAL (U"Another example of inaccuracy is the formula") |
| 405 | CODE (U"1 / 7 / 59 * 413") |
| 406 | NORMAL (U"Because of rounding errors, the result will be") |
| 407 | CODE (U"0.9999999999999999") |
| 408 | ENTRY (U"Integer numbers") |
| 409 | NORMAL (U"Formulas can work with integer (whole) numbers between -1,000,000,000,000,000 and +1,000,000,000,000,000. " |
| 410 | "You type them without commas and without the plus sign: 337, -848947328345289.") |
| 411 | NORMAL (U"You %can work with larger numbers than that (up to 10^^308^), but there will again be rounding errors. " |
| 412 | "For instance, the formula") |
| 413 | CODE (U"1000000000000000 + 1") |
| 414 | NORMAL (U"correctly yields") |
| 415 | CODE (U"1000000000000001") |
| 416 | NORMAL (U"but the formula") |
| 417 | CODE (U"10000000000000000 + 1") |
| 418 | NORMAL (U"yields an incorrect outcome:") |
| 419 | CODE (U"1e16") |
| 420 | MAN_END |
| 421 | |
| 422 | MAN_BEGIN (U"Formulas 2.2. Representation of strings", U"ppgb", 20021203) |
| 423 | INTRO (U"Formulas can work with strings that are put between two double quotes, " |
| 424 | "as in “goodbye” or “how are you doing?”.") |
| 425 | NORMAL (U"If a string has to contain a double quote, " |
| 426 | "you have to type it twice. For instance, if you type") |
| 427 | CODE (U"\"I asked: \"\"how are you doing?\"\"\"") |
| 428 | NORMAL (U"into the calculator, the outcome will be") |
| 429 | CODE (U"I asked: \"how are you doing?\"") |
| 430 | MAN_END |
| 431 | |
| 432 | MAN_BEGIN (U"Formulas 2.3. Representation of arrays", U"ppgb", 20170916) |
| 433 | INTRO (U"Formulas can work with vectors that are put between opening and closing braces, " |
| 434 | "as in { 11, 17, 13.5 }. If you type this into the calculator, the outcome will be") |
| 435 | CODE (U"11") |
| 436 | CODE (U"17") |
| 437 | CODE (U"13.5") |
| 438 | MAN_END |
| 439 | |
| 440 | MAN_BEGIN (U"Formulas 3. Operators", U"ppgb", 20230124) |
| 441 | NORMAL (U"In formulas you can use the numerical and logical operators that are described on this page. " |
| 442 | "The order of evaluation of the operators is the order that is most usual in programming languages. " |
| 443 | "To force a different order, you use parentheses.") |
| 444 | NORMAL (U"The operators with the highest precedence are #negation (-) and #exponentation (\\^ ):") |
| 445 | CODE (U"--6 \\-> 6") |
| 446 | CODE (U"2^6 \\-> 64") |
| 447 | /*@praat |
| 448 | # Tests for the manual page "Operators". |
| 449 | |
| 450 | assert --6 = 6 |
| 451 | assert 2^6 = 64 |
| 452 | @*/ |
| 453 | NORMAL (U"Sequences of negation and exponentiation are evaluated from right to left:") |
| 454 | CODE (U"2^-6 \\-> 0.015625") //@praat assert 2^-6 = 0.015625 |
| 455 | CODE (U"-(1+1)^6 \\-> -64") //@praat assert -(1+1)^6 = -64 |
| 456 | CODE (U"4^3^2 \\-> 4^9 \\-> 262144") //@praat assert 4^3^2 = 262144 |
| 457 | NORMAL (U"Note that changing the spacing does not change the meaning:") |
| 458 | CODE (U"4^3 ^ 2 \\-> 262144") //@praat assert 4^3 ^ 2 = 262144 |
| 459 | NORMAL (U"To change the order of evaluation, you have to use parentheses:") |
| 460 | CODE (U"(4 ^ 3) ^ 2 \\-> 4096") //@praat assert (4 ^ 3) ^ 2 = 4096 |
| 461 | NORMAL (U"The following construction is not allowed because of an ambiguity between a negative number " |
| 462 | "and negation of a positive number:") |
| 463 | CODE (U"-2^6 \\-> ?") |
| 464 | NORMAL (U"Instead, you use any of the following:") |
| 465 | CODE (U"(-2)^6 \\-> 64") //@praat assert (-2)^6 = 64 |
| 466 | CODE (U"-(2^6) \\-> -64") //@praat assert -(2^6) = -64 |
| 467 | CODE (U"-(2)^6 \\-> -64") //@praat assert -(2)^6 = -64 |
| 468 | NORMAL (U"The operators with the next highest precedence are #multiplication (*) and #division (/). They are evaluated " |
| 469 | "from left to right:") |
| 470 | CODE (U"1/4*5 \\-> 1.25 (from left to right)") |
| 471 | CODE (U"1 / 4*5 \\-> 1.25 (spacing does not help)") |
| 472 | CODE (U"1 / (4*5) \\-> 0.05 (use parentheses to change the order)") |
| 473 | /*@praat |
| 474 | assert 1/4*5 = 1.25 |
| 475 | assert 1 / 4*5 = 1.25 |
| 476 | assert 1 / (4*5) = 0.05 |
| 477 | @*/ |
| 478 | CODE (U"3 * 2 ^ 4 \\-> 48 (exponentiation before multiplication)") |
| 479 | CODE (U"3*2 ^ 4 \\-> 48 (this spacing does not matter and is misleading)") |
| 480 | CODE (U"(3 * 2) ^ 4 \\-> 1296 (use parentheses to change the order)") |
| 481 | /*@praat |
| 482 | assert 3 * 2 ^ 4 = 48 |
| 483 | assert 3*2 ^ 4 = 48 |
| 484 | assert (3 * 2) ^ 4 = 1296 |
| 485 | @*/ |
| 486 | NORMAL (U"##Integer division# operators (#div and #mod) have the same precedence as * and /, " |
| 487 | "and are likewise evaluated from left to right:") |
| 488 | CODE (U"54 div 5 \\-> 10 (division rounded down)") //@praat assert 54 div 5 = 10 |
| 489 | CODE (U"54 mod 5 \\-> 4 (the remainder)") //@praat assert 54 mod 5 = 4 |
| 490 | CODE (U"54.3 div 5.1 \\-> 10 (works for real numbers as well)") //@praat assert 54.3 div 5.1 = 10 |
| 491 | CODE (U"54.3 mod 5.1 \\-> 3.3 (the remainder)") //@praat assert abs ((54.3 mod 5.1) - 3.3) < 1e-14 |
| 492 | CODE (U"-54 div 5 \\-> -11 (division rounded down; negation before division)") //@praat assert -54 div 5 = -11 |
| 493 | CODE (U"-54 mod 5 \\-> 1 (the remainder)") //@praat assert -54 mod 5 = 1 |
| 494 | CODE (U"-(54 div 5) \\-> -10 (use parentheses to change the order)") //@praat assert -(54 div 5) = -10 |
| 495 | CODE (U"-(54 mod 5) \\-> -4") //@praat assert -(54 mod 5) = -4 |
| 496 | CODE (U"3 * 18 div 5 \\-> 10 (from left to right)") //@praat assert 3 * 18 div 5 = 10 |
| 497 | CODE (U"3 * (18 div 5) \\-> 9") //@praat assert 3 * (18 div 5) = 9 |
| 498 | CODE (U"3 * 18 mod 5 \\-> 4") //@praat assert 3 * 18 mod 5 = 4 |
| 499 | CODE (U"3 * (18 mod 5) \\-> 9") //@praat assert 3 * (18 mod 5) = 9 |
| 500 | CODE (U"54 div 5 * 3 \\-> 30 (from left to right)") //@praat assert 54 div 5 * 3 = 30 |
| 501 | CODE (U"54 div (5 * 3) \\-> 3") //@praat assert 54 div (5 * 3) = 3 |
| 502 | CODE (U"54 mod 5 * 3 \\-> 12") //@praat assert 54 mod 5 * 3 = 12 |
| 503 | CODE (U"54 mod (5 * 3) \\-> 9") //@praat assert 54 mod (5 * 3) = 9 |
| 504 | NORMAL (U"The operators with the next highest precedence are #addition (+) and #subtraction (-), " |
| 505 | "evaluated from left to right:") |
| 506 | CODE (U"3 - 8 + 7 \\-> 2 (from left to right)") //@praat assert 3 - 8 + 7 = 2 |
| 507 | CODE (U"3 - (8 + 7) \\-> -12 (use parentheses to change the order)") //@praat assert 3 - (8 + 7) = -12 |
| 508 | CODE (U"3 + 8 * 7 \\-> 59 (multiplication before addition)") //@praat assert 3 + 8 * 7 = 59 |
| 509 | CODE (U"(3 + 8) * 7 \\-> 77 (use parentheses to change the order)") //@praat assert (3 + 8) * 7 = 77 |
| 510 | CODE (U"3 + - (2 \\^ 4) \\-> -13 (exponentiation, negation, addition)") //@praat assert 3 + - (2 ^ 4) = -13 |
| 511 | CODE (U"3 + 5 / 2 + 3 \\-> 8.5") //@praat assert 3 + 5 / 2 + 3 = 8.5 |
| 512 | CODE (U"(3 + 5) / (2 + 3) \\-> 1.6") //@praat assert (3 + 5) / (2 + 3) = 1.6 |
| 513 | NORMAL (U"The operators with the next highest precedence are the #comparison operators " |
| 514 | "(`=, <>, <, >, <=, >=`). These operators always yield 0 (%false) or 1 (%true):") |
| 515 | CODE (U"5 + 6 = 10 \\-> 0 (equal)") //@praat assert ( 5 + 6 = 10 ) = 0 |
| 516 | CODE (U"5 + 6 = 11 \\-> 1") //@praat assert ( 5 + 6 = 11 ) = 1 |
| 517 | CODE (U"5 + 6 <> 10 \\-> 1 (unequal)") //@praat assert ( 5 + 6 <> 10 ) = 1 |
| 518 | CODE (U"5 + 6 <> 11 \\-> 0") //@praat assert ( 5 + 6 <> 11 ) = 0 |
| 519 | CODE (U"5 + 6 < 10 \\-> 0 (less than)") //@praat assert ( 5 + 6 < 10 ) = 0 |
| 520 | CODE (U"5 + 6 < 11 \\-> 0") //@praat assert ( 5 + 6 < 11 ) = 0 |
| 521 | CODE (U"5 + 6 > 10 \\-> 1 (greater than)") //@praat assert ( 5 + 6 > 10 ) = 1 |
| 522 | CODE (U"5 + 6 > 11 \\-> 0") //@praat assert ( 5 + 6 > 11 ) = 0 |
| 523 | CODE (U"5 + 6 <= 10 \\-> 0 (less than or equal)") //@praat assert ( 5 + 6 <= 10 ) = 0 |
| 524 | CODE (U"5 + 6 <= 11 \\-> 1") //@praat assert ( 5 + 6 <= 11 ) = 1 |
| 525 | CODE (U"5 + 6 >= 10 \\-> 1 (greater or equal)") //@praat assert ( 5 + 6 >= 10 ) = 1 |
| 526 | CODE (U"5 + 6 >= 11 \\-> 1") //@praat assert ( 5 + 6 >= 11 ) = 1 |
| 527 | NORMAL (U"The comparison operators are mainly used in #if, #while, and #until conditions.") |
| 528 | NORMAL (U"The operators of lowest precedence are the #logical operators (#not, #and, and #or), of which #not has the " |
| 529 | "highest precedence and #or the lowest:") |
| 530 | CODE (U"not 5 + 6 = 10 \\-> 1") //@praat assert ( not 5 + 6 = 10 ) = 1 |
| 531 | CODE (U"x > 5 and x < 10 (is x between 5 and 10?)") |
| 532 | CODE (U"not x <= 5 and not x >= 10 (means the same as the previous line, except if x is undefined)") |
| 533 | CODE (U"not (x <= 5 or x >= 10) (again means the same, except if x is undefined)") |
| 534 | /*@praat |
| 535 | x = 7 |
| 536 | assert ( x > 5 and x < 10 ) = 1 |
| 537 | assert ( not x <= 5 and not x >= 10 ) = 1 |
| 538 | assert ( not (x <= 5 or x >= 10) ) = 1 |
| 539 | x = 3 |
| 540 | assert ( x > 5 and x < 10 ) = 0 |
| 541 | assert ( not x <= 5 and not x >= 10 ) = 0 |
| 542 | assert ( not (x <= 5 or x >= 10) ) = 0 |
| 543 | x = 11 |
| 544 | assert ( x > 5 and x < 10 ) = 0 |
| 545 | assert ( not x <= 5 and not x >= 10 ) = 0 |
| 546 | assert ( not (x <= 5 or x >= 10) ) = 0 |
| 547 | x = undefined |
| 548 | assert ( x > 5 and x < 10 ) = 0 |
| 549 | assert ( not x <= 5 and not x >= 10 ) = 1 |
| 550 | assert ( not (x <= 5 or x >= 10) ) = 1 |
| 551 | @*/ |
| 552 | ENTRY (U"String comparison") |
| 553 | TERM (U"#`a$ = b$`") |
| 554 | DEFINITION (U"gives the value %true (= 1) if the strings are equal, and %false (= 0) otherwise.") |
| 555 | TERM (U"#`a$ <> b$`") |
| 556 | DEFINITION (U"gives the value %true if the strings are unequal, and %false otherwise.") |
| 557 | TERM (U"#`a$ < b$`") |
| 558 | DEFINITION (U"gives %true if the string %`a$` precedes the string %`b$` in Unicode sorting order. " |
| 559 | "Thus, \"ha\" < \"hal\" and \"ha\" < \"ja\" are true, but \"ha\" < \"JA\" is false, " |
| 560 | "because all capitals precede all lower-case characters in the Unicode sorting order.") |
| 561 | TERM (U"#`a$ > b$`") |
| 562 | DEFINITION (U"%true if %`a$` comes after %`b$` in Unicode sorting order. ") |
| 563 | TERM (U"#`a$ <= b$`") |
| 564 | DEFINITION (U"gives the value %true if the string %`a$` precedes the string %`b$` in Unicode sorting order, " |
| 565 | "or if the strings are equal.") |
| 566 | TERM (U"#`a$ >= b$`") |
| 567 | DEFINITION (U"%true if %`a$` comes after %`b$` or the two are equal.") |
| 568 | ENTRY (U"String concatenation and truncation") |
| 569 | TERM (U"#`a$ + b$`") |
| 570 | DEFINITION (U"concatenates the two strings. After") |
| 571 | CODE1 (U"text$ = \"hallo\" + \"dag\"") |
| 572 | DEFINITION (U"the variable %`text$` contains the string “hallodag”.") |
| 573 | TERM (U"#`a$ - b$`") |
| 574 | DEFINITION (U"subtracts the second string from the end of the first. After") |
| 575 | CODE2 (U"soundFileName$ = \"hallo.aifc\"") |
| 576 | CODE2 (U"textgridFileName$ = soundFileName$ - \".aifc\" + \".TextGrid\"") |
| 577 | DEFINITION (U"the variable %`textgridFileName$` contains the string \"hallo.TextGrid\". " |
| 578 | "If the first string %`a$` does not end in the string %`b$`, the result of the subtraction is the string %`a$`.") |
| 579 | MAN_END |
| 580 | |
| 581 | MAN_BEGIN (U"Formulas 4. Constants", U"ppgb", 20080318) |
| 582 | TERM (U"##pi") |
| 583 | DEFINITION (U"%\\pi, 3.14159265358979323846264338328") |
| 584 | TERM (U"##e") |
| 585 | DEFINITION (U"%e, 2.71828182845904523536028747135") |
| 586 | TERM (U"##undefined") |
| 587 | DEFINITION (U"a special value, see @undefined") |
| 588 | MAN_END |
| 589 | |
| 590 | MAN_BEGIN (U"Formulas 5. Mathematical functions", U"ppgb", 20200801) |
| 591 | TERM (U"##abs (%x)") |
| 592 | DEFINITION (U"absolute value") |
| 593 | TERM (U"##round (%x)") |
| 594 | DEFINITION (U"nearest integer; round (1.5) = 2") |
| 595 | TERM (U"##floor (%x)") |
| 596 | DEFINITION (U"round down: highest integer value not greater than %x") |
| 597 | TERM (U"##ceiling (%x)") |
| 598 | DEFINITION (U"round up: lowest integer value not less than %x") |
| 599 | TERM (U"##sqrt (%x)") |
| 600 | DEFINITION (U"square root: \\Vr%x, %x \\>_ 0") |
| 601 | TERM (U"##min (%x, ...)") |
| 602 | DEFINITION (U"the minimum of a series of numbers, e.g. min (7.2, -5, 3) = -5") |
| 603 | TERM (U"##max (%x, ...)") |
| 604 | DEFINITION (U"the maximum of a series of numbers, e.g. max (7.2, -5, 3) = 7.2") |
| 605 | TERM (U"##imin (%x, ...)") |
| 606 | DEFINITION (U"the location of the minimum, e.g. imin (7.2, -5, 3) = 2") |
| 607 | TERM (U"##imax (%x, ...)") |
| 608 | DEFINITION (U"the location of the maximum, e.g. imax (7.2, -5, 3) = 1") |
| 609 | TERM (U"##sin (%x)") |
| 610 | DEFINITION (U"sine") |
| 611 | TERM (U"##cos (%x)") |
| 612 | DEFINITION (U"cosine") |
| 613 | TERM (U"##tan (%x)") |
| 614 | DEFINITION (U"tangent") |
| 615 | TERM (U"##arcsin (%x)") |
| 616 | DEFINITION (U"arcsine, -1 \\<_ %x \\<_ 1") |
| 617 | TERM (U"##arccos (%x)") |
| 618 | DEFINITION (U"arccosine, -1 \\<_ %x \\<_ 1") |
| 619 | TERM (U"##arctan (%x)") |
| 620 | DEFINITION (U"arctangent") |
| 621 | TERM (U"##arctan2 (%y, %x)") |
| 622 | DEFINITION (U"argument angle") |
| 623 | TERM (U"##sinc (%x)") |
| 624 | DEFINITION (U"sinus cardinalis: sin (%x) / %x") |
| 625 | TERM (U"##sincpi (%x)") |
| 626 | DEFINITION (U"sinc__%\\pi_: sin (%\\pi%x) / (%\\pi%x)") |
| 627 | TERM (U"##exp (%x)") |
| 628 | DEFINITION (U"exponentiation: %e^%x; same as ##e\\^ %x") |
| 629 | TERM (U"##ln (%x)") |
| 630 | DEFINITION (U"natural logarithm, base %e") |
| 631 | TERM (U"##log10 (%x)") |
| 632 | DEFINITION (U"logarithm, base 10") |
| 633 | TERM (U"##log2 (%x)") |
| 634 | DEFINITION (U"logarithm, base 2") |
| 635 | TERM (U"##sinh (%x)") |
| 636 | DEFINITION (U"hyperbolic sine: (%e^%x - %e^^-%x^) / 2") |
| 637 | TERM (U"##cosh (%x)") |
| 638 | DEFINITION (U"hyperbolic cosine: (%e^%x + %e^^-%x^) / 2") |
| 639 | TERM (U"##tanh (%x)") |
| 640 | DEFINITION (U"hyperbolic tangent: sinh (%x) / cosh (%x)") |
| 641 | TERM (U"##arcsinh (%x)") |
| 642 | DEFINITION (U"inverse hyperbolic sine: ln (%x + \\Vr(1+%x^2))") |
| 643 | TERM (U"##arccosh (%x)") |
| 644 | DEFINITION (U"inverse hyperbolic cosine: ln (%x + \\Vr(%x^2−1))") |
| 645 | TERM (U"##arctanh (%x)") |
| 646 | DEFINITION (U"inverse hyperbolic tangent") |
| 647 | TERM (U"##sigmoid (%x)") |
| 648 | DEFINITION (U"#R \\-> (0,1): 1 / (1 + %e^^−%x^) or 1 − 1 / (1 + %e^%x)") |
| 649 | TERM (U"##invSigmoid (%x)") |
| 650 | DEFINITION (U"(0,1) \\-> #R: ln (%x / (1 − %x))") |
| 651 | TERM (U"##erf (%x)") |
| 652 | DEFINITION (U"the error function: 2/\\Vr%\\pi __0_\\in^%x exp(-%t^2) %dt") |
| 653 | TERM (U"##erfc (%x)") |
| 654 | DEFINITION (U"the complement of the error function: 1 - erf (%x)") |
| 655 | TERM (U"##randomUniform (%min, %max)") |
| 656 | DEFINITION (U"a uniform random real number between %min (inclusive) and %max (exclusive)") |
| 657 | TERM (U"##randomInteger (%min, %max)") |
| 658 | DEFINITION (U"a uniform random integer number between %min and %max (inclusive)") |
| 659 | TERM (U"##randomGauss (%\\mu, %\\si)") |
| 660 | DEFINITION (U"a Gaussian random real number with mean %\\mu and standard deviation %\\si") |
| 661 | TERM (U"##randomPoisson (%mean)") |
| 662 | DEFINITION (U"a Poisson random real number") |
| 663 | TERM (U"##randomGamma (%%shape%, %%rate%)") |
| 664 | DEFINITION (U"a random number drawn from a Gamma distribution with shape parameter %\\al " |
| 665 | "and rate parameter %\\be, which is defined as " |
| 666 | "%f(%x; %\\al, %\\be) = (1 / \\Ga (%\\al)) %\\be%^^%\\al^ %x^^%\\al−1^ %e^^−%\\be %x^, " |
| 667 | "for %x > 0, %\\al > 0 and %\\be > 0, following the method by @@Marsaglia & Tsang (2000)@") |
| 668 | TERM (U"##random\\_ initializeWithSeedUnsafelyButPredictably (%seed)") |
| 669 | DEFINITION (U"can be used in a script to create a reproducible sequence of random numbers " |
| 670 | "(warning: this exceptional situation will continue to exist throughout Praat until you call the following function)") |
| 671 | TERM (U"##random\\_ initializeSafelyAndUnpredictably ()") |
| 672 | DEFINITION (U"undoes the exceptional situation caused by the previous function") |
| 673 | TERM (U"##lnGamma (%x)") |
| 674 | DEFINITION (U"logarithm of the \\Ga function") |
| 675 | TERM (U"##gaussP (%z)") |
| 676 | DEFINITION (U"the area under the Gaussian distribution between −\\oo and %z") |
| 677 | TERM (U"##gaussQ (%z)") |
| 678 | DEFINITION (U"the area under the Gaussian distribution between %z and +\\oo: " |
| 679 | "the one-tailed \"statistical significance %p\" of a value that is %z standard deviations " |
| 680 | "away from the mean of a Gaussian distribution") |
| 681 | TERM (U"##invGaussQ (%q)") |
| 682 | DEFINITION (U"the value of %z for which $gaussQ (%z) = %q") |
| 683 | TERM (U"##chiSquareP (%`chiSquare`, %`df`)") |
| 684 | DEFINITION (U"the area under the %\\ci^2 distribution between 0 and %chiSquare, for %`df` degrees of freedom") |
| 685 | TERM (U"##chiSquareQ (%`chiSquare`, %`df`)") |
| 686 | DEFINITION (U"the area under the %\\ci^2 distribution between %`chiSquare` and +\\oo, " |
| 687 | "for %df degrees of freedom: the \"statistical significance %p\" " |
| 688 | "of the %\\ci^2 difference between two distributions in %df+1 dimensions") |
| 689 | TERM (U"##invChiSquareQ (%q, %df)") |
| 690 | DEFINITION (U"the value of %\\ci^2 for which $chiSquareQ (%\\ci^2, %df) = %q") |
| 691 | TERM (U"##studentP (%t, %df)") |
| 692 | DEFINITION (U"the area under the student T-distribution from -\\oo to %t") |
| 693 | TERM (U"##studentQ (%t, %df)") |
| 694 | DEFINITION (U"the area under the student T-distribution from %t to +\\oo") |
| 695 | TERM (U"##invStudentQ (%q, %df)") |
| 696 | DEFINITION (U"the value of %t for which $studentQ (%t, %df) = %q") |
| 697 | TERM (U"##fisherP (%f, %df1, %df2)") |
| 698 | DEFINITION (U"the area under Fisher's F-distribution from 0 to %f") |
| 699 | TERM (U"##fisherQ (%f, %df1, %df2)") |
| 700 | DEFINITION (U"the area under Fisher's F-distribution from %f to +\\oo") |
| 701 | TERM (U"##invFisherQ (%q, %df1, %df2)") |
| 702 | DEFINITION (U"the value of %f for which $fisherQ (%f, %df1, %df2) = %q") |
| 703 | TERM (U"##binomialP (%p, %k, %n)") |
| 704 | DEFINITION (U"the probability that in %n experiments, an event with probability %p will occur at most %k times") |
| 705 | TERM (U"@`binomialQ` (%p, %k, %n)") |
| 706 | DEFINITION (U"the probability that in %n experiments, an event with probability %p will occur at least %k times; equals 1 - $binomialP (%p, %k - 1, %n)") |
| 707 | TERM (U"##invBinomialP (%P, %k, %n)") |
| 708 | DEFINITION (U"the value of %p for which $binomialP (%p, %k, %n) = %P") |
| 709 | TERM (U"@`invBinomialQ` (%Q, %k, %n)") |
| 710 | DEFINITION (U"the value of %p for which $binomialQ (%p, %k, %n) = %Q") |
| 711 | TERM (U"##hertzToBark (%x)") |
| 712 | DEFINITION (U"from acoustic frequency to Bark-rate (perceptual spectral frequency; place on basilar membrane): " |
| 713 | "7 ln (%x/650 + \\Vr(1 + (%x/650)^2))") |
| 714 | TERM (U"##barkToHertz (%x)") |
| 715 | DEFINITION (U"650 sinh (%x / 7)") |
| 716 | TERM (U"##hertzToMel (%x)") |
| 717 | DEFINITION (U"from acoustic frequency to perceptual pitch: 550 ln (1 + %x / 550)") |
| 718 | TERM (U"##melToHertz (%x)") |
| 719 | DEFINITION (U"550 (exp (%x / 550) - 1)") |
| 720 | TERM (U"##hertzToSemitones (%x)") |
| 721 | DEFINITION (U"from acoustic frequency to a logarithmic musical scale, relative to 100 Hz: 12 ln (%x / 100) / ln 2") |
| 722 | TERM (U"##semitonesToHertz (%x)") |
| 723 | DEFINITION (U"100 exp (%x ln 2 / 12)") |
| 724 | TERM (U"##erb (%f)") |
| 725 | DEFINITION (U"the perceptual %%equivalent rectangular bandwidth% (ERB) in hertz, for a specified acoustic frequency (also in hertz): " |
| 726 | "6.23\\.c10^^-6^ %f^2 + 0.09339 %f + 28.52") |
| 727 | TERM (U"##hertzToErb (%x)") |
| 728 | DEFINITION (U"from acoustic frequency to ERB-rate: 11.17 ln ((%x + 312) / (%x + 14680)) + 43") |
| 729 | TERM (U"##erbToHertz (%x)") |
| 730 | DEFINITION (U"(14680 %d - 312) / (1 - %d) where %d = exp ((%x - 43) / 11.17)") |
| 731 | TERM (U"@`phonToDifferenceLimens` (%x)") |
| 732 | DEFINITION (U"from perceptual loudness (intensity sensation) level in phon, to the number of intensity " |
| 733 | "difference limens above threshold: 30 · ((61/60)^^ %x^ − 1).") |
| 734 | TERM (U"@`differenceLimensToPhon` (%x)") |
| 735 | DEFINITION (U"the inverse of the previous: ln (1 + %x / 30) / ln (61 / 60).") |
| 736 | TERM (U"##beta (%x, %y)") |
| 737 | TERM (U"##besselI (%n, %x)") |
| 738 | TERM (U"##besselK (%n, %x)") |
| 739 | NORMAL (U"For functions with arrays, see @@Scripting 5.7. Vectors and matrices@.") |
| 740 | MAN_END |
| 741 | |
| 742 | MAN_BEGIN (U"Formulas 6. String functions", U"ppgb", 20180825) |
| 743 | INTRO (U"String functions are functions that either return a text string or have at least one text string as an argument. " |
| 744 | "Since string computations are not very useful in the @calculator, in settings windows, or in creation and " |
| 745 | "modification formulas, this page only gives examples of strings in scripts, so that the example may contain " |
| 746 | "string variables.") |
| 747 | /*@praat |
| 748 | string$ = "hallo" |
| 749 | length = length (string$ + "dag") |
| 750 | assert length = 8 |
| 751 | @*/ |
| 752 | TERM (U"#`length (a$)") |
| 753 | DEFINITION (U"gives the length of the string. After") |
| 754 | CODE2 (U"string$ = \"hallo\"") |
| 755 | CODE2 (U"length = length (string$ + \"dag\")") |
| 756 | DEFINITION (U"the variable %`length` contains the number 8 (by the way, from this example " |
| 757 | "you see that variables can have the same names as functions, without any danger of confusing the interpreter).") |
| 758 | /*@praat |
| 759 | head$ = left$ ("hallo", 3) |
| 760 | assert head$ = "hal" |
| 761 | @*/ |
| 762 | TERM (U"#`left$ (a$, n)") |
| 763 | DEFINITION (U"gives a string consisting of the first %n characters of %`a$`. After") |
| 764 | CODE2 (U"head$ = left$ (\"hallo\", 3)") |
| 765 | DEFINITION (U"the variable %`head$` contains the string \"hal\".") |
| 766 | /*@praat |
| 767 | english$ = "he" + right$ ("hallo", 3) |
| 768 | assert english$ = "hello" |
| 769 | @*/ |
| 770 | TERM (U"#`right$ (a$, n)") |
| 771 | DEFINITION (U"gives a string consisting of the last %n characters of %`a$`. After") |
| 772 | CODE2 (U"english$ = \"he\" + right$ (\"hallo\", 3)") |
| 773 | DEFINITION (U"the variable %`english$` contains the string \"hello\".") |
| 774 | /*@praat |
| 775 | assert mid$ ("hello", 3, 2) = "ll" |
| 776 | @*/ |
| 777 | TERM (U"#`mid$ (\"hello\" , 3, 2)") |
| 778 | DEFINITION (U"gives a string consisting of 2 characters from “hello”, starting at the third character. Outcome: ll.") |
| 779 | /*@praat |
| 780 | where = index ("hallo allemaal", "al") |
| 781 | assert where = 2 |
| 782 | assert index ("hallo allemaal", "fhjgfhj") = 0 |
| 783 | @*/ |
| 784 | TERM (U"#`index (a$, b$)") |
| 785 | DEFINITION (U"gives the index of the first occurrence of the string %`b$` in the string %`a$`. After") |
| 786 | CODE2 (U"where = index (\"hallo allemaal\", \"al\")") |
| 787 | DEFINITION (U"the variable %`where` contains the number 2, because the first “al” starts at the second character of the longer string. " |
| 788 | "If the first string does not contain the second string, %`index` returns 0.") |
| 789 | /*@praat |
| 790 | where = rindex ("hallo allemaal", "al") |
| 791 | assert where = 13 |
| 792 | assert rindex ("hallo allemaal", "fhjgfhj") = 0 |
| 793 | @*/ |
| 794 | TERM (U"#`rindex (a$, b$)") |
| 795 | DEFINITION (U"gives the index of the last occurrence of the string %`b$` in the string %`a$`. After") |
| 796 | CODE2 (U"where = rindex (\"hallo allemaal\", \"al\")") |
| 797 | DEFINITION (U"the variable %`where` contains the number 13, because the last “al” starts at the 13th character. " |
| 798 | "If the first string does not contain the second string, %`rindex` returns 0.") |
| 799 | /*@praat |
| 800 | where = startsWith ("internationalization", "int") |
| 801 | assert where = 1 |
| 802 | @*/ |
| 803 | TERM (U"#`startsWith (a$, b$)") |
| 804 | DEFINITION (U"determines whether the string %`a$` starts with the string %`b$`. After") |
| 805 | CODE2 (U"where = startsWith (\"internationalization\", \"int\")") |
| 806 | DEFINITION (U"the variable %`where` contains the number 1 (true).") |
| 807 | /*@praat |
| 808 | where = endsWith ("internationalization", "nation") |
| 809 | assert where = 0 |
| 810 | @*/ |
| 811 | TERM (U"#`endsWith (a$, b$)") |
| 812 | DEFINITION (U"determines whether the string %`a$` ends with the string %`b$`. After") |
| 813 | CODE2 (U"where = endsWith (\"internationalization\", \"nation\")") |
| 814 | DEFINITION (U"the variable %where contains the number 0 (false).") |
| 815 | TERM (U"#`replace$ (a$, b$, c$, n)") |
| 816 | DEFINITION (U"gives a string that is like %`a$`, but where (at most %n) occurrences of %`b$` are replaced with the string %`c$`. After") |
| 817 | CODE2 (U"s$ = replace$ (\"hello\", \"l\", \"m\", 0)") |
| 818 | DEFINITION (U"the variable %`s$` contains the string \"hemmo\". After") |
| 819 | CODE2 (U"s$ = replace$ (\"hello\", \"l\", \"m\", 1)") |
| 820 | DEFINITION (U"the variable %`s$` contains the string \"hemlo\". The number %n determines the maximum number of occurrences of %`b$` " |
| 821 | "that can be replaced. If %n is 0, all occurrences are replaced.") |
| 822 | TERM (U"#`index_regex (a$, b$)") |
| 823 | DEFINITION (U"determines where the string %`a$` first matches the @@regular expressions|regular expression@ %`b$`. After") |
| 824 | CODE2 (U"where = index_regex (\"internationalization\", \"a.*n\")") |
| 825 | DEFINITION (U"the variable %`where` contains the number 7. If there is no match, the outcome is 0.") |
| 826 | TERM (U"#`rindex_regex (a$, b$)") |
| 827 | DEFINITION (U"determines where the string %`a$` last matches the @@regular expressions|regular expression@ %`b$`. After") |
| 828 | CODE2 (U"where = rindex_regex (\"internationalization\", \"a.*n\")") |
| 829 | DEFINITION (U"the variable %`where` contains the number 16. If there is no match, the outcome is 0.") |
| 830 | TERM (U"#`replace_regex$ (a$, b$, c$, n)") |
| 831 | DEFINITION (U"gives a string that is like %`a$`, but where (at most %n) substrings that match the @@regular expressions|regular expression@ %`b$` " |
| 832 | "are replaced with the expression %`c$`. After") |
| 833 | CODE2 (U"s$ = replace_regex$ (\"hello\", \".\", \"&&\", 0)") |
| 834 | DEFINITION (U"the variable %`s$` contains the string \"hheelllloo\". If there is no match, " |
| 835 | "the outcome is the original string %`a$`. After") |
| 836 | CODE2 (U"s$ = replace_regex$ (\"hello\", \".\", \"&&\", 1)") |
| 837 | DEFINITION (U"the variable %`s$` contains the string \"hhello\". The number %n determines the maximum number of text pieces " |
| 838 | "that can be replaced. If %n is 0, all matching text pieces are replaced.") |
| 839 | TERM (U"#`string$ (number)") |
| 840 | DEFINITION (U"formats a number as a string. Thus, `string$ (5e6)` " |
| 841 | "becomes the string `5000000`, and `string$ (56%)` becomes the string `0.56`.") |
| 842 | TERM (U"#`fixed$ (number, precision)") |
| 843 | DEFINITION (U"formats a number as a string with %precision digits after the decimal point. Thus, `fixed$ (72.65687, 3)` " |
| 844 | "becomes the string `72.657`, and `fixed$ (72.65001, 3)` becomes the string `72.650`. " |
| 845 | "In these examples, we see that the result can be rounded up and that trailing zeroes are kept. " |
| 846 | "At least one digit of precision is always given, e.g. `fixed$ (0.0000157, 3)` becomes the string `0.00002`. " |
| 847 | "The number 0 always becomes the string `0`.") |
| 848 | TERM (U"#`percent$ (number, precision)") |
| 849 | DEFINITION (U"the same as `fixed$` (), but with a percent sign. For instance, `percent$ (0.157, 3)` becomes `15.700%`, " |
| 850 | "`percent$ (0.000157, 3)` becomes `0.016%`, and `percent$ (0.000000157, 3)` becomes `0.00002%`. " |
| 851 | "The number 0 always becomes the string `0`.") |
| 852 | /*@praat |
| 853 | string$ = "5e6" |
| 854 | assert 3 + number (string$) = 5000003 |
| 855 | @*/ |
| 856 | TERM (U"#`number (a$)") |
| 857 | DEFINITION (U"interprets a string as a number. After") |
| 858 | CODE2 (U"string$ = \"5e6\"") |
| 859 | CODE2 (U"writeInfoLine: 3 + number (string$)") |
| 860 | DEFINITION (U"the Info window contains the number 5000003.") |
| 861 | TERM (U"#`date$ ( )") |
| 862 | DEFINITION (U"gives the date and time in the following format:") |
| 863 | CODE2 (U"Mon Jun 24 17:11:21 2002") |
| 864 | DEFINITION (U"To write the day of the month into the Info window, you type:") |
| 865 | CODE2 (U"date$ = date$ ()") |
| 866 | CODE2 (U"day$ = mid$ (date$, 9, 2)") |
| 867 | CODE2 (U"writeInfoLine: \"The month day is \", day$, \".\"") |
| 868 | TERM (U"#`unicode$ (228)") |
| 869 | DEFINITION (U"gives the 228th Unicode codepoint, i.e. \"ä\".") |
| 870 | TERM (U"#`unicode (\"ä\")") |
| 871 | DEFINITION (U"gives the Unicode codepoint number of \"ä\", i.e. 228.") |
| 872 | TERM (U"#`extractNumber (\"Type: Sound\" + newline$ + \"Name: hello there\" + newline$ + \"Size: 44007\", \"Size:\")") |
| 873 | DEFINITION (U"looks for a number after the first occurrence of “Size:” in the long string. Outcome: 44007. " |
| 874 | "This is useful in scripts that try to get information from long reports, as the following script that " |
| 875 | "runs in the Sound editor window:") |
| 876 | CODE2 (U"report$ = Editor info") |
| 877 | CODE2 (U"maximumFrequency = extractNumber (report$, \"Spectrogram window length:\")") |
| 878 | TERM (U"#`extractWord$ (\"Type: Sound\" + newline$ + \"Name: hello there\" + newline$ + \"Size: 44007\", \"Type:\")") |
| 879 | DEFINITION (U"looks for a word without spaces after the first occurrence of \"Type:\" in the long string. Outcome: Sound.") |
| 880 | TERM (U"#`extractLine$ (\"Type: Sound\" + newline$ + \"Name: hello there\" + newline$ + \"Size: 44007\", \"Name: \")") |
| 881 | DEFINITION (U"looks for the rest of the line (including spaces) after the first occurrence of “Name: ” in the long string. " |
| 882 | "Outcome: hello there. Note how “Name: ” includes a space, so that the “rest of the line” starts with the %h.") |
| 883 | TERM (U"#`backslashTrigraphsToUnicode$ (x$), unicodeToBackslashTrigraphs$ (x$)") |
| 884 | DEFINITION (U"converts e.g. \\bsct to \\ct or the reverse. See @@Special symbols@.") |
| 885 | MAN_END |
| 886 | |
| 887 | MAN_BEGIN (U"Formulas 7. Control structures", U"ppgb", 20031228) |
| 888 | ENTRY (U"if ... then ... else ... fi") |
| 889 | NORMAL (U"You can use conditional expressions in all formulas. For example, ") |
| 890 | CODE (U"3 * if 52% * 3908 > 2000 then 5 else 6 fi") |
| 891 | NORMAL (U"evaluates to 15. Instead of #`fi`, you can also use #`endif`.") |
| 892 | NORMAL (U"Another example: you can clip the absolute amplitude of a Sound to 0.5 by supplying the following formula:") |
| 893 | CODE (U"if abs(self)>0.5 then if self>0 then 0.5 else -0.5 fi else self fi") |
| 894 | ENTRY (U"The semicolon") |
| 895 | NORMAL (U"The semicolon ends the evaluation of the formula. This can be convenient " |
| 896 | "if you do not want to overwrite a long formula in your text field: the formula") |
| 897 | CODE (U"800;sqrt(2)*sin(2*pi*103*0.5)+10^(-40/20)*randomGauss(0,1)") |
| 898 | NORMAL (U"evaluates to 800.") |
| 899 | MAN_END |
| 900 | |
| 901 | MAN_BEGIN (U"Formulas 8. Attributes of objects", U"ppgb", 20221202) |
| 902 | NORMAL (U"You can refer to several attributes of objects that are visible in the @@List of Objects@. " |
| 903 | "To do so, use either the unique ID of the object, or the type and the name of the object. " |
| 904 | "Thus, `object[113]` refers to the object that has the number 113 in the list, " |
| 905 | "and `object[\"Sound hallo\"]` refers to an existing Sound object whose name is “hallo” " |
| 906 | "(if there is more than one such object, it refers to the one that was created last).") |
| 907 | NORMAL (U"To refer to an attribute, you use the period (\".\"). " |
| 908 | "Thus, `object[\"Sound hallo\"].nx` is the number of samples of the Sound called `hallo`, and " |
| 909 | "`1/object[\"Sound hallo\"].dx` is its sampling frequency.") |
| 910 | ENTRY (U"Attributes in the calculator") |
| 911 | NORMAL (U"Record a Sound (read the @Intro if you do not know how to do that), " |
| 912 | "and name it `mysound` (or anything else). An object with a name like `3. Sound mysound` " |
| 913 | "will appear in the list. Then type into the @calculator the formula") |
| 914 | CODE (U"object[3].nx") |
| 915 | NORMAL (U"or") |
| 916 | CODE (U"object[\"Sound mysound\"].nx") |
| 917 | NORMAL (U"After you click OK, the Info window will show the number of samples. Since you could have got this result " |
| 918 | "by simply selecting the object and choosing ##Get number of samples# from the @@Query submenu@, " |
| 919 | "these attribute tricks are not very useful in the calculator. " |
| 920 | "We will see that they are much more useful in creation and modification formulas and in scripts.") |
| 921 | ENTRY (U"List of possible attributes") |
| 922 | NORMAL (U"The following attributes are available:") |
| 923 | TERM (U"#`xmin`") |
| 924 | DEFINITION (U"the start of the time domain (usually 0) for a @Sound, @Pitch, @Formant, " |
| 925 | "@Spectrogram, @Intensity, @Cochleagram, @PointProcess, or @Harmonicity object, in seconds; " |
| 926 | "the lowest frequency (always 0) for a @Spectrum object, in hertz; " |
| 927 | "the lowest frequency (usually 0) for an @Excitation object, in Bark; " |
| 928 | "the left edge of the %x domain for a @Matrix object.") |
| 929 | TERM (U"#`xmax`") |
| 930 | DEFINITION (U"the end of the time domain (usually the duration, if %xmin is zero) for a Sound, Pitch, Formant, " |
| 931 | "Spectrogram, Intensity, Cochleagram, PointProcess, or Harmonicity object, in seconds; " |
| 932 | "the highest frequency (@@Nyquist frequency@) for a Spectrum object, e.g. 11025 hertz; " |
| 933 | "the highest frequency for an Excitation object, often 25.6 Bark; " |
| 934 | "the right edge of the %x domain for a Matrix object.") |
| 935 | TERM (U"#`ncol`") |
| 936 | DEFINITION (U"the number of columns in a @Matrix, @TableOfReal, or @Table object.") |
| 937 | TERM (U"#`nrow`") |
| 938 | DEFINITION (U"the number of rows in a @Matrix, @TableOfReal, or @Table object.") |
| 939 | TERM (U"#`col$` [%`i`]") |
| 940 | DEFINITION (U"the name of column %`i` in a @TableOfReal or @Table object.") |
| 941 | TERM (U"#`row$` [%`i`]") |
| 942 | DEFINITION (U"the name of row %`i` in a @TableOfReal object.") |
| 943 | TERM (U"#`nx`") |
| 944 | DEFINITION (U"the number of samples in a Sound object; " |
| 945 | "the number of analysis frames in a Pitch, Formant, Spectrogram, Intensity, Cochleagram, or Harmonicity object; " |
| 946 | "the number of frequency bins in a Spectrum or Excitation object; " |
| 947 | "the number of divisions of the %x domain for a Matrix object (= %`ncol`).") |
| 948 | TERM (U"#`dx`") |
| 949 | DEFINITION (U"the sample period (time distance between consecutive samples) in a Sound object (the inverse of the sampling frequency), in seconds; " |
| 950 | "the time step between consecutive frames in a Pitch, Formant, Spectrogram, Intensity, Cochleagram, or Harmonicity object, in seconds; " |
| 951 | "the width of a frequency bin in a Spectrum object, in hertz; " |
| 952 | "the width of a frequency bin in an Excitation object, in Bark; " |
| 953 | "the horizontal distance between cells in a Matrix object.") |
| 954 | TERM (U"#`ymin`") |
| 955 | DEFINITION (U"the lowest frequency (usually 0) for a Spectrogram object, in hertz; " |
| 956 | "the lowest frequency (usually 0) for a Cochleagram object, in Bark; " |
| 957 | "the bottom of the %y domain for a Matrix object.") |
| 958 | TERM (U"#`ymax`") |
| 959 | DEFINITION (U"the highest frequency for a Spectrogram object, e.g. 5000 hertz; " |
| 960 | "the highest frequency for a Cochleagram object, often 25.6 Bark; " |
| 961 | "the top of the %y domain for a Matrix object.") |
| 962 | TERM (U"#`ny`") |
| 963 | DEFINITION (U"the number of frequency bands in a Spectrogram or Cochleagram object; " |
| 964 | "for a Spectrum object: always 2 (first row is real part, second row is imaginary part) " |
| 965 | "the number of divisions of the %y domain for a Matrix object (= %`nrow`).") |
| 966 | TERM (U"#`dy`") |
| 967 | DEFINITION (U"the distance between adjacent frequency bands in a Spectrogram object, in hertz; " |
| 968 | "the distance between adjacent frequency bands in a Cochleagram object, in Bark; " |
| 969 | "the vertical distance between cells in a Matrix object.") |
| 970 | ENTRY (U"Attributes in a creation formula") |
| 971 | NORMAL (U"In formulas for creating a new object, you can refer to the attributes of any object, " |
| 972 | "but you will often want to refer to the attributes of the object that is just being created. You can do that in two ways.") |
| 973 | NORMAL (U"The first way is to use the name of the object, as above. Choose @@Create Sound from formula...@, supply %hello for its name, " |
| 974 | "supply arbitrary values for the starting and finishing time, and type the following formula:") |
| 975 | CODE (U"(x - object[\"Sound hello\"].xmin) / (object[\"Sound hello\"].xmax - object[\"Sound hello\"].xmin)") |
| 976 | NORMAL (U"When you edit this sound, you can see that it creates a straight line that rises from 0 to 1 within the time domain.") |
| 977 | NORMAL (U"The formula above will also work if the Sound under creation is called %goodbye, and a Sound called %hello already exists; " |
| 978 | "of course, in such a case `object[\"Sound hello\"].xmax` refers to a property of the already existing sound.") |
| 979 | NORMAL (U"If a formula refers to an object under creation, there is a shorter way: you do not have to supply the name of the object at all, " |
| 980 | "so you can simply write") |
| 981 | CODE (U"(x - xmin) / (xmax - xmin)") |
| 982 | NORMAL (U"The attributes that you can use in this implicit way are %xmin, %xmax, %ncol, %nrow, %nx, %dx, %ny, and %dy. " |
| 983 | "To disambiguate in case there exists a script variable %xmin as well " |
| 984 | "(Praat will complain if this is the case), you can write `Self.xmin`.") |
| 985 | ENTRY (U"Attributes in a modification formula") |
| 986 | NORMAL (U"In formulas for modifying an existing object, you refer to attributes in the same way as in creation formulas, " |
| 987 | "i.e., you do not have to specify the name of the object that is being modified. The formula") |
| 988 | CODE (U"self * 20 ^ (- (x - xmin) / (xmax - xmin))") |
| 989 | NORMAL (U"causes the sound to decay exponentially in such a way that it has only 5 percent of its initial amplitude at the end. " |
| 990 | "If you apply this formula to multiple Sound objects at the same time, $xmax will refer to the finishing time of each Sound separately " |
| 991 | "as that Sound is modified.") |
| 992 | NORMAL (U"More examples of the use of attributes are on the next page.") |
| 993 | MAN_END |
| 994 | |
| 995 | MAN_BEGIN (U"Formulas 9. Data in objects", U"ppgb", 20170614) |
| 996 | NORMAL (U"With square brackets, you can get the values inside some objects.") |
| 997 | ENTRY (U"Object contents in the calculator") |
| 998 | NORMAL (U"The outcomes of the following examples can be checked with the @calculator.") |
| 999 | TERM (U"##object [%%objectName\\$ or id%, %rowNumber, %columnNumber]") |
| 1000 | TERM (U"$$object [\"Matrix hello\", 10, 3]") |
| 1001 | DEFINITION (U"gives the value in the cell at the third column of the 10th row of the Matrix called %hello.") |
| 1002 | TERM (U"$$object [5, 10, 3]") |
| 1003 | DEFINITION (U"gives the value in the cell at the third column of the 10th row of the Matrix whose unique ID is 5 " |
| 1004 | "(i.e. that is labelled with the number 5 in the list of objects).") |
| 1005 | TERM (U"$$object [\"Sound hello\", 0, 10000]") |
| 1006 | DEFINITION (U"gives the value (in Pa) of the 10000th sample of the Sound %hello, averaged over the channels.") |
| 1007 | TERM (U"$$object [23, 1, 10000]") |
| 1008 | DEFINITION (U"gives the value (in Pa) of the 10000th sample of the left channel of the Sound with ID 23.") |
| 1009 | TERM (U"$$object [23, 2, 10000]") |
| 1010 | DEFINITION (U"gives the value (in Pa) of the 10000th sample of the right channel of the Sound with ID 23.") |
| 1011 | TERM (U"$$object [\"TableOfReal tokens\", 5, 12]") |
| 1012 | DEFINITION (U"gives the value in the cell at the fifth row of the 12th column of the TableOfReal called %tokens.") |
| 1013 | TERM (U"$$object [\"TableOfReal tokens\", 5, \"F1\"]") |
| 1014 | DEFINITION (U"gives the value in the cell at the fifth row of the column labelled %F1 of the TableOfReal %tokens.") |
| 1015 | TERM (U"$$object [\"TableOfReal tokens\", \"\\bsct\", \"F1\"]") |
| 1016 | DEFINITION (U"gives the value in the cell at the row labelled %%\\bsct% of column %F1 of the TableOfReal %tokens.") |
| 1017 | TERM (U"$$object [\"Table listeners\", 3, \"m3ae\"]") |
| 1018 | DEFINITION (U"gives the numeric value in the cell at the third row of column %m3ae of the Table %listeners.") |
| 1019 | TERM (U"$$object [\"Table listeners\", 3, 12]") |
| 1020 | DEFINITION (U"gives the numeric value in the cell at the third row of the 12th column of the Table %listeners.") |
| 1021 | TERM (U"$$object\\$ [\"Table results\", 3, \"response\"]") |
| 1022 | DEFINITION (U"gives the string value in the cell at the third row of column %response of the Table %results.") |
| 1023 | TERM (U"$$object\\$ [\"Table results\", 3, 12]") |
| 1024 | DEFINITION (U"gives the string value in the cell at the third row of the 12th column of the Table %results.") |
| 1025 | TERM (U"$$object [\"PitchTier hello\", 8]") |
| 1026 | DEFINITION (U"gives the pitch (in Hertz) of the 8th point in the PitchTier %hello.") |
| 1027 | NORMAL (U"Cells (or samples, or points) outside the objects are considered to contain zeroes.") |
| 1028 | ENTRY (U"Interpolation") |
| 1029 | NORMAL (U"The values inside some objects can be interpolated.") |
| 1030 | TERM (U"$$object (\"Sound hello\", 0.7, 0)") |
| 1031 | DEFINITION (U"gives the value (in Pa) at a time of 0.7 seconds in the Sound %hello, by linear interpolation between " |
| 1032 | "the two samples that are nearest to 0.7 seconds. The channels are averaged.") |
| 1033 | TERM (U"$$object (\"Sound hello\", 0.7, 1)") |
| 1034 | DEFINITION (U"gives the interpolated value (in Pa) at a time of 0.7 seconds in the left channel of the Sound %hello.") |
| 1035 | TERM (U"$$object (\"Sound hello\", 0.7, 2)") |
| 1036 | DEFINITION (U"gives the interpolated value (in Pa) at a time of 0.7 seconds in the right channel of the Sound %hello.") |
| 1037 | TERM (U"$$object (\"Spectrogram hallo\", 0.7, 2500)") |
| 1038 | DEFINITION (U"gives the value at a time of 0.7 seconds and at a frequency of 2500 Hz in the Spectrogram %hallo, " |
| 1039 | "by linear interpolation between the four samples that are nearest to that point.") |
| 1040 | TERM (U"$$object (\"PitchTier hullo\", 0.7)") |
| 1041 | DEFINITION (U"gives the pitch (in Hertz) at a time of 0.7 seconds in the PitchTier %hullo.") |
| 1042 | NORMAL (U"In the interpolation, times outside the time domain of the objects are considered to contain zeroes (this does not apply to PitchTiers and the like, " |
| 1043 | "which undergo @@constant extrapolation@).") |
| 1044 | ENTRY (U"Object contents in a modification formula") |
| 1045 | NORMAL (U"Suppose you want to do the difficult way of reversing the contents of a Sound called `hello` (the easy way is to choose #Reverse " |
| 1046 | "from the @Modify menu). You select this sound, then choose @@Copy...@ to duplicate it to a new Sound, which you name `hello_reverse`. " |
| 1047 | "You select this new Sound and choose ##Formula...# from the @Modify menu. The formula will be") |
| 1048 | CODE (U"object [\"Sound hello\", row, ncol + 1 - col]") |
| 1049 | NORMAL (U"From this example, you see that the indices between [ ] may be formulas themselves, and that you can use implicit attributes like %ncol " |
| 1050 | "and position references like %col (also %row, which here means that the reversal is performed for each channel). " |
| 1051 | "An alternative formula is") |
| 1052 | CODE (U"object (\"Sound hello\", xmax - x, y)") |
| 1053 | NORMAL (U"at least if %xmin is zero. The advantage of the second method is that it also works correctly if the two sounds have different sampling frequencies; " |
| 1054 | "the disadvantage is that it may do some interpolation between the samples, which deteriorates the sound quality " |
| 1055 | "(the use of %y here means that the reversal is done for all %y values, i.e. all channels).") |
| 1056 | ENTRY (U"Object contents in a script") |
| 1057 | NORMAL (U"In scripts, the indices between [ ] and the values between ( ) may be formulas themselves and contain variables. " |
| 1058 | "The following script computes the sum of all the cells along the diagonal of a Matrix.") |
| 1059 | CODE (U"matrix = Create simple matrix: 10, 10, \"x*y\"") |
| 1060 | CODE (U"sumDiagonal = 0") |
| 1061 | CODE (U"for i to object[matrix].ncol") |
| 1062 | CODE1 (U"sumDiagonal += object [matrix, i, i]") |
| 1063 | CODE (U"endfor") |
| 1064 | CODE (U"writeInfoLine: \"The sum of the cells along the diagonal is \", sumDiagonal, \".\"") |
| 1065 | NORMAL (U"This example could have been written completely with commands from the dynamic menu:") |
| 1066 | CODE (U"matrix = Create simple matrix: 10, 10, \"x*y\"") |
| 1067 | CODE (U"sumDiagonal = 0") |
| 1068 | CODE (U"ncol = Get number of columns") |
| 1069 | CODE (U"for i to ncol") |
| 1070 | CODE1 (U"value = Get value in cell: i, i") |
| 1071 | CODE1 (U"sumDiagonal += value") |
| 1072 | CODE (U"endfor") |
| 1073 | CODE (U"writeInfoLine: \"The sum of the cells along the diagonal is \", sumDiagonal, \".\"") |
| 1074 | NORMAL (U"The first version, which accesses the contents directly, is not only two lines shorter, but also three times faster.") |
| 1075 | MAN_END |
| 1076 | |
| 1077 | } |
| 1078 | |
| 1079 | /* End of file manual_formulas.cpp */ |