How to define time vectors using different sampling rates and plot.

82 views (last 30 days)
I have 3 data columns (3000x1) which were sampled at 10Hz, 35Hz, and 100Hz. I want to define a time vector for each of these columns that will allow me to plot them together against time.
if true
% code
end
% SAMPLED AT 10Hz, SMALL ORIFICE
ts = 0:(1/10):(3000/10)-(1/10);
p1small = importdata('Psmall.txt');
psmall = p1small(:,2);
figure
plot(ts,psmall)
hold on
% SAMPLED AT 35Hz, MEDIUM ORIFICE
tm = 0:1/35:(3000/35)-(1/35);
p1med = importdata('Pmed.txt');
pmed = p1med(:,2);
plot(tm,pmed)
hold on
% SAMPLED AT 100Hz, LARGE ORIFICE
tl = 0:1/100:(3000/100)-(1/100);
p1large = importdata('Plarge.txt');
plarge = p1large(:,2);
plot(tl,plarge)
hold off

Accepted Answer

Cam Salzberger
Cam Salzberger on 18 May 2018
Edited: Cam Salzberger on 18 May 2018
Hello Charles,
How about using linspace? This makes it easy to ensure you get the right number of points:
t = linspace(firstValue, finalValue, numPoints);
What really matters is to match the time vector up with when the data is collected. So if you have the data collected at 10 Hz, does it start collecting at t = 0, or t = 0.1? If you know this, you can still work out the colon notation:
nPts = numel(P);
tStart = 0; % Or 0.1
tGap = 0.1;
t = tStart:tGap:(nPts-1)*tGap+tStart;
However, you are very likely to run into floating point arithmetic issues if you do it that way, so using linspace is likely to be the better bet. Or manually specifying the end point with colon notation (which would be either 299.9 or 300 in this case). With linspace, it's probably either:
t = linspace(0, 299.9, 3000);
or
t = linspace(0.1, 300, 3000);
The reason you were seeing a vector of 30,000 points instead of 3000 is because you were using the period (0.1) as the gap, but using the number of points (3000) as the end time. Instead, it makes sense to use either all time values (0.1 and 300) or all index values if time doesn't really matter (1 and 3000).
Hope that helps!
-Cam
  6 Comments
Cam Salzberger
Cam Salzberger on 18 May 2018
From what I understand, seconds is correct. Hertz is literally just the unit of 1/seconds. You're using the inverse of that to form the time vector, and not adjusting the scaling, so that would make the units seconds.

Sign in to comment.

More Answers (1)

Jim Riggs
Jim Riggs on 18 May 2018
Edited: Jim Riggs on 18 May 2018
You have the right idea. The convention is
startValue:stepSize:EndValue
t=0:0.1:300
will give 3001 samples at 0.1 step size. To get exactly 3000 samples, just take one step off the end:
t=0:0.1:300-0.1
  1 Comment
Charles Naegele
Charles Naegele on 18 May 2018
This method worked but I'm afraid it's not fully addressing my concerns. Let me rephrase the question.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!