In: Electrical Engineering
Write a new MATLAB function as described in the following:
1. The new function will have three input parameters: f, W, C. 2. Parameter f will specify the frequency of the square wave. 3. The parameter W will specify the width of the single pulse as a number of sample periods. 4. The parameter C will specify the number of square wave cycles. 5. Calculate a number of samples, N, to run the simulation for both waveforms. 6. Create one signal that is a square wave of period 2W samples and lasts for a duration of C cycles. 7. Create a signal beginning with a single pulse of amplitude 1 and a width of W samples followed by N-W samples with amplitude of 0. 8. Plot time and frequency domain plots for each. Use subplots to show all four graphs on the screen at once, arranged the same way as in the reference lecture slide. Scale the plots as necessary so that the desired characteristics can be easily seen. Label the x-axis with the correct units of time/frequency. Run the new function with f=1000, W=20, and C=10.
(F,W,C) function signal-pulse:
% define default input parameter values
if nargin < 3 C = 10;
end % C = number of cycles of square wave
if nargin < 2 W = 20;
end % W = number of samples for pulse width
if nargin < 1 f = 1e3;
end % f = frequency of square wave
disp(f)
fs = f*W*2;
% fs = sampling frequency
N = fs/W;
t = [0: N-1]* 1/fs;
% generate the square waves
S1 = square(f*t);
% Periodic Squarewave
S2 = square(f*t);
% Aperiodic Squarewave
F1 = abs(fft(S1));
F2 = abs(fft(S2));
% Plot each sinewave and then various sums
figure % plot should open figure
subplot(2,2,1);
plot(t,S1);
title('Periodic Squarewave(Time Domain)');
xlabel('Time');
ylabel('Amplitude');
subplot(2,2,2);
plot(t,S2);
title('Aperiodic Squarewave(Time Domain)');
xlabel('Time');
ylabel('Amplitude');
subplot(2,2,3);
plot(fs,F1);
title('Periodic Squarewave(Frequency Domain)');
xlabel('Frequency');
ylabel('Amplitude');
subplot(2,2,4);
plot(f,F2);
title('Aperiodic Squarewave(Frequency Domain)');
xlabel('Frequency');
ylabel('Amplitude');
end
signal_pulse(1000,20,10);
% calling function