# Using MATLAB to get the fourier transform of dirac delta function

## Homework Statement

Dear all,

I have a problem when I using MATLAB to get the fourier transform of dirac delta function. below is my code.

## Homework Equations

clear all;
clc;
close all;

% t=0:0.002:2;
t=0:0.002:4;
dt=t(2)-t(1);
u=zeros(size(t));
pos0=find(t>=1,1);
u(pos0)=1/dt;

f=linspace(-.5/dt,.5/dt,length(t));
U=fftshift(fft(u))/length(t);

figure;subplot(2,1,1);hold on;plot(t,u);
subplot(2,1,2);hold on;plot(f,abs(U));

## The Attempt at a Solution

I have a doubt that why when I change the end of time to 4, the absolute value of U(f) would be the half when I set the time as 2?

Thank you very much.

Related Engineering and Comp Sci Homework Help News on Phys.org
That part's tricky and it took me a looong time to figure it out.
What they're pretty much doing in the examples where you see them divide by L is that they're actually multiplying by the sampling rate dt. It's just that in those examples where they divide by L the time ends at 1s so 1s/length(t) = dt.

Last edited:
Thanks so much.

However, if I using this way for cosine function, then it doesn't work again.
u2=1*cos(20*t);
U2=fftshift(fft(u2))*dt;

This time, when I change the time setting, the value of U2 will change.

It's so strange.

You mean when you change the end time the value given changes from 0.5 to something else?

Sorry I had that problem too and I don't understand why...

I haven't been able to find an answer to this either, but I know they discuss this topic in Digital Signal Processing classes/books.

Hi, thanks again :)

If I change from t(end)=2 to other value then U2(f) will change its amplitude if I use dt instead of length(t).
But it's ok, I will check it with books. Do you know in which book or which section they discuss this topic?

Nope sorry, I haven't taken that class yet.
But I think it should be discussed in the same section as the sampling theorem or maybe the fourier integrals.