Computation of energy of rectangular pulse

Aim: Computation of energy of rectangular pulse

PC with windows OS.

MATLAB Software

%This Program generates a rectangular pulse and finds its energy

f=input('enter the sampling frequency')
T=1/f;
t=-T:T/f:T;
x=zeros(1,length(t));
c=input('enter the duration of the rectangular pulse in terms of  number of samples to included')
i=find(t==0);
x(i:i+c-1)=0.25;
E=sum(x.*x);
plot(t,x)
xlabel('time')
ylabel('amplitude')
title('Rectangular Pulse width')
grid
disp('Energy of the generated signal measured in joules is')
disp(E)

Result:

enter the sampling frequency100

f = 100

enter the duration of the rectangular pulse in terms of  number of samples to included50

c =50

Energy of the generated signal measured in joules is

   3.1250

 Output: