Is there a way to find the impulse response of the given system without using a loop?

6 次查看(过去 30 天)
Is it possible to modify this code so that it would work without using any loop for the given system.
%y[n] = 1/3(x[n]-x[n-3])
y = zeros(1,21);
for i = 1:1:21
if i <= 3
y(i) = x(i)*(1/3);
else
y(i) = (x(i)-x(i-3))*(1/3);
end
end

回答(2 个)

Ram Sreekar
Ram Sreekar 2023-6-26
编辑:Ram Sreekar 2023-6-26
Hi Akash knrs,
It is possible to calculate y without using a loop. You can refer to the example code given below.
% Consider,
x = [1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21];
y = zeros(1,21);
y(1:3) = x(1:3) * (1/3);
y(4:end) = (x(4:end) - x(1:end-3)) * (1/3);
Hope this helps you resolve your query.

Mahesh Chilla
Mahesh Chilla 2023-6-26
Hi Akash!
Assuming that the array "x" is the unit impulse. To solve this, you can create an array of "x" which is shifted by 3 units to the right.
The following code will solve this
n = 1:21 ; %defining an interval
x = (n==0) ; % unit impulse
x3 = (n==3) ; % unit impulse shifted by 3 units to the right
y = (x-x3)/3 ;
To learn more about the functions used in the code, refer the MATLAB's documentation.
  • https://www.mathworks.com/help/signal/gs/impulse-step-and-ramp-functions.html
Hope this helps,
Thank you!!

类别

Help CenterFile Exchange 中查找有关 Get Started with MATLAB 的更多信息

产品


版本

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by