# Creating time vector using number of samples

2 views (last 30 days)
Lina Fatin on 2 Jul 2022
Answered: Aniruddh Maini on 2 Jul 2022
I have the following parameters:Total number of samples: 5000, samplerate: 2500.I created my time vector as usual: t=0:1/2500:5000-1/2500. But the time vector is a lot bigger than my related data vector which makes plotting impossible. Am I misunderstanding the meaning of sample number?

Voss on 2 Jul 2022
Defining t like this:
t=0:1/2500:5000-1/2500
t = 1×12500000
0 0.0004 0.0008 0.0012 0.0016 0.0020 0.0024 0.0028 0.0032 0.0036 0.0040 0.0044 0.0048 0.0052 0.0056 0.0060 0.0064 0.0068 0.0072 0.0076 0.0080 0.0084 0.0088 0.0092 0.0096 0.0100 0.0104 0.0108 0.0112 0.0116
is creating a time vector that has samplerate=2500 [2500 samples/second, say] for 5000 seconds, essentially. (Or 2500 samples/hour for 5000 hours or 2500 samples/day for 5000 days, etc. - I don't know that samplerate is per second necessarily, but it doesn't matter), which gives you a vector with 2500*5000 samples:
numel(t)
ans = 12500000
But you are given that the number of samples is 5000, not that the end time is 5000, so the time vector should be:
t = 1/2500*(0:5000-1) % 5000 samples, spaced 1/2500 apart
t = 1×5000
0 0.0004 0.0008 0.0012 0.0016 0.0020 0.0024 0.0028 0.0032 0.0036 0.0040 0.0044 0.0048 0.0052 0.0056 0.0060 0.0064 0.0068 0.0072 0.0076 0.0080 0.0084 0.0088 0.0092 0.0096 0.0100 0.0104 0.0108 0.0112 0.0116
numel(t)
ans = 5000

Aniruddh Maini on 2 Jul 2022
Hi Lina
As per your question NumSamples = 5000 and SampleRate = 2500
That means, in one seconds the signal will be sampled 2500 times or we will get 2500 samples each second, and we have to get 5000 samples in total.
So let's say
At t = 0 sec we get a sample
At t = 0 + (1/2500) we get another sample
At t = 0+(1/2500)+(1/2500) we get another sample.
and so on...
So from the observation we can say that total time required to get 5000 samples is (1/2500)*(5000-1)
Since we are getting 5000 samples between t = 0 and t = (1/2500)*(5000-1), so the SampleTime vector can be found as
timeVector = 0:1/2500:(1/2500)*(5000-1) or alternatively as
timeVector = linspace(0,(1/2500)*(5000-1),5000)
Hope it helps.

R2022a

### Community Treasure Hunt

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

Start Hunting!