What is the problem in this code?

1 回表示 (過去 30 日間)
Hassan Ali Shah
Hassan Ali Shah 2015 年 1 月 2 日
コメント済み: Hassan Ali Shah 2015 年 1 月 2 日
f0=100;
f=(0:f0/40:f0*2);
Ts=1/fs;
n=0:8;
xs = 5*cos(2*pi*f0*n*Ts);
i=fft(xs);
plot(f,abs(i(1:81)))
  1 件のコメント
Hassan Ali Shah
Hassan Ali Shah 2015 年 1 月 2 日
it gives the error Index exceeds matrix dimensions.
Error in Untitled (line 7) plot(n,abs(i(1:81)));

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

採用された回答

Shoaibur Rahman
Shoaibur Rahman 2015 年 1 月 2 日
編集済み: Shoaibur Rahman 2015 年 1 月 2 日
Your n has a length of 9, as n=0:8, so i will have length of 9 as well,and hence, you cannot write i(1:81). There are many ways to solve this. Here is one: replace n = 0:8 by n = linspace(0,8,length(f)). This will make both f and i of same length.
Also, although plot(f,abs(i(1:81))) works fine, but you can simply use plot(f,abs(i))
Another way may be: keep everything same, but write i = fft(xs,length(f)) . This will perform 81 point fft on xs. Again, you can use plot(f,abs(i))
  1 件のコメント
Hassan Ali Shah
Hassan Ali Shah 2015 年 1 月 2 日
thanks dear. it worked.though both the ways are doing good,but your "another way" suits me more. thanks alot

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeFilter Analysis についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by