【问题标题】:Matplotlib Magnitude_spectrum Units in Python for Comparing Guitar Strings用于比较吉他弦的 Python 中的 Matplotlib Magnitude_spectrum 单位
【发布时间】:2015-07-22 12:05:56
【问题描述】:

我正在使用 matplotlib 的 magnitude_spectrum 来比较吉他弦的音调特性。 Magnitude_spectrum 将 y 轴显示为具有“幅度(能量)”单位。我使用两个不同的“过程”来比较 FFT。过程 2(由于缺乏更好的描述)更容易解释——下面的代码和图表

我的问题是:

  • 就单位而言,“幅度(能量)”是什么意思,它与 dB 有何关系?
  • 使用#Process 2(参见下面的代码和图表),我查看的是什么类型的单位,dB?
  • 如果 #Process 2 不是 dB,那么将其缩放到 dB 的最佳方法是什么?

我下面的代码(简化)显示了我正在谈论/查看的示例。

import numpy as np
from scipy.io.wavfile import read
from pylab import plot
from pylab import plot, psd, magnitude_spectrum
import matplotlib.pyplot as plt

#Hello Signal!!!
(fs, x) = read('C:\Desktop\Spectral Work\EB_AB_1_2.wav') 

#Remove silence out of beginning of signal with threshold of 1000 
def indices(a, func):
#This allows to use the lambda function for equivalent of find() in matlab
return [i for (i, val) in enumerate(a) if func(val)]

#Make the signal smaller so it uses less resources
x_tiny = x[0:100000]
#threshold is 1000, 0 is calling the first index greater than 1000
thresh = indices(x_tiny, lambda y: y > 1000)[1]
# backs signal up 20 bins, so to not ignore the initial pluck sound...
thresh_start = thresh-20
#starts at threshstart ends at end of signal (-1 is just a referencing     thing)
analysis_signal = x[thresh_start-1:] 

#Split signal so it is 1 second long
one_sec = 1*fs
onesec = x[thresh_start-1:one_sec+thresh_start-1]

#process 1
(spectrum, freqs, _) = magnitude_spectrum(onesec, Fs=fs) 
#process 2
spectrum1 = spectrum/len(spectrum)

我不知道如何批量处理多个 .wav 文件,所以我在一大堆不同的 .wav 文件上分别运行此代码,然后将它们放入 excel 中进行比较。但是为了不看难看的图,我用 Python 画了图。以下是绘制时#process1 和#process2 的样子:

流程 1

流程 2

【问题讨论】:

    标签: python unit-testing matplotlib signal-processing acoustics


    【解决方案1】:

    磁力只是频谱的绝对值。正如您在流程 1 中所标注的,“能源”是一种很好的思考方式。

    进程 1 和进程 2 都在同一个单元中。唯一的区别是过程 2 中的值已除以数组的总长度(一个标量,因此单位没有变化)。通常这会作为 FFT 的一部分发生,但有时不会(例如 numpy.FFT 不包括除以长度)。

    将其缩放到 dB 的最简单方法是:

    (频谱, 频率, _) = 幅度_频谱(onesec, Fs=fs, scale='dB')

    如果您想自己执行此操作,则需要执行以下操作: 频谱2 = 20*numpy.log10(频谱)

    **值得注意的是,我不确定您是否应该应用 /len(spectrum)。我建议使用 scale='dB' !!

    【讨论】:

      【解决方案2】:

      要转换为 dB,请在绘制之前记录任何非零频谱幅度的对数并进行缩放(缩放以匹配校准的麦克风和声源,如果可用,或使用任意缩放以使电平看起来很熟悉) .

      对于零幅度值,也许只需将日志替换或钳制为您想要位于日志图底部的任何内容(当然不是负无穷大)。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-11-23
        • 2011-07-24
        • 2018-03-08
        • 2011-05-28
        • 2011-10-04
        • 2011-06-22
        • 2010-11-05
        相关资源
        最近更新 更多