% eda06_10
% derivative using fft example

clear all;

% standard set up
N=256;
Dt=1.0;
tmax=Dt*(N-1); 
t=Dt*[0:N-1]'; 
fmax=1/(2.0*Dt);
df=fmax/(N/2);
f=df*[0:N/2,-N/2+1:-1]'; 
Nf=N/2+1;
dw=2*pi*df;
w=dw*[0:N/2,-N/2+1:-1]';
Nw=Nf;

% exemplary function
t0 = tmax/2;
s = 50;
d = exp( -((t-t0).^2) / (2*s^2) );

% derivative using finite differences
dddt1 = Dt*(d(2:N)-d(1:N-1));

% derivative using fft
dddt=ifft(i*w.*fft(d));

figure(1);
clf;
subplot(3,1,1);
set(gca,'LineWidth',2);
hold on;
axis( [0, tmax, -1, 1] );
plot(t,d,'k-','LineWidth',2);
xlabel('time, t');
ylabel('d(t)');
subplot(3,1,2);
set(gca,'LineWidth',2);
hold on;
axis( [0, tmax, -0.02, 0.02] );
plot(t(1:N-1),dddt1,'k-','LineWidth',2);
xlabel('time, t');
ylabel('dd/dt(t)');
subplot(3,1,3);
set(gca,'LineWidth',2);
hold on;
axis( [0, tmax, -0.02, 0.02] );
plot(t,real(dddt),'k-','LineWidth',2);
xlabel('time, t');
ylabel('dd/dt(t)');








