## 5.1 The Spectral Slope

We can use the power spectrum to estimate a self-affinity parameter, or scaling exponent.

• Download ts1.txt, ts2.txt, ts3.txt here. If you use R and have package rio installed you can run this code. It loads the data into a data.frame object directly from Github.
library(rio)
TS1 <- rio::import("https://raw.githubusercontent.com/FredHasselman/DCS/master/assignmentData/Fluctuation_PSDslope/ts1.txt")
TS2 <- rio::import("https://raw.githubusercontent.com/FredHasselman/DCS/master/assignmentData/Fluctuation_PSDslope/ts2.txt")
TS3 <- rio::import("https://raw.githubusercontent.com/FredHasselman/DCS/master/assignmentData/Fluctuation_PSDslope/ts3.txt")

# These objects are now data.frames with one column named V1.
# If you want to change the column names
colnames(TS1) <- "TS1"
colnames(TS2) <- "TS2"
colnames(TS3) <- "TS3"
• Plot the three ‘raw’ time series.

### 5.1.1 Basic data checks and preparations

For spectral analysis we need to check some data assumptions (see notes on data preparation, Lecture 4).

#### Normalize

1. Are the lengths of the time series a power of 2? (Use log2(length of var) )
• Computation of the frequency domain is greatly enhanced if data length is a power (of 2).
1. Are the data normalized? (we will not remove datapoints outside 3SD)
• To normalize we have to subtract the mean from each value in the time series and divide it by the standard deviation, the function scale() can do this for you, but you could also use mean() and sd() to construct your own function.
2. Plot the normalized time series.

#### Detrend

Before a spectral analysis you should remove any linear trends (it cannot deal with nonstationary signals!)

1. Detrend the normalized data (just the linear trend).
• This can be done using the function pracma::detrend().
• Extra: Try to figure out how to detrend the data using stats::lm() or stats::poly()
2. Plot the detrended data.

#### Get the log-log slope in Power Spectral Density

The function fd.psd() will perform the spectral slope fitting procedure.

1. Look at the manual pages to figure out how to call the function. The manual is on blackboard and Github
• Remember, we have already normalized and detrended the data.
• You can also look at the code itself by selecting the function name inR and pressing F2
2. Calculate the spectral slopes for the three normalized and detrended time series.
• Call with plot = TRUE
• Compare the results… What is your conclusion?