To compute the frequency spectrum of a signal x and plot its magnitude in matlab, we can use the fft function to compute the Discrete Fourier Transform (DFT) of the signal, and then plot the magnitude of the result using the plot function.
Here's the code:
main.m362 chars14 lines
This code generates a signal x which is the sum of two sinusoids, one at 50 Hz and the other at 120 Hz. The sampling frequency is set to 1000 Hz. We then compute the DFT of x using the fft function, and compute the corresponding frequency vector f using linspace. We also compute the magnitude of the DFT and plot it against the frequency using the plot function. The x-axis limits are set to the range [-fs/2, fs/2] to display the negative and positive frequencies. The resulting plot shows the magnitude of the signal as a function of frequency.
gistlibby LogSnag