Sei sulla pagina 1di 4

6/19/2019 Can you help remove the noise from this audio file?

- MATLAB Answers - MATLAB Central

MATLAB Answers

See Also
MATLAB Answers

I need help building bandstop


filter to filter noise from my wav
file.

 Vote 1 Answer
Follow 1 how to filter noise?
Can you help remove the noise from this audio file? 1 Answer
Asked by Christopher Vergara on 17 Sep 2017
Latest activity Commented on by Star Strider  on 20 Sep 2017 Filter specific frequencies in an
Accepted Answer by Star Strider  audio file
503 views (last 30 days)
1 Answer
I'm having trouble removing the noise heard from this audio file audio_sample.wav.
Entire Website
I've attempted to use the "butter" function to experiment with removing certain frequencies in an effort to
reduce the noise as much as possible.
You will need to have the audio file in the search path your MATLAB is using. Basic Spectral Analysis
The code will play my "current solution".
Thanks for any help. Documentation

%%1) Load the 'audio_sample.wav' file in MATLAB


imdct
[sample_data, sample_rate] = audioread('audio_sample.wav');
Documentation
% a. Plot the signal in time and the amplitude of its frequency
% components using the FFT.
Fourier Transforms
sample_period = 1/sample_rate;
t = (0:sample_period:(length(sample_data)-1)/sample_rate);
Documentation
subplot(2,2,1)
plot(t,sample_data)
title('Time Domain Representation - Unfiltered Sound')
xlabel('Time (seconds)')
Tags
ylabel('Amplitude')
fft noise
xlim([0 t(end)])
m = length(sample_data); % Original sample length.
n = pow2(nextpow2(m)); % Transforming the length so that the number of filter
% samples is a power of 2. This can make the transform computation
% significantly faster,particularly for sample sizes with large prime signal processing
% factors.
y = fft(sample_data, n);
f = (0:n-1)*(sample_rate/n); butter
amplitude = abs(y)/n;
subplot(2,2,2)
plot(f(1:floor(n/2)),amplitude(1:floor(n/2)))
title('Frequency Domain Representation - Unfiltered Sound') Products
xlabel('Frequency')
Signal Processing Toolbox
ylabel('Amplitude')
% b. Listen to the audio file.
% sound(sample_data, sample_rate)
%%2) Filter the audio sample data to remove noise from the signal.
order = 7;
[b,a] = butter(order,1000/(sample_rate/2),'low');
filtered_sound = filter(b,a,sample_data);
sound(filtered_sound, sample_rate)
t1 = (0:sample_period:(length(filtered_sound)-1)/sample_rate);
subplot(2,2,3)

Deep
plot(t1,filtered_sound)
title('Time Domain Representation - Filtered Sound')
xlabel('Time (seconds)')
ylabel('Amplitude') Learning for
xlim([0 t1(end)])
m1 = length(sample_data); % Original sample length. Signal
n1 = pow2(nextpow2(m1)); % Transforming the length so that the number of
% samples is a power of 2. This can make the transform computation

https://www.mathworks.com/matlabcentral/answers/357022-can-you-help-remove-the-noise-from-this-audio-file 1/4
6/19/2019 Can you help remove the noise from this audio file? - MATLAB Answers - MATLAB Central
% significantly faster,particularly for sample sizes with large prime
% factors. Processing
y1 = fft(filtered_sound, n1);
f = (0:n1-1)*(sample_rate/n1); with MATLAB
amplitude = abs(y1)/n1;
subplot(2,2,4)  Download white
plot(f(1:floor(n1/2)),amplitude(1:floor(n1/2))) paper
title('Frequency Domain Representation - Filtered Sound')
xlabel('Frequency')
ylabel('Amplitude')

 0 Comments
Sign in to comment.

 1 Answer


Vote
2
 Link
Answer by Star Strider  on 18 Sep 2017

 Accepted Answer

It is not possible to eliminate broadband noise with a frequency-selective filter. You have to use wavelets to
effectively de-noise it.
I got reasonable results with this filter, and using the filtfilt function:

Fs = sample_rate; % Sampling Frequency (Hz)


Fn = Fs/2; % Nyquist Frequency (Hz)
Wp = 1000/Fn; % Passband Frequency (Normalised)
Ws = 1010/Fn; % Stopband Frequency (Normalised)
Rp = 1; % Passband Ripple (dB)
Rs = 150; % Stopband Ripple (dB)
[n,Ws] = cheb2ord(Wp,Ws,Rp,Rs); % Filter Order
[z,p,k] = cheby2(n,Rs,Ws,'low'); % Filter Design
[soslp,glp] = zp2sos(z,p,k); % Convert To Second-Order-Section For Stability
figure(3)
freqz(soslp, 2^16, Fs) % Filter Bode Plot
filtered_sound = filtfilt(soslp, glp, sample_data);
sound(filtered_sound, sample_rate)
Experiment to get the results you want.

 4 Comments
