0
votes

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.

1
Be careful with the start argument inside ts, it expects a single number OR a vector of two integers. Using start(s2_dts) does not give year and month but the first 2 integers you gave inside ts. 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."bVa
And use a correct end argument inside window to have a correct output : end = as.numeric(as.POSIXct("2017-08-17 17:04:35")), you forgot as.POSIXct(). @OutibVa
Thank you for your reply. I added as.POSIXCT() to end but do not still get the right answer. > head(s2_dts) [1] -0.02425873 -0.15219589 -0.39131684 -0.42728653 0.25058982 1.64021640 > head(x) [1] 0.32049328 0.35900218 0.01488998 0.51875873 1.69127310 1.55796914 > These two heads should be the same.Outi
I have read the documentation of ts many times but do not understand how to give the start and end time in my case. As you can see in my answer, I have tried to change the date time Posixct object to a single number as said in the documentation. Can you give a suggestion how to do it @bVa?Outi

1 Answers

0
votes
data <- runif(10, 1, 10) # mock data
> data
[1] 9.455453 4.596560 9.675503 7.437548 2.722311 7.729820 5.737903 2.540364 3.512660 5.555681

# 2 samples/second 
begin <- as.POSIXct("2017-08-17 17:04:34", tz="UTC")
end <- as.POSIXct("2017-08-17 17:04:39", tz="UTC")

# Create time-series object
df_ts <- ts(data, 
            start = as.numeric(begin), 
            end = as.numeric(end), 
            frequency = 2)

> window(df_ts, 
         start = as.numeric(begin), 
         end = as.numeric(as.POSIXct("2017-08-17 17:04:36", tz="UTC")))
Time Series:
Start = c(1502989474, 1) 
End = c(1502989476, 1) 
Frequency = 2 
[1] 9.455453 4.596560 9.675503 7.437548 2.722311