spectrogram: make a spectrogram from a file in WAV format

Description Usage Arguments Value Author(s) See Also Examples

View source: R/spectrogram.R

Description

computes a spectrogram with Hamming windows and user-defined raster resolution

Usage

1
spectrogram(path, time.res = 1/1000, freq.res = 100)

Arguments

path

the location of the sound file

time.res

the desired time resolution of the spectrogram

freq.res

the desired frequency resolution of the spectrogram

Value

an object of class "spg", which is a matrix with the following additional attributes:

time

a vector of time values for each row

frequency

a vector of frequency mid-points for each column

RMS

the root-mean-square amplitude of each row

envelope

a matrix of highest and lowest amplitudes for each time step. useful for plotting waveforms

Author(s)

Benjamin N. Taft [email protected]

See Also

hamming get.freq.res get.time.res sample.rate.spg standard.spg.image

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
##---- Should be DIRECTLY executable !! ----
##-- ==>  Define data, use random,
##--	or do  help(data=index)  for the standard data sets.

## The function is currently defined as
function (path, time.res = 1/1000, freq.res = 100) 
{
    require(tuneR)
    wav <- readWave(path)
    sample.rate <- attr(wav, "samp.rate")
    W <- 2^ceiling(log(sample.rate, 2) - log(freq.res, 2) - 1)
    span <- -(W - 1):W
    S <- floor(time.res * sample.rate)
    wav <- as.numeric(attr(wav, "left"))
    t.max <- length(wav)
    N <- 1 + floor(t.max/S)
    spg <- matrix(NA, N, W)
    envelope <- matrix(NA, N, 2)
    rms <- numeric(N)
    H <- hamming(2 * W)
    for (i in 1:N) {
        ix <- S * (i - 1) + span
        ix[ix < 1 | ix > t.max] <- NA
        envelope[i, ] <- range(wav[ix], na.rm = T)
        win <- H * wav[ix]
        win[is.na(win)] <- 0
        rms[i] <- sqrt(mean(win^2))
        spg[i, ] <- log(spk(win), 10)
    }
    attributes(spg) <- c(attributes(spg), list(time = 0:(N - 
        1) * S/sample.rate, frequency = sample.rate * 0:(W - 
        1)/(2 * W), RMS = rms, envelope = envelope))
    class(spg) <- c("spg", class(spg))
    invisible(spg)
  }

landmarkacoustics/SoundPoints-R documentation built on Sept. 26, 2018, 9:08 a.m.