14 views (last 30 days)

The full code is:

Y = fft(X);

P2 = abs(Y/L);

P1 = P2(1:L/2+1);

P1(2:end-1) = 2*P1(2:end-1);

f = Fs*(0:(L/2))/L;

plot(f,P1);

title('Single-Sided Amplitude Spectrum of X(t)');

John D'Errico
on 27 Feb 2018

Edited: John D'Errico
on 27 Feb 2018

end (when used as a subscript) is a shorthand to indicate the last position for the corresponding index.

So V(end) refers to the last element of a vector V. V(2:end) takes all elements but the first in a vector. And V(2:end-1) is the 2nd through penultimate element. This also applies to array indexing.

Therefore

P1(2:end-1) = 2*P1(2:end-1);

merely doubles the indicated elements in the vector P1.

I suppose use of the end keyword might have been confusing to some. They might have created some new keyword name for use in subscripting. They might, for example, have given us a lastIndex keyword, which might be clearer to some. But that in turn is longer to write, and establishes an entirely new keyword, possibly disabling some older code. Since end already existed as a keyword, but one that had no valid context in a subscript, it was deemed safe to use for indexing.

Sign in to comment.

Sign in to answer this question.

Opportunities for recent engineering grads.

Apply Today
## 8 Comments

## Direct link to this comment

https://uk.mathworks.com/matlabcentral/answers/385136-why-doubling-is-done-from-2-to-end-1-only-in-plotting-single-sided-spectrum-p1-2-end-1-2-p1-2-en#comment_540035

⋮## Direct link to this comment

https://uk.mathworks.com/matlabcentral/answers/385136-why-doubling-is-done-from-2-to-end-1-only-in-plotting-single-sided-spectrum-p1-2-end-1-2-p1-2-en#comment_540035

## Direct link to this comment

https://uk.mathworks.com/matlabcentral/answers/385136-why-doubling-is-done-from-2-to-end-1-only-in-plotting-single-sided-spectrum-p1-2-end-1-2-p1-2-en#comment_540045

⋮## Direct link to this comment

https://uk.mathworks.com/matlabcentral/answers/385136-why-doubling-is-done-from-2-to-end-1-only-in-plotting-single-sided-spectrum-p1-2-end-1-2-p1-2-en#comment_540045

## Direct link to this comment

https://uk.mathworks.com/matlabcentral/answers/385136-why-doubling-is-done-from-2-to-end-1-only-in-plotting-single-sided-spectrum-p1-2-end-1-2-p1-2-en#comment_540052

⋮## Direct link to this comment

https://uk.mathworks.com/matlabcentral/answers/385136-why-doubling-is-done-from-2-to-end-1-only-in-plotting-single-sided-spectrum-p1-2-end-1-2-p1-2-en#comment_540052

## Direct link to this comment

https://uk.mathworks.com/matlabcentral/answers/385136-why-doubling-is-done-from-2-to-end-1-only-in-plotting-single-sided-spectrum-p1-2-end-1-2-p1-2-en#comment_540088

⋮## Direct link to this comment

https://uk.mathworks.com/matlabcentral/answers/385136-why-doubling-is-done-from-2-to-end-1-only-in-plotting-single-sided-spectrum-p1-2-end-1-2-p1-2-en#comment_540088

## Direct link to this comment

https://uk.mathworks.com/matlabcentral/answers/385136-why-doubling-is-done-from-2-to-end-1-only-in-plotting-single-sided-spectrum-p1-2-end-1-2-p1-2-en#comment_540321

⋮## Direct link to this comment

https://uk.mathworks.com/matlabcentral/answers/385136-why-doubling-is-done-from-2-to-end-1-only-in-plotting-single-sided-spectrum-p1-2-end-1-2-p1-2-en#comment_540321

## Direct link to this comment

https://uk.mathworks.com/matlabcentral/answers/385136-why-doubling-is-done-from-2-to-end-1-only-in-plotting-single-sided-spectrum-p1-2-end-1-2-p1-2-en#comment_540396

⋮## Direct link to this comment

https://uk.mathworks.com/matlabcentral/answers/385136-why-doubling-is-done-from-2-to-end-1-only-in-plotting-single-sided-spectrum-p1-2-end-1-2-p1-2-en#comment_540396

## Direct link to this comment

https://uk.mathworks.com/matlabcentral/answers/385136-why-doubling-is-done-from-2-to-end-1-only-in-plotting-single-sided-spectrum-p1-2-end-1-2-p1-2-en#comment_540413

⋮## Direct link to this comment

https://uk.mathworks.com/matlabcentral/answers/385136-why-doubling-is-done-from-2-to-end-1-only-in-plotting-single-sided-spectrum-p1-2-end-1-2-p1-2-en#comment_540413

## Direct link to this comment

https://uk.mathworks.com/matlabcentral/answers/385136-why-doubling-is-done-from-2-to-end-1-only-in-plotting-single-sided-spectrum-p1-2-end-1-2-p1-2-en#comment_546981

⋮## Direct link to this comment

https://uk.mathworks.com/matlabcentral/answers/385136-why-doubling-is-done-from-2-to-end-1-only-in-plotting-single-sided-spectrum-p1-2-end-1-2-p1-2-en#comment_546981

Sign in to comment.