2016-11-29 96 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中的一列。


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