Discussion in 'Programmer's Corner' started by cloud, Mar 3, 2007.

1. ### cloud Thread Starter Member

Dec 15, 2006
11
0
I have obtained my waveform in time axis but I would like to convert to frequency domain. How do I do it?

Thank you.

2. ### kender Senior Member

Jan 17, 2007
263
0
Discrete Fourier Transform and it's fast version Fast Fourier Transform (FFT). MATLAB has a function called fft

3. ### Dave Retired Moderator

Nov 17, 2003
6,960
145
As an example:

Code ( (Unknown Language)):
1. >> t = [0:0.1:10]; % Declare a time range
2. >> y = sin(t); % Calculate a sine wave - this is your wave in the time domain
3. >> X = fft(y); % Compute the FFT using the Matlab fft function
4. >> Xyy = X.* conj(X) / 101; % Calculate the power spectrum at different frequencies
5. >> f = [0:(2*pi):(2*pi*100)]; % Declare the frequency range
6. >> plot(f,Xyy) % Plot the frequency represenation. You have a spike at a frequency of 2[FONT=Verdana]π which is as expected from y = sin(t)[/FONT]
Dave

4. ### cloud Thread Starter Member

Dec 15, 2006
11
0
How do I do it in Simulink? I use FFT block,but not able to get it.

5. ### Dave Retired Moderator

Nov 17, 2003
6,960
145
You can use User-Defined Functions > Embedded Matlab Function - you can now insert your own m-code as above.

Dave

6. ### cloud Thread Starter Member

Dec 15, 2006
11
0
But where should I connect the input and output of Embedded Matlab Function to?

I use another method by connecting to FFT block, and then to Spectrum Scope (shown as attached), but not sure whether this is correct. Can you advise me?

Thank you.

File size:
79.5 KB
Views:
165
File size:
13.2 KB
Views:
148
7. ### Dave Retired Moderator

Nov 17, 2003
6,960
145
Well your input is the time-domain signal, and the output is the frequency domain signal. For example, if your time-domain signal is stored in an array tdsignal and your frequency-domain signal is stored in an array fdsignal, then your function signature in the m-file should read:

fdsignal = fcnname(tdsignal)

Followed by your domain conversion code.

Your Embedded Matlab Function should merely replace the FFT block which IIRC just implements the Matlab fft function (I may be wrong here). I would advise you to get your domain conversion code working in Matlab as you require, then think about your integration into a Simulink model.

I'll dig around my Simulink notes since it is a while (years) since I have used it.

Dave

8. ### azzu New Member

Nov 17, 2009
1
0
Connecting it to FFT block followed by vector scope block( frequency) is correct

9. ### ashish.eeiitm New Member

Dec 18, 2009
5
0
hi
i have a sine wave with a varying frequency term, as in the f is 10 for a particular time interval and zero everywhere else.
I want to plot the frequency vs time.

any help on how to do the same using matlab/simulink.

I tried using fft but i'm sure of it's working. Just used the code given in matlab help file.