找一段完整的VAD检测的matlab程序和python程序
时间: 2023-12-15 17:03:57 浏览: 104
以下是一个基于MATLAB的VAD检测程序:
```matlab
clc;clear all;
[x,fs]=audioread('example.wav');
x=x(:,1);
N=length(x);
frame_size=20;
frame_shift=10;
frame_length=frame_size*fs/1000;
frame_shift_length=frame_shift*fs/1000;
num_frames=floor((N-frame_length)/frame_shift_length)+1;
f=zeros(num_frames,frame_length);
for i=1:num_frames
f(i,:)=x((i-1)*frame_shift_length+1:(i-1)*frame_shift_length+frame_length);
end
energy=sum(f.^2,2);
energy=energy/max(energy);
subplot(2,1,1);plot(x);title('Raw Signal');
subplot(2,1,2);plot(energy);title('Normalized Energy');
```
以下是一个基于Python的VAD检测程序:
```python
import numpy as np
import soundfile as sf
from scipy.signal import butter, lfilter
def vad(signal, window_size, window_shift, threshold):
window_size_samples = int(window_size * fs)
window_shift_samples = int(window_shift * fs)
num_frames = int(np.ceil(float(np.abs(len(signal) - window_size_samples)) / window_shift_samples))
pad_signal_length = num_frames * window_shift_samples + window_size_samples
z = np.zeros((pad_signal_length - len(signal),))
pad_signal = np.concatenate((signal, z))
indices = np.tile(np.arange(0, window_size_samples), (num_frames, 1)) + \
np.tile(np.arange(0, num_frames * window_shift_samples, window_shift_samples), (window_size_samples, 1)).T
frames = pad_signal[indices.astype(np.int32, copy=False)]
frames = frames * np.hamming(window_size_samples)
energy = np.sum(np.square(frames), axis=1)
energy = energy / np.max(energy)
return np.array(np.where(energy > threshold))
if __name__ == '__main__':
signal, fs = sf.read('example.wav')
threshold = 0.1
window_size = 20 # ms
window_shift = 10 # ms
vad_result = vad(signal, window_size/1000, window_shift/1000, threshold)
print(vad_result)
```
阅读全文