• Python技巧1: 使用python分析wav文件


    关注公众号:Python爬虫数据分析挖掘,回复【开源源码】免费获取更多开源项目源码

    对于声音类的文件分析起来除了听最好是先可以把声音转换成图形,这样对于声音文件之间的不同有一个视觉上的认知,对于后续分析可以是一个很有用的补充。

    python可以利用SCIPY库装载wav文件,并使用matplotlib绘制图形。首先我从这个网站上下载了1M和2M的wav文件作为wav样例文件:https://file-examples.com/index.php/sample-audio-files/sample-wav-download/

    然后使用下面的代码装在并绘制wav文件的音调图形:

    from scipy.io import wavfile
    from matplotlib import pyplot as plt
    from matplotlib.pyplot import figure
     
    # load wav files
    fs_1m,data_1m = wavfile.read("./wav/file_example_WAV_1MG.wav")
    fs_2m,data_2m = wavfile.read("./wav/file_example_WAV_2MG.wav")
     
    # set plt style
    plt.style.use('seaborn-whitegrid')
     
    # plot data
    fig, (ax1, ax2) = plt.subplots(1, 2)
    ax1.plot(data_1m, color='b')
    ax1.set_title("auido with 1M size")
    ax2.plot(data_2m, color='y')
    ax2.set_title("auido with 2M size")
     
    plt.savefig('audio.png', dpi=150)

    输出的图形如下:

    可以看到两个图形基本一样,但是2M文件的图形的X坐标是1M文件的2倍。

    然后我们可以使用fastdtw库很容易计算出两个音频数据之间的欧几里得距离:

    from fastdtw import fastdtw
    from scipy.spatial.distance import euclidean
     
    # calculate euclidean distance
    distance,path = fastdtw(data_1m, data_2m, dist=euclidean)
    print("the distance between the two clips is %s" % distance)

    输出结果如下:

    the distance between the two clips is 4093034781.337242

    耐得住寂寞,才能登得顶
    Gitee码云:https://gitee.com/lyc96/projects
  • 相关阅读:
    java操作生成jar包 和写入jar包
    jboss配置jndi连接池
    windows 域的LDAP查询相关举例
    LDAP error Code 及解决方法
    HDU 6417
    CF1299D Around the World
    codechef Chef and The Colored Grid
    Educational Codeforces Round 82 (Rated for Div. 2)
    CF1237F Balanced Domino Placements
    CF1254E Send Tree to Charlie
  • 原文地址:https://www.cnblogs.com/chenlove/p/13625288.html
Copyright © 2020-2023  润新知