Question: %[**Please copy and paste below code to Matlab inorder to compute the power of two square wave signals f(t) and g(t) from their complex Fourier
%[**Please copy and paste below code to Matlab inorder to compute the power of two square wave signals f(t) and g(t) from their complex Fourier series with n= 10 for two cases: 1. A period of 2 seconds and a pulse width of 1 second. 2. A period of 4 seconds and a pulse width of 1 second. and edit : Line 11: Enter a value for the period of the square waves. Line 13: Enter a value for the pulse width of the square waves. Line 118: Enter a MATLAB expression to calculate the power of the f(t) signal. Line 120: Enter a MATLAB expression to calculate the power of the g(t) signal. **]%
% Plot amplitude and phase spectra of square waves offset from each other
% by time a/2.
close all; % close all open figures. Comment out to leave figures from previous runs open
%% CHANGE VALUES OF a,T
% ---------- INPUT NEEDED ---------- %
% Period (sec)
T = 2; % CHANGE THIS VALUE
% Peak width (sec)
a = 1; % CHANGE THIS VALUE
% max value of n include in the discrete complex spectra's double sided sum
n_max = 10; % Optional: mess around with different values of n_max as well
% ----------------------------------- %
% Angular fundamental frequency (rad/s)
omega0 = 2*pi/T;
%% Time-domain Square waveforms
% Square waveform f(t): break into sections (overlap at endpoints ok for
% plotting)
tsq1 = linspace(-T, -T+a, 31)'; % 31-element x column vector
gsq1 = ones(size(tsq1)); % vector of ones the same size as xsq1
tsq2 = linspace(-T+a, 0, 31)';
gsq2 = zeros(size(tsq2));
tsq3 = linspace(0, a, 31)';
gsq3 = ones(size(tsq3));
tsq4 = linspace(a, T, 31)';
gsq4 = zeros(size(tsq4));
tsq5 = linspace(T, T+a, 31)';
gsq5 = ones(size(tsq5));
% Assemble all these sections into one square wave x vector (xsq) and y
% vector (ysq), both column vectors
tsq = [tsq1; tsq2; tsq3; tsq4; tsq5];
gsq = [gsq1; gsq2; gsq3; gsq4; gsq5];
% Plot of square wave f(t)
fig_f = figure; % create a new figure window and assign to handle fig_f
set(gcf,'Position',[18 1 560 800]); % prescribe figure position and dimensions
subplot(3,1,1) % multiple plots in one figure. 3x1 array of figures, plot in spot 1
% plot original square wave with black line, line width 2
plot(tsq-a/2,gsq,'-b','LineWidth',2); % note that f(t) = g(t - a/2)
grid on; hold on
xlabel('t'); ylabel('f(t)'); % always label axes
title('Square wave f(t) in time domain');
set(gca,'FontSize',18,'LineWidth',3,'FontWeight','bold',...
'TickLength',[0.025 0.025]); % Make axes legible
axis([-1.5*T 1.5*T -0.25 1.25]) % prescribe axis limits
axf = gca; % save handle of time-domain waveform plot for f(t)
% Plot of square wave g(t)
fig_g = figure;
set(gcf,'Position',[632 1 560 800]); % prescribe figure position and dimensions
subplot(3,1,1)
plot(tsq,gsq,'-r','LineWidth',2);
grid on;
xlabel('t'); ylabel('g(t)');
title('Square wave g(t) in time domain');
set(gca,'FontSize',18,'LineWidth',2,'FontWeight','bold',...
'TickLength',[0.025 0.025]);
axis([-1.5*T 1.5*T -0.25 1.25]);
axg = gca;
%% COMPLEX SPECTRA OF f(t), g(t)
n = (-n_max:n_max)'; % harmonic index n, column vector
% f(t)***************************** amplitude spectrum*******************
fampl = a/T*abs(sinc(n*pi*a/T));
% f(t) phase spectrum
fphase = zeros(size(n));
% g(t) ************************amplitude spectrum************************
gampl = a/T*abs(sinc(n*pi*a/T));
% g(t) amplitude spectrum
gphase = -n*pi*a/T;
% Plot f(t) spectra
figure(fig_f); % plot next commands on f(t) figure
% f(t) amplitude spectrum
subplot(3,1,2)
stem(n*pi*a/T, fampl,'LineWidth',1.5)
xlabel('Frequency n\pia/T (rad/s)'); ylabel('|c_n|');
title('Amplitude spectrum')
set(gca,'FontSize',18,'LineWidth',2,'FontWeight','bold',...
'TickLength',[0.025 0.025]);
% f(t) phase spectrum
subplot(3,1,3)
stem(n*pi*a/T, fphase,'LineWidth',1.5)
xlabel('Frequency n\pia/T (rad/s)'); ylabel('\phi_n');
title('Phase spectrum')
set(gca,'FontSize',18,'LineWidth',2,'FontWeight','bold',...
'TickLength',[0.025 0.025]);
% Plot g(t) spectra
figure(fig_g); % plot next commands on f(t) figure
% g(t) amplitude spectrum
subplot(3,1,2)
stem(n*pi*a/T, gampl,'LineWidth',1.5)
xlabel('Frequency n\pia/T (rad/s)'); ylabel('|c_n|');
title('Amplitude spectrum')
set(gca,'FontSize',18,'LineWidth',2,'FontWeight','bold',...
'TickLength',[0.025 0.025]);
% g(t) phase spectrum
subplot(3,1,3)
stem(n*pi*a/T, gphase,'LineWidth',1.5)
xlabel('Frequency n\pia/T (rad/s)'); ylabel('\phi_n');
title('Phase spectrum')
set(gca,'FontSize',18,'LineWidth',2,'FontWeight','bold',...
'TickLength',[0.025 0.025]);
%% Power of signal
% ---------- INPUT NEEDED ---------- %
% Calculate the power of the signal f(t)
power_f = abs(famp1).^2;
% Calculate the power of the signal g(t)
power_g = gamp1gamp1.^2;
% Output results to screen
fprintf(' For square waves of pulse width: %3.2f sec, period %3.2f sec:',a,T);
fprintf(' \tPower of signal f(t): %5.4e',power_f);
fprintf(' \tPower of signal g(t): %5.4e ',power_g);
% ---------------------------------- %
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
