mr.T
mr.T

Reputation: 634

fft() get amplitude, frequency, phase and invert back in series

I am very bad at spectral analysis, so please forgive me for stupid questions. I found someone's code on the net to calculate the amplitude of the frequency and phase

y <- rnorm(50) # some series
samp.freq <- 10
N <- length(y)
fk <- fft(y)
fk <- fk[2:length(fk)/2+1]
fk <- 2*fk[seq(1, length(fk), by = 2)]/N


freq <- (1:(length(fk)))*samp.freq/(2*length(fk))
amp <- Mod(fk)
pha <- Arg(fk) 

My question is: knowing freq ,amp, pha how can I restore back the y series

This code is not obligatory for me, it is important for me to see how I can get the amplitude, frequency, phase from the series, and then assemble the series back

Upvotes: 0

Views: 667

Answers (1)

Waldi
Waldi

Reputation: 41240

You won't be able to restore fully the timeseries as fft result is subsampled :

fk <- 2*fk[seq(1, length(fk), by = 2)]/N

meaning you're losing some frequencies.

Without subsampling, restoration can be done with inverse fft :

set.seed(123)
y <- rnorm(50) # some series
samp.freq <- 10
N <- length(y)
fk <- fft(y)

# No subsampling
#fk <- fk[2:length(fk)/2+1]
#fk <- 2*fk[seq(1, length(fk), by = 2)]/N

freq <- (1:(length(fk)))*samp.freq/(2*length(fk))
amp <- Mod(fk)
pha <- Arg(fk) 

fk.restored <- amp * complex(real = cos(pha),im = sin(pha))
y.restored <- Re(fft(fk.restored,inverse=TRUE)/length(fk))
all.equal(y.restored,y)
#> [1] TRUE

Upvotes: 2

Related Questions