MATLAB Code
MATLAB Code
a) Plot the magnitude and phase of the frequency domain of a rectangular pulse of
2 V amplitude and 40 samples (choose three values of N).
% MATLAB Code for Rectangular Pulse Analysis
% Rectangular Pulse Parameters
amplitude = 2; % Amplitude of the rectangular pulse
num_samples = 40; % Number of samples
pulse = amplitude * ones(1, num_samples); % Define the rectangular pulse
% Different values of N for Fourier Transform
N_values = [20, 40, 80]; % Chosen values of N
for i = 1:length(N_values)
N = N_values(i); % Current N value
% Zero-padding to match N
padded_pulse = [pulse, zeros(1, N - num_samples)];
% Compute the DFT
freq_domain = fft(padded_pulse);
% Magnitude and Phase
magnitude = abs(freq_domain);
phase = angle(freq_domain);
% Plot Magnitude and Phase
figure;
subplot(2, 1, 1);
plot(magnitude, 'LineWidth', 1.5);
title(['Magnitude Spectrum (N = ', num2str(N), ')']);
xlabel('Frequency Index');
ylabel('Magnitude');
grid on;
subplot(2, 1, 2);
plot(phase, 'LineWidth', 1.5);
title(['Phase Spectrum (N = ', num2str(N), ')']);
xlabel('Frequency Index');
ylabel('Phase (radians)');
grid on;
end
b) Plot the time domain of the frequency domain obtained in (a).
% MATLAB Code for Rectangular Pulse Analysis
% Rectangular Pulse Parameters
amplitude = 2; % Amplitude of the rectangular pulse
num_samples = 40; % Number of samples
pulse = amplitude * ones(1, num_samples); % Define the rectangular pulse
% Different values of N for Fourier Transform
N_values = [20, 40, 80]; % Chosen values of N
for i = 1:length(N_values)
N = N_values(i); % Current N value
% Zero-padding to match N
padded_pulse = [pulse, zeros(1, N - num_samples)];
% Compute the DFT
freq_domain = fft(padded_pulse);
% Magnitude and Phase
magnitude = abs(freq_domain);
phase = angle(freq_domain);
% Plot Magnitude and Phase
figure;
subplot(2, 1, 1);
plot(magnitude, 'LineWidth', 1.5);
title(['Magnitude Spectrum (N = ', num2str(N), ')']);
xlabel('Frequency Index');
ylabel('Magnitude');
grid on;
subplot(2, 1, 2);
plot(phase, 'LineWidth', 1.5);
title(['Phase Spectrum (N = ', num2str(N), ')']);
xlabel('Frequency Index');
ylabel('Phase (radians)');
grid on;
% Inverse DFT to get Time Domain Signal
time_domain = ifft(freq_domain);
% Plot Time Domain Signal
figure;
plot(real(time_domain), 'LineWidth', 1.5);
title(['Time Domain Signal (N = ', num2str(N), ')']);
xlabel('Sample Index');
ylabel('Amplitude');
grid on;
end