改变幅度和pylab 中 numpy.sin(wt) 的频率

发布于 2024-10-18 00:38:05 字数 454 浏览 3 评论 0原文

对于另一个项目的一部分,我只需要制作一个频率为 f 的简单正弦波。

更改“样本”会对 pylab 图产生一些奇怪的影响,我只是不知道为什么!

使用样本 = 500 给出频率 = 1/50 Hz 的图。

使用样本 = 1000 给出频率 = 1/100 Hz 的图。

然后,对于 5000 和 10000 等较大样本,绘制的波会沿 t 轴以模式改变振幅。

import numpy as N
f = 10.
w = 2. * N.pi * f
time_interval = 100
samples = 5000
t = N.linspace(0, time_interval, samples)
y = N.sin(w * t)
pylab.plot(t, y)
pylab.show()

这里的任何帮助都会很棒!我只想要一个基本的正弦波,但似乎无法做到这一点!

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.linspace(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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

叶落知秋 2024-10-25 00:38:05

我认为您对样本有轻微的误解。它仅给出时间的分辨率。尝试使用 time_interval= 1 进行绘图并改变样本(从较小的值(例如 10)开始,然后逐渐增加)。你会看到的。

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.

千寻… 2024-10-25 00:38:05

为了使 eat 的答案明确,我将 time_interval 设置为 1,并改变 samples,正如他建议的那样:

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 个样本对于 time_interval 显然是不够的1 个;这就是为什么 5000 个样本对于 time_interval 100 来说是不够的。

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.

空宴 2024-10-25 00:38:05

这是一个基本示例。

import pylab 
x=pylab.arange(0,150,0.2)
y=pylab.sin(x);
pylab.plot(x,y)
pylab.show()

仔细观察正在创建的数据:

x 是一个从 0 到 149.8 的数组(技术上 datatype = numpy.ndarray),间隔为 0.2,即输入 x 可以看到 array([0. , 0.2, 0.4, ..., 149.8])

y 是一个从 sin(0) 到 sin(149.8) 的数组,即 array([0., 0.198. ..,...,-0.839...])

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...])

决绝 2024-10-25 00:38:05

根据给定的参数:频率,F = 10 Hz,时间周期,T = 100 s,T = 100 s 的样本数,N = 5000。

这意味着,周期数 = F * T = 10 * 100 = 1000。选择 T = 10/F,以可视化 10 个周期。这意味着我们将在 1 秒内从 10 Hz 正弦波获得 10 个周期。这也意味着我们将在 10 个周期中有 5000 个样本,或者每个周期有 500 个样本,这对于信号的复制来说是相当多的。

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()

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()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文