Answer by Sulaymon Eshkabilov
on 18 May 2019 at 4:12

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 at 4:18

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 at 9:01

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

HADIMARGO
on 18 May 2019 at 17:41

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 at 17:49

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 at 19:15

fftshift()

HADIMARGO
on 19 May 2019 at 2:32

can u explain more mr Walter Roberson.

Walter Roberson
on 19 May 2019 at 3:51

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.