上述代码中错误使用 plot 向量长度必须相同。 出错 Untitled (line 38) plot(t,energy)
时间: 2024-02-07 22:04:00 浏览: 69
非常抱歉,我的错误!你说的对,短时能量和过零率计算得到的向量长度可能会与原始信号的长度不同,需要进行处理。在这里,我们可以通过在短时能量和过零率向量前面添加一些0,使得它们的长度与原始信号相同,代码如下:
```
% 读入语音文件
[x,fs] = audioread('sample.wav');
x = x(:); % 将语音信号强制转换为列向量
% 设置参数
frame_len = 0.02; % 帧长20ms
frame_overlap = 0.5; % 帧重叠50%
energy_threshold = 0.1; % 能量阈值
zero_crossing_threshold = 10; % 过零率阈值
% 计算帧长和帧移
frame_length = round(frame_len*fs);
frame_shift = round(frame_length*(1-frame_overlap));
% 分帧
frames = buffer(x,frame_length,frame_length-frame_shift,'nodelay');
% 计算每帧的短时能量和过零率
energy = sum(frames.^2);
energy = [zeros(1,frame_shift), energy, zeros(1,frame_shift)];
zero_crossing = sum(abs(diff(sign(frames))),1);
zero_crossing = [zeros(1,frame_shift), zero_crossing, zeros(1,frame_shift)];
% 第一级判决
energy_detection = energy > energy_threshold*max(energy);
zero_crossing_detection = zero_crossing > zero_crossing_threshold*max(zero_crossing);
% 第二级判决
detection = energy_detection & zero_crossing_detection;
% 可视化结果
figure
t = (1:length(x))/fs;
subplot(3,1,1)
plot(t,x)
xlabel('Time (s)')
ylabel('Amplitude')
title('Original Signal')
subplot(3,1,2)
plot(t,energy)
hold on
plot(t,energy_threshold*max(energy)*ones(size(t)),'r')
xlabel('Time (s)')
ylabel('Energy')
title('Short-time Energy')
subplot(3,1,3)
plot(t,zero_crossing)
hold on
plot(t,zero_crossing_threshold*max(zero_crossing)*ones(size(t)),'r')
xlabel('Time (s)')
ylabel('Zero Crossing')
title('Zero Crossing Rate')
figure
plot(t,detection)
xlabel('Time (s)')
ylabel('Detection')
title('Endpoint Detection')
```
再次感谢你的指正!
阅读全文