I can't see the cosine graph. How do I generate the cosine graph using 2^10 samples?

27 次查看(过去 30 天)
deltat=0.1;
nsamples=2^10;
time=[0 : deltat : deltat*(nsamples-1)];
for i=1: nsamples
ydata(i,:)=[5+10*cos(2*pi*time+pi/6)];
end
figure(1);
plot(time,ydata,'b')
I try to generate the cosine graph with 2^10 samples.
I used 1024 (=2^10) time samples to create the cosine graph.
The time interval is 0.1 seconds.
Could you explain why I have 1024*1024 ydata rather than 1*1024 ydata?
How do I create a cosine graph using time sample data?

采纳的回答

Voss
Voss 2024-4-4,20:24
for i=1: nsamples
ydata(i,:)=[5+10*cos(2*pi*time+pi/6)];
end
That calculates 5+10*cos(2*pi*time+pi/6) and assigns it to the first row of ydata, then calculates the same thing again and assigns it to the second row of ydata, etc., etc., for nsamples rows. At the end ydata is a matrix where each row is the same and each row contains the calculated signal for all times.
What you mean to do is simpler than that; just calculate ydata once:
deltat = 0.1;
nsamples = 2^10;
time = (0 : nsamples-1) * deltat;
ydata = 5+10*cos(2*pi*time+pi/6);
figure(1);
plot(time,ydata,'b')
% zooming in
figure
plot(time,ydata,'b')
xlim([0 10])

更多回答(2 个)

Mitchell Thurston
Mitchell Thurston 2024-4-4,20:20
You should be able to just get rid of the for loop,
ydata = 5 + 10*cos(2*pi*time+pi/6)

Sam Chak
Sam Chak 2024-4-4,20:23
deltat = 0.001;
nsamples = 2000;
time =[0 : deltat : deltat*(nsamples-1)];
size(time)
ans = 1x2
1 2000
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
% for i = 1:nsamples
% ydata(i,:) = 5 + 10*cos(2*pi*time + pi/6);
% end
ydata = 5 + 10*cos(2*pi*time + pi/6);
size(ydata)
ans = 1x2
1 2000
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
plot(time, ydata), grid on

产品


版本

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by