Filtering out a Specific Sound from a Audio File

440 views (last 30 days)
I would appreciate any help with my program. The problem and what I attempted is below.
problem: You have been provided with a sound file, buzzjc.wav, which is an audio clip of someone saying something, but it has been corrupted by adding a buzzer signal consisting of a few tones. First load the signal into MATLAB using the command wavread (using older MATLAB versions) and audioread (using newer versions) which you can read more about in the MATLAB help file. Example of loading audio file and playing audio in MATLAB (2017a):
% Read the audio file and store the audio in data and sampling rate into fs [data, fs] = audioread('test3_Q3.wav');
% Play audio file
player = audioplayer(data,fs);
a.) Use the DFT to figure out the tone frequencies (at least approximately). List the tone frequencies.
b.) Describe how you would remove these unwanted tones in the audio signal using topics covered during this course.
c.) Next, remove these tones and then listen to the clip (note: if you are using an older version of MATLAB, you may have to use the command wavplay to play your audio clip. What the person in the clip is saying?
My solution:
[data, fs]=audioread('buzzjc.wav');
player = audioplayer(data,fs);
dt = 1/fs; % Sampling time [s]
t = 0:dt:1; % Time
N = length(data);
f = (1:N/2+1)*fs/N; % Frequency
clip = 205; % Clip size (number of points)
X = zeros(clip, length(data)-clip); % Clips matrix
Y = zeros(length(f), length(data)-clip); % Spectrogram
for i = 1:length(data)-clip
X(:,i) = data(i:i+clip-1);
tmpS = fft(X(:,i), N);
Y(:,i) = abs(tmpS(1:N/2+1));
imagesc(t, f, (Y))
title('Spectrogram of Hidden words');
xlabel('Time [s]')
ylabel('Frequency [Hz]')
beginFreq = 800 / (fs/2);
endFreq = 900 / (fs/2);
[b,a] = butter(6, (beginFreq/endFreq/2));
fOut = filtfilt(b, a, f);
p = audioplayer(fOut, fs);;
I got the frequencies that needed to be removed were. 975Hz, 875Hz, 675Hz and 450Hz.
When ran it had the error message Attempt to execute SCRIPT butter as a function: /Users/Leah/Documents/MATLAB/butter.m
Error in Test3Bonus (line 32) [b,a] = butter(6, (beginFreq/endFreq/2))
It wouldn't allow me to attach the file. Any help would be greatly appreciated.
  1 Comment
Allen Bastian
Allen Bastian on 14 May 2020
Edited: Adam Danz on 15 May 2020
how can we remove instrumenatl music to give just the audio file

Sign in to comment.

Answers (6)

Greg Dionne
Greg Dionne on 25 Apr 2017
Use periodogram to figure out the frequencies. Then:
load buzzjc
n = numel(f);
fold = f;
for fi=[220 440 660 880]
a = sum(f .* cos(2*pi*fi/fs*(1:n)'))/n;
b = sum(f .* sin(2*pi*fi/fs*(1:n)'))/n;
f = f - a.*cos(2*pi*fi/fs*(1:n)')*2;
f = f - b.*sin(2*pi*fi/fs*(1:n)')*2;
plot([fold f])

Image Analyst
Image Analyst on 24 Apr 2017
You called your script butter.m (/Users/Leah/Documents/MATLAB/butter.m). Call it something else because butter() is a built in function and it's getting confused as to which butter to use. Call it exercise6.m or something descriptive. But not butter.m
Star Strider
Star Strider on 24 Apr 2017
Try this:
save('buzzjc.mat', 'f', 'fs')
Then upload it.

Sign in to comment.

Leah Chamberlain
Leah Chamberlain on 24 Apr 2017
Edited: Leah Chamberlain on 24 Apr 2017
Okay I'll try it again, I think it worked. Sorry I didn't mean to put it as a new answer.

Star Strider
Star Strider on 24 Apr 2017
Edited: Star Strider on 25 Apr 2017
I may be missing something, but I can’t hear (or see) anything other than the four frequencies in that signal. I designed a multiple-notch FIR filter, then because of its length, used fftfilt to filter it. There was nothing left that I can hear as intelligible words.
The Code
d = load('buzzjc.mat');
signal = d.f;
Fs = d.fs;
% player = audioplayer(f,fs);
N = size(signal,1); % Determine total number of samples in audio file
Fn = Fs/2;
FTsignal = fft(signal)/N;
Fv = linspace(0, 1, fix(N/2)+1)*Fn; % Frequency Vector
Iv = 1:length(Fv); % Index Vector
[pks,frqs] = findpeaks(abs(FTsignal(Iv))*2, Fv, 'MinPeakHeight',0.1);
plot(Fv, abs(FTsignal(Iv))*2)
hold on
plot(frqs, pks, '+r')
hold off
notch_frq = sort([frqs-2.5 frqs+2.5 frqs-1.5 frqs+1.5]);;
mags = [1 0 1 0 1 0 1 0 1];
devs = [0.05 0.01 0.05 0.01 0.05 0.01 0.05 0.01 0.05];
[n,Wn,beta,ftype] = kaiserord(notch_frq,mags,devs,Fs);
n = n + rem(n,2);
hh = fir1(n,Wn,ftype,kaiser(n+1,beta),'scale');
freqz(hh, 1, 2^16, Fs)
fOut = fftfilt(hh, signal);
FTOut = fft(fOut)/N;
plot(Fv, abs(FTOut(Iv))*2)
plot(Fv, abs(FTOut(Iv)./FTsignal(Iv))*2)
p1 = audioplayer(fOut*50, Fs);
I leave you to experiment with this to see if you can get something out of it. The filter seems to have worked successfully, although I can’t figure out the reason the output is unintelligible. You may want to create separate Chebyshev Type II filters for each identified frequency (the ‘frqs’ output of the findpeaks call), then cascade them in series, and see if you can get a better result. You can use the filtfilt function with shorter filters. My filter here was longer than your signal, so I went with an alternative filtering method. You can get very narrow transition regions (steep rolloffs) with Chebyshev filters, so transition regions on the order of 1-2 Hz are possible.

Jim Thompson
Jim Thompson on 19 Jun 2019
I have just registered, so I'm a brand new member of this forum.
Please note: I have never coded anything in my life, just cut and pasted published arduino sketches - with success I should add.
I have an interest in this due to several friends suffereing from tinnitus and I would like to create a notched music file, using the frequency or frequencies specific to each individual sufferer.
  1. What is the supportive (underlying) program or application for the above files?
  2. What prerequisites would be needed to use them? (what coding skills etc.)
  3. Any tips for the beginner into this subject will be most appreciated.
Thanks in advance,
Jim Thompson
Jim Thompson
Jim Thompson on 19 Jun 2019
Image Analyst.
Thanks for the reply with the question.
There is no short answer to your question, so I will have to defer any attempts to answer it until later on. I fully intend to do so, with some explanatory links etc.
More later when time permits........

Sign in to comment.

Kavitha Kavi
Kavitha Kavi on 31 Dec 2020
How to test the FIR filter with an audio file and also plot the spectrum of audio signal before and after filtering?


Find more on Audio I/O and Waveform Generation in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!