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

조회 수: 114 (최근 30일)
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

채택된 답변

Cam Salzberger
Cam Salzberger 2018년 5월 18일
편집: Cam Salzberger 2018년 5월 18일
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
Cam Salzberger
Cam Salzberger 2018년 5월 18일
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.
Charles Naegele
Charles Naegele 2018년 5월 18일
Awesome! Thank you for your help.

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

추가 답변 (1개)

Jim Riggs
Jim Riggs 2018년 5월 18일
편집: Jim Riggs 2018년 5월 18일
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
Charles Naegele
Charles Naegele 2018년 5월 18일
This method worked but I'm afraid it's not fully addressing my concerns. Let me rephrase the question.

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

카테고리

Help CenterFile Exchange에서 Programming에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by