首页 > 其他分享 >音频处理数据增强实验与代码分享

音频处理数据增强实验与代码分享

时间:2023-05-27 18:37:11浏览次数:41  
标签:shifted aug 音频 wave spectrogram sig print 分享 代码

对音频(audio)信号做数据增强(Data Augmentation)可以有多重方式,这里通过sox库、soundfile库、librosa库进行实验,希望可以帮助到有需要的人。可用于深度学习音频处理实验的数据预处理。

音高变换增强(Pitch Shift Augmentation)

音高变化增强,是围绕频率轴的±5%范围内的随机滚动。环绕式转换以保留所有信息。

def pitch_shift_spectrogram(wavepath):
    #     """ Shift a spectrogram along the frequency axis in the spectral-domain at
    #     random
    #     """
    wave, sr = sf.read(wavepath)

    # 将波形转换为 spectrogram
    spectrogram = librosa.stft(wave)
    nb_cols = spectrogram.shape[0]
    max_shifts = nb_cols // 20  # around 5% shift
    nb_shifts = np.random.randint(-max_shifts, max_shifts)

    shifted_spectrogram = np.roll(spectrogram, nb_shifts, axis=0)
    # 将平移后的 spectrogram 转换回波形
    shifted_wave = librosa.istft(shifted_spectrogram)
    return shifted_wave, sr

时移增强(Time Shift Augmentation)

时移增强是通过沿时间轴滚动信号来随机移位信号。

def time_shift_spectrogram(wavepath):
    """ Shift a spectrogram along the time axis in the spectral-domain at random
    """
    wave, sr = sf.read(wavepath)

    # 将波形转换为 spectrogram
    spectrogram = librosa.stft(wave)

    nb_cols = spectrogram.shape[1]
    nb_shifts = np.random.randint(0, nb_cols)

    # 对 spectrogram 进行时间轴的随机平移
    shifted_spectrogram = np.roll(spectrogram, nb_shifts, axis=1)

    # 将平移后的 spectrogram 转换回波形
    shifted_wave = librosa.istft(shifted_spectrogram)

    # # 保存平移后的波形为 WAV 文件
    # shifted_wavepath = wavepath[:-4] + "_shifted.wav"
    # sf.write(shifted_wavepath, shifted_wave, sr)

    return shifted_wave, sr

噪声增强(Noise Augmentation)

噪声增强只是在增强信号之上增加一个随机噪声段,阻尼系数限制在0.4下。

def noise_augmentation(wavepath):
    """ Perform noise augmentation of the wave by loading three noise segments
    from the noise_dir and add these on top of the wave with a dampening factor
    of 0.4
    """
    wave, fs = sf.read(wavepath)
    # dampening_factor = 0.4
    # 生成随机阻尼系数
    dampening_factor = np.random.uniform(0.0, 0.4)
    # 生成与音频长度相同的随机噪声
    noise = np.random.randn(len(wave))

    # 将随机噪声混入到音频中
    wave = wave + noise * dampening_factor
    return wave, fs

相同类别增强(Same Class Augmentation)

相同类别的增强,简单将两个音频片段s1和s2,按照一定比例相加。

def same_class_augmentation(wavepath, class_dir):
    """ Perform same class augmentation of the wave by loading a random segment
    from the class_dir and additively combine the wave with that segment.
    """
    sig_paths = glob.glob(os.path.join(class_dir, "*.wav"))

    # print('sig_paths:', sig_paths)

    aug_sig_path = np.random.choice(sig_paths, 1, replace=False)[0]
    print('aug_sig_path:', aug_sig_path)


    aug_sig, fs = sf.read(aug_sig_path)
    wave, fs1 = sf.read(wavepath)

    # 调整aug_sig的长度与wave相同
    aug_sig = interp1d(np.arange(len(aug_sig)), aug_sig)(np.linspace(0, len(aug_sig) - 1, len(wave)))

    alpha = np.random.rand()
    wave = (1.0 - alpha) * wave + alpha * aug_sig

    return wave, fs

变速增强(Time Augmentation)

请注意,需要Python安装sox库,以及Win上下载好并安装sox软件,exe所在文件夹需要加入系统环境变量。

