MATLAB Answers

How to plot spectral density given a column of data?

13 views (last 30 days)
Ryan Colvin
Ryan Colvin on 17 Sep 2020
Edited: Ryan Colvin on 13 Oct 2020 at 16:55
My question is how do I plot the spectral density? The data itself is associated with an error trajectory which is equal to the difference between an optimal trajectory and a less ideal trajectory. The data itself already was polled at a rate of Ts = .034s and is time stamped. I thought it would be as simple as taking that column of data applying fft and then plot(abs(x)) in order to apply the fourier transform to find the predominant frequencies? But the graph seems to not show me anything interesting.
xfft = fft(x);
title('Fourier Transform');


Sign in to comment.

Answers (1)

Star Strider
Star Strider on 17 Sep 2020
My guess is that there is a significant d-c (constant) offfset. This is the mean of ‘x’, and will show up as the amplitude at 0 Hz.
Using this to plot it might be a bit more revealing:
Fs = 1/0.034; % Sampling Frequency (Hz)
Fn = Fs/2; % Nyquist Frequency (Hz)
L = numel(x); % ‘x’ Is A Vector
xm = mean(x);
xfft = fft(x - xm)/L; % Subtract Mean & Normaloise By ‘L’
Fv = linspace(0, 1, fix(L/2)+1)*Fn; % Frequency Vector
Iv = 1:numel(Fv); % Index Vector
plot(Fv, abs(xfft(Iv))*2)
xlabel('Frequency (Hz)')


Ryan Colvin
Ryan Colvin on 17 Sep 2020
I tried that and the result I am getting is as follows. Is it possible to get no "primary" frequency in a given signal? It is evident in the following graph but on the more theoretical side of things. Is it possible that my frequency sampling rate isn't high enough?
Star Strider
Star Strider on 17 Sep 2020
If you eliminated the d-c offsset, then you have at least two frequencies that could be significant, both less than 0.5 Hz. If you have the Signal Processing Toolbox, use the findpeaks function to get more information from them, and other peaks that may be of interest.
With respect to the sampling frequency, I can’t determine that. A sampling frequency of 29 Hz may be high enough for some applications, not high enough for others. Note that for best results, the sampling interval must be constant. If it isn’t, use the resample function to resample it to a constant sampling frequency.
Ryan Colvin
Ryan Colvin on 13 Oct 2020 at 16:54
I see, I have a 2-D error trajectory (in terms of x and y coordinates) with a sampling rate of Ts = .034s. Would I be able to do a 2-D fft to see the frequency domain of it? The above graph is only looking at the x error coordinates.
Based on the Fs shouldn't I see a spike around 29 hertz if I were to double Fs to the nyquist frequency?

Sign in to comment.


Community Treasure Hunt

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

Start Hunting!