Energy and Power of a Piecewise Function

17 views (last 30 days)
I have an exercise where I must calculate the Power and Energy of a Signal based on these equations:
Px = lim T→∞ (1/2T)* integral from -T to T of |x(t)|^2 dt. This is for Power
Ex = lim T→∞ integral from -T to T of |x(t)|^2 dt. This is for Energy
My signal is a piecewise function defined as such:
Cos(π*t) -3 <= t <= 0
2 0 < t <= 2
x(t) = t- 1 2 < t <= 4
t + 2 4 < t <= 5
0 otherwise
I am able to solve this problem by hand but I am unsure what is the code I should be writing to get the result. (I know that my Ex is approx. 60.5 while Px is 0 based on hand solving).
My latest itteration is the following:
syms t T
d1 = int(abs(cos(pi*t))^2,t,-T,T);
d2 = int(abs(2)^2,t,-T,T);
d3 = int(abs(t - 1)^2,t,-T,T);
d4 = int(abs(t + 2)^2,t,-T,T);
Ex1 = limit(d1,T,inf);
Ex2 = limit(d2,T,inf);
Ex3 = limit(d3,T,inf);
Ex4 = limit(d4,T,inf);
Px1 = limit((1/2*T)*d1,T,inf);
Px2 = limit((1/2*T)*d2,T,inf);
Px3 = limit((1/2*T)*d3,T,inf);
Px4 = limit((1/2*T)*d4,T,inf);
SumEx = Ex1 + Ex2 + Ex3 + Ex4;
SumPx = Px1 + Px2 + Px3 + Px4;
fprintf("The Energy and Power are: \n", SumEx, SumPx);
(This does not print anything as the values of Px and Ex are infinity)
I'm quite lost at this point and not entirely sure what I could try next to fix it.
  1 Comment
Iakovos Michail
Iakovos Michail on 18 Nov 2022
So upon further analysis of how I defined this function in my handwritten solution, I came up with new code that seems to solve the exercise, albeit it seems a little "forced" by my standards. Based on the equations I provided above, these following transformations are done for finite time calculations:
Px = lim T→∞ (1/2T)* integral from -t1 to t2 of |x(t)|^2 dt.
Ex = integral from -t1 to t2 of |x(t)|^2 dt
Now assume my t1 and t2 are from Pos Inf to Neg Inf with intermediate values as well. By hand, this would resolve in multiple integrals with boundaries changing values (Neg Inf to -3, -3 to 0, 0 to 2 .... 5 to Pos Inf)
So that is exactly what I did. The code below is an implementation of that thought process. I am not sure if it is an optimal solution, but at least it works.
clear all
%Seperating the domain from neg inf to pos inf with
%intermediate values used for the various parts of
%the piecewise function x
tninf = -inf;
t1 = -3;
t1_2 = 0;
t2_3 = 2;
t3_4 = 4;
t4 = 5;
tpinf = inf;
syms t T
%Defining the integrals and Energies for each domain component
%each integral contains the value of x at the appropriate
%domain withing the abs() function
dninf = int(abs(0)^2,t,tninf,t1);
d1 = int(abs(cos(pi*t))^2,t,t1,t1_2);
d2 = int(abs(2)^2,t,t1_2,t2_3);
d3 = int(abs(t -1)^2,t,t2_3,t3_4);
d4 = int(abs(t + 2)^2,t,t3_4,t4);
dpinf = int(abs(0)^2,t,t4,tpinf);
Ex_ninf = dninf;
Ex1 = d1;
Ex2 = d2;
Ex3 = d3;
Ex4 = d4;
Ex_pinf = dpinf;
%Outputing the resulting Power and Energy values
Px = limit((1/2*T)*(dninf+d1+d2+d3+d4+dpinf),T,inf)
Px = 
Ex = Ex1 + Ex2 + Ex3 + Ex4
Ex = 

Sign in to comment.

Accepted Answer

Paul on 18 Nov 2022
Hi Iakovos,
Try using piecewise to simplify things.
syms t real
x(t) = piecewise(-3 <= t <= 0, cos(sym(pi)*t),0 < t <= 2, 2, 2 < t <= 4, t-1, 4 < t <= 5, t + 2, 0)
x(t) = 
fplot(x(t),[-5 10])
E = int(x(t)^2,t,-inf,inf)
E = 
Because the energy in the signal is finite, the power must be zero as you stated in the Question and no computation is needed.
But in your comment you calculated P = inf, which can't be correct. I didn't run your code in that comment, but I did notice there is a term (1/2*T) in the calculation of Px, which should be 1/2/T or 1/(2*T).
If we need to do the power calculation formally, it should be like this I believe
syms T real
P = limit(1/2/T*int(x(t)^2,t,-T,T),T,inf)
P = 
Paul on 19 Nov 2022
Actually, I think the definitions of energy and power in the links I provided are more natural. Maybe it depends on how one learned the concepts.
Of course, those definitions apply only when the integrals in question exist. I'm not sure if that's the case for random signals. I'm getting a bit out of my comfort zone, but I think the power in a random signal that satisfies certain properties (which I think is a power signal by definition) is characterized by a power spectral density via its autocorrelation function (link).
Also, but don't quote me on this, I thought that spectral estimation techniques based on a finite number of samples assume that the underlying signal from which the samples were collected is "stationary," i.e., the properties of the signal across the entire real line are the same (or close enough?) as the propeties of the signal over the portion of the real line on which the samples were collected. The point being that the underlying signal is really of infinite duration, but the finite duration samples are sufficient for characterization.

Sign in to comment.

More Answers (1)

Askic V
Askic V on 18 Nov 2022
Edited: Askic V on 18 Nov 2022
This is how I would do it and how it seems natural to me. It doesn't make sense to perform integration based on theoretical definitions when signal doesn't exist from -Inf to Inf.
I have provided to ways, one purely numerical and other with using function handles:
% I way: pure numerical integration
dt = 1e-3; % decreasing step dt will increase the accuracy of computation
t0 = -3;
tf = 5;
t = t0:dt:tf;
y = zeros(size(t));
% define piecewise function y = f(t)
% 1st segment
ind = find(t>=-3 & t<=0);
y(ind) = cos(pi*t(ind));
% 2nd segment
ind = find(t>0 & t<=2);
y(ind) = 2;
% 3rd segment
ind = find(t>2 & t<=4);
y(ind) = t(ind)-1;
% 4th segment
ind = find(t>4 & t<=5);
y(ind) = t(ind)+2;
%Calclate power
P = cumtrapz(t, y.^2)/(tf-t0);
totalPower = P(end)
totalPower = 7.5608
% calculate energy
E = cumtrapz(t, y.^2);
totalEnergy = E(end)
totalEnergy = 60.4865
%% II way using function handles
% 1st segment
t1 = -3;
t2 = 0;
y = @(t) (cos(pi*t).^2);
E1 = integral(y,t1, t2);
% 2nd segment
t3 = 0;
t4 = 2;
y = @(t) 2^2;
E2 = integral(y,t3, t4, 'ArrayValued', true);
% 3rd segment
t5 = 2;
t6 = 4;
y = @(t) (t-1).^2;
E3 = integral(y,t5, t6);
% 4th segment
t7 = 4;
t8 = 5;
y = @(t) (t+2).^2;
E4 = integral(y,t7, t8);
totalE = E1+E2+E3+E4
totalE = 60.5000
totalP = totalE/(t8-t1)
totalP = 7.5625


Find more on Fourier Analysis and Filtering in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by