-2

I am trying to remove noise from a wav file. But I keep getting the following error after run the script. The wav file I use is https://drive.google.com/file/d/0BzIyOj_KUKufTTNWMFlRMW9fT2c/view?usp=sharing

I use the code from 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 ';']);

Here is the code:

%% 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;
Community
  • 1
  • 1

1 Answers1

1

The code assumes that the signal is stereo (a.k.a. two channels). Your sound file most likely is mono (judging from the way it sounds... a.k.a. one channel), so any references in the code that uses the right channel should be removed.

Simply put, the only part of the code that is affected is displaying the right channel in time-domain. The rest of the code should work as it accesses the left channel in stereo, which is coincidentally the first column of the sound file and the only column in a mono file.

Replace this code:

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');

with:

N = size(f,1); % Determine total number of samples in audio file
figure;
stem(1:N, f(:,1));
title('Mono Channel');

In the future, try and read the MATLAB errors more carefully. They're very verbose and descriptive on what the problem in your code is. In this case, it's complaining that you are trying to access a column in the sound file f that doesn't exist.


Note: I am the original author of the answer you have linked.

rayryeng
  • 102,964
  • 22
  • 184
  • 193