## Contents

## DT Fourier Series in Matlab with ONE Command

# DT Fourier Series with a single MATLAB command!

Calculating fourier series by hand can often become time consuming and error prone. Matlab has an easy and fast built-in fuction for computing discrete time fourier series coefficents. Unfortunely, this wont help you on exams, but it might save you considerable time on homework assignemnts.

The command is ifft. It takes in a vector representing your signal and produces a vector of the fourier series coefficients. Two examples are provided below:

Example 1: The signal is represented by the graph below and is periodic for all time:

This signal can be represented by a vector. Each element in the vector corresponds to the value that the signal takes at each time interval. At time 0, the value is 2. At time 1 the value is 1 and for time 2 and 3 the value is 0. This can be represented by the vector below:

[2,1,0,0]?

To find the fourier series coefficients, we would use the following matlab code:

- signal = [2,1,0,0]; fouriercoefs = ifft(signal)

The output gives:

- fouriercoefs = 0.7500 0.5000 + 0.2500i 0.2500 0.5000 - 0.2500i

This means that the fourier series coefficients are: a0 = .75 a1 = .5+.25j a2 = .25 a3 = .5-.25j.

Example 2: The signal is represented by the graph below and is periodic for all time:

This signal can be represented by a vector like before. You may recognize this signal as x(t) = t for 0<=t<=2 in continuous time. Since we're in discrete time, the vector below represents the signal:

[0,1,2]?

Note that this is NOT the same as [0,.5,1,1.5,2]?. In that case, the continuous time example would be x(t) = .5t for 0<=t<=4. To find the fourier series coefficients, we would use the following matlab code:

- signal = [0,1,2]; fouriercoefs = ifft(signal)

The output gives:

- fouriercoefs = 1.0000 -0.5000 - 0.2887i -0.5000 + 0.2887i

This means that the fourier series coefficients are: a0 = 1 a1 = -.5 - .2887j a2 = -.5 + .2877j

Hopefully this will help you take DT fourier series in matlab easier and faster. These are examples which you can easily verify by hand.

comments:

... --shaun.p.greene.1, Wed, 26 Sep 2007 22:46:19 Good find in the functions library.

Although I'm not sure that the ifft() does what we really want, I tried it on the homework, and it gives me pretty much the same answer, so I definately think its close to what we need.

I found a function called dftmtx() that will generate the k and n matrix that is needed for finding the ak values. I apologize in advance because I'm not good in latex.

we have

$ \displaystyle a_k = \frac{1}{N}\cdot \sum_{k=0}^{k=N-1} \left( signal \cdot e^{(\jmath\frac{2\pi}{N} k n)} \right) $

the dftmtx() command should give you the matrix that has your exp(......).

Then, all you have left to find the aks is to

- ak = signal * dfftmtx(N);

which gives a matrix of ak values that is almost identical to what the ifft() command gives you.

Thanks again for the good start with the ifft function, it got me moving on this homework.

... --shaun.p.greene.1, Wed, 26 Sep 2007 22:43:05 sry, that paragraph is really hard to read.

help? --andrew.c.daniel.1, Thu, 27 Sep 2007 00:04:44 if you used ifft() in the homework and it didn't work you could try transposing the [1xn]? vector wavread gives you to a [nx1]? horizontal vector matlab syntax: A_transposed = A';

... --tom.l.moffitt.1, Thu, 27 Sep 2007 00:15:22 If you do it on the homework, make sure you take it over one period. Since with voice recordings each period wont be exactly the same, it's a good idea to just do it over one.

A note about fft vs ifft --ross.a.howard.1, Thu, 27 Sep 2007 09:31:01 If you look at the help fft page it gives the equations it represents with fft and ifft. There are some differences between them and our book. fft has the correct sign in the complex exponential, but is not multiplied by 1/N. ifft finds the conjugate of the aks (which when you plot abs(ak) it does not matter) and has the 1/N term. For finding the aks using fft: aks=aks./length(aks); One should also note the helpful function shiftfft. It is useful because the matrix returned from fft starts at index 1 but contains a0, index 2 contains a1, and so on until it reaches the highest k, then it starts counting down. This means that when you plot the aks, they will not be in the right order. The shiftfft function correctly puts the negative aks on the left of the a0. (aks=shiftfft(aks);) Note: if you used ifft to find the aks, then use the shiftifft instead. Hope this helps. :)

## Tutorial: Vector/Matrix Manipulation in Matlab

## The Best Way

The best way to reverse a vector is probably as follows:

Say we have a vector,

- test = [1,2,3,4,5]

and we want to reverse the numbers to provide us with a new vector:

- newVector = [5,4,3,2,1]

To accomplish this without using a while loop or for loop, we can simply use the variable 'end', that is predefined in matlab:

- newVector = test(end:-1:1)

What this statement say is take the last element of test ('end'), place that in the first element of newVector, then take the next last element of test ('end - 1') and put it into the second element of newVector, and so on, until newVector is a reversed copy of test. ** This method works with both row and column vectors**.

## Other Ways

Use the function flipud(vector) to reverse a column vector quickly.

- >> flipud([1;2;3])
- ans =
- 3
- 2
- 1

Use the function fliplr(vector) to reverse a row vector quickly.

- >> fliplr([1,2,3])
- ans =
- 3 2 1