Unable to fully filter out a tone with a notch filter.
I am working on coding a matlab notch filter that is supposed to fitler out a tone in the audio file listed in the code. The Notch Filter works as it is supposed to except at the very beginning and at the very end the tone is still there and not filtered out and I need to have the tone completely gone. I am unable to figure out what to change in my code I have tried changing the frequencies I have tried changing where my filter starts and my notch width but nothing has solved my issue. Any help or suggestions is appreciated Thanks!
Code here below
% Load input sound
[Sound, fs] = audioread(‘SunshineSquare.wav’);
figure(1);
% Plot specgram of input sound
specgram(Sound, 512, fs);
title(‘Spectrogram of Input Sound (SunshineSquare.wav)’);
% Parameters of Notch Filter
frequencies = [1574.99, 3149.97, 4724.96];
notchwidth = 10;
% Tone Removal
a_total = 1;
b_total = 1;
for i = 1:length(frequencies)
Wn = [(frequencies(i) – notchwidth/2) / (fs/2), (frequencies(i) + notchwidth/2) / (fs/2)];
Wn = max(min(Wn, 0.99), 0.01);
[b, a] = butter(2, Wn, ‘stop’);
b_total = conv(b_total, b);
a_total = conv(a_total, a);
end
filteredAudio = filter(b_total, a_total, Sound);
% Play filtered sound
soundsc(filteredAudio, fs);
figure(2);
% Plot specgram of output sound after using notch filter
specgram(filteredAudio, 512, fs);
title(‘Specgram after Notch Filter’);
% Plot SunshineSquare sound
figure(3);
t = (0:length(Sound)-1)/fs;
plot(t, Sound);
xlabel(‘Time (sec)’);
ylabel(‘Amplitude’);
title(‘Notch-Filtered SunshineSquare Sound Plot’);
figure(4);
plot (ww,abs(HH));
zplane(bb,aa)
xlabel(‘Real’);
ylabel(‘Imaginary’);
title(‘Magnitude IIR Frequency Response (Z-Plane)’);
grid on
figure(5);
% Plot Notch-filtered sound
t = (0:length(Sound)-1)/fs;
plot(t, filteredAudio);
xlabel(‘Time (sec)’);
ylabel(‘Amplitude’);
title(‘Notch-Filtered Z-Plot’);
figure(6);
sgtitle(‘Redo Tone Removal Step 2: Magnitude Response for Notch Filter’);
f1 = 1574.99/fs * 2 * pi;
f2 = 3149.97/fs * 2 * pi;
f3 = 4724.96/fs * 2 * pi;
% Plot nulling filter frequency response and Z-plane representation
ww = -pi:pi/100:pi;
HH = freqz(bb,aa,ww);
bb = poly([exp(1j*f1) exp(-1j*f1) exp(1j*f2) exp(-1j*f2) exp(1j*f3) exp(-1j*f3)]);
aa = poly(0.80*[exp(1j*f1) exp(-1j*f1) exp(1j*f2) exp(-1j*f2) exp(1j*f3) exp(-1j*f3)]);
y = filteredAudio;
% Display the nulling filter frequency response
subplot(1,1,1);
plot(ww, abs(HH));
title(‘Notch Filter Frequency Response’);
xlabel(‘Frequency (Hz)’);
ylabel(‘Magnitude’);
fout = filter(bb,aa,y);
% Calculate the DC gain of the IIR Notch filter
ww_low = -pi:pi/10000:pi; % Frequencies close to zero
HH_low = freqz(b_total, a_total, ww_low, fs);
% Display the DC gain
dc_gain_iir = abs(HH_low(1));
disp([‘DC Gain of IIR Notch Filter: ‘, num2str(dc_gain_iir)]);
Specgrams of the input and output of the file from matlab for reference are attached.I am working on coding a matlab notch filter that is supposed to fitler out a tone in the audio file listed in the code. The Notch Filter works as it is supposed to except at the very beginning and at the very end the tone is still there and not filtered out and I need to have the tone completely gone. I am unable to figure out what to change in my code I have tried changing the frequencies I have tried changing where my filter starts and my notch width but nothing has solved my issue. Any help or suggestions is appreciated Thanks!
Code here below
% Load input sound
[Sound, fs] = audioread(‘SunshineSquare.wav’);
figure(1);
% Plot specgram of input sound
specgram(Sound, 512, fs);
title(‘Spectrogram of Input Sound (SunshineSquare.wav)’);
% Parameters of Notch Filter
frequencies = [1574.99, 3149.97, 4724.96];
notchwidth = 10;
% Tone Removal
a_total = 1;
b_total = 1;
for i = 1:length(frequencies)
Wn = [(frequencies(i) – notchwidth/2) / (fs/2), (frequencies(i) + notchwidth/2) / (fs/2)];
Wn = max(min(Wn, 0.99), 0.01);
[b, a] = butter(2, Wn, ‘stop’);
b_total = conv(b_total, b);
a_total = conv(a_total, a);
end
filteredAudio = filter(b_total, a_total, Sound);
% Play filtered sound
soundsc(filteredAudio, fs);
figure(2);
% Plot specgram of output sound after using notch filter
specgram(filteredAudio, 512, fs);
title(‘Specgram after Notch Filter’);
% Plot SunshineSquare sound
figure(3);
t = (0:length(Sound)-1)/fs;
plot(t, Sound);
xlabel(‘Time (sec)’);
ylabel(‘Amplitude’);
title(‘Notch-Filtered SunshineSquare Sound Plot’);
figure(4);
plot (ww,abs(HH));
zplane(bb,aa)
xlabel(‘Real’);
ylabel(‘Imaginary’);
title(‘Magnitude IIR Frequency Response (Z-Plane)’);
grid on
figure(5);
% Plot Notch-filtered sound
t = (0:length(Sound)-1)/fs;
plot(t, filteredAudio);
xlabel(‘Time (sec)’);
ylabel(‘Amplitude’);
title(‘Notch-Filtered Z-Plot’);
figure(6);
sgtitle(‘Redo Tone Removal Step 2: Magnitude Response for Notch Filter’);
f1 = 1574.99/fs * 2 * pi;
f2 = 3149.97/fs * 2 * pi;
f3 = 4724.96/fs * 2 * pi;
% Plot nulling filter frequency response and Z-plane representation
ww = -pi:pi/100:pi;
HH = freqz(bb,aa,ww);
bb = poly([exp(1j*f1) exp(-1j*f1) exp(1j*f2) exp(-1j*f2) exp(1j*f3) exp(-1j*f3)]);
aa = poly(0.80*[exp(1j*f1) exp(-1j*f1) exp(1j*f2) exp(-1j*f2) exp(1j*f3) exp(-1j*f3)]);
y = filteredAudio;
% Display the nulling filter frequency response
subplot(1,1,1);
plot(ww, abs(HH));
title(‘Notch Filter Frequency Response’);
xlabel(‘Frequency (Hz)’);
ylabel(‘Magnitude’);
fout = filter(bb,aa,y);
% Calculate the DC gain of the IIR Notch filter
ww_low = -pi:pi/10000:pi; % Frequencies close to zero
HH_low = freqz(b_total, a_total, ww_low, fs);
% Display the DC gain
dc_gain_iir = abs(HH_low(1));
disp([‘DC Gain of IIR Notch Filter: ‘, num2str(dc_gain_iir)]);
Specgrams of the input and output of the file from matlab for reference are attached. I am working on coding a matlab notch filter that is supposed to fitler out a tone in the audio file listed in the code. The Notch Filter works as it is supposed to except at the very beginning and at the very end the tone is still there and not filtered out and I need to have the tone completely gone. I am unable to figure out what to change in my code I have tried changing the frequencies I have tried changing where my filter starts and my notch width but nothing has solved my issue. Any help or suggestions is appreciated Thanks!
Code here below
% Load input sound
[Sound, fs] = audioread(‘SunshineSquare.wav’);
figure(1);
% Plot specgram of input sound
specgram(Sound, 512, fs);
title(‘Spectrogram of Input Sound (SunshineSquare.wav)’);
% Parameters of Notch Filter
frequencies = [1574.99, 3149.97, 4724.96];
notchwidth = 10;
% Tone Removal
a_total = 1;
b_total = 1;
for i = 1:length(frequencies)
Wn = [(frequencies(i) – notchwidth/2) / (fs/2), (frequencies(i) + notchwidth/2) / (fs/2)];
Wn = max(min(Wn, 0.99), 0.01);
[b, a] = butter(2, Wn, ‘stop’);
b_total = conv(b_total, b);
a_total = conv(a_total, a);
end
filteredAudio = filter(b_total, a_total, Sound);
% Play filtered sound
soundsc(filteredAudio, fs);
figure(2);
% Plot specgram of output sound after using notch filter
specgram(filteredAudio, 512, fs);
title(‘Specgram after Notch Filter’);
% Plot SunshineSquare sound
figure(3);
t = (0:length(Sound)-1)/fs;
plot(t, Sound);
xlabel(‘Time (sec)’);
ylabel(‘Amplitude’);
title(‘Notch-Filtered SunshineSquare Sound Plot’);
figure(4);
plot (ww,abs(HH));
zplane(bb,aa)
xlabel(‘Real’);
ylabel(‘Imaginary’);
title(‘Magnitude IIR Frequency Response (Z-Plane)’);
grid on
figure(5);
% Plot Notch-filtered sound
t = (0:length(Sound)-1)/fs;
plot(t, filteredAudio);
xlabel(‘Time (sec)’);
ylabel(‘Amplitude’);
title(‘Notch-Filtered Z-Plot’);
figure(6);
sgtitle(‘Redo Tone Removal Step 2: Magnitude Response for Notch Filter’);
f1 = 1574.99/fs * 2 * pi;
f2 = 3149.97/fs * 2 * pi;
f3 = 4724.96/fs * 2 * pi;
% Plot nulling filter frequency response and Z-plane representation
ww = -pi:pi/100:pi;
HH = freqz(bb,aa,ww);
bb = poly([exp(1j*f1) exp(-1j*f1) exp(1j*f2) exp(-1j*f2) exp(1j*f3) exp(-1j*f3)]);
aa = poly(0.80*[exp(1j*f1) exp(-1j*f1) exp(1j*f2) exp(-1j*f2) exp(1j*f3) exp(-1j*f3)]);
y = filteredAudio;
% Display the nulling filter frequency response
subplot(1,1,1);
plot(ww, abs(HH));
title(‘Notch Filter Frequency Response’);
xlabel(‘Frequency (Hz)’);
ylabel(‘Magnitude’);
fout = filter(bb,aa,y);
% Calculate the DC gain of the IIR Notch filter
ww_low = -pi:pi/10000:pi; % Frequencies close to zero
HH_low = freqz(b_total, a_total, ww_low, fs);
% Display the DC gain
dc_gain_iir = abs(HH_low(1));
disp([‘DC Gain of IIR Notch Filter: ‘, num2str(dc_gain_iir)]);
Specgrams of the input and output of the file from matlab for reference are attached. matlab MATLAB Answers — New Questions