fourier principle in matlab

The Fourier principle states that any signal can be represented as a sum of sine and cosine waves of different frequencies. In Matlab, we can use the "fft" (fast Fourier transform) function to transform a signal from the time domain to the frequency domain. Here is an example code:

main.m
% Create a signal
Fs = 1000;                      % Sampling frequency
T = 1/Fs;                       % Sampling period
L = 1000;                       % Length of signal
t = (0:L-1)*T;                  % Time vector
S = 0.7*sin(2*pi*50*t) + sin(2*pi*120*t); % Signal

% Compute Fourier transform
Y = fft(S);

% Compute two-sided spectrum
P2 = abs(Y/L);
P1 = P2(1:L/2+1);
P1(2:end-1) = 2*P1(2:end-1);
f = Fs*(0:(L/2))/L;

% Plot spectrum
plot(f,P1)
title('Single-Sided Amplitude Spectrum of S(t)')
xlabel('f (Hz)')
ylabel('|P1(f)|')
534 chars
22 lines

This code creates a signal composed of a 50 Hz sine wave and a 120 Hz sine wave, computes the Fourier transform using "fft", and then plots the magnitude of the spectrum up to the Nyquist frequency. This code can be modified for other signals by changing the values of the frequency and amplitude of the sine waves in the signal.

related categories

gistlibby LogSnag