How to sum particular values on the y axis
Show older comments

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))
s = sum(Y(X>=300 & X<=1000))
7 Comments
Dan Lecoutre
on 3 May 2023
Torsten
on 3 May 2023
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.
Dan Lecoutre
on 3 May 2023
Torsten
on 3 May 2023
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
Dan Lecoutre
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".
Dan Lecoutre
on 3 May 2023
Categories
Find more on Numerical Integration and Differentiation 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!
