Clear Filters
Clear Filters

trouble making a firfilter to remove a tone out of a wavefile

2 views (last 30 days)
I am trying to write a matlab code to design a firfilter to filter a tone out of a wav.file but I am having trouble getting the filter to work this code below is supposed to determine the frequencies of the audio in the wav.file and filter the tone out of the sunshinesquare.wav file. however when I go to run the program matlab tells me there is an error with the notchfilter line but I can't figure out what is wrong with it any suggestions/help is greatly appreciated.
My code is below
[x, fs] = audioread('SunshineSquare.wav');
X = fft(x);
f = linspace(0, fs/2, length(X)/2);
[~, f0] = max(abs(X(1:length(X)/2)));
notchFilter = fdesign.notch('N,Fc', 100, f0, fs);
b = firpm(notchFilter);
y = filter(b, 1, x);
sound(x, fs);
  3 Comments
Walter Roberson
Walter Roberson on 29 Nov 2023
Error using fdesign.abstractpeaknotch/set.Specification
Expected Specification to match one of these values:
'N,F0,Q', 'N,F0,Q,Ap', 'N,F0,Q,Ast', 'N,F0,Q,Ap,Ast', 'N,F0,BW', 'N,F0,BW,Ap', 'N,F0,BW,Ast', 'N,F0,BW,Ap,Ast'
The input, 'N,Fc', did not match any of the valid values.

Sign in to comment.

Accepted Answer

Walter Roberson
Walter Roberson on 29 Nov 2023
Edited: Walter Roberson on 29 Nov 2023
The error is correct, in that the documentation lists specific filter specifications, none of which include Fc
I do not promise that I calculated f0 correctly.
[x, fs] = audioread('SunshineSquare.wav');
X = fft(x);
L = floor(length(X)/2);
f = linspace(0, fs/2, L);
[~, f0idx] = max(abs(X(1:L)));
f0 = (f0idx-1)/L;
Q = 80;
notchFilter = fdesign.notch('N,F0,Q', 100, f0, Q);
Hd = design(notchFilter, 'SystemObject', true);
y = Hd(x);
sound(y, fs)
  18 Comments
Walter Roberson
Walter Roberson on 30 Nov 2023
The audiofile has a primary frequency and two further peaks at 2 and 3 times the primary. Removing those three leaves only low level information.
X = fft(x);
L = floor(length(X)/2);
f = linspace(0, fs/2, L);
[~, f0idx] = max(abs(X(1:L)));
f0 = (f0idx-1)/L;
Q = 2.5;
notchFilter1 = fdesign.notch('N,F0,Q', 6, f0, Q);
notchFilter2 = fdesign.notch('N,F0,Q', 6, f0*2, Q);
notchFilter3 = fdesign.notch('N,F0,Q', 6, f0*3, Q);
Hd1 = design(notchFilter1, 'SystemObject', true);
Hd2 = design(notchFilter2, 'SystemObject', true);
Hd3 = design(notchFilter3, 'SystemObject', true);
y = Hd3(Hd2(Hd1(x)));
tiledlayout('flow');
nexttile
plot(f, abs(X(1:L))); title('original spectrum');
nexttile
Y = fft(y);
plot(f, abs(Y1(1:L))); title('filtered spectrum');
Jacob
Jacob on 30 Nov 2023
Awesome Thank you! It works and I don't hear the tone anymore which is what I needed! One more thing the end of the file is still a little noisy but I can hear the file is there a way to clean the file up or is it what it is? I know there is only so much that can be done cleaning up an audio file so I am happy with what I have if not.

Sign in to comment.

More Answers (0)

Tags

Products


Release

R2022b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!