Getting error using syms to implement power formula

Hi,
I am trying to implement the power formula which is in the attached image.
I have written my code like this:
.
.
syms N; %Power formula
syms fs_sym;
fs_sym=sym(fs);
N=fs_sym*3;
syms x_hsym;
syms n;
syms f1;
syms f;
syms Power;
x_hsym=sym(x_h);
%f= (1/(2*N+1))*symsum((abs(x_h(n))),n,-N,N);
f1=symsum((abs(x_hsym(n))),n,-N,N);
f= (1/(2*N+1))*f1;
Power=limit(f,N,inf);
This code is in continuation with some more code. So x_h along with other variabes were double, so I changed all of them to symbolic variales. But I am getting error:
Error using sym/subsindex (line 814)
Invalid indexing or function definition. Indexing must follow MATLAB indexing. Function arguments must be symbolic variables, and
function body must be sym expression.
Error in sym/subsref (line 859)
R_tilde = builtin('subsref',L_tilde,Idx);
Error in BestAlgorithmDetection (line 97)
f1=symsum((abs(x_hsym(n))),n,-N,N);
I tried a couple of things, but cannot fix it. Can anyone help or provide a better way to implement the avove formula?

 Akzeptierte Antwort

Like this?
% Arbitrary values - replace with your own
fs = 1;
x_h = @(n) 1/(n+10)^2;
%Power formula
syms N fs_sym x_hsym n f Power;
fs_sym=sym(fs);
N=fs_sym*3;
f= (1/(2*N+1))*symsum((abs(x_h(n))),n,[-N,N]);
Power=limit(f,n,inf);
disp(Power)

17 Kommentare

