spectrogram: Spectrogram

Description Usage Arguments Value Examples

View source: R/spectrogram.R

Description

Produces the spectrogram of a sound using short-term Fourier transform. This is a simplified version of spectro with fewer plotting options, but with added routines for noise reduction, smoothing in time and frequency domains, and controlling contrast and brightness.

Usage

1
2
3
4
5
6
7
spectrogram(x, samplingRate = NULL, windowLength = 50, step = NULL,
  overlap = 70, wn = "gaussian", zp = 0, smoothFreq = 0,
  smoothTime = 0, qTime = 0, percentNoise = 10, noiseReduction = 0,
  contrast = 0.2, brightness = 0, method = c("spectrum",
  "spectralDerivative")[1], output = c("none", "original", "processed")[1],
  ylim = NULL, plot = TRUE, osc = FALSE, colorTheme = c("bw", "seewave",
  "...")[1], xlab = "", frameBank = NULL, duration = NULL, ...)

Arguments

x

path to a .wav file or a vector of amplitudes with specified samplingRate

samplingRate

sampling rate of x (only needed if x is a numeric vector, rather than a .wav file)

windowLength

length of FFT window, ms

step

you can override overlap by specifying FFT step, ms

overlap

overlap between successive FFT frames, %

wn

window type: gaussian, hanning, hamming, bartlett, rectangular, blackman, flattop

zp

window length after zero padding, points

smoothFreq, smoothTime

length of the window, in data points (0 to +inf), for calculating a rolling median. Applies median smoothing to spectrogram in frequency and time domains, respectively

qTime

the quantile to be subtracted for each frequency bin. For ex., if qTime = 0.5, the median of each frequency bin (over the entire sound duration) will be calculated and subtracted from each frame (see examples)

percentNoise

percentage of frames (0 to 100%) used for calculating noise spectrum

noiseReduction

how much noise to remove (0 to +inf, recommended 0 to 2). 0 = no noise reduction, 2 = strong noise reduction: spectrum - (noiseReduction * noiseSpectrum), where noiseSpectrum is the average spectrum of frames with entropy exceeding the quantile set by percentNoise

contrast

spectrum is exponentiated by contrast (-inf to +inf, recommended -1 to +1). Contrast >0 increases sharpness, <0 decreases harpness

brightness

how much to "lighten" the image (>0 = lighter, <0 = darker)

method

plot spectrum ('spectrum') or the first time derivative of the spectrum ('spectralDerivative')

output

specifies what to return: nothing ('none'), unmodified spectrogram ('original'), or denoised and/or smoothed spectrogram ('processed')

ylim

frequency range to plot, kHz (defaults to 0 to Nyquist frequency)

plot

should a spectrogram be plotted? TRUE / FALSE

osc

should an oscillogram be shown under the spectrogram? TRUE / FALSE

colorTheme

black and white ('bw'), as in seewave package ('seewave'), or another color theme (e.g. 'heat.colors')

xlab

label for x-axis

frameBank

ignore (only needed for pitch tracking)

duration

ignore (only needed for pitch tracking)

...

other graphical parameters passed to seewave:::filled.contour.modif2

Value

Returns nothing (if output = 'none'), raw spectrum (if output = 'original'), denoised and/or smoothed spectrum (if output = 'processed'), or spectral derivatives (if method = 'spectralDerivative') as a matrix of real numbers.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
## Not run: 
# synthesize a sound 1 s long, with gradually increasing hissing noise
sound = soundgen(sylLen = 1000, temperature = 0, noiseAnchors = list(
  time = c(0, 1300), value = c(-120, 0)), formantsNoise = list(
  f1 = list(time = 0, freq = 5000, width = 10000, amp = 0)))
# playme(sound, samplingRate = 16000)

# basic spectrogram
spectrogram(sound, samplingRate = 16000)
# add an oscillogram
spectrogram(sound, samplingRate = 16000, osc = TRUE)
# broad-band instead of narrow-band
spectrogram(sound, samplingRate = 16000, windowLength = 5)
# spectral derivatives
spectrogram(sound, samplingRate = 16000, method = 'spectralDerivative')

# focus only on values in the upper 5% for each frequency bin
spectrogram(sound, samplingRate = 16000, qTime = 0.95)

# detect 10% of the noisiest frames based on entropy and remove the pattern
# found in those frames (in this cases, breathing)
spectrogram(sound, samplingRate = 16000,  noiseReduction = 1.1,
  brightness = -2)  # white noise almost gone

# apply median smoothing in both time and frequency domains
spectrogram(sound, samplingRate = 16000, smoothFreq = 5,
  smoothTime = 5)

# increase contrast, reduce brightness
spectrogram(sound, samplingRate = 16000, contrast = 1, brightness = -1)

# add bells and whistles
spectrogram(sound, samplingRate = 16000, osc = TRUE, noiseReduction = 1.1,
  brightness = -1, colorTheme = 'heat.colors', xlab = 'Time, ms',
  ylab = 'Frequency, kHz', ylim = c(0,5))

## End(Not run)

tatters/soundgen_beta documentation built on May 14, 2019, 9 a.m.