2016-11-29 110 views
-1

我想从wav文件中移除噪声。但是在运行脚本后我仍然收到以下错误。 我使用的wav文件是https://drive.google.com/file/d/0BzIyOj_KUKufTTNWMFlRMW9fT2c/view?usp=sharing噪声在Matlab中取消

我使用的代码从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; 

回答

0

的代码假定该信号是立体声(又名两个通道)。您的声音文件很可能是单声道的(从声音的方式来看,也就是一个声道),因此应删除使用正确声道的代码中的任何参考。

简而言之,受影响的代码的唯一部分是在时域中显示正确的通道。剩下的代码应该能够以立体声的方式访问左声道,这恰好是声音文件的第一列和单声道文件中的唯一一列。

替换此代码:

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中的一列。


注意:我是您已链接答案的原始作者。