Answer by Sulaymon Eshkabilov
on 18 May 2019

n = -5:1:5;

x=1*(n>=-2 & n<=2)+0*(n<-2 & n>2);

L = length(n); Fs = 1;

Nblock = 128; % Block size

Y = fft(x, Nblock)/L;

f = Fs/2*linspace(0,5, Nblock/2+1);

plot(f, 2*abs(Y(1:Nblock/2+1)))

xlabel('frequency, [Hz]'), ylabel('Amplitude')

title('Single-sided Spectrum'), shg

madhan ravi
on 18 May 2019

x=1*(n>=-2 & n<=2)+0*(n<-2 & n>2); % what is the necessity to multiply it by zero ??

% Hint: Not necessary

HADIMARGO
on 18 May 2019

so many tnx for ur answering. but why the shapes didnot match together?

HADIMARGO
on 18 May 2019

hi.

i could find this code that the result and shape is near to my shape.

clc

clear

t=linspace(-5,5,100);

y=heaviside(t+0.5)-heaviside(t-0.5);

z=fft(y);

z=abs(z);

stem(z)

Sign in to comment.

Answer by Sulaymon Eshkabilov
on 18 May 2019

Hi,

Here is the full spectrum calculation:

n = -5:1:5;

x=1*(n>=-2 & n<=2);

L = length(n); Fs = 1;

Nblock = 128; % Block size

Y = fft(x, Nblock)/L;

f = Fs/2*linspace(0,5, Nblock);

plot(f, 2*abs(Y(1:Nblock)))

xlabel('frequency, [Hz]'), ylabel('Amplitude')

title('Single-sided Spectrum'), shg

Good luck.

Walter Roberson
on 18 May 2019

fftshift()

HADIMARGO
on 19 May 2019

can u explain more mr Walter Roberson.

Walter Roberson
on 19 May 2019

t = linspace(0,10);

ft = fft(t);

subplot(1,2,1); plot(abs(ft)); %what you are doing now

subplot(1,2,2); plot(abs(fftshift(ft))); %what you need to do in order to get the 0 in the center

Sign in to comment.

Opportunities for recent engineering grads.

Apply Today
## 4 Comments

## darova (view profile)

## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/462727-how-to-plot-this-function#comment_706332

## HADIMARGO (view profile)

## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/462727-how-to-plot-this-function#comment_706338

## HADIMARGO (view profile)

## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/462727-how-to-plot-this-function#comment_706339

## Walter Roberson (view profile)

## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/462727-how-to-plot-this-function#comment_706451

Sign in to comment.