Help to comment code
조회 수: 2 (최근 30일)
이전 댓글 표시
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
댓글 수: 0
답변 (1개)
Image Analyst
2013년 11월 22일
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
Image Analyst
2013년 11월 22일
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.
참고 항목
카테고리
Help Center 및 File Exchange에서 Discrete Data Plots에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!