I am trying to convert sound data to a time-series object in order to analyse it further (using f.ex. WelchPSD from 'bspec' package). The frequency of my data is 25811 samples/second (Hz).
My sound file s2_d
> str(s2_d)
int [1:52422753] -442 -434 -428 -413 -389 -386 -382 -387 -403 -373 ...
is an integer after I have extracted the data from the Wave object created by Waveread in package signal.
The file starts and ends:
> begin <- as.POSIXct("2017-08-17 17:04:34", tz="UTC")
> end <- as.POSIXct("2017-08-17 17:38:25", tz="UTC")
I have tried converting s2_d to ts with following lines:
> s2_dts <- ts(s2_d, start = as.numeric(begin), end = as.numeric(end), frequency = 25811)
> s2_dts <- as.ts(s2_d, start = as.numeric(begin), end = as.numeric(end), frequency = 25811)
> s2_dts <- ts(s2_d, start = as.numeric(as.POSIXct(begin)), end = as.numeric(as.POSIXct(end)), frequency = 25811)
> s2_dts <- as.ts(s2_d, start = as.numeric(as.POSIXct(begin)), end = as.numeric(as.POSIXct(end)), frequency = 25811)
They all convert s2_d to a ts
> class(s2_dts)
[1] "ts"
> start(s2_dts)
[1] 1502989474 1
but when I try the function window(), I can not extract f.ex. 1 second snippet out of my time series so something is not quite right.
> x <- window(s2_dts, start = as.numeric(begin), end = as.numeric("2017-08-17 17:04:35"), extend= TRUE)
Error in if (start > end) stop("'start' cannot be after 'end'") :
missing value where TRUE/FALSE needed
In addition: Warning message:
In window.default(x, ...) : NAs introduced by coercion
I finally tried:
> s2_dts <- ts(s2_d, start = c(2017,8,17,17,04,34), end = c(2017,8,17,17,38,25), frequency = 25811)
> start(s2_dts)
[1] 2017 8
> time(s2_dts)
Time Series:
Start = c(2017, 8)
End = c(2017, 8)
Frequency = 25811 [1] 2017
This is better evenif only year and month are recognised. How can I get the whole time including day, hour, minute and second to be the start time of the time series?
Any help will be greatly appreciated.
start
argument insidets
, it expects a single number OR a vector of two integers. Usingstart(s2_dts)
does not give year and month but the first 2 integers you gave insidets
. Read carefully the documentation of start argument : "the time of the first observation. Either a single number or a vector of two integers, which specify a natural time unit and a (1-based) number of samples into the time unit. See the examples for the use of the second form." – bVaend
argument insidewindow
to have a correct output :end = as.numeric(as.POSIXct("2017-08-17 17:04:35"))
, you forgotas.POSIXct()
. @Outi – bVa