我试图从wav文件中删除噪音。但是在运行脚本后我一直收到以下错误。我使用的wav文件是https://drive.google.com/file/d/0BzIyOj_KUKufTTNWMFlRMW9fT2c/view?usp=sharing
我使用Remove noise from wav file, MATLAB的代码。
>> run sample3
Index exceeds matrix dimensions.
Error in sample3 (line 17)
stem(1:N, f(:,2));
Error in run (line 96)
evalin('caller', [script ';']);
这是代码:
%% Read in the file
clearvars;
close all;
[f,fs] = audioread('noise.wav');
%% Play original file
pOrig = audioplayer(f,fs);
pOrig.play;
%% Plot both audio channels
N = size(f,1); % Determine total number of samples in audio file
figure;
subplot(2,1,1);
stem(1:N, f(:,1));
title('Left Channel');
subplot(2,1,2);
stem(1:N, f(:,2));
title('Right Channel');
%% Plot the spectrum
df = fs / N;
w = (-(N/2):(N/2)-1)*df;
y = fft(f(:,1), N) / N; % For normalizing, but not needed for our analysis
y2 = fftshift(y);
figure;
plot(w,abs(y2));
[B,A] = butter(n, [beginFreq, endFreq], 'bandpass');
%% Design a bandpass filter that filters out between 700 to 12000 Hz
n = 7;
beginFreq = 700 / (fs/2);
endFreq = 12000 / (fs/2);
[B,A] = butter(n, [beginFreq, endFreq], 'bandpass');
%% Filter the signal
fOut = filter(b, a, f);
%% Construct audioplayer object and play
p = audioplayer(fOut, fs);
p.play;
该代码假定信号是立体声(也就是两个声道)。您的声音文件很可能是单声道的(从声音的方式判断... a.k.a.一个声道),因此应删除使用正确声道的代码中的任何引用。
简而言之,受影响的代码的唯一部分是在时域中显示正确的通道。代码的其余部分应该在访问立体声中的左声道时起作用,这恰好是声音文件的第一列和单声道文件中的唯一列。
替换此代码:
N = size(f,1); % Determine total number of samples in audio file
figure;
subplot(2,1,1);
stem(1:N, f(:,1));
title('Left Channel');
subplot(2,1,2);
stem(1:N, f(:,2));
title('Right Channel');
有:
N = size(f,1); % Determine total number of samples in audio file
figure;
stem(1:N, f(:,1));
title('Mono Channel');
将来,请仔细阅读MATLAB错误。它们对代码中的问题非常详细和描述。在这种情况下,它抱怨您正在尝试访问声音文件f
中不存在的列。
注意:我是您链接的答案的原作者。