1
$\begingroup$

I have a rather simple question, sorry in advance if it's too easy. I'm working on a waveform generation project on MATLAB. When I look at the examples, I see that the time variables are defined like this;

t = 0:1/fs:T-1/fs;

What is the point of -1/fs?

$\endgroup$

2 Answers 2

5
$\begingroup$

It's a matter of personal preference.

  • Some prefer that the length $N$ of the vector $t$ matches $$N = \lfloor f_sT\rfloor$$ where $\lfloor\rfloor$denotes the $\mathrm{floor}()$ function. This is done so that $$\frac{N}{f_s} \leq T$$

    Example 1: $f_sT$ is an integer: $f_s = 200\,\texttt{Hz}$, $T = 3\texttt{s}$ so $N = 600$:

    t = 0:1/fs:T % length(t) = 601, which would give 601/200 = 3.005s
    t = 0:1/fs:T-1/fs % length(t) = 600, which gives 3s 
    

    Example 2: $f_sT$ is not an integer: $f_s = 200\,\texttt{Hz}$, $T = 3.142\texttt{s}$ so $N = \lfloor628.4\rfloor = 628$:

    t = 0:1/fs:T % length(t) = 629, which would give 629/200 = 3.145s
    t = 0:1/fs:T-1/fs % length(t) = 628, which gives 628/200 = 3.14s
    

  • However, others prefer that the last sample of $t$ be as close to $T$ as possible:
    With t = (0:1/fs:T), the very last sample of t will be closer to T:

    Example 1:

       t = 0:1/fs:T % t(end) = 3s
       t = 0:1/fs:T-1/fs % t(end) = 2.995s
    

    Example 2:

       t = 0:1/fs:T % t(end) = 3.14s
       t = 0:1/fs:T-1/fs % t(end) = 3.135s
    

$\endgroup$
2
$\begingroup$

It really depends what they are trying to do, but I assume they simulate several chunks of duration T with sampling interval 1/fs.

So they need to make sure that the last sample of each chunk is not computed again as the first sample of the next chunk.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.