简体   繁体   English

Python振幅谱图

[英]Python amplitude spectrum plot

I have two lists of float values, one for time and other for voltage values taken from an oscilloscope (I assume).我有两个浮点值列表,一个用于时间,另一个用于从示波器获取的电压值(我假设)。 I have to draw an amplitude spectrum plot, but i'm not exactly sure what function I need to use and what parameters I need to give it, I tried fft(u), but it didn't work.我必须绘制幅度谱图,但我不确定我需要使用什么函数以及我需要给它什么参数,我尝试了 fft(u),但它没有用。

Any help is appreciated, let me know if you need more info.任何帮助表示赞赏,如果您需要更多信息,请告诉我。

Use numpy .使用numpy

As an example, let me show how I analysed the frequencies in a stereo WAV file;例如,让我展示我如何分析立体声 WAV 文件中的频率;

First I read the data and separated it in the left and right channels;首先我读取数据并在左右声道中进行分离;

import wave
import numpy as np

wr = wave.open('input.wav', 'r')
sz = 44100 # Read and process 1 second at a time.
da = np.fromstring(wr.readframes(sz), dtype=np.int16)
left, right = da[0::2], da[1::2]

Next I run a discrete fourier transform on it;接下来我对其进行离散傅立叶变换;

lf, rf = abs(np.fft.rfft(left)), abs(np.fft.rfft(right))

And we plot the left channel with mathplotlib;我们用 mathplotlib 绘制左声道;

import matplotlib.pyplot as plt

plt.figure(1)
a = plt.subplot(211)
r = 2**16/2
a.set_ylim([-r, r])
a.set_xlabel('time [s]')
a.set_ylabel('sample value [-]')
x = np.arange(44100)/44100
plt.plot(x, left)
b = plt.subplot(212)
b.set_xscale('log')
b.set_xlabel('frequency [Hz]')
b.set_ylabel('|amplitude|')
plt.plot(lf)
plt.savefig('sample-graph.png')

The graph looks something like this;该图看起来像这样;

Here is the Frequency-Time spectrum of the signal, stored in the wave file这是信号的频率 - 时间频谱,存储在波形文件中

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

signal_wave = wave.open('voice.wav', 'r')
sample_frequency = 16000

data = np.fromstring(signal_wave.readframes(sample_frequency), dtype=np.int16)
sig = signal_wave.readframes(-1)

sig = np.fromstring(sig, 'Int16')

For the wave file对于波形文件

sig = sig[:]

For some segment of the wave file对于波形文件的某些片段

sig = sig[25000:32000]

To plot spectrum of the signal wave file绘制信号波形文件的频谱

plt.figure(1)
c = plt.subplot(211)
Pxx, freqs, bins, im = c.specgram(sig, NFFT=1024, Fs=16000, noverlap=900)
c.set_xlabel('Time')
c.set_ylabel('Frequency')
plt.show()

图例

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

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