0

In R, if I would like to plot the spectrogram from a wave, it is as following:

>library(sound)
>library(tuneR)
>library(seewave)
>s1<-readWave('sample1.wav')
>spectro(s1,main='s1')
>str(s1)
 Formal class 'Wave' [package "tuneR"] with 6 slots
  ..@ left     : int [1:312000] 2293 2196 1964 1640 1461 1285 996 600 138 -195 ...
  ..@ right    : num(0) 
  ..@ stereo   : logi FALSE
  ..@ samp.rate: int 8000
  ..@ bit      : int 16
  ..@ pcm      : logi TRUE

But what if I just have data.txt as

2293 2196 1964 1640 1461 1285 996 600 138 -195 ...

What should I put in the spectro function? spectro(wave, f, ...), wave is said to be an R object. Or I should use others to get the plot? I tried

>s_1<-read.table("s_1.txt", sep=" ")
>spectro(s_1,f=8000)
Error in filled.contour.modif2(x = X, y = Y, z = Z, levels = collevels,  :
no proper 'z' matrix specified

and ended with error. Thank you.

1 Answers1

1

I agree the documentation is a little hazy.

What I believe it is trying to say is that the first argument must be a Wave object. You can convert a numeric vector into a Wave object using the TuneR Wave() function.

v <- runif(5000, -2^15, 2^15-1)
v.wav <- Wave(v, samp.rate=8000, bit=16)

spectro(v.wav)

I didn't manage to install seewave on my current computer, so I tested this on an old computer with software a couple of years old. I can't guarantee that this example will work.

AkselA
  • 8,153
  • 2
  • 21
  • 34
  • Thank you. I found it yesterday with the same idea as you and got stuck just because there existed a limit length for the number set. s_1 should have more than 1000 entries. So I added 0's to make it and then it worked. – Heidi Xiao Jul 14 '17 at 19:10