Learn R Programming

phonTools (version 0.2-0)

spectralslice: Spectral Slice

Description

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

Usage

spectralslice (sound, padding = length(sound) * 2, fs = 22050,  
show = TRUE, color = 1, add = FALSE, xlim, ylim, window = "kaiser",	
windowparameter = 3, zeromax = TRUE, preemphasisf = 0, type, xlab, pwr = FALSE, 
line = FALSE,...)

Arguments

sound
A vector representing a sound wave, or a 'sound' object.
padding
The amount of zero-padding desired for the analysis.
fs
The sampling frequency of the sound. If a 'sound' object is passed this does not need to be specified.
show
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/magnitude) range to plot.
window
The window to be applied to the signal, applied by windowfunc(), provided in this package.
windowparameter
The parameter for the window to be applied to the signal, if appropriate. Passed to windowfunc().
zeromax
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.
preemphasisf
Preemphasis of 3 dB per octave is added to frequencies above the specified frequency.
xlab
the x-axis label may be specified if desired.
pwr
If TRUE, a power spectrum is calculated, if FALSE, a magnitude spectrum is calculated.
line
If TRUE, a line spectrum is created.
...
any additional arguments will be passed to the internal calls of 'plot' or 'lines'.

Value

  • A dataframe with the following elements is returned:
  • hzThe center frequency of the analysis bins.
  • dBThe dB magnitude/power for the analysis bin.

Details

This function can be used to calculate and plot short-time discrete spectra for sections of signals.

Examples

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

spectralslice (sound$sound[11000:11200], padding = 0, xlim = c(0,5000), 
               main = 'few points, no padding')
spectralslice (sound$sound[11000:11600], padding = 0, xlim = c(0,5000), 
               main = 'many points, no padding')

spectralslice (sound$sound[11000:11200], padding = 5000, xlim = c(0,5000), 
               main = 'few points, with padding')
spectralslice (sound$sound[11000:11600], padding = 5000, xlim = c(0,5000), 
               main = 'many points, with padding')

## line spectrum
spectralslice (sound$sound[11000:11100], padding = 0, line = TRUE, xlim = c(0,5000))

Run the code above in your browser using DataLab