fourier transform in matlab

In order to Fourier transform a signal in MATLAB, you can use the built-in fft function. Here is an example usage:

main.m
% Generate a signal to transform
Fs = 1000; % Sampling frequency
t = 0:1/Fs:1-1/Fs; % Time vector
f = 10; % Signal frequency
x = sin(2*pi*f*t); % Signal

% Fourier transform the signal
X = fft(x);

% Plot the magnitude of the Fourier transform
frequencies = linspace(0,Fs/2,length(x)/2+1);
magnitude = abs(X(1:length(x)/2+1));
plot(frequencies, magnitude);
xlabel('Frequency (Hz)');
ylabel('Magnitude');
404 chars
16 lines

This code generates a 1 second sine wave at 10Hz, Fourier transforms it using the fft function, and plots the magnitude of the Fourier transform (up to the Nyquist frequency). Note that the output of fft is a complex vector, and the magnitude of each element represents the strength of the corresponding frequency component.

related categories

gistlibby LogSnag