fft - Reconstructing a signal from its discrete fourier transform in R -


i trying replicate following figure in r: (adapted http://link.springer.com/article/10.1007/pl00011669)

dft figure

the basic concept of figure show first few components of dft, plotted in time domain, , show reconstructed wave in time domain using these components (x') relative original data (x). modify above figure such of lines shown overlaid on single plot.

i have been trying adapt figure real data sampled @ 60 hz. example:

## 3 second sample where: time in seconds , var variable of interest temp = data.frame(time=seq(from=0,to=3,by=1/60),             var = c(0.054,0.054,0.054,0.072,0.072,0.072,0.072,0.09,0.09,0.108,0.126,0.126,                   0.126,0.126,0.126,0.144,0.144,0.144,0.144,0.144,0.162,0.162,0.144,0.126,                   0.126,0.108,0.144,0.162,0.18,0.162,0.126,0.126,0.108,0.108,0.126,0.144,                   0.162,0.144,0.144,0.144,0.144,0.162,0.162,0.126,0.108,0.09,0.09,0.072,                   0.054,0.054,0.054,0.036,0.036,0.018,0.018,0.018,0.018,0,0.018,0,                   0,0,-0.018,0,0,0,-0.018,0,-0.018,-0.018,0,-0.018,                   -0.018,-0.018,-0.018,-0.036,-0.036,-0.054,-0.054,-0.072,-0.072,-0.072,-0.072,-0.072,                   -0.09,-0.09,-0.108,-0.126,-0.126,-0.126,-0.144,-0.144,-0.144,-0.162,-0.162,-0.18,                   -0.162,-0.162,-0.162,-0.162,-0.144,-0.144,-0.144,-0.126,-0.126,-0.108,-0.108,-0.09,                   -0.072,-0.054,-0.036,-0.018,0,0,0,0,0.018,0.018,0.036,0.054,                   0.054,0.054,0.054,0.054,0.054,0.054,0.054,0.054,0.054,0.072,0.054,0.072,                   0.072,0.072,0.072,0.072,0.072,0.054,0.054,0.054,0.036,0.036,0.036,0.036,                   0.036,0.054,0.054,0.072,0.09,0.072,0.036,0.036,0.018,0.018,0.018,0.018,                   0.036,0.036,0.036,0.036,0.018,0,-0.018,-0.018,-0.018,-0.018,-0.018,0,                   -0.018,-0.036,-0.036,-0.018,-0.018,-0.018,-0.036,0,0,-0.018,-0.018,-0.018,-0.018))  ##plot original data ggplot(temp, aes(x=time, y=var))+geom_line() 

i believe can use fft() accomplish goal leap output of fft() goal bit unclear.

i realize question similar to: how calculate amplitude , phase angle of fft() output real-valued input? more interested in actual code specific data above.

please note relatively new time series analysis clarity provide w.r.t. putting output of fft() in context, or package recommend accomplish task efficiently appreciated.

thank you

matlab best tool, , specific function fft(). use it, first determine several basic parameters of time domain data:

1, time duration (t), equals 3s.

2, sampling interval t_s, equals 1/60 s.

3, frequency domain revolution f_s, equals frequency difference between 2 adjacent fourier basis. may define f_s according needs. however, smallest possible f_s equals 1/t=0.333 hz. result, if want better frequency domain revolution (smaller f_s), need longer time domain data.

4, maximum frequency f_m, equals 1/(2t_s)=30 according shannon sampling theory.

5, dft length n, equals 2*f_m/f_s.

then find out specific frequencies of 4 fourier basis want use approximate data. example, 3,6,9 , 12 hz. f_s = 3 hz. n=2*f_m/f_s=20.

your matlab code looks this:

var=[0.054,0.054,0.054 ...]; % input data points here f_full=fft(var,20); % 20-point fft f_useful=f_full(2:5); % interested lowest 4 frequencies except dc 

here f_useful contains 4 complex coefficients of 4 fourier basis. reconstruct var, following:

% generate basis functions dt=0:1/60:3; df=[3:3:12]; basis1=exp(1j*2*pi*df(1)*dt); basis2=exp(1j*2*pi*df(2)*dt); basis3=exp(1j*2*pi*df(3)*dt); basis4=exp(1j*2*pi*df(4)*dt);  % reconstruct var var_recon=basis1*f_useful(1)+... basis2*f_useful(2)+... basis3*f_useful(3)+... basis4*f_useful(4); var_recon=real(var_recon);  % plot both curves figure; plot(var); hold on; plot(var_recon); 

adapt code paper :)


Comments

Popular posts from this blog

java - JavaFX 2 slider labelFormatter not being used -

Detect support for Shoutcast ICY MP3 without navigator.userAgent in Firefox? -

web - SVG not rendering properly in Firefox -