clear all
close all

% generate data example
dt = 0.02
t_start = -0.5
t_end = 0.5-dt
duration = t_end-t_start+dt
dduration = duration*3 
n = floor(duration / dt)
nn = 3*n;

t = linspace(t_start,t_end-dt,n);
x = t;

tt = linspace(t_start-duration,t_end+duration-dt,nn);
xx = zeros(1,nn)
xx(1,1:n)=x(1,1:n);
xx(1,n+1:2*n)=x(1,1:n);
xx(1,2*n+1:3*n)=x(1,1:n);

figure(1)
subplot(3,1,1)
plot(tt,xx)
xlabel('time')
ylabel('signal')

subplot(3,1,2)
nmax=60
% compute FT
ft=fft(x);
ftt=fft(xx);
absfft=abs(ft);
aabsfft=abs(ftt);
dfreq = 1.0/duration
ddfreq = 1.0/dduration
freq_max = 1.0/(2.0*dt)
num_freq=freq_max/dfreq
nnum_freq=freq_max/ddfreq
freq = linspace(0,freq_max-dfreq,n/2);
ffreq = linspace(0,freq_max-ddfreq,nn/2);
%plot(freq,absfft(1:floor(n/2)),'-*')
%plot(1:nmax,absfft(1:nmax),'-*')
plot(1:nmax,aabsfft(1:nmax),'-*')
xlabel('mode number')
ylabel('amplitude power')

subplot(3,1,3)
plot(ffreq(1:nmax),aabsfft(1:nmax))
xlabel('frequency')
ylabel('amplitude power')

n1 = 5
ft_partial1=zeros(1,nn);
ft_partial1(1,1:n1)=ftt(1:n1);
ft_partial1(1,nn:-1:nn-n1+1)=ftt(nn:-1:nn-n1+1);
ifft1=ifft(ft_partial1);

n11 = 20
ft_partial11=zeros(1,nn);
ft_partial11(1,1:n11)=ft(1:n11);
ft_partial11(1,nn:-1:nn-n11+1)=ftt(nn:-1:nn-n11+1);
ifft11=ifft(ft_partial11);

n2 = 50
ft_partial2=zeros(1,nn);
ft_partial2(1,1:n2)=ftt(1,1:n2);
ft_partial2(1,nn:-1:nn-n2+1)=ftt(1,nn:-1:nn-n2+1);
ifft2=ifft(ft_partial2);
%ifft2=ifft(ft);


figure(2)
subplot(3,1,1)
plot(tt,xx,'b',tt,real(ifft1),'r-.')
xlabel('time')
ylabel('signal+reconstruction')

subplot(3,1,2)
plot(tt,xx,'b',tt,real(ifft11),'r-.')
xlabel('time')
ylabel('signal+reconstruction')

subplot(3,1,3)
plot(tt,xx,'b',tt,real(ifft2),'r-.')
xlabel('time')
ylabel('signal+reconstruction')

