compute the frequency spectrum of signal x. in matlab

To compute the frequency spectrum of a signal x in MATLAB, you can use the Fast Fourier Transform (FFT) algorithm. Here's how you can do it:

main.m
% Generate a sample signal
Fs = 1000; % Sampling frequency
t = 0:1/Fs:1-1/Fs; % Time vector
x = sin(2*pi*50*t) + randn(size(t)); % Signal

% Compute the FFT
N = length(x);
X = fft(x);
f = (0:N-1)*(Fs/N); % Frequency vector

% Plot the frequency spectrum
figure;
plot(f,abs(X))
xlabel('Frequency (Hz)')
ylabel('Magnitude')
322 chars
16 lines

In this example, we first generate a sample signal x which is a sine wave at 50 Hz with some random noise. We then compute the FFT using the fft function. The FFT returns a complex-valued vector X containing the frequency domain representation of the signal. To convert the frequency representation to Hz, we compute the frequency vector f. Finally, we plot the magnitude of the frequency spectrum using the plot function.

gistlibby LogSnag