To get a spectrum from data in Matlab, you can use the fast Fourier transform (FFT) algorithm. Here is an example code to illustrate how to do it:
main.m560 chars21 lines
In this code, we first generate a sample signal x
that consists of a cosine waveform with a frequency of 10 Hz. Then, we apply the FFT algorithm to x
and normalize the result by dividing by the signal length N
. We also compute the frequency axis faxis
. Since the output of the FFT algorithm is a double-sided spectrum, we only keep the one-sided part of the spectrum X_one_sided
, which corresponds to frequencies from 0 to the Nyquist frequency (fs/2
). Finally, we plot the one-sided amplitude spectrum using the stem
function.
gistlibby LogSnag