How to plot my sine waves in one graph, one right after another with 100ms in between and not on top of each other?

조회 수: 10 (최근 30일)
I have a number of sine waves I need to plot one following the other, with 100 ms in between each, but I keep getting all of them ending up on top of one another. Also my x and y label wont appear on the graph for some reason I can't figure out why. Code below:
%plot of all
f=(0:5:150) %starting at (1,5,10,15...150)
f = [ ]
f(1) = 1
figure
for f=(0:5:150)
if f==0
f=1;
end
N = 10000*40*(1/f); %400,000
T=40/f
t= 0:(40*(1/f))/N:(40*(1/f));
plot(t,f*sin(2*pi*f*t),'r');
xlabel=({'Time','(in sec)'})
ylabel=({'Voltage','(in V)'})
title('Stimulus voltage over time')
t_100 = T/N:T/N:0.1;
Sine_100 = zeros(length(t_100),1);
hold on
end
  댓글 수: 1
Kristin Aldridge
Kristin Aldridge 2021년 10월 12일
It does work but my frequency is exponentially growing, which was easily solved by deleting the f part of f*sin out of plot(t,f*sin(2*pi*f*t),'r').

댓글을 달려면 로그인하십시오.

채택된 답변

Mathieu NOE
Mathieu NOE 2021년 10월 12일
편집: Mathieu NOE 2021년 10월 12일
hello Kristin
I fixed your code
I prefered to use a constant fixed time increment for all frequencies instead of keeping the same number of samples as in your original code
the 100 ms of zero data between successives sine waves is also implemented , you can see it when zooming in the figure
hope it helps !
%plot of all
f=(0:5:150); %starting at (1,5,10,15...150)
fs = 100*max(f); % fixed sampling rate
dt = 1/fs;
t_all = [];
out_all = [];
for f=(0:5:150)
if f==0
f=1;
end
T=40/f;
t= 0:dt:T-dt;
% individual vector
out = f*sin(2*pi*f*t);
% add 100 ms zero data
samples = round(100e-3*fs);
tzero = max(t) + (1:samples)*dt;
t = [t tzero];
% padd out data with zeros
out = [out zeros(1,samples)];
% now concatenate all sine waves
if isempty(t_all) % first iteration
t_all = [t_all t];
else % next iterations
t_all = [t_all t+max(t_all)];
end
out_all = [out_all out];
end
figure(1)
plot(t_all,out_all,'r');
xlabel=({'Time','(in sec)'})
ylabel=({'Voltage','(in V)'})
title('Stimulus voltage over time')

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 2-D and 3-D Plots에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by