Lab 2-Spectrum Analysis of Sine Wave Using FFT in
MATLAB
1. Aim
To calculate the spectrum of a sinusoidal waveform using the ‘fft’ command in
MATLAB and analyze the effect of changing amplitude and frequency on the
waveform and its spectrum.
2. Software Used
- MATLAB R2022b
3. MATLAB Code
% Parameters
fs = 1000;
T = 1;
n = fs * T; % Total number of samples for resolution= total time *number of sample
in 1 second
t = linspace(0, T, n); % Time vector
% Initial Sine Wave Parameters
f = 19; % Frequency of sine wave in Hz
A = 1; % Amplitude of sine wave
% Generate Sine Wave
sine_wave = A * sin(2 * pi * f * t);
% Perform FFT
spectrum = fft(sine_wave); % Compute FFT
frequencies = (-n/2:n/2-1) * (fs / n); % Frequency vector for two-sided spectrum
magnitude = abs(fftshift(spectrum)) / n; % Shift and normalize magnitude
LAB-2/SPECTRUM OF SINE WAVE/BT23ECE119
% Plot Time-Domain Signal
figure;
subplot(3, 2, 1);
plot(t, sine_wave, 'b');
title('Original Sine Wave (19 Hz, Amplitude = 1)');
xlabel('Time (s)');
ylabel('Amplitude');
grid on;
% Plot Frequency Spectrum
subplot(3, 2, 2);
plot(frequencies, magnitude, 'r');
title('Frequency Spectrum of Original Sine Wave');
xlabel('Frequency (Hz)');
ylabel('Magnitude');
grid on;
% Analyze Effect of Changing Amplitude
A_2 = 2; % New amplitude
sine_new = A_2 * sin(2 * pi * f * t);
spectrum_new_amp = fft(sine_new);
magnitude_new_amp = abs(fftshift(spectrum_new_amp)) / n ;
% Plot New Amplitude Time-Domain Signal
subplot(3, 2, 3);
plot(t, sine_new, 'g');
title('Sine Wave (19 Hz, Amplitude = 2)');
xlabel('Time (s)');
ylabel('Amplitude');
grid on;
% Plot Spectrum for New Amplitude
subplot(3, 2, 4);
plot(frequencies, magnitude_new_amp, 'm');
title('Spectrum of Sine Wave (Amplitude = 2)');
xlabel('Frequency (Hz)');
ylabel('Magnitude');
grid on;
% Analyze Effect of Changing Frequency
LAB-2/SPECTRUM OF SINE WAVE/BT23ECE119
f_2 = 50; % New frequency
sine_new = A * sin(2 * pi * f_2 * t);
spectrum_new= fft(sine_new);
magnitude_new = abs(fftshift(spectrum_new)) / n;
% Plot New Frequency Time-Domain Signal
subplot(3, 2, 5);
plot(t, sine_new, 'c');
title('Sine Wave (50 Hz, Amplitude = 1)');
xlabel('Time (s)');
ylabel('Amplitude');
grid on;
% Plot Spectrum for New Frequency
subplot(3, 2, 6);
plot(frequencies, magnitude_new, 'k');
title('Spectrum of Sine Wave (50 Hz)');
xlabel('Frequency (Hz)');
ylabel('Magnitude');
grid on;
4. Output
1. Signal 1:
o Time domain: Sinusoidal waveform, amplitude = 1, frequency = 19 Hz.
o Frequency domain: Peaks at ±19 Hz with magnitude = amplitude.
2. Signal 2:
o Time domain: Sinusoidal waveform, amplitude = 2, frequency = 19 Hz.
o Frequency domain: Peaks at ±19 Hz with double the magnitude of Signal
1.
3. Signal 3:
o Time domain: Sinusoidal waveform, amplitude = 1, frequency = 50 Hz.
o Frequency domain: Peaks at ±50 Hz with magnitude = amplitude.
LAB-2/SPECTRUM OF SINE WAVE/BT23ECE119
LAB-2/SPECTRUM OF SINE WAVE/BT23ECE119
[Link]
Sampling converts a continuous signal into a digital one that can be processed
by computers. It breaks the signal into discrete points in time.
The frequency array is created based on the sampling rate and the number of
samples, spreading frequencies evenly from −fs/2 to 0 and 0 to fs/2, which
covers all possible frequencies in the signal.
Dividing by the total number of samples (n) ensures the FFT results reflect the
actual amplitude of the signal, as the raw FFT output is scaled by the number of
points.
fft-shift is used to rearrange the FFT output, moving the zero frequency (DC) to
the center, so the spectrum looks symmetric and easier to interpret.
The Fourier Transform of a pure sine wave is just two sharp peaks (delta
functions) at its positive and negative frequencies .When the sine wave is
limited in time (like in real life), it’s essentially multiplied by a rectangular
window. This makes its spectrum spread out into a sinc shape instead of sharp
peaks because the rectangular window causes this spreading in the frequency
domain. So, the sinc comes from limiting the sine wave’s duration.
[Link]
In this lab, we used MATLAB fft function to analyze the spectrum of a sinusoidal
waveform. By adjusting the amplitude and frequency of the signal, we saw how
these changes directly affected the spectrum. Increasing the amplitude boosted
the spectrum's magnitude, while changing the frequency shifted the spectrum
accordingly. This demonstrates how signal properties influence their behavior in
the frequency domain, providing valuable insights for signal analysis.
LAB-2/SPECTRUM OF SINE WAVE/BT23ECE119