Changing Amplitude & Frequency of numpy.sin(wt) in pylab
For part of another project, I just need to make a simple sine wave with some frequency f.
Changing "samples" gives some strange effects on the pylab plot and I just don't know why!
using samples=500 gives a plot with frequency = 1/50 Hz.
using samples=1000 gives a plot with frequency = 1/100 Hz.
then with larger samples like 5000 and 10000, the plotted wave changes amplitude along the t axis, in patterns.
import numpy as N
f = 10.
w = 2. * N.pi * f
time_interval = 100
samples = 5000
t = N.linsp开发者_C百科ace(0, time_interval, samples)
y = N.sin(w * t)
pylab.plot(t, y)
pylab.show()
Any help here would be great! I just want a basic sine wave but can't even seem to do that!
I think you have a slight misconception with samples
. It only gives the resolution of time. Try to plot with time_interval= 1
and vary the samples
(Start with small values like 10 and increase it then gradually). You'll see.
To make eat's answer explicit, I set time_interval
to 1, and varied samples
, as he suggested:
import pylab
import numpy as N
f = 10.
w = 2. * N.pi * f
time_interval = 1
fig = pylab.figure()
for i, samples in enumerate((5, 50, 500, 5000)):
pylab.subplot(2, 2, i+1)
pylab.title('%i samples'%samples)
t = N.linspace(0, time_interval, samples)
y = N.sin(w * t)
pylab.plot(t, y, '.-')
fig.show()
50 samples is clearly not enough for a time_interval
of 1; This is why 5000 isn't enough samples for a time_interval
of 100.
Here's a basic example.
import pylab
x=pylab.arange(0,150,0.2)
y=pylab.sin(x);
pylab.plot(x,y)
pylab.show()
Look closely at the data being created:
x
is an array (technically datatype = numpy.ndarray) from 0 to 149.8 with an interval of 0.2, i.e. type x to see array([0., 0.2, 0.4, ..., 149.8])
y
is an array from sin(0) to sin(149.8), i.e. array([0., 0.198..., ..., -0.839...])
From the given parameters: Frequency, F = 10 Hz, Time period, T = 100 s and Number of samples for T = 100 s, N = 5000.
This implies, the No. of cycles = F * T = 10 * 100 = 1000. Let choose T = 10/F, to visualize 10 cycles. This means that we will get 10 cycles from the 10 Hz sine wave in 1 sec. This also means that we will have 5000 samples for the 10 cycles, or 500 samples per cycle which is quite a bit for replication of the signal.
import numpy as np
import matplotlib.pyplot as plt
F = 10
T = 10/F
Fs = 5000
Ts = 1./Fs
N = int(T/Ts)
t = np.linspace(0, T, N)
signal = np.sin(2*np.pi*F*t)
plt.plot(t, signal)
plt.show()
精彩评论