# 变速增强,运用sox库
def speed_aug(input_wav, output_wav, speed_factor=1.2):
    # 创建 SoX 转换器对象
    transformer = sox.Transformer()
    transformer.speed(speed_factor)
    # 设置变速参数
    # speed_factor = 1.2  # 变速因子,大于 1 加快速度,小于 1 减慢速度

    # 执行变速和数据增强转换
    transformer.build(input_wav, output_wav)

代码实操

# -*- coding: utf-8 -*-
# @Time      :   2023/5/27 0027 15:18
# @Author    :   Jason
# -*- coding: utf-8 -*-
# @Time      :   2023/5/26 0026 22:07
# @Author    :   Jason
import glob
import os

import numpy as np
import sox  # py
import soundfile as sf
from scipy.interpolate import interp1d
import librosa
from config import *

# sox库
os.environ['PATH'] = 'G:\Program Files (x86)\sox-14-4-2'


# 相同类别的增强,简单将两个音频片段s1和s2,按照一定比例相加
def same_class_augmentation(wavepath, class_dir):
    """ Perform same class augmentation of the wave by loading a random segment
    from the class_dir and additively combine the wave with that segment.
    """
    sig_paths = glob.glob(os.path.join(class_dir, "*.wav"))

    # print('sig_paths:', sig_paths)

    aug_sig_path = np.random.choice(sig_paths, 1, replace=False)[0]
    print('aug_sig_path:', aug_sig_path)


    aug_sig, fs = sf.read(aug_sig_path)
    wave, fs1 = sf.read(wavepath)

    # 调整aug_sig的长度与wave相同
    aug_sig = interp1d(np.arange(len(aug_sig)), aug_sig)(np.linspace(0, len(aug_sig) - 1, len(wave)))

    alpha = np.random.rand()
    wave = (1.0 - alpha) * wave + alpha * aug_sig

    return wave, fs


# 噪声增强只是在增强信号之上增加一个随机噪声段,阻尼系数为0.4
def noise_augmentation(wavepath):
    """ Perform noise augmentation of the wave by loading three noise segments
    from the noise_dir and add these on top of the wave with a dampening factor
    of 0.4
    """
    wave, fs = sf.read(wavepath)
    # dampening_factor = 0.4
    # 生成随机阻尼系数
    dampening_factor = np.random.uniform(0.0, 0.4)
    # 生成与音频长度相同的随机噪声
    noise = np.random.randn(len(wave))

    # 将随机噪声混入到音频中
    wave = wave + noise * dampening_factor
    return wave, fs


# 时移增强是通过沿时间轴滚动信号来随机移位信号。包裹着移动
def time_shift_spectrogram(wavepath):
    """ Shift a spectrogram along the time axis in the spectral-domain at random
    """
    wave, sr = sf.read(wavepath)

    # 将波形转换为 spectrogram
    spectrogram = librosa.stft(wave)

    nb_cols = spectrogram.shape[1]
    nb_shifts = np.random.randint(0, nb_cols)

    # 对 spectrogram 进行时间轴的随机平移
    shifted_spectrogram = np.roll(spectrogram, nb_shifts, axis=1)

    # 将平移后的 spectrogram 转换回波形
    shifted_wave = librosa.istft(shifted_spectrogram)

    # # 保存平移后的波形为 WAV 文件
    # shifted_wavepath = wavepath[:-4] + "_shifted.wav"
    # sf.write(shifted_wavepath, shifted_wave, sr)

    return shifted_wave, sr


# 音高变化增强,是围绕频率轴的±5%范围内的随机滚动。环绕式转换以保留所有信息
def pitch_shift_spectrogram(wavepath):
    #     """ Shift a spectrogram along the frequency axis in the spectral-domain at
    #     random
    #     """
    wave, sr = sf.read(wavepath)

    # 将波形转换为 spectrogram
    spectrogram = librosa.stft(wave)
    nb_cols = spectrogram.shape[0]
    max_shifts = nb_cols // 20  # around 5% shift
    nb_shifts = np.random.randint(-max_shifts, max_shifts)

    shifted_spectrogram = np.roll(spectrogram, nb_shifts, axis=0)
    # 将平移后的 spectrogram 转换回波形
    shifted_wave = librosa.istft(shifted_spectrogram)
    return shifted_wave, sr


