简体   繁体   English

Python频率与时间图

[英]Python Frequency vs. Time Graph

I am attempting to get a frequency vs. time graph of a .wav file using Python. 我正在尝试使用Python获取.wav文件的频率与时间的关系图。 At the moment I have code that is graphing Amplitude vs. Time as well as Frequency vs. Power (dB). 目前,我有一些代码可以显示幅度与时间以及频率与功率(dB)的关系。 I have attempted, unsuccessfully, to use the code for my Frequency vs. Power graph to instead plot Frequency vs. Time. 我尝试使用频率与功率图的代码来绘制频率与时间的关系,但未成功。 I know that the Frequency data is symmetric, which means I have 1/2 of the data points compared to my time data. 我知道频率数据是对称的,这意味着与时间数据相比,我有1/2的数据点。 I can plot them both by keeping the duplicate frequency data, but I am skeptical as to whether or not this actually produces an accurate representation of the frequency vs. time. 我可以通过保留重复的频率数据来对它们进行绘制,但是我怀疑这是否真正产生了频率与时间的精确表示。

I get the sense that the method will involve using a Short-time Fourier Transform on segments of the data and then plotting the results. 我认为该方法将涉及对数据段使用短时傅立叶变换,然后绘制结果。 That said, I have found some code that does something similar to this, but I am having a hard time understanding what is happening with the code and making any meaningful adjustments that would help me accomplish my goals. 就是说,我已经找到了一些与此功能类似的代码,但是我很难理解代码正在发生什么,并且很难做出有意义的调整以帮助我实现目标。

In summary, I was hoping that someone would have some sample code or a method for creating a frequency vs. time graph of a .wav file in Python. 总而言之,我希望有人能提供一些示例代码或一种在Python中创建.wav文件的频率与时间图的方法。 Thanks a ton! 万分感谢! Please let me know if I could post the code that I have been using thus far. 请让我知道是否可以发布到目前为止一直在使用的代码。

#Import the required functions
from scipy.io.wavfile import read
from scipy.fftpack import fft, fftfreq, fftshift
from scipy.signal import get_window
from math import ceil
from pylab import figure, imshow, clf, gray, xlabel, ylabel

# Read in a wav file 
#   returns sample rate (samples / sec) and data
rate, data = read('waveTest.wav')
data = data[:,0]
# Define the sample spacing and window size.
dT = 1.0/rate
T_window = 50e-3
N_window = int(T_window * rate)
N_data = len(data)

# 1. Get the window profile
window = get_window('hamming', N_window)

# 2. Set up the FFT
result = []
start = 0
while (start < N_data - N_window):
    end = start + N_window
    result.append(fftshift(fft(window*data[start:end])))
    start = end

result.append(fftshift(fft(window*data[-N_window:])))
result = array(result,result[0].dtype)

# Display results
freqscale = fftshift(fftfreq(N_window,dT))[150:-150]/1e3
figure(1)
clf()

s.imshow(abs(result[:,150:-150]), extent=(5,-5,(N_data*dT-T_window/2.0),T_window/2.0)) #19.04, -19.04, 6.41, 0.025 
s.xlabel('Frequency (kHz)')
s.ylabel('Time (sec.)')

s.show()

As requested, above is the code that I am trying to get working. 根据要求,上面是我要开始工作的代码。 I actually seem to have it working fine, but I have a couple of questions. 我实际上似乎工作正常,但是我有两个问题。

1) What exactly is abs(result[:,150:-150])? 1)abs(result [:,150:-150])到底是什么? I realize that he is taking the absolute value of the Fourier Transform (in order to remove the complex component?). 我意识到他正在使用傅立叶变换的绝对值(以便删除复杂分量?)。 Is this then the frequency? 这是频率吗?

2) How would I swap the data to have the time on the X axis with frequency on the Y axis? 2)我该如何交换数据,使X轴上的时间与Y轴上的频率一致?

3) How does the image know which frequency corresponds to which time? 3)图像如何知道哪个频率对应哪个时间? If I understand correctly the extents takes the final two parameters which are the length of the file in time and the step that the file should make? 如果我理解正确,那么扩展区将采用最后两个参数,即文件的时间长度和文件应执行的步骤?

4) Is it possible to graph the data in a plot rather then on an image? 4)是否可以在图表中而不是图像上绘制数据图形?

I hope these are not too many, and too specific of questions. 我希望这些问题不是太多,也不是太具体。 Thanks again for any help that can be provided! 再次感谢您提供的任何帮助!

1) result[:,150:-150] gives a numpy.array containing all rows (each row corresponds to a frequency calculated by fft) and columns from 150 to number of columns - 150 . 1) result[:,150:-150]给出一个numpy.array其中包含所有行(每行对应于一个由fft计算的频率)和从150number of columns - 150 150 number of columns - 150 Each column corresponds to time. 每列对应于时间。 Yes, abs takes absolute value for given frequency which roughly corresponds to the frequency of the signal. 是的,对于给定的频率, abs取绝对值,该绝对值大致对应于信号的频率。

2) In abs(result[:,150:-150]) you need to transpose the matrix, like so: abs(result[:,150:-150]).transpose() . 2)在abs(result[:,150:-150]) ,需要转置矩阵,如下所示: abs(result[:,150:-150]).transpose()

3) The extent arguments specifies ranges in the final plot. 3)范围参数指定最终图中的范围。 Since each column corresponds to specific point in time it's a simple mapping. 由于每一列都对应于特定的时间点,因此这是一个简单的映射。

4) The data that you get is how much given frequency contributes to the signal at given time (given a window of time, because frequency at specific point doesn't make sense). 4)您获得的数据是给定时间给定频率对信号的贡献(给定一个时间窗口,因为特定点的频率没有意义)。 It's by nature a 2D data. 它本质上是2D数据。 You may try to find dominating frequency at given time and then plot it as a simple function. 您可以尝试找到给定时间的主导频率,然后将其绘制为简单函数。

Also your code as it is does not work. 同样,您的代码不起作用。 Perhaps you have missed some variable definitions and imports from the rest of you program. 也许您错过了程序其余部分的一些变量定义和导入。

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

相关问题 如何在python中估计(给定频率的信号功率)与时间的关系 - how to estimate the (power of a signal at a given frequency) vs. time in python 如何使用python计算功率与时间图的上升时间、功率过冲和稳定时间 - How to calculate the rise time, power overshoot and settling time of a power vs. time graph using python 时变频率的频谱图与标度图 - Spectrogram vs. Scaleogram for Time-Varying Frequency Python - time.time()与bash时间 - Python – time.time() vs. bash time Python解析器/编译器与解释器,以及字符串串联编译时与运行时? - Python parser/compiler vs. interpreter, and string concatenation compile-time vs. run-time? Plot 幅度 [dB] 与频率 [deg] 与 matplotlib.pyplot python 其中 Mag 和 Freq 是两个列表 - Plot Magnitude[dB] vs. Frequency[deg] with matplotlib.pyplot python where Mag and Freq are two lists 绘制(时间段内的离散总和)与(时间段)的关系会产生具有不连续性的图形 - Plotting (discrete sum over time period) vs. (time period) yields graph with discontinuities 通过Python与InfluxDB了解unix时代的差异 - Understanding difference in unix epoch time via Python vs. InfluxDB Python 中的“美国/东部”与“美国东部时间”时区 - "US/Eastern" vs. "EST" time zone in Python Python - calendar.timegm() 与 time.mktime() - Python - calendar.timegm() vs. time.mktime()
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM