# Matlab - fft

#### Treasa

Joined Oct 20, 2010
6
Okay I'm really struggling with Matlab. I've tried doing this question using the ones zeros code and stuff but I guess I don't really have a clue what I'm doing.

So I have that f(t) = 0 for t > 30 and f(t) = 15 if t =< 30. So I was wondering what I do, because f(t) has no actual t in it so like that confuses me? Like if I said f1=0 f2=15 f=[f1 f2] and then get fft of f I can't imagine that's correct so could anyone offer any help please?

Thank you!

#### MrChips

Joined Oct 2, 2009
25,935
Post the code that you have written.

#### Treasa

Joined Oct 20, 2010
6
I've tried loads of things, I have a few questions with the same problem - that because f(t) is just a number, there is no actual t, like how can that even work? I was trying:
f=[15*ones(1,N/2) zeros(1,N/2)]; then getting fft of that but I'm not sure I can do it that way?

#### MrChips

Joined Oct 2, 2009
25,935
What do you mean by "because f(t) has no actual t in it"

and "that because f(t) is just a number, there is no actual t"?

#### Treasa

Joined Oct 20, 2010
6
f(t) = 0 or f(t) = 15, depending on time. But like do I need to include the time constraints anywhere when plotting fft? since f(t) isn't like 15t or anything with an actual t in it?
or can I use that ones and zeros thing I posted in my last post? Like would that be the correct way of getting fft or not?

#### steveb

Joined Jul 3, 2008
2,436
f(t) = 0 or f(t) = 15, depending on time. But like do I need to include the time constraints anywhere when plotting fft? since f(t) isn't like 15t or anything with an actual t in it?
or can I use that ones and zeros thing I posted in my last post? Like would that be the correct way of getting fft or not?
Your function is represented as a vector and the integer index to identify the array element is basically your discrete time index. To get real time, you need to know your sample time, which is the time between your sample points in the array.

Figuring out the time and frequency domain scaling for FFTs is a bit of a pain in the neck in Matlab. I've done it many times and every time I go to do it, I forget how I did it the last time. I end up going back into the documentation to figure it out. Then I have to run test signals, whose frequency content is known, to make sure I'm doing it correctly and getting the correct answer.

#### MrChips

Joined Oct 2, 2009
25,935
f(t) means a function with respect to time.
Here is an example of your function:

0 0 0 0 15 15 15 15 0 0 0 0

This is a time series, that is, a series of values with respect to time. Time is inherently included.

f(0) = 0
f(1) = 0
f(2) = 0
f(3) = 0
f(4) = 15
f(5) = 15
etc.

The value in the braces ( ) represents the time.

Like I said before, you need to post your Matlab code.

Figuring out the time and frequency domain scaling for FFTs is a bit of a pain in the neck in Matlab. I've done it many times and every time I go to do it, I forget how I did it the last time. I end up going back into the documentation to figure it out. Then I have to run test signals, whose frequency content is known, to make sure I'm doing it correctly and getting the correct answer.
There is an easy way to figure out the frequency scale once you understand how it is done.

Suppose your sampling frequency is fs.
Suppose you take N samples.
Determine how long the sampling record is in time as follows:
The sampling period = 1/fs
Hence the total time T = N/fs

Perform the FFT.
Each point on the frequency spectrum will be spaced by 1/T,
that is, your frequency resolution will be 1/T = fs/N

By the Nyquist Theorem, the maximum frequency will be fs/2

But you have to observe that the second half of the frequency spectrum is the negative frequencies.

Hence the frequency scale goes as
0....fs/2 -fs/2..... -fs/N

You can use the fftshift function to move the 0 frequency to the center.

#### steveb

Joined Jul 3, 2008
2,436
There is an easy way to figure out the frequency scale once you understand how it is done.

Suppose your sampling frequency is fs.
Suppose you take N samples.
Determine how long the sampling record is in time as follows:
The sampling period = 1/fs
Hence the total time T = N/fs

Perform the FFT.
Each point on the frequency spectrum will be spaced by 1/T,
that is, your frequency resolution will be 1/T = fs/N

By the Nyquist Theorem, the maximum frequency will be fs/2

But you have to observe that the second half of the frequency spectrum is the negative frequencies.

Hence the frequency scale goes as
0....fs/2 -fs/2..... -fs/N

You can use the fftshift function to move the 0 frequency to the center. Yep! That's what I come up with every time. If only I could keep it in my head so I wouldn't have to go through the effort each time. I agree, it's not really that hard, but for a person (like the OP) doing it for the first time, it can be confusing.

#### Treasa

Joined Oct 20, 2010
6
Thanks guys. I was doing it right. I've only started using it in the last few weeks and no one has really taught my class how to use it or do the questions so I keep second guessing everything I do!

#### MrChips

Joined Oct 2, 2009
25,935
I should also add that your FFT in frequency space will be complex, i.e. with real and imaginary components. Each element of the vector represents a sine wave with a given phase angle.

If you wish you can convert to the absolute value using abs( )
or you can convert to the power spectrum by multiplying by the complex conjugate
P = Y.*conj(Y)