# 变速增强,运用sox库
def speed_aug(input_wav, output_wav, speed_factor=1.2):
    # 创建 SoX 转换器对象
    transformer = sox.Transformer()
    transformer.speed(speed_factor)
    # 设置变速参数
    # speed_factor = 1.2  # 变速因子,大于 1 加快速度,小于 1 减慢速度

    # 执行变速和数据增强转换
    transformer.build(input_wav, output_wav)


# 增加方法如下

if __name__ == "__main__":
    rootPath = 'H:\\Codes\\AudioClassification-Pytorch-master\\dataset\\audio'
    sourceType = ['C', 'M', 'E', '0', 'ru'] # 增强前类别名
    source_list = []
    newlist = ['C_a', 'M_a', 'E_a', '0_a', 'ru_a'] # 增强后类别名
    s = []
    t = []

    for i in range(0, len(sourceType) - 1):
        s = []
        t = []
        sourcePath = os.path.join(rootPath, sourceType[i])
        # print(sourcePath)
        # 获取文件夹中的所有文件和子文件夹列表
        source_list = os.listdir(sourcePath)
        # print(source_list)
        for j in source_list:
            s.append(os.path.join(sourcePath, j))

        print('s: ', s)

        for x in range(len(s)):
            targetPath = os.path.join(rootPath, newlist[i])
            # print(targetPath)
            # target_list = os.listdir(targetPath)
            # print(target_list)

            t.append(os.path.join(targetPath, str(startIdx)) + '~~~~.wav')
            startIdx += 1

        print('t: ', t)
        """
        从这里开始调用函数生成新样本
        """
        # 变速增强
        # for i in range(len(s)): # 1.2
        #     speed_aug(s[i], t[i])
        # for i in range(len(s)):
        #     speed_aug(s[i], t[i], 0.8)
        # for i in range(len(s)):
        #     speed_aug(s[i], t[i], 1.1)
        # for i in range(len(s)):
        #     speed_aug(s[i], t[i], 0.9)

        # 3724-4579
        # 混合两文件增强
        # for k in range(len(s)):
        #     print('-')
        #     print(s[k])
        #     # print(os.path.join('H:\Codes\AudioClassification-Pytorch-master\dataset\\audio', sourceType[i]))
        #     w, fs = same_class_augmentation(s[k], os.path.join('H:\Codes\AudioClassification-Pytorch-master\dataset\\audio',
        #                                                        sourceType[i]))
        #     print('t[k]: ', t[k])
        #     sf.write(t[k], w, fs)
        #     print('-')

        # 4580-5435
        # 5436-6291
        # 加噪增强
        # for k in range(len(s)):
        #     print('-')
        #     print(s[k])
        #     # print(os.path.join('H:\Codes\AudioClassification-Pytorch-master\dataset\\audio', sourceType[i]))
        #     w, fs = noise_augmentation(s[k])
        #     print('t[k]: ', t[k])
        #     sf.write(t[k], w, fs)
        #     print('-')

        # 时移增强
        # 6292-7147~~~
        # for k in range(len(s)):
        #     print('-')
        #     print(s[k])
        #     # print(os.path.join('H:\Codes\AudioClassification-Pytorch-master\dataset\\audio', sourceType[i]))
        #     w, fs = time_shift_spectrogram(s[k])
        #     print('t[k]: ', t[k])
        #     sf.write(t[k], w, fs)
        #     print('-')

        # 音高变化增强
        # 7148-
        for k in range(len(s)):
            print('-')
            print(s[k])
            # print(os.path.join('H:\Codes\AudioClassification-Pytorch-master\dataset\\audio', sourceType[i]))
            w, fs = pitch_shift_spectrogram(s[k])
            print('t[k]: ', t[k])
            sf.write(t[k], w, fs)
            print('-')

        """
        结束
        """

        print('---------1 class end')

增强后如图

音频处理数据增强实验与代码分享_数据增强

参考文献:

处理库的官方文档

https://zhuanlan.zhihu.com/p/41679490

https://github.com/johnmartinsson/bird-species-classification/wiki/Data-Augmentation


标签:shifted,aug,音频,wave,spectrogram,sig,print,分享,代码
From: https://blog.51cto.com/u_15945549/6362728

