Learn R Programming

seewave (version 2.1.8)

corspec: Cross-correlation between two frequency spectra

Description

This function tests the similarity between two frequency spectra by returning their maximal correlation and the frequency shift related to it.

Usage

corspec(spec1, spec2, f = NULL, mel = FALSE, plot = TRUE, plotval = TRUE,
method = "spearman", col = "black", colval = "red",
cexval = 1, fontval = 1, xlab = NULL,
ylab = "Coefficient of correlation (r)", type="l",...)

Arguments

spec1

a first data set resulting of a spectral analysis obtained with spec or meanspec (not in dB). This can be either a two-column matrix (col1 = frequency, col2 = amplitude) or a vector (amplitude).

spec2

a first data set resulting of a spectral analysis obtained with spec or meanspec (not in dB). This can be either a two-column matrix (col1 = frequency, col2 = amplitude) or a vector (amplitude).

f

sampling frequency of waves used to obtain spec1 and spec2 (in Hz). Not necessary if spec1 and/or spec2 is a two columns matrix obtained with spec or meanspec.

mel

a logical, if TRUE the (htk-)mel scale is used.

plot

logical, if TRUE plots r values against frequency shift (by default TRUE).

plotval

logical, if TRUE adds to the plot maximum r value and frequency offset (by default TRUE).

method

a character string indicating which correlation coefficient is to be computed ("pearson", "spearman", or "kendall") (see cor).

col

colour of r values.

colval

colour of r max and frequency offset values.

cexval

character size of r max and frequency offset values.

fontval

font of r max and frequency offset values.

xlab

title of the frequency axis.

ylab

title of the r axis.

type

if plot is TRUE, type of plot that should be drawn. See plot for details (by default "l" for lines).

other plot graphical parameters.

Value

If plot is FALSE, corspec returns a list containing four components:

r

a two-column matrix, the first colum corresponding to the frequency shift (frequency x-axis) and the second column corresponding to the successive r correlation values between spec1 and spec2 (correlation y-axis).

rmax

the maximum correlation value between spec1 and spec2.

p

the p value corresponding to rmax.

f

the frequency offset corresponding to rmax.

Details

It is important not to have data in dB. Successive correlations between spec1 and spec2 are computed when regularly shifting spec2 towards lower or higher frequencies. The maximal correlation is obtained at a particular shift (frequency offset). This shift may be positive or negative. The corresponding p value, obtained with cor.test, is plotted. Inverting spec1 and spec2 may give slight different results, see examples.

References

Hopp, S. L., Owren, M. J. and Evans, C. S. (Eds) 1998. Animal acoustic communication. Springer, Berlin, Heidelberg.

See Also

spec, meanspec, corspec, covspectro, cor, cor.test.

Examples

Run this code
# NOT RUN {
data(tico)
## compare the two first notes spectra
a<-spec(tico,f=22050,wl=512,at=0.2,plot=FALSE)
c<-spec(tico,f=22050,wl=512,at=1.1,plot=FALSE)
op<-par(mfrow=c(2,1), mar=c(4.5,4,3,1))
spec(tico,f=22050,at=0.2,col="blue")
par(new=TRUE)
spec(tico,f=22050,at=1.1,col="green")
legend(x=8,y=0.5,c("Note A", "Note C"),lty=1,col=c("blue","green"),bty="o")
par(mar=c(5,4,2,1))
corspec(a,c, ylim=c(-0.25,0.8),xaxs="i",yaxs="i",las=1)
par(op)
## different correlation methods give different results...
op<-par(mfrow=c(3,1))
corspec(a,c,xaxs="i",las=1, ylim=c(-0.25,0.8))
title("spearmann correlation (by default)")
corspec(a,c,xaxs="i",las=1,ylim=c(0,1),method="pearson")
title("pearson correlation")
corspec(a,c,xaxs="i",las=1,ylim=c(-0.23,0.5),method="kendall")
title("kendall correlation")
par(op)
## inverting x and y does not give exactly similar results
op<-par(mfrow=c(2,1),mar=c(2,4,3,1))
corspec(a,c)
corspec(c,a)
par(op)
## mel scale
require(tuneR)
data(orni)
orni.mel <- melfcc(orni, nbands = 256, dcttype = "t3", fbtype = "htkmel", spec_out=TRUE)
orni.mel.mean <- apply(orni.mel$aspectrum, MARGIN=2, FUN=mean)
tico.mel <- melfcc(tico, nbands = 256, dcttype = "t3", fbtype = "htkmel", spec_out=TRUE)
tico.mel.mean <- apply(tico.mel$aspectrum, MARGIN=2, FUN=mean)
corspec(orni.mel.mean, tico.mel.mean, f=22050, mel=TRUE, plot=TRUE)
# }

Run the code above in your browser using DataLab