When I executed below code it got executed perfectly but in the graph I am getting just the coordinates but not the plot.

f=10; %frequency of the impulse in Hz
fs=f*100; % sample frequency is 10 times higher
t=-1:1/fs:1; % time vector
x= 100*cos(2*pi*1000*t);
y=zeros(size(t));
y(1:fs/f:end)=1;
for t= -1:1/fs:1
m = x.*y;
end
plot(t,m);

 Accepted Answer

f=10; %frequency of the impulse in Hz
fs=f*100; % sample frequency is 10 times higher
t=-1:1/fs:1; % time vector
numt = length(t);
y=zeros(size(t));
y(1:fs/f:end)=1;
for tidx = 1 : numt
x = 100*cos(2*pi*1000*t(tidx));
m(tidx, :) = x.*y;
end
plot(t,m);

7 Comments

By the way, the above can be rewritten without a for loop. R2015b or later, it would be
f=10; %frequency of the impulse in Hz
fs=f*100; % sample frequency is 10 times higher
t=-1:1/fs:1; % time vector
x = 100*cos(2*pi*1000*t);
y=zeros(size(t));
y(1:fs/f:end)=1;
m = x(:) .* y;
plot(t, m)
Can you please share the graph you got after executing your code?
f=10; %frequency of the impulse in Hz
fs=f*100; % sample frequency is 10 times higher
t=-1:1/fs:1; % time vector
x = 100*cos(2*pi*1000*t);
y=zeros(size(t));
y(1:fs/f:end)=1;
m = x(:) .* y;
plot(t, m)
So, 0's and 100's. Not very interesting.
You might be thinking the plot is wrong, but it is correct for what you asked for. You asked for a sampling frequency of 10*100 = 1000, so 1/1000 between entries, and you used cos(2*pi*1000*t) and 1000*1/1000 = 1, so you are doing cos(2*pi*1*integer) which is going to be a constant.
Yeah, but I am trying to get something like the sampled signal in below picture.
f=10; %frequency of the impulse in Hz
fs=f*100; % sample frequency is 10 times higher
t=-1:1/fs:1; % time vector
x = 100*cos(2*pi*1000*t);
y=zeros(size(t));
y(1:fs/f:end)=1;
m = x(:) .* y;
stem(t, m)
This will be slow to draw, and will not look at all informative. You will need to zoom in a fair bit to see the signal.
Your code has little to do with your required output.
f = 10; %frequency of the impulse in Hz
fs = f * 7; % sample frequency is 7 times higher
t =-1/10:1/fs:1/10; % time vector
y = 100*cos(2*pi*1000*t);
stem(t, y)
Notice that we reduced the sampling frequency and the time interval.
Notice too that no loop was needed.

Sign in to comment.

More Answers (1)

Based on the above code it is clear that there is no use of the for loop by redefining the variable t to iterate over the loop and the value of m = x.*y; will be same for all the iterations. I think what you are looking for is may be the following:
f=10; %frequency of the impulse in Hz
fs=f*100; % sample frequency is 10 times higher
t=-1:1/fs:1; % time vector
x= 100*cos(2*pi*1000*t);
y=zeros(size(t));
y(1:fs/f:end)=1;
% Removing the for loop
m = x.*y;
plot(t,m);

4 Comments

But,actually I want "x" values at different "t" values for "m". So, that is why I put for loop from "t=-1:1/fs:1". Whenever, "t" value changes "x" value also changes as there is "t" in function of "x". If for loop is not used then "x" value will be constant right. I hope I explained it clearly.
I am trying to write code for smapling theorem.
You can Set Breakpoints at lines where the value of x and y are computed and you can see that they have been computed for all the values of t after executing that line itself. Also you can set the breakpoint within the for loop and see that the value of m will be same for every iteration.
Yeah, I just checked putting breakpoints. But, can you tell me how can I get different x at different t. But y value should be constant and when I execute m = x.*y I need to get discrete values.
If your equation is m(tidx) = x(tidx) * y(tidx) then the above code in my answer should work fine because with the operator times, .* element wise multiplication is performed so you don't have to iterate over individual elements of the vectors x and y, refer to the documentation of times, .* for more information. If your equation is m(tidx) = x(tidx) * y then you can refer to the below code posted by Walter.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!