相关文章

  • 基于餐厅消费数据的隐形资助研究|校内数模竞赛分享
    前言幸亏校赛当做期末考试,才第一次这么认真点去审视一道建模同题目,前期的莽撞,对数据无奈是很崩溃的。另寻它解或是坚持耕耘都可以作为这次建模收官的关键词,因为它们真的都同时存在,并且不相上下。由于这是一道有关我们"本家"————大数据的题目,我们把数据处理当做了一个比较重要......
  • 武汉星起航:亚马逊高效选品指南分享,助力卖家有效提升销量
    在亚马逊平台上选择合适的产品是一个关键的成功因素。高效选品不仅可以帮助亚马逊卖家实现销售增长,还可以降低竞争压力并提高盈利能力。武汉星起航将为亚马逊卖家提供一些高效选品的策略和指南。市场研究和分析:在选择产品之前,进行充分的市场研究和分析是至关重要的。了解当前市场趋......
  • 武汉星起航:欧洲站五月运营攻略分享,卖家应该如何布局
    五月是亚马逊欧洲站点运营的重要时期,为了最大化销售和利润,卖家需要制定有效的运营策略。以下是武汉星起航整理的一些关键的运营建议,帮助您在五月取得成功:产品选品:五月是许多国家的节假日和庆祝活动的季节,因此选择与这些活动相关的产品是明智的。了解不同国家的节日和特殊活动,选择热......
  • MarkDown快捷键分享
    MarkDown学习typora的一些快捷键标题一级标题:一个#号加一个空格加标题再回车二级标题:两个#号加一个空格加标题再回车三级标题:三个#号加一个空格加标题再回车四级标题:四个#号加一个空格加标题再回车...以此类推字体加粗:字体两边加上两个*斜体:字体两......
  • 信源编码的代码实现 (香农编码、费诺编码、哈夫曼编码、游程编码、算术编码)
    @[TOC](文章目录)香农编码(1)将信源消息符号按其出现的概率大小依次排列p1≥p2≥...≥pn(2)确定满足下列不等式的整数码长Ki为-log2(pi)≤Ki<-log2(pi)+1(3)为了编成唯一可译码,计算第i个消息的累加概率(4)将累加概率Pi转换成二进制数。(5)取Pi二进数......
  • 源代码管理工具:提升团队协作与开发效率的利器
    在软件开发领域,源代码管理是一项至关重要的任务。随着团队规模的扩大和项目复杂性的增加,有效地管理和协调代码的变更变得尤为重要。为了应对这一挑战,源代码管理工具应运而生。本文将介绍源代码管理工具的概念、作用以及一些流行的工具,以帮助读者理解并选择适合自己团队的工具。......
  • 低代码的“钱景”——专业的事交给专业的人来做
    你需要知道的低代码低代码通常是指一种可视化的开发方法,用较少的代码、较快的速度来交付应用程序,相似的概念还有“无代码”,也是一种开发方法,通常是面向非技术性员工,让业务人员也可以成为“技术人员”,不需要写任何一行代码来构建应用程序,用低代码甚至无代码创建应用、数据和分析工......
  • 源代码管理工具——GitHub
    GitHub——敏捷开发,CI/CD的倡导者和受益者1.简介GitHub是一个面向开源及私有软件项目的托管平台,因为只支持Git作为唯一的版本库格式进行托管,故名GitHub。Github拥有1亿以上的开发人员,400万以上组织机构和3.3亿以上资料库。2.发展历程GitHub平台于2007年10月1日开始开发,由GitHu......
  • C# 中的字符串——新增功能,通过代码示例进行解释
    我们在代码中使用的大部分内容都是字符串。让我们看一下C#字符串的一些新功能……包括C#11中新增的原始字符串文字和原始字符串插值。原始字符串字面量可以简单灵活地构建复杂的多行字符串,包括JSON。无需逃避。对应视频教程:https://www.java567.com/open/1在本文中,我们将......
  • Python相关性分析代码
    进行相关性分析的代码主要涉及数据处理和统计分析。以下是使用Python进行相关性分析的一般步骤:1.导入必要的库:importpandasaspdimportnumpyasnpimportseabornassnsimportmatplotlib.pyplotasplt2.读取数据:将你的数据加载到PandasDataFrame中。data=pd.read_c......