MATLAB: How to write equation in matlab to get the required graph

digital image processingdigital signal processinghomeworkimage processingmatlab codersignal processing

i have some equations and corresponding graph, i am trying to find graph using that equations and i found second graph (green) correctly but in first graph (red color) something being wrong, i am unable to detect the problem, so please help……. my code is:
clc;
clear all;
r=1024;
c=1024;
p=32;
m=8;
for i= 1:r
for j=1:c
if mod(i/(m*p),2) %even
SP(i,j) = pi-round((i/p)*2*pi/m) ;
else % odd
SP(i,j)=-pi+round(((i/p)+1)*2*pi/m) ;
end
end
end
figure(1),plot(SP,'r')
hold on
%%stair phase-2
p=256;
m=1;
n=4;
for i= 1:r
for j=1:c
SP1(i,j) = pi-round(i/(m*p))*(2*pi/n);
end
end
plot(SP1,'g')
hold off

Best Answer

Hello Ajeet,
Just at first glance, I can see that mod(x,2) will be cast to true when "x" is odd, not even. Normally, just to be clear, you would use
if mod(x,2) == 0 % Even
...
else % odd
...
end
I'm not sure why you are using "round" in your equation at all. That doesn't seem to be in the original equation in the image.
In terms of coding, the equation doesn't seem to depend on "j" (y-value) at all. So you can just calculate the vector once, then use repmat to create the matrix, if you really need one.
I would really suggest looking into using logical indexing. There's no need to loop through such a simple equation. In fact, there's no need to even use logical indexing. You can just use element-wise operators (e.g. .*), and work on the even and odd indices separately.
Hope that helps.
-Cam