# Calculating power and energy of a signal in matlab

I am a little new to matlab and would like to get some help on how to write a program or a function that can calculate the energy/power of a signal depending on whether the signal is an energy or a power signal. Here is the program the I tried:

function [ ] = power_or_energy( s,t)
%UNTITLED Summary of this function goes here
%   s is the input signal
%   t is the independent variable

s2=s.^2;
energy=int(s2,t,-inf,inf);

if energy~=inf && energy~=-inf
disp ('the signal is an energy signal with energy equal to ' );
disp(energy);
end

power=limit((int(s2,t,-t/2,t/2))/t,t,inf);

if power~=inf && power~=-inf
disp('the signal is a power signal with power equal to ');
disp(power);
end


But the problem here is that when i want to check it for a function like

t=0:0.01:10;
s=sin(t);
power_or_energy(s,t);


then i get the following error: "Undefined function 'int' for input arguments of type 'double'."

So how to i actually calculate the energy for such signals?

Also the above function works fine with the below input:

syms t;
s=sin(t);
power_or_energy(s,t);


Please help me in writing a function that can be used to find the energy of any input signal.

The Matlab function int needs a symbolic expression as an integrand, and a symbolic variable as an integration variable. In your example s and t are both vectors, and you can't use int in this case. If you want to compute the power or energy of discrete time signals, then you need to use the corresponding definitions:
$$E_x=\sum_{n=-\infty}^{\infty}|x_n|^2\\ P_x=\lim_{N\rightarrow\infty}\frac{1}{2N+1}\sum_{n=-N}^N|x_n|^2$$
In practice you will need to consider finite length signals unless you have an analytic formula for $x_n$ and some software package that can deal with the analytic evaluation of infinite sums. Obviously, finite length signals always have finite energy and $P_x=0$ according to above definition.
• @user94533: You mean a periodic ramp (sawtooth)? For periodic signals the power is $P_x=\frac{1}{N}\sum_{n=0}^{N-1}|x_n|^2$, where $N$ is the period. – Matt L. Sep 8 '14 at 12:28