how to plot this function

4 件のコメント

darova
darova 2019 年 5 月 17 日
Do you have any attempts?
HADIMARGO
HADIMARGO 2019 年 5 月 17 日
Yes sir. But it didnot give me the right shape:
HADIMARGO
HADIMARGO 2019 年 5 月 17 日
Walter Roberson
Walter Roberson 2019 年 5 月 18 日
(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.

サインインしてコメントする。

回答 (2 件)

Sulaymon Eshkabilov
Sulaymon Eshkabilov 2019 年 5 月 18 日

1 投票

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

3 件のコメント

madhan ravi
madhan ravi 2019 年 5 月 18 日
編集済み: madhan ravi 2019 年 5 月 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
HADIMARGO 2019 年 5 月 18 日
so many tnx for ur answering. but why the shapes didnot match together? compare.jpg
HADIMARGO
HADIMARGO 2019 年 5 月 18 日
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)
near.jpg

サインインしてコメントする。

Sulaymon Eshkabilov
Sulaymon Eshkabilov 2019 年 5 月 18 日

1 投票

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.

4 件のコメント

HADIMARGO
HADIMARGO 2019 年 5 月 18 日
tnx. this is so near also to final shape but my result is:
question2.jpg
Walter Roberson
Walter Roberson 2019 年 5 月 18 日
fftshift()
HADIMARGO
HADIMARGO 2019 年 5 月 19 日
can u explain more mr Walter Roberson.
Walter Roberson
Walter Roberson 2019 年 5 月 19 日
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

サインインしてコメントする。

カテゴリ

質問済み:

2019 年 5 月 17 日

コメント済み:

2019 年 5 月 19 日

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by