change from time domain to frequency domain in matlab

To convert a signal from the time domain to the frequency domain in Matlab, we can use the Fast Fourier Transform (FFT) function. Here's an example:

main.m
% Generate a time series signal
Fs = 100; % Sampling frequency
t = 0:1/Fs:1-1/Fs; % Time vector
y = sin(2*pi*10*t) + sin(2*pi*20*t); % Two sine waves at 10 Hz and 20 Hz

% Compute the FFT of the signal
Y = fft(y);

% Compute the corresponding frequency vector
L = length(y);
f = Fs*(0:(L/2))/L;

% Plot the magnitude of the FFT
plot(f,2*abs(Y(1:L/2+1)))
title('Single-Sided Amplitude Spectrum of y(t)')
xlabel('Frequency (Hz)')
ylabel('|Y(f)|')
445 chars
18 lines

In this example, we first generated a time series signal y consisting of two sine waves with frequencies 10 Hz and 20 Hz. We then computed the FFT of the signal using fft(y). This returns a vector Y containing the complex values of the FFT coefficients.

To obtain the corresponding frequency vector, we first calculated the length L of the signal and used it to create a vector f from 0 to the Nyquist frequency (Fs/2) with steps of Fs/L. We then plotted the magnitude of the FFT to obtain the single-sided amplitude spectrum of the signal.

gistlibby LogSnag