在Python中制作一个动画

发布于 2025-01-31 21:14:14 字数 1290 浏览 1 评论 0原文

这个问题很简单,但我是Python的初学者,并以最佳的方式来实现此问题:

  • 我有一个.txt文件,其中包含时间(s)和频率。
  • 我希望将这些数据的可视化为垂直轴上下移动时的点。
  • 我需要DOT以相应的频率时间邮票移动,因为我计划输出MP4文件并将动画同步到原始声音文件。
  • Y轴的最大和最小将是文件中频率的最大值。

通过“移动”,我认为使点出现并消失在下一个应该起作用之前,因此它不是连续的。 以下是我到目前为止所拥有的:

import matplotlib.pyplot as plt
import matplotlib.animation
from matplotlib.colors import LinearSegmentedColormap

# Import Data
time = np.loadtxt("sentence 1.txt", usecols=0, skiprows=1, dtype=float)
print(time)
print(time.shape)
f0 = np.loadtxt("sentence 1.txt", usecols=1, skiprows=1, dtype=float)
print(f0)
print(f0.shape)

# These are the indices of onset times of syllables
# time[0], time[10], time[22], time[34], time[85], time[100]


onset = [0,10,22,34,85,100]

# Do f0 averages for each syllable and create an array

def utterances(array):
  f0_mean = []
  end = 0
  for i in range(len(array)):
    if i == len(array)-1:
      end = len(f0) - 1
    else:
      end = array[i+1]-1
    avg = np.mean(f0[array[i]:end])
    f0_mean.append(avg)
  return f0_mean

utterance = utterances(onset)
print(utterance)
print(min(f0), max(f0))

是否有一种方法可以在几秒钟内处理特定时间(例如[0.15 0.160714 0.171429 0.182143 0.192857 0.203571 0.214286 0.2255 0.235714 0.246429])在matplotlib.animation.funcanimation()中?

The problem is straightforward but I am beginner in python and stuck on the optimal way to implement this:

  • I have a .txt file that contains time (s) and frequency.
  • I want a visualization of this data as a dot moving up and down a vertical axis.
  • I need the dot to move at the corresponding time stamp of the frequency, since I plan to output an mp4 file and sync up the animation to the original sound file.
  • The max and min of the y axis would be those of the frequency in the file.

By "moving" I think making the dot appear and disappear before the next one should work, so it's not continuous.
Below is what I have so far:

import matplotlib.pyplot as plt
import matplotlib.animation
from matplotlib.colors import LinearSegmentedColormap

# Import Data
time = np.loadtxt("sentence 1.txt", usecols=0, skiprows=1, dtype=float)
print(time)
print(time.shape)
f0 = np.loadtxt("sentence 1.txt", usecols=1, skiprows=1, dtype=float)
print(f0)
print(f0.shape)

# These are the indices of onset times of syllables
# time[0], time[10], time[22], time[34], time[85], time[100]


onset = [0,10,22,34,85,100]

# Do f0 averages for each syllable and create an array

def utterances(array):
  f0_mean = []
  end = 0
  for i in range(len(array)):
    if i == len(array)-1:
      end = len(f0) - 1
    else:
      end = array[i+1]-1
    avg = np.mean(f0[array[i]:end])
    f0_mean.append(avg)
  return f0_mean

utterance = utterances(onset)
print(utterance)
print(min(f0), max(f0))

Is there a way to handle specific times in seconds (for example [0.15 0.160714 0.171429 0.182143 0.192857 0.203571 0.214286 0.225
0.235714 0.246429]) in the matplotlib.animation.FuncAnimation()?

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文