如何更改 pyplot.specgram x 和 y 轴缩放比例? [英] How to change pyplot.specgram x and y axis scaling?

查看:89
本文介绍了如何更改 pyplot.specgram x 和 y 轴缩放比例?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我以前从未处理过音频信号,对信号处理知之甚少.不过,我需要使用 ma​​tplotlib 库中的 pyplot.specgram 函数来表示和音频信号.这是我的做法.

 将matplotlib.pyplot导入为plt将 scipy.io.wavfile 导入为 wavfile速率,帧= wavfile.read("song.wav")plt.specgram(帧)

我得到的结果是下面这张漂亮的频谱图:

当我查看我认为是 频率时间 域的 x 轴和 y 轴时,我无法理解频率被缩放的事实从 0 到 1.0,时间从 0 到 80k.它背后的直觉是什么,更重要的是,如何以人类友好的格式表示它,使频率为 0 到 100k,时间以秒为单位?

解决方案

  • 首先,频谱图表示信号的频谱含量随时间的变化-这是时域的频域表示波形(例如正弦波,文件"song.wav"或其他任意波形-即幅度随时间的变化).

  • 频率值(y轴,赫兹)完全取决于波形的采样频率("song.wav"),范围从"0"到采样频率/2",其中上限为奈奎斯特频率"或折叠频率"(

    I have never worked with audio signals before and little do I know about signal processing. Nevertheless, I need to represent and audio signal using pyplot.specgram function from matplotlib library. Here is how I do it.

    import matplotlib.pyplot as plt
    import scipy.io.wavfile as wavfile
    
    rate, frames = wavfile.read("song.wav")
    plt.specgram(frames)
    

    The result I am getting is this nice spectrogram below:

    When I look at x-axis and y-axis which I suppose are frequency and time domains I can't get my head around the fact that frequency is scaled from 0 to 1.0 and time from 0 to 80k. What is the intuition behind it and, what's more important, how to represent it in a human friendly format such that frequency is 0 to 100k and time is in sec?

    解决方案

    • Firstly, a spectrogram is a representation of the spectral content of a signal as a function of time - this is a frequency-domain representation of the time-domain waveform (e.g. a sine wave, your file "song.wav" or some other arbitrary wave - that is, amplitude as a function of time).

    • The frequency values (y-axis, Hertz) are wholly dependant on the sampling frequency of your waveform ("song.wav") and will range from "0" to "sampling frequency / 2", with the upper limit being the "nyquist frequency" or "folding frequency" (https://en.wikipedia.org/wiki/Aliasing#Folding). The matplotlib specgram function will automatically determine the sampling frequency of the input waveform if it is not otherwise specified, which is defined as 1 / dt, with dt being the time interval between discrete samples of the waveform. You can can pass the option Fs='sampling rate' to the specgram function to manually define what it is. It will be easier for you to get your head around what is going on if you figure out and pass these variables to the specgram function yourself

    • The time values (x-axis, seconds) are purely dependent on the length of your "song.wav". You may notice some whitespace or padding if you use a large window length to calculate each spectra slice (think- the individual spectra which are arranged vertically and tiled horizontally to create the spectrogram image)

    • To make the axes more intuitive in the plot, use x- and y-axes labels and you can also scale the axes values (i.e. change the units) using a method similar to this

    Take home message - try to be a bit more verbose with your code: see below for my example.

        import matplotlib.pyplot as plt
        import numpy as np
    
        # generate a 5Hz sine wave
        fs = 50
        t = np.arange(0, 5, 1.0/fs)
        f0 = 5
        phi = np.pi/2
        A = 1
        x = A * np.sin(2 * np.pi * f0 * t +phi)
    
        nfft = 25
    
        # plot x-t, time-domain, i.e. source waveform
        plt.subplot(211)
        plt.plot(t, x)
        plt.xlabel('time')
        plt.ylabel('amplitude')
    
        # plot power(f)-t, frequency-domain, i.e. spectrogram
        plt.subplot(212)
        # call specgram function, setting Fs (sampling frequency) 
        # and nfft (number of waveform samples, defining a time window, 
        # for which to compute the spectra)
        plt.specgram(x, Fs=fs, NFFT=nfft, noverlap=5, detrend='mean', mode='psd')
        plt.xlabel('time')
        plt.ylabel('frequency')
        plt.show()
    

    5Hz_spectrogram:

    这篇关于如何更改 pyplot.specgram x 和 y 轴缩放比例?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