# how to plot this function

21 views (last 30 days)
Commented: Walter Roberson on 19 May 2019

Show 1 older comment
Yes sir. But it didnot give me the right shape:
Walter Roberson on 18 May 2019
(n<-2 & n>2)
is never true. However, since you are multiplying that by 0, if it somehow did match it would contribute 0 anyhow. You can just leave out that term.
Your x axis is wrong. You are effectively using your n as your frequency, including negative n. If you are going to plot negative frequencies then you need to fftshift() . And use more points.
Read the first example of fft() to see how to properly plot.

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
so many tnx for ur answering. but why the shapes didnot match together?
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)

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.

Show 1 older comment
Walter Roberson on 18 May 2019
fftshift()