Time-domain data to Frequency-domain data Questions

2 views (last 30 days)
Hi!
I have a lot of questions regarding this topic, so to give a little background. I am accumulating data from the serial port and importing it to matlab. I need to transform it to time-domain and view it in frequency spectrum (frequency domain using FFT).
So for example purposes, let us assume that I have a variable x whose elements are [ 1 2 3 4 5].
x = [ 1 2 3 4 5];
n = length(x);
T = (0:n) * 0.1;
^ for this block of code, I am creating a time variable whose interval is 100ms, since the data that I accumulate from the serial port is 100ms apart from each other.
So first question is: (1) How do I transform it to time-domain? I'm confused if whether I should collaborate it as a whole (like a signal wave). Is that possible? and how can I do that?
My guts is telling me that I should proceed directly to FFT by these block of codes:
% Get the sampling frequency based on the number of elements
Fs = 1/n;
% ?
NFFT = 2^nextpow2(n); % Next power of 2 from length of data
% ?
y = fft(x, NFFT)/n; % ?
f = Fs/2*linspace(0,1,NFFT/2+1); % ?
plot(f,2*abs(y(1:NFFT/2+1))); % ?
^ for this block of code, I'm confused since I did not really used the "time" but just directly convert it to frequency domain. Can somebody help and give me an idea on how to correctly convert recorded data time domain to frequency domain? Please, and also, if you can, please provide an explanation for each line of code for me to fully understand it.
Hoping for your replies, please help. Thank you very much and good day!

Answers (1)

Honglei Chen
Honglei Chen on 6 Feb 2015
FFT is just an operation on the data, whether it's time domain or frequency domain is an interpretation of the data. So what you are defining is really how to explain data. For example,
x = [1 2 3 4 5]
If you say the corresponding time is
t1 = [0 0.1 0.2 0.3 0.4]
Then this is sampled at 10ms intervals. But you can perfectly say its time is
t2 = [0 0.01 0.02 0.03 0.04]
Now suddenly your data is interpreted as sampled with 1ms intervals. So that time vector helps you to interpret the data. Similarly, for the same data, if you do an FFT operation, obviously you will get the same output. Again the difference is how you interpret them. If you get the same data with a faster sample rate, it means your signal is changing at a faster pace. That's why your frequency axis is associated with the sample rate. Again that is what you use to interpret your frequency domain result.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!