Help to comment code

Hi, i found an example on the net as to how to get my code to work, but im not entirely sure ow to comment it so that i can understand what each of the functions does, any help??
k=-1:9;
x=[1 2 4 0 0 0];% input x in the form [1,2,3,4,5]
h=[0 1 1 1 1 1];
m=length(x);
n=length(h);
X=[x,zeros(1,n)]; % padding of n zeros
H=[h,zeros(1,m)]; % padding of m zeros
for i=1:n+m-1
Y(i)=0;
for j=1:i
Y(i)=Y(i)+X(j)*H(i-j+1);
end
end
stem(k,Y,'--rs', 'Linewidth' ,2,...% plot the reponse
'MarkerEdgeColor','R',...
'MarkerEdgeColor','B',...
'MarkerSize',4)
ylabel('Y[n]');
xlabel('n');
title('4.a');
title('4.a: Y1','fontsize', 12)%Title name and characteristics
grid on %puts grid lines into the graph

回答(1 个)

Put a % symbol, followed by your explanation. Perhaps something like this:
% Initialize variables.
k=-1:9;
x=[1 2 4 0 0 0];% input x in the form [1,2,3,4,5]
h=[0 1 1 1 1 1];
m=length(x);
n=length(h);
% Create new vectors where we append zeros
% onto the end of the vectors.
X=[x,zeros(1,n)]; % Append n zeros
H=[h,zeros(1,m)]; % Append m zeros
% Create Y vector.
for ki=1:n+m-1
Y(i)=0;
for j=1:i
Y(i)=Y(i)+X(j)*H(i-j+1);
end
end
% Plot Y as a function of k.
stem(k,Y,'--rs', 'Linewidth' ,2,...% plot the reponse
'MarkerEdgeColor','R',...
'MarkerEdgeColor','B',...
'MarkerSize',4)
% Add titles to plot and axes.
ylabel('Y[n]');
xlabel('n');
title('4.a: Y1','fontsize', 12);
% Add gridlines onto plot area.
grid on;

2 个评论

Thanks for this, I have discovered that when i remove the first for statement i am able to output the same graph, could you tell me why?
That was a typo. It should be i, not ki. I started to change your index to k, because you're not supposed to use i as a loop counter to avoid confusion with the imaginary variable , but then I saw you already had used k so I decided to undo the changes. I guess I didn't hit the undo button enough times. Sorry about that.

请先登录,再进行评论。

类别

帮助中心File Exchange 中查找有关 Phased Array Design and Analysis 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by