Below are the two different methods of generating time to create an audio sine wave. While framerate is being used as steps in one method, the inverse of sampling rate is being used as steps in the other method. Where is the difference?

Method 1

# Create a sound
framerate = 44100
time = np.linspace(0,5,framerate*5)         #Multiplying framerate

signal = np.sin(2*np.pi*220*time) + np.sin(2*np.pi*224*time)


Method 2

#Simulation parameters
srate = 500 #Sampling Rate in Hz
time = np.arange(0.0, 2.0, 1.0/srate)       #Dividing Sampling rate

#Sine Wave Parameters
freq = 3         #Frequency in Hz
ampl = 2         #Amplitude in a.u.
phas = np.pi/3   #Phase in Radians

#Generate the Sine Wave
sine_wave = ampl*np.sin(2*np.pi*freq*time + phas )
  • $\begingroup$ How do you define a frame here? $\endgroup$ Apr 22 '20 at 6:54
  • $\begingroup$ It's a part of my question. $\endgroup$ Apr 22 '20 at 11:31

One method uses np.linspace() and the other one np.arrange() to create the time domain vector.

I suggest reading the documentation

The first method has a small error, so the frequency will not be 100% correct.


Common error with using linspace: Let's say you want three samples that are 1 apart. linspace(0,3,3) will give you [0 1.5 3], so the spacing is bigger than you want. The correct way is to adjust the upper limit by subtracting 1 interval (or 1/samplerate), so it should be linspace(0,2,3) or

  • $\begingroup$ What's the error with the first method? $\endgroup$ Apr 22 '20 at 11:30

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.