# Matlab - sinewaves

1. Oct 11, 2009

### zxc01

Hello.

I have recently transferred courses, and am expected to know how to use Matlab, but have never been taught before. Therefore I have been trying to go through and teach myself some of the bits I will need to know from it.

We were told we had to know how to do certain things already and I have been going through these.

Create a sine wave, calculate the FFT of the sine wave, extract the frequency of the FFT of the sine wave from the FFT data, and know what happens to the time-domain and FFT-domain of this data if the phase of the sine wave were shifted by a few degrees.

I tried with the help of the getting started guide in Matlab itself and input:-

x=0:pi/100:2*pi
y=sin(x)
plot(x,y)

y=fft(x)

And couldn't do any of the others.

Would appreciate some help.
Thank you.

2. Oct 13, 2009

### matonski

I always get slighty confused about this so I wrote myself a demo program that does just this thing. It plots a sign wave and it's fourier transform, showing spikes at just the right places.

Code (Text):

clear all
% play with different values of fo
fo = 4;
N = 100;
T = 2*pi;
t = linspace(0,T,N);

% the function and its fft (shifted so zero frequency occurs in the middle)
f = sin(2*pi*fo*t);
ft = fftshift(fft(f));

% frequency goes in steps 1/T
s = (-N/2+1:N/2) * 1/T;
figure(1),plot(t,f),title('f(t) versus t')
figure(2),plot(s,abs(ft)),title('fourier transform of f at s versus s')

As far as really understanding the relationship between the discrete fourier transform and the continuous one, this lecture really cleared things up. It's lecture number 20 in an entire class on fourier transforms. This is the class where he introduces the discrete fourier transform. The whole class is amazing and I highly recommend it to everyone.