Hide 1 older comment
 Christopher Vergara on 18 Sep 2017

Thanks for your help.


Having worked on it a little more, I realized that the noise plays in isolation from approximately
0s to 3.44s.

https://www.mathworks.com/matlabcentral/answers/357022-can-you-help-remove-the-noise-from-this-audio-file 2/4
6/19/2019 Can you help remove the noise from this audio file? - MATLAB Answers - MATLAB Central
Therefore I analyzed the first 3.44s of audio_sample.wav and displayed its frequency
components using the FFT.
Given that I have the frequency components of the:
• Unfiltered Audio, and

• Unfiltered Noise of the Audio

What is the method of subtracting the frequency components of the noise from the entire
frequency spectrum of audio_sample.wav?

close all; clear all; clc;


%%Analyse the audio
[y, Fs] = audioread('audio_sample.wav');
% Plot the Time Domain Representation
t = (0:1/Fs:(length(y)-1)/Fs);
subplot(2,2,1)
plot(t,y)
title('Time Domain - Unfiltered Audio')
xlabel('Time (seconds)')
ylabel('Amplitude')
xlim([0 t(end)])
% By visual inspection, the noise plays from 0 sec to approximately 3.44
% sec
% Plot the Frequency Domain Representation
m = length(y);
x = fft(y, m);
f = (0:m-1)*(Fs/m);
amplitude = abs(x)/m;
subplot(2,2,2)
plot(f(1:(m/2)),amplitude(1:(m/2)))
title('Frequency Domain - Unfiltered Audio')
xlabel('Frequency')
ylabel('Amplitude')
xlim([0 2000])
%%Analyse the noise audio
noise_Fs = [1, 3.44*Fs]; % Collect the first 3.44 sec
[y_n, Fs_n] = audioread('audio_sample.wav', noise_Fs);
% Plot the Time Domain Representation
t_n = (0:1/Fs_n:(length(y_n)-1)/Fs_n);
subplot(2,2,3)
plot(t_n,y_n)
title('Time Domain - Unfiltered Noise of the Audio')
xlabel('Time (seconds)')
ylabel('Amplitude')
xlim([0 t(end)])
% Plot the Frequency Domain Representation
a = length(y_n);
v = fft(y_n, a);
f_n = (0:a-1)*(Fs/a);
amplitude = abs(v)/a;
subplot(2,2,4)
plot(f_n(1:(a/2)),amplitude(1:(a/2)))
title('Frequency Domain - Unfiltered Noise of the Audio')
xlabel('Frequency')
ylabel('Amplitude')
xlim([0 2000])
ylim([0 0.02])

 Star Strider  on 18 Sep 2017

If you have the unfiltered noise, just subtract it from the signal+noise in the time domain. That
should produce a clean signal.

https://www.mathworks.com/matlabcentral/answers/357022-can-you-help-remove-the-noise-from-this-audio-file 3/4
6/19/2019 Can you help remove the noise from this audio file? - MATLAB Answers - MATLAB Central
The noise spectrum you plotted seems to be correlated to the frequency content of your
signal. That is going to complicate any frequency-based filtering approach.
You could theoretically design a bandstop filter that simulates the inverse of the noise signal.
The easiest way to do that would be to ‘smooth’ it in the frequency domain with a Savitzsky-
Golay filter (the sgolayfilt function), and the use the inverse of that (subtract it from the
maximum) and the firls function (or related functions) to produce a filter that approximates and
specifically filters that noise. You will have to experiment with that to get the result you want.
The best option to remove broadband noise (that is not frequency-limited) is to use wavelets.

 Christopher Vergara on 20 Sep 2017

Thanks Star Strider. I ended up taking a low filter to the signal and then using your
recommended method of wavelets to try and reduce as much of the noise as possible whilst
preserving as much of the music.

 Star Strider  on 20 Sep 2017

As always, my pleasure.

Sign in to comment.

Sign in to answer this question.

mathworks.com
© 1994-2019 The MathWorks, Inc. MATLAB and Simulink are registered trademarks of The MathWorks, Inc. See mathworks.com/trademarks for a list of additional
trademarks. Other product or brand names may be trademarks or registered trademarks of their respective holders.

https://www.mathworks.com/matlabcentral/answers/357022-can-you-help-remove-the-noise-from-this-audio-file 4/4

Potrebbero piacerti anche