Arithmetic Calculations on Spectral Data

The function fcalc enables arithmetic calculations to be performed on spectral data returned from muspec. The function takes an obligatory argument, which is a matrix of spectral data (output from muspec). The argument fun controls which function is to be applied to the data. By default the function is applied to each row of the data, but if byrow=F it is applied to each column. fcalc is analogous to the Splus primitive apply, the function you supply must take a numeric vector as its first argument and return either a single numer or a vector. As with apply, you can supply further arguments to the function as the last arguments to fplot. The difference between this function and apply is that this function converts all dB values out of the log domain; then performs the operations (e.g. mean) in the linear domain, then converts the result back into the log domain (dB). Some examples of possible functions are summarised in the following table: \begin{tabular}{ll} Value of & fun & Effect \hline sum & sum the values (this is the default) mean & calculate the mean values var & calculate the variance median & calculate the median value max & calculate the maximum value \end{tabular}

In the first set of examples, fcalc is used to compute functions on the rows of data. The following instruction will compute an averaged spectrum, across all the rows of mvals$spec:

meanvals _ fcalc(mvals$spec, byrow=F, fun=mean)

This can then be plotted using fplot:

fplot(meanvals)

The default behaviour (without byrow=F) of fcalc function is applied to each row. For example, in the following instruction:

vals _ fcalc(mvals$spec, fun=sum)
vals
 [1]  18.928696  16.730773  19.618509  17.603053  19.496954  -8.309321
 [7]  23.558464  17.647078  -1.135109  18.898600   9.785858  16.360845
[13]  13.306643  14.725367  15.129053  18.218959  -2.996481  21.129058
[19]  19.907438  -2.079232  17.212338

Note that in this case, there are 21 values, which represent summed energy values corresponding to each of the separate rows of mvals$spec (and therefore to the separate rows of segs from which mvals$spec was derived).

The arguments low and high allow multiple calculations to be made on frequency data. In the following example, the variance of the energy in the 0-2000 Hz range is calculated using the low and high arguments:

vals _ fcalc(mvals$spec, fun=var, low=0, high=2000)

Frequency calculations can be made in a number of frequency ranges by passing a vector to the low and high arguments. For example, to calculate the average spectral energy in the 0-1 kHz and 4-7 kHz ranges:

vals _ fcalc(mvals$spec, fun=mean, low=c(0, 4000), high=c(1000, 7000))

In this case, the return value (vals) is a two-columned matrix: column 1 contains the averaged values, per segment, in the 0-1000 Hz range; column 2 contains the averaged values in the 4-7 kHz ranges.