Help to comment code

2 vues (au cours des 30 derniers jours)
Benjamin Garvin
Benjamin Garvin le 22 Nov 2013
Commenté : Image Analyst le 22 Nov 2013
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

Réponses (1)

Image Analyst
Image Analyst le 22 Nov 2013
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 commentaires
Benjamin Garvin
Benjamin Garvin le 22 Nov 2013
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?
Image Analyst
Image Analyst le 22 Nov 2013
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.

Connectez-vous pour commenter.

Catégories

En savoir plus sur Discrete Data Plots dans Help Center et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by