Assignment 2
Assignment 2
➢ Find the Fourier transform for the generated signals (n=1024). (hint: FFT is symmetric,
throw away second half, only take the first half number of points of the signal after finding
FFT)
➢ Find the magnitude for the generated signals after Fourier transform.
➢ Plot each generated signal with its corresponding magnitude after Fourier transform (use
subplot command). Make sure to title the graph, label the x axis and the y axis.
Hint: Search how to create the square wave and the square pulse in MATLAB.
Hint: Use the time vector from -0.5 to 0.5 in the square pulse instead of using it from 0 to 1.
CODE:
clear
clc
% Sampling rate
fs = 150;
% Time vector
t = linspace(0, 1, fs);
% Sine Wave
f_sin = 5;
x_sin = sin(2*pi*f_sin*t);
% Cosine Wave
x_cos = cos(2*pi*f_sin*t);
% Cosine Wave with Phase Shift
phase_shift = pi/4;
x_cos_shifted = cos(2*pi*f_sin*t + phase_shift);
% Square Wave
x_square = square(2*pi*f_sin*t);
% Square Pulse
width = 0.2; % Width of the rectangle
x_pulse = rectpuls(t - 0.5, width);
% Exponential Decay
decay_constant = 5;
x_decay = exp(-decay_constant*t);
% Fourier Transform
N = 1024;
frequencies = linspace(0, fs/2, N/2);
fft_sin = abs(fft(x_sin, N));
fft_cos = abs(fft(x_cos, N));
fft_cos_shifted = abs(fft(x_cos_shifted, N));
fft_square = abs(fft(x_square, N));
fft_pulse = abs(fft(x_pulse, N));
fft_decay = abs(fft(x_decay, N));
% Plotting
figure;
% Plotting Sine Wave vs magnitude spectrum
subplot(3, 2, 1);
plot(t, x_sin);
title('Sine Wave');
xlabel('Time (s)');
ylabel('Amplitude');
subplot(3, 2, 2);
plot(frequencies, fft_sin(1:N/2));
title('Magnitude Spectrum - Sine Wave');
xlabel('Frequency (Hz)');
ylabel('Magnitude');
% Plotting Cosine Wave vs magnitude spectrum
subplot(3, 2, 3);
plot(t, x_cos);
title('Cosine Wave');
xlabel('Time (s)');
ylabel('Amplitude');
subplot(3, 2, 4);
plot(frequencies, fft_cos(1:N/2));
title('Magnitude Spectrum - Cosine Wave');
xlabel('Frequency (Hz)');
ylabel('Magnitude');
% Plotting Cosine Wave with Phase Shift vs magnitude spectrum
subplot(3, 2, 5);
plot(t, x_cos_shifted);
title('Cosine Wave with Phase Shift');
xlabel('Time (s)');
ylabel('Amplitude');
subplot(3, 2, 6);
plot(frequencies, fft_cos_shifted(1:N/2));
title('Magnitude Spectrum - Cosine Wave with Phase Shift');
xlabel('Frequency (Hz)');
ylabel('Magnitude');
figure;
% Plotting Square Wave vs magnitude spectrum
subplot(2, 3, 1);
plot(t, x_square);
title('Square Wave');
xlabel('Time (s)');
ylabel('Amplitude');
subplot(2, 3, 2);
plot(frequencies, fft_square(1:N/2));
title('Magnitude Spectrum - Square Wave');
xlabel('Frequency (Hz)');
ylabel('Magnitude');
% Plotting Square Pulse vs magnitude spectrum
subplot(2, 3, 3);
plot(t, x_pulse);
title('Square Pulse');
xlabel('Time (s)');
ylabel('Amplitude');
subplot(2, 3, 4);
plot(frequencies, fft_pulse(1:N/2));
title('Magnitude Spectrum - Square Pulse');
xlabel('Frequency (Hz)');
ylabel('Magnitude');
% Plotting Exponential Decay vs magnitude spectrum
subplot(2, 3, 5);
plot(t, x_decay);
title('Exponential Decay');
xlabel('Time (s)');
ylabel('Amplitude');
subplot(2, 3, 6);
plot(frequencies, fft_decay(1:N/2));
title('Magnitude Spectrum - Exponential Decay');
xlabel('Frequency (Hz)');
ylabel('Magnitude');
RESULTS: