Learn R Programming

phonTools (version 0.1-2)

spectralslice: Plot the Spectrum of a Vector

Description

A function to plot the power spectrum of a vector representing a sound wave.

Usage

spectralslice (sound, padding = length(sound)*4, fs = 22050, output = FALSE, 
show = TRUE, color = 1, add = FALSE, xlim, ylim, window = 'kaiser', 
windowparameter = 4, zeromax = TRUE, preemphasis = 50000, type, ...)

Arguments

sound
a vector representing a sequence of samples taken at equal time intervals. Each sample should indicate the amplitude of the wave at that time interval.
padding
the amount of zero-padding desired for the analysis.
fs
the sampling frequency of the sound.
output
boolean. If TRUE, the frequency of bin centers and power at those centers is returned.
show
boolean. If FALSE, no plot is created.
color
set the color used for plotting the spectrum.
add
if TRUE, the spectrum is added to an existing plot. If FALSE a new one is created.
xlim
the range of frequencies to be plotted. By default the function will plot from 0 Hz to fs/2 Hz.
ylim
The y axis (power) range to plot.
window
the window to be applied to the signal, applied by the windowfunc function in this package.
windowparameter
the parameter for the window to be applied to the signal, if appropriate.
zeromax
boolean. If TRUE, the maximum dB is set to 0.
type
the line type to be used for plotting, passes its value to the 'lty' parameter.
preemphasis
preemphasis of 6 dB per octave is added to frequencies above the specified frequency. For no preemphasis, set to a frequency higher than the samplling frequency.
...
any additional arguments will be passed to the internal calls of 'plot' or 'lines'.

Value

  • If output is set to TRUE, a dataframe with the following elements is returned:
  • hzthe center frequency of the analysis bins.
  • ampthe dB amplitude for the analysis bin.

Details

This function can be used to create spectral slices.

Examples

Run this code
data (sound)
par (mfrow = c(2,2))

spectralslice (sound$sound[1:100], padding = 0, xlim = c(0,5000), 
          main = 'few points, no padding')
spectralslice (sound$sound[1:600], padding = 0, xlim = c(0,5000), 
          main = 'many points, no padding')

spectralslice (sound$sound[1:100], padding = 5000, xlim = c(0,5000), 
          main = 'few points, with padding')
spectralslice (sound$sound[1:600], padding = 50000, xlim = c(0,5000), 
          main = 'many points, with padding')

Run the code above in your browser using DataLab