MATLAB代码丨信号处理:对Python中Librosa库部分函数的重现

目录

Librosa:用于信号处理的Python库

对Librosa部分函数的MATLAB重现

谱重心(Centroid)

滚降频率(Rolloff)

均方根(RMS)

过零率(ZeroCrossingRate)


Librosa:用于信号处理的Python库

官方介绍:A python package for music and audio analysis

网址:librosa --- librosa 0.11.0 documentation

代码网址:https://github.com/librosa/librosa

安装:https://github.com/librosa/librosa/blob/main/README.md

对Librosa部分函数的MATLAB重现

常用的信号处理特征及指标有均方根(RMS)、谱重心、谱宽度、过零率、滚降频率、梅尔频率倒谱系数(MFCCs)等等。

MATLAB中也有一些用于信号处理的拓展包,例如Audio Toolbox、Signal Processing Toolbox等等,其中是否有和Librosa函数直接对标的函数暂时没有进行调研。

这里对于几个Librosa中的函数进行Matlab重现,并且对于IRMAS数据集中的部分音频进行测试,与Librosa提取得到结果进行比对。

谱重心(Centroid)

Librosa库中调用

python 复制代码
spec_cent = librosa.feature.spectral_centroid(y=y, sr=sr)

MATLAB复现

Matlab 复制代码
function centroid = getCentroid(y, fs)
    framelength = 2048;
    hoplength = 512;
    nfft = 2048;
    if size(y, 2) > 1
        y = y(:, 1);
    end
    numFrames = ceil((length(y) - framelength) / hoplength) + 1;
    padLength = (numFrames - 1) * hoplength + framelength - length(y);
    y = [y; zeros(padLength, 1)];
    centroid = zeros(numFrames, 1);
    freqs = (0:(nfft/2)) * (fs / nfft);
    for i = 1:numFrames
        startIdx = (i - 1) * hoplength + 1;
        endIdx = startIdx + framelength - 1;
        frame = y(startIdx:endIdx);
        frame = frame .* hanning(framelength);
        spectrum = abs(fft(frame, nfft));
        spectrum = spectrum(1:nfft/2+1);
        numerator = sum(freqs.*spectrum);
        denominator = sum(spectrum);
        if denominator > 0
            centroid(i) = numerator / denominator;
        else
            centroid(i) = 0;
        end
    end
end

验证结果可视化

滚降频率(Rolloff)

Librosa库中调用

python 复制代码
rolloff = librosa.feature.spectral_rolloff(y=y, sr=sr)

MATLAB复现

Matlab 复制代码
function rolloff = getRolloff(y, fs, roll_percent)
    if nargin < 3
        roll_percent = 0.85;
    end
    if roll_percent < 0 || roll_percent >= 1
        error('roll_percent must lie in the range (0, 1)');
    end
    if size(y, 2) > 1
        y = mean(y, 2);
    end
    n_fft = 2048;
    hop_length = 512;
    window = hann(n_fft);
    [S, f, t] = spectrogram(y, window, n_fft-hop_length, n_fft, fs);
    S = abs(S);
    total_energy = cumsum(S, 1);
    if isempty(f)
        f = linspace(0, fs/2, n_fft/2 + 1);
    end
    threshold = roll_percent * total_energy(end, :);
    rolloff = NaN(1, length(t));
    for i = 1:length(t)
        [~, ind] = find(total_energy(:, i) >= threshold(:, i), 1, 'first');
        if ~isempty(ind)
            rolloff(i) = f(ind);
        end
    end
end

验证结果可视化

均方根(RMS)

Librosa库中调用

python 复制代码
rms = librosa.feature.rms(y=y)

MATLAB复现

Matlab 复制代码
function rms_vals = getRMS(y, fs)
    frame_length = 2048;
    hop_length = 512;
    if size(y, 2) > 1
        y = mean(y, 2);
    end
    y = y / max(abs(y));
    num_frames = ceil((length(y) - frame_length) / hop_length) + 1;
    padded_length = (num_frames - 1) * hop_length + frame_length;
    y_padded = [y; zeros(padded_length - length(y), 1)];
    rms_vals = zeros(1, num_frames);
    for i = 1:num_frames
        frame_start = (i - 1) * hop_length + 1;
        frame_end = frame_start + frame_length - 1;
        frame = y_padded(frame_start:frame_end);
        rms_vals(i) = sqrt(mean(frame.^2));
    end
end

验证结果可视化

过零率(ZeroCrossingRate)

Librosa库中调用

python 复制代码
zero_cross = librosa.feature.zero_crossing_rate(y=y)

MATLAB复现

Matlab 复制代码
function zcr = getZeroCrossing_rate(y)
    frame_length = 2048;
    hop_length = 512;
    center = true;
    if size(y, 2) > 1
        y = mean(y, 2);
    end
    if ~isvector(y)
        error('y should be a vector (signal)');
    end
    if center
        padding_length = floor(frame_length / 2);
        y = [repmat(y(1), padding_length, 1); y; repmat(y(end), padding_length, 1)];
    end
    frames = buffer(y, frame_length, frame_length - hop_length);
    num_frames = size(frames, 2);
    zcr = zeros(1, num_frames);
    for i = 1:num_frames
        frame = frames(:, i);
        crossings = sum(abs(diff(sign(frame))) > 0);
        zcr(i) = crossings / frame_length;
    end
end

验证结果可视化


END

相关推荐
wangsir.3 小时前
测试之自动化测试常用函数
python·测试
微小冷3 小时前
Rust异步编程详解
开发语言·rust·async·await·异步编程·tokio
铁蛋AI编程实战3 小时前
MemoryLake 实战:构建超长对话 AI 助手的完整代码教程
人工智能·python·microsoft·机器学习
A9better3 小时前
C++——不一样的I/O工具与名称空间
开发语言·c++·学习
清水白石0083 小时前
《为什么说 deque 是 Python 滑动窗口的“隐藏神器”?深入解析双端队列的高效之道》
开发语言·python
杜子不疼.3 小时前
Ascend_C自定义算子开发
c语言·开发语言
kjkdd3 小时前
5. LangChain设计理念和发展历程
python·语言模型·langchain·ai编程
摘星编程3 小时前
CANN ops-nn 激活函数算子全解析:从ReLU到GELU的演进与实现
python
WooaiJava4 小时前
流式TTS音频播放项目 - 面试问答(后端)
java·开发语言
love530love4 小时前
【高阶编译】Windows 环境下强制编译 Flash Attention:绕过 CUDA 版本不匹配高阶指南
人工智能·windows·python·flash_attn·flash-attn·flash-attention·定制编译