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

12 views (last 30 days)
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 Comment
Kristin Aldridge
Kristin Aldridge on 12 Oct 2021
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').

Sign in to comment.

Accepted Answer

Mathieu NOE
Mathieu NOE on 12 Oct 2021
Edited: Mathieu NOE on 12 Oct 2021
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')

More Answers (0)

Categories

Find more on 2-D and 3-D Plots in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!