Formulas 1.7. Formulas for modification

Analogously to the formulas that you can use for creating new objects (see the previous page), you can use formulas for modifying existing objects. You do this with the command Formula... that you can find in the Modify menu when you select an object.

## Modifying a Sound with a formula

Record a sound with your microphone and talk very lowly. If you don't know how to record a sound in Praat, consult the Intro. Once the Sound object is in the list, click Play. The result will sound very soft. Then choose Formula... from the Modify menu and type

`self * 3
`

Click OK, then click Play again. The sound is much louder now. You have multiplied the amplitude of every sample in the sound with a factor of 3.

## Replacing the contents of a Sound with a formula

If you don’t use `self` in your formula, the formula does not refer to the existing contents of the Sound. Hence, the formula

`1/2 * sin (2 * pi * 377 * x)
`

will simply replace your recorded speech with a 377-Hz sine wave.

## Modifying a Matrix with a formula

Many objects can be thought of as matrices: they consist of a number of rows and columns with data in every cell:

  Sound: one row; columns represent samples.
  Spectrum: two rows (first row is real part, second row is imaginary part); columns represent frequencies.
  Spectrogram, Cochleagram: rows represent frequencies; columns represent times.
  Excitation: one row; columns represent frequency bands.
  Harmonicity: one row; columns represent time frames.

The formula is performed on every column of every row. The formula

`self^2
`

will square all matrix elements.

The formula first works on the first row, and in that row from the first column on; this can work recursively. The formula

`self + self [row, col - 1]
`

integrates each row.

## Referring to the current position in the object

You can refer to the current position in a Matrix (or Sound, etc.) by index or by x and y values:

`row`
  the current row
`col`
  the current column
`x`
  the x value associated with the current column:
  for a Sound, Spectrogram, Cochleagram, or Harmonicity: time, as in the 377-Hz sine wave example above
  for a Spectrum: frequency (Hz)
  for an Excitation: frequency (Bark)
`y`
  the y value associated with the current row:
  for a Spectrogram: frequency (Hz)
  for a Cochleagram: frequency (Bark)

## Referring to the contents of the object itself

You can refer to values in the current Matrix (or Sound, etc.) by index.

`self`
  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.
`self` [`column-expression`]
  refers to the value in the current Sound (or Intensity etc.) at the current sample (or frame). The `column-expression` is rounded to the nearest integer. If the index is out of range (less than 1 or greater than nx), the expression evaluates as 0.

Example. An integrator is

`self [col - 1] + self * dx
`

`self` [`row-expression`, `column-expression`]
  refers to the value in the current Matrix (or Spectrogram etc.) at the specified row and column. The expressions are rounded to the nearest integers.

You can refer to values in the current Matrix (or Spectrogram, etc.) by x and y position:

`self` (`x-expression`, `y-expression`)
  the expressions are linearly interpolated between the four nearest matrix points.

You can refer to values in the current Sound (or Intensity etc.) by x position:

`self` (`x-expression`)
  the expression is linearly interpolated between the two nearest samples (or frames).

### Links to this page

- `col`
- `row`
- `x`
- `y`
- Formulas
- Formulas 1. My first formulas
- Formulas 1.8. Formulas in scripts