作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个 .wav
文件(在本例中称为“piano2.wav”)。
我想在Python中找到光谱质心。
使用这里另一篇文章中的代码我有这个功能:
import numpy as np
from scipy.io.wavfile import read
def spectral_centroid(x, samplerate=44100):
magnitudes = np.abs(np.fft.rfft(x))
length = len(x)
freqs = np.abs(np.fft.fftfreq(length, 1.0/samplerate)[:length//2+1])
return np.sum(magnitudes*freqs) / np.sum(magnitudes)
我使用 scipy.io.wavfile.read 将 wav 文件读取到 numpy 数组中,然后尝试将其输入到上面的函数中
a=read("piano2.wav")
print("Spectral centroid is " + spectral_centroid(a[1]))
这是我收到的错误
File "test.py", line 20, in <module>
print("Spectral centroid is " + spectral_centroid(a[1]))
File "test.py", line 8, in spectral_centroid
return np.sum(magnitudes*freqs) / np.sum(magnitudes)
ValueError: operands could not be broadcast together with shapes (302712,2) (151357,)
最佳答案
您正在尝试将不同形状的数组(幅度
和频率
)相乘:
a = np.arange(10)
b = np.arange(5)
print(a*b)
ValueError: operands could not be broadcast together with shapes (10,) (5,)
这可能有帮助:
def spectral_centroid(x, samplerate=44100):
magnitudes = np.abs(np.fft.rfft(x))
length = len(x)
freqs = np.abs(np.fft.fftfreq(length, 1.0/samplerate)[:length//2+1])
magnitudes = magnitudes[:length//2+1]
return np.sum(magnitudes*freqs) / np.sum(magnitudes)
关于python - numpy 数组的谱质心,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54032515/
前言 一年一度的虐狗节终于过去了,朋友圈各种晒,晒自拍,晒娃,晒美食,秀恩爱的。程序员在晒什么,程序员在加班。但是礼物还是少不了的,送什么好?作为程序员,我准备了一份特别的礼物,用以往发的微博数据
默认情况下,我有一个 V3 map 加载并以特定的经度/纬度为中心。加载后,用户可以输入他们的地址以获取前往该地点的路线。发生这种情况时, map 会调整大小以适应其左侧的方向框。因此,路线在 map
我是一名优秀的程序员,十分优秀!