% Script to analyze the frequency spectrum of a signal using FFT
% Define signal parameters sampling_frequency = 1000; % Sampling frequency in Hz time = 0:1/sampling_frequency:1;
% Time vector for 1 second
% Create a sample signal (sum of two sine waves) f1 = 50; % Frequency of the first sine wave (Hz) f2 = 120; %
Frequency of the second sine wave (Hz) signal = sin(2 * pi * f1 * time) + 0.5 * sin(2 * pi * f2 * time + pi/4);
% Calculate the Fast Fourier Transform (FFT) N = length(signal); fft_signal = fft(signal);
% Calculate the magnitude spectrum (single-sided) magnitude_spectrum = abs(fft_signal(1:N/2+1)) / N;
magnitude_spectrum(2:end-1) = 2 * magnitude_spectrum(2:end-1); % Scale for single-sided
% Create the frequency vector for the single-sided spectrum frequency_vector = sampling_frequency * (0:(N/2)) / N;
% Plot the magnitude spectrum figure; plot(frequency_vector, 20 * log10(magnitude_spectrum)); % Magnitude in dB
xlabel('Frequency (Hz)'); ylabel('Magnitude (dB)'); title('Frequency Spectrum of the Signal'); grid on;
% Theory: The Fast Fourier Transform (FFT) is a fundamental algorithm in % signal processing that efficiently
computes the Discrete Fourier Transform (DFT). % The DFT decomposes a time-domain signal into its constituent
frequency % components. This code generates a sample signal composed of two sine waves % and then uses the fft
function in MATLAB to compute its frequency spectrum. % The magnitude spectrum shows the amplitude of each
frequency component present % in the signal. For real-valued signals, the frequency spectrum is symmetric, % so we
typically analyze the single-sided spectrum (from DC to the Nyquist % frequency, which is half the sampling frequency).
The peaks in the magnitude % spectrum correspond to the frequencies present in the original signal.