How can I implement a simple difference equation and plot the resulting output, all using MATLAB

18 ビュー (過去 30 日間)
upinderjeet
upinderjeet 2013 年 12 月 4 日
回答済み: Mohazam Awan 2017 年 10 月 10 日
I have a simple case of all initial conditions to be zero. We can consider a general form as:
a0y(n) = b0x(n) + b1x(n-1) + b2x(n-2) + ...+ bM x(n-M)
-a1y(n-1) - a2y(n-2) - ...- aN y(n-N)
I have to plot the output y(n) with n varying from 0 to 500.
Both y(n) and x(n) are zero for n<0.

回答 (4 件)

Wayne King
Wayne King 2013 年 12 月 4 日
You have not said what x(n) is, is it a white noise input. Is it the Kronecker delta sequence.
If we don't know that x(n) is we certainly can't tell you what y(n) is.
If you know x(n), you can implement using filter
B = [b0 b1 b2 b3 .... bM];
A = [1 a1/a0 a2/a0 ... ];
x(n) = zeros(100,1);
x(1) = 1; % Kronecker delta input
y = filter(B,A,x);
  1 件のコメント
upinderjeet
upinderjeet 2013 年 12 月 5 日
Actually x(n) is uniformly distributed random signal between -1 and 1. Now please tell me how to modify the above code according to this input and also how to define this x(n) in matlab.

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


Wayne King
Wayne King 2013 年 12 月 5 日
編集済み: Wayne King 2013 年 12 月 5 日
Please look at the help for rand
x = -1+ 2*rand(100,1);
B = [b0 b1 b2 b3 .... bM];
A = [1 a1/a0 a2/a0 ... ];
y = filter(B,A,x);
  1 件のコメント
upinderjeet
upinderjeet 2013 年 12 月 5 日
dont you think it shud be -1+ 2*rand(1,500) since I want to plot for k=0 to k=500 and also it shud be a row vector of 500 elements instead of column vector.

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


sixwwwwww
sixwwwwww 2013 年 12 月 5 日
may be you can try something like this:
N = 500;
for n = 1:N
xValues = sum(randi(10, 1, n) .* (rand(1, n) - 1));
yValues = sum(randi(10, 1, n - 1) .* (rand(1, n - 1) - 1));
y(n) = xValues - yValues;
end
plot(1:N, y)
  2 件のコメント
upinderjeet
upinderjeet 2013 年 12 月 5 日
actually I think, x = -1+ 2*rand(1,500); should just work fine for my case as it gives me a array of 500 elements(all in one row), except the fact that when I plot this x, the points are connected in the plot. I want them to be discrete points so that I can simulate my discrete system. But the plot I am getting is have all these points connected by straight linesinstead of discrete representation where the previous value is held until the next random number. Please tell me if I can achieve that.
sixwwwwww
sixwwwwww 2013 年 12 月 5 日
you can discrete the plot the following way:
plot(x, y, '*')

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


Mohazam Awan
Mohazam Awan 2017 年 10 月 10 日
y1=zeros(1,length(n)); % Pre-allocating
for i=1:length(n)
if(n(i)<0)
y1(i)=0; % $ y[n]=0 for n<0
end
if (n(i)>=0)
y1(i)=y1(i-1)+x(i);
end
end

カテゴリ

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

Community Treasure Hunt

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

Start Hunting!

Translated by