简体   繁体   English

像Audacity一样绘制波谱

[英]Plot spectrum of a wave as in Audacity

I would like to plot frequency graphs similar to the ones that Audacity can draw: 我想绘制类似于Audacity可以绘制的频率图:

大胆频率PNG

I did not find a software to do that (in command line) so I started to play with python to do it, using the specgram function. 我没有找到可以在命令行中执行此操作的软件,因此我开始使用specgram函数使用python来执行此操作。 As I am not able to redo such a graph (the purple one from audacity), I was wondering if someone know exactly what Audacity is plotting, waht it means, and is there any pseudo-code somewhere? 由于我无法重做这样的图表(来自audacity的紫色图表),我想知道是否有人确切知道Audacity正在绘制什么内容,这意味着什么,并且某处是否有伪代码? I have less than basic knowledge in audio processing, but if someone guides me on that part, I think I can code any suggestion/pseudo code/procedure. 我在音频处理方面的知识不足,但是如果有人指导我,我想我可以编写任何建议/伪代码/过程。 Right now, I'm here, plotting something like this which is roughly what I have seen everywhere so far. 现在,我在这里,绘制类似这样的图像,这大概是到目前为止我在所有地方看到的图像。

我的振幅和频率图PNG

> pxx, freqs, bins, _ = plt.specgram(y, NFFT=s, Fs=rate, noverlap=0,
                                        cmap=plt.cm.binary, sides='onesided',
                                        window=signal.blackmanharris(s),
                                        scale_by_freq=True,
                                        mode='magnitude')
plot(freqs, numpy.log10(pxx.max(axis=1)))

I don't understand how can I get these "decrease" of dB vs. frequency which I can see on any audio WAV with Audacity 我不明白如何获得这些dB相对于频率的“降低”,我可以在任何带有Audacity的音频WAV中看到

Cheers 干杯

As far as I see Audacity plots the magnitude spectrum, ie the absolute value of the fourier transform of the first 2048 samples. 据我所知,Audacity绘制了幅度谱,即前2048个样本的傅立叶变换的绝对值。 You plot the maximum amplitude in all time chunks relating to each frequency bin of the short time fourier transform . 您在与短时傅立叶变换的每个频率仓相关的所有时间块中绘制最大幅度。

Maybe this fits your needs: 也许这符合您的需求:

import numpy as np
from scipy import signal
from matplotlib import pyplot as plt

y = y[0:2048] * signal.blackmanharris(2048)
X_amp = np.abs(np.fft.rfft(y))
X_db = 20 * np.log10(X_amp)
freqs = np.fft.rfftfreq(2048, 1/rate)
plt.plot(freqs, X_db)

EDIT: 编辑:

Oh I found this page in the Audacity manual . 哦,我在Audacity手册中找到了此页面。 So plt.specgram should be fine to imitate Audacity, just take np.average(pxx, axis=1) . 所以plt.specgram应该可以模仿Audacity,只要取np.average(pxx, axis=1) The manual does not say what hop size is used... maybe try to set the noverlap -parameter to s/2 . 手册没有说使用什么跳数...可能尝试将noverlap -parameter设置为s/2 (A common choice.) (一个常见的选择。)

Thanks, I was close but you found it. 谢谢,我很亲近,但是你找到了。 Finally the code is quite simple: 最后,代码非常简单:

pxx, freqs, bins, _ = plt.specgram(y, NFFT=s, Fs=rate, noverlap=0,
                                   cmap=plt.cm.binary, sides='onesided',
                                   window=signal.blackmanharris(s),
                                   scale_by_freq=True,
                                   mode='magnitude')
plot(freqs, 20 * log10(mean(pxx, axis=1)), 'g')

And, except the y-axis unit, I'm almost one pixel accurate against audacity: 而且,除了y轴单位外,我对胆量的准确度几乎是一个像素:

Final plot 最终情节

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM