MATLAB: I need help building bandstop filter to filter noise from the wav file.

bandpass filterfftMATLABnoise removalnotch filter

I have a wav file that I an trying to remove noise from. I used fft and plotted it across frequency. I built a bandstop filter but all it does is lower the noise of the beep. Can someone help me with my filter.
clc;
close all;
clear;
[y,Fs]= audioread('beep_removal.wav');
%sound(y,Fs)
info = audioinfo('beep_removal.wav')
t = 0:seconds(1/Fs):seconds(info.Duration);
t = t(1:end-1);
subplot(2,2,1); plot(t,y)
title('Original Audio with Beep')
xlabel('Time')
ylabel('Audio Signal x(t)')
L=length(y);
NEFT = 2^nextpow2(L);
Y=abs(fft(y,NEFT));
freq = Fs/2*linspace(0,1,NEFT/2+1);
subplot(2,2,2); plot(freq, Y(1:length(freq)))
title('fft')
xlabel('Hz')
ylabel('Magnitude')
o = 5;
wn = [2100 2400]*2/Fs;
[b, a] = butter(o,wn,'stop');
figure; freqz(b,a,Fs);
[h,w] = freqz(b,a,Fs);
subplot(2,2,3); plot(w,20*log10(abs(h)));
z_filt = filter(b,a,y);
subplot (2,2,4); plot(t,z_filt);
title('Audio with filter')
xlabel('Time')
ylabel('Audio Signal z(t)')
%sound(z_filt,Fs)

Best Answer

I made a slight change in your freqz calls so they would use ‘Fs’ correctly, and I use filtfilt rather than filter to do the filtering. Your code works correctly for me.
The last part of your code with my changes:
L=length(y);
NEFT = 2^nextpow2(L);
Y=abs(fft(y,NEFT)/L);
freq = Fs/2*linspace(0,1,NEFT/2+1);
subplot(2,2,2)
plot(freq, Y(1:length(freq)))
title('fft')
xlabel('Hz')
ylabel('Magnitude')
o = 5;
wn = [2100 2400]*2/Fs;
[b, a] = butter(o,wn,'stop');
figure
freqz(b,a,2^14,Fs);
[h,w] = freqz(b,a,2^14,Fs);
subplot(2,2,3)
plot(w,20*log10(abs(h)))
z_filt = filtfilt(b,a,y);
subplot (2,2,4)
plot(t,z_filt)
title('Audio with filter')
xlabel('Time')
ylabel('Audio Signal z(t)')
sound(y,Fs)
pause(12)
sound(z_filt,Fs)
I added the sound calls at the end to demonstrate that your code works correctly. (I don’t hear the beep at all now.)
Related Question