1
$\begingroup$

Here is an example of plotting a square wave given in SciPy Documentation

A $5\textrm{ Hz}$ waveform sampled at $500\textrm{ Hz}$ for 1 second:

from scipy import signal
import matplotlib.pyplot as plt
t = np.linspace(0, 1, 500, endpoint=False)
plt.plot(t, signal.square(2 * np.pi * 5 * t))
plt.ylim(-2, 2)**strong text**

The documentation page states that

The square wave has a period 2*pi, has value +1 from 0 to 2*pi*duty and -1 from 2*pi*duty to 2*pi. duty must be in the interval [0,1].

  1. Why in the example 5*t is multipled by 2*np.pi? Doesn't the signal.sqrt() function take care of multiplying the frequency 5*t by 2*np.pi?
  2. What is the meaning of sampled at $500\textrm{ Hz}$ for second? I understand it is a $5\textrm{ Hz}$ waveform since 2*np.pi*5*t takes care of creating $5\textrm{ Hz}$ wave. But I do not understand

sampled at 500 Hz for 1 second

$\endgroup$
  • $\begingroup$ " Doesn't the signal.sqrt() function take care of multiplying the frequency 5*t by 2*np.pi?" you mean signal.square(), not signal.sqrt() ? $\endgroup$ – arpit jain Apr 5 '17 at 12:48
  • $\begingroup$ @arpitjain: No, he means actual square wave. $\endgroup$ – jojek Apr 6 '17 at 11:21
1
$\begingroup$
  1. The basic square wave function signal.square() has a period of 2*pi. Multiplying each time by 2*pi*f is a way of stretching the wave along the time axis to get a wave with a period of 1/f.

  2. To understand what the sample rate refers to, look at the first line:

    t=np.linspace(0,1,500,endpoint=False)
    

    If you look at the numpy documentation for this function, you'll see this creates a list of 500 evenly spaced numbers between 0 and 1. This is an array of times. When you then call the signal.square() function with this array, it returns an array of points on the square wave for each of those times. Thus you have sampled the square wave at 500Hz for 1 second.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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