Giggs B.
Giggs B. am 28 Jun. 2021
Hi Alan, Thak you for the reply.
However, x_h is a matrix of [1x452280] elements and I need to use those elements. I checked and realized that I was creating only [1x1] matrix (x_hsym) and trying to store the bigger matrix. But then how do I change the numberic matrix to sym matrix?
I used this https://www.mathworks.com/help/symbolic/numeric-to-symbolic-conversion-1.html for referece, but about matrix isn't mentioned here.
I don't fully understand what you are trying to do here. You need a function in symsum. rather than a list of numbers. How does n relate to the numbers in your big vector. What are you trying to add up with symsum? What is the function you are trying to find the limit of?
Giggs B.
Giggs B. am 28 Jun. 2021
Bearbeitet: Giggs B. am 28 Jun. 2021
Hi Alan, I think you caught my problem. I was doing very basic mistake.
Basically, I have an audio signal, which I am reading through audioread() function and storing in x, then I am using using a high pass filter to convert to x_h. Now, I want to find power of this signal.
[x,fs]=audioread('fr1.mp3');
x_h=highpass(x,1000,fs);
I understand that this signal doesn't have a defined function and hence no "n" and I was trying to implement the symsum function which is totally invalid.
But how do I find power of this signal using the above formula where I have matrix x_h containing values of samples.
Don't you just need
N = numel(x_h);
Power = sum(x_h.^2)/(2*N+1);
Thank you Alan. I am confused on how you reached to that formula from the formula that I mentioned. I believe I would have to read more so as to get a grasp of why that happened.
However, in that case...shouldn't these both formulas (below) produce the same result? Since RMS also means the same thing as mentioned here: https://www.mathworks.com/help/signal/ug/measure-the-power-of-a-signal.html
PowerWaterflow1 = rms(x_h)^2
PowerWaterflow = sum(x_h.^2)/(2*N+1);
But I receive different values or to be precise PowerWaterflow is half of PowerWaterflow1:
PowerWaterflow 4.3219e-06
PowerWaterflow1 8.6438e-06
Why would that be?
Alan Stevens
Alan Stevens am 28 Jun. 2021
Bearbeitet: Alan Stevens am 28 Jun. 2021
I used your formula without thinking about the fact that you had the sum from -N to N. My sum just goes from 1 to N. I should have divided by N instead of 2N+1.
Giggs B.
Giggs B. am 28 Jun. 2021
Got it. Thank you!
The formula for Power that was posted in the question defines Power as a limit, which is a common definition from what I've seen. With this definition, any finite (duration) sequence, as appears to the be case here, would have Power = 0, wouldn't it? I did find one source the explicitly defines "average power of a discrete-time signal x[n] over a finite interval as
sum(x.^2)/(numel(x)-1)
So i guess the answer to the question really depends on the definiton of "Power" that is intended to be used.
Giggs B.
Giggs B. am 28 Jun. 2021
@Paul I know that energy signals have zero power and power sigals have zero energy. In this case, I don't think it's either a power signal or energy signal, hence power should not be zero. Could you please attach the link for this post if it's possible? I want to understand how did that come.
Paul
Paul am 28 Jun. 2021
I thought that power signals, i.e., signals that have non-zero finite power (using the definition of power in the original Question) have infinite energy. In this case, you have an audio signal xh[n], which is zero for n < 1 and for n > 452280. So that sounds like an energy signal because the sum(xh[n].^2) < inf.
Because you asked, I'll post the link that has a defnition of average power over a finite interval: link. However, I'll caution that this is the only reference I've found that has that definition.
@Paul, yes you are absolutely right and thanks for the explanantion and correcting the defination of the power signals. My bad, what I said was incorrect. Power signals have finite power and infinite energy, however, energy signals have finite energy and zero average power.
And yes,
sum(x_h[n].^2)
is finite but
sum(x_h[n].^2)/N
is also finite so that is why I said, it is neither an energy signal nor power signal. To confirm, I also get non-zero/non-inf values when calculating power/energy in MATLAB. I checked the link and I saw the formula that you stated...in my signal since the time goes from 0 to N, hence the denominator would be just N, as stated by @Alan Stevens
sum(x.^2)/numel(x)
Thanks for the explanation, please correct me if I am wrong somewhere.
Paul
Paul am 29 Jun. 2021
Bearbeitet: Paul am 30 Jun. 2021
Except the definition of a "power signal" (to my understanding) is based on "power" as defined in your question, which is defined with that limit. So any finite duration signal, as in this case, will have zero power using that definition. Even the link that I posted that included a defnition of power "over a finite interval" defines a power signal based on power over the infinite interval. So this signal is an energy signal, unless you want to redefine the definition of power for determining if a signal is a power signal. Note that if you (re)define power to be only taken over the finitie duration of a sequence, then every finite duration sequence will have finite energy and finite power (which migh be why a power signal isn't defined that way?).
If you want to use that link's defnition of power over a finite interval, bear in mind that definition has n2 - n1 in the denominator, which in this case is 452280 - 1 = 452279 = numel(xh) - 1.
Having said all of this, if x[n] is periodic with period N, then x[n] is a power signal with power
P=sum(xp[n].^2)/N;
where xp is one period of x. But I didn't think we are talking about a periodic signal.
Giggs B.
Giggs B. am 30 Jun. 2021
Hi @Paul, Thank you...I think I got the point you were trying to make. But I just have one doubt, since my time interval starts from 0 and not from 1...wouldn't the denominator be 452280-0 instead of subtracting 1?
Paul
Paul am 30 Jun. 2021
The Matlab variable xh has dimensions 1x452280; it has 452280 elements. So the independent, discrete variable, n, must also have 452280 elements. So you could have n = 1:452280, n = 0:452279 (it sounds like this is what you want based on your comment), or more generally n = (1:452280) + k, where k is any integer. What you actually use depends on what you're trying to model and/or what you're going to do with xh.
Giggs B.
Giggs B. am 30 Jun. 2021
Oh, I see...I get it now. Thank you for the explanation @Paul.
Paul
Paul am 30 Jun. 2021
I should have been more precise. The independent variable n is defined as -inf < n < inf. But I think you know what I meant.
Giggs B.
Giggs B. am 30 Jun. 2021
@Paul yes, I got that. Thanks :)

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Mathematics finden Sie in Hilfe-Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by