0
$\begingroup$

I'm extremely new to DSP, so I apologise if it's a very basic question.

I'm having issues understanding the following exercise:

"Create an array of 201 samples containing a sin wave with a frequency , f, of 2 Hz, i.e. the period is 0.5 seconds. Use a sampling rate, fs of 100 Hz, i.e 100 samples per second. Use the numpy method arange to generate the array. The first step is to declare the variables for f and fs and then create an array of times for the samples in a 2 second range, i.e.

t = np.arange(−1,1+1/fs,1/fs)

which gives a array starting at -1 and ending at 1.01."

I get how you create a range in Python, and I get that the parameters are the starting value, the end value, and the steps. However, I'm not quite sure what this is doing: why does it start at -1? Why does it end at 1+1/fs? Why are the time steps 1/fs? And lastly, what exactly is this giving me? Just the times at which the wave is sampled?

I've tried Googling, but I don't really know what I should be looking for. Even a pointer to the right keyword to Google to answer those questions would be greatly appreciated.

$\endgroup$

1 Answer 1

1
$\begingroup$

The Hz values are used to get your parameters for defining your signal.

I don't think there is any reason that they went from -1 to 1 instead of, say, 0 to 2. Notice that the end point is not included in the range.

The spacing in your sampling is 1/100 of a second. Yes, the array is giving you values on a time scale.

To generated the signal your are going to need:

f = 2
signal = np.sin( f * 2*np.pi * t )

The $2\pi$ comes as a unit conversion of radians per cycle. Therefore the argument, in units is:

$$ \frac{cycles}{second} \cdot \frac{radians}{cycle} \cdot seconds = radians $$

Hope this helps.

Ced

================================

Followup:

Here is a sample program.

import numpy as np
import matplotlib.pyplot as plt

f = 2.0
fs = 100.0

t = np.arange( -1.0, 1.0+1.0/fs, 1.0/fs )
signal = np.sin( f * 2*np.pi * t )

plt.plot(t, signal, c='g')
plt.show()

Notice that I added a bunch of ".0"s to force Python into using floats. Without it, the program passed 1/fs as the integer 0 to arange.

$\endgroup$
0

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.