How to sum particular values on the y axis

I wish to sum the y axis values for the frequencies between 300Hz to 1000Hz, how do I do this?

Answers (1)

X = 0:1300;
Y = rand(size(X))
Y = 1×1301
0.5067 0.8325 0.5811 0.4961 0.8288 0.9811 0.1431 0.7304 0.9984 0.4814 0.9243 0.8970 0.6703 0.2280 0.5562 0.2614 0.9846 0.0387 0.7453 0.3483 0.9077 0.3178 0.5999 0.7899 0.3604 0.9220 0.2648 0.7657 0.1160 0.4923
s = sum(Y(X>=300 & X<=1000))
s = 355.7618

7 Comments

Can you explain what you have done here
I generated X and Y arrays that can be interpreted as one curve in your graphics.
Then I chose the Y-values for which the corresponding X-values are between 300 and 1000 and summed them up.
Apologies for the lengthy chunk of code but I have used this for loop to plot the 10 curves as shown in the original message, does it look like I am applying the sum function correctly to return back the sum of the Y values corresponding to the X values from 300Hz to 1000Hz.
Your code only returns the sum over the Y-values for the last curve (i=10).
If you want to sum over all the 10 curves, you should code
Mag = 0;
for i = 1:10
...
Mag = Mag + sum(Y(X>=300 & X <=1000));
...
end
If you want to sum over each curve separately, you should code
Mag = zeros(10,1);
for i = 1:10
...
Mag(i) = sum(Y(X>=300 & X <=1000));
...
end
I wish to sum the value of each curve, I have used your code to do this. Does the code now look fine to sum over these ten curves?
Torsten
Torsten on 3 May 2023
Edited: Torsten on 3 May 2023
I wish to sum the value of each curve, I have used your code to do this. Does the code now look fine to sum over these ten curves?
Yes, if by "summing" you really mean "summing the Y-values". If you mean "integrate over the Frequency", you will have to multiply the result by "sampleFreq/burst_length". Or use "trapz".
No I just mean summing the Y values corresponding to the X values between 300Hz and 1000Hz but it seems that I have done that now.
Thanks Torsten

Sign in to comment.

Products

Release

R2022a

Asked:

on 3 May 2023

Commented:

on 3 May 2023

Community Treasure Hunt

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

Start Hunting!