{"id":238,"date":"2010-06-30T17:35:44","date_gmt":"2010-06-30T17:35:44","guid":{"rendered":"https:\/\/blogs.mathworks.com\/loren\/2010\/06\/30\/vuvuzela-denoising-with-parametric-equalizers\/"},"modified":"2017-07-26T11:04:39","modified_gmt":"2017-07-26T16:04:39","slug":"vuvuzela-denoising-with-parametric-equalizers","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/loren\/2010\/06\/30\/vuvuzela-denoising-with-parametric-equalizers\/","title":{"rendered":"Vuvuzela Denoising with Parametric Equalizers"},"content":{"rendered":"<div xmlns:mwsh=\"https:\/\/www.mathworks.com\/namespace\/mcode\/v1\/syntaxhighlight.dtd\" class=\"content\">\r\n   <introduction>\r\n      <p>Today I&#8217;d like to introduce a guest blogger, Charu, who is a Technical Marketing Manager at The MathWorks.  Charu regularly\r\n         presents webinars on signal processing topics, and most recently presented a webinar highlighting R2010a capabilities in the area of MATLAB signal processing.\r\n      <\/p>\r\n   <\/introduction>\r\n   <h3>Contents<\/h3>\r\n   <div>\r\n      <ul>\r\n         <li><a href=\"#1\">Introduction<\/a><\/li>\r\n         <li><a href=\"#2\">Set up input and output streams<\/a><\/li>\r\n         <li><a href=\"#8\">Extracting the noise spectrum<\/a><\/li>\r\n         <li><a href=\"#11\">Identifying vuvuzela frequencies<\/a><\/li>\r\n         <li><a href=\"#13\">Designing the parametric equalizers<\/a><\/li>\r\n         <li><a href=\"#16\">Visualizing magnitude response of the filters<\/a><\/li>\r\n         <li><a href=\"#18\">Implementing the parametric equalizers<\/a><\/li>\r\n         <li><a href=\"#19\">Filtering the input in a stream processing loop<\/a><\/li>\r\n         <li><a href=\"#20\">Visualizing and listening to output results<\/a><\/li>\r\n         <li><a href=\"#23\">Cleanup<\/a><\/li>\r\n         <li><a href=\"#24\">Things to Try<\/a><\/li>\r\n      <\/ul>\r\n   <\/div>\r\n   <h3>Introduction<a name=\"1\"><\/a><\/h3>\r\n   <p>Hi there - soccer fever is in full swing, and the incessant sounds of the vuvuzela are a big part of the soccer viewing experience.\r\n      This South African instrument has become so popular that it now has a dedicated Facebook page!  Well, 2 weeks ago, Loren featured\r\n      a <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/27912-vuvuzela-sound-denoising-algorithm\">very interesting MATLAB Central submission<\/a> by Vincent Choqueuse on vuvuzela denoising using the spectral subtraction method. This cool demo inspired me to try out other\r\n      filtering techniques for the vuvuzela denoising problem.\r\n   <\/p>\r\n   <p>A quick search on &#8220;vuvuzela filtering&#8221; suggested that notch filters and adaptive filters were the most common methods, so\r\n      I decided to try a method that uses notch parametric equalizers. Parametric equalizers allow the user to control the bandwidth,\r\n      gain and center frequency of the filter.  Since the game commentary is a streaming broadcast, I also wanted to see if I could\r\n      apply stream processing techniques using new System objects in MATLAB.<\/p>\r\n   <p>This example demonstrates how you can design parametric equalizer filters in MATLAB and use them to filter out vuvuzela sounds\r\n      from an audio stream. This demo uses Signal Processing Toolbox, Filter Design Toolbox and Signal Processing Blockset.\r\n   <\/p>\r\n   <h3>Set up input and output streams<a name=\"2\"><\/a><\/h3>\r\n   <p>First, let us look at how we can create streaming inputs and outputs in MATLAB.  To get the data, download 'vuvuzela.wav'\r\n      from <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/27912-vuvuzela-sound-denoising-algorithm\">this MATLAB Central entry<\/a>.\r\n   <\/p>\r\n   <p>We need an input audio stream that can read data from a WAV file. To do this, we can use the MultimediaFileReader System object.<\/p>\r\n   <p>Create a System object to read an audio stream from the WAV file<\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">hSound = signalblks.MultimediaFileReader(<span style=\"color: #0000FF\">...<\/span>\r\n    <span style=\"color: #A020F0\">'Vuvuzela.wav'<\/span>,<span style=\"color: #0000FF\">...<\/span>\r\n    <span style=\"color: #A020F0\">'SamplesPerAudioFrame'<\/span>, 1024, <span style=\"color: #0000FF\">...<\/span>\r\n    <span style=\"color: #A020F0\">'AudioOutputDataType'<\/span>, <span style=\"color: #A020F0\">'double'<\/span>);<\/pre><p>Get the input sampling frequency<\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">hInfo = info(hSound);\r\nFs = hInfo.AudioSampleRate;<\/pre><p>Create a System object to play back audio to the sound card<\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">hPlayer = signalblks.AudioPlayer(<span style=\"color: #A020F0\">'SampleRate'<\/span>, Fs);<\/pre><p>Create a System object to write the demo output to a WAV file.<\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">hOutputFile = signalblks.MultimediaFileWriter(<span style=\"color: #0000FF\">...<\/span>\r\n    <span style=\"color: #A020F0\">'with_then_without_vuvuzela.wav'<\/span>,<span style=\"color: #0000FF\">...<\/span>\r\n    <span style=\"color: #A020F0\">'SampleRate'<\/span>, Fs);<\/pre><p>Create System objects for logging pure noise and filtered output signals.<\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">hLogNoise = signalblks.SignalLogger;\r\nhLogOutput = signalblks.SignalLogger;<\/pre><h3>Extracting the noise spectrum<a name=\"8\"><\/a><\/h3>\r\n   <p>The first 5 seconds of the input do not contain any significant data, ie, no noise even, so let&#8217;s advance the input by 5 seconds,\r\n      i.e., about 100 frames (@Fs = 22050Hz, 1024 samples per frame).\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\"><span style=\"color: #0000FF\">for<\/span> frame_index=1:100\r\n    step(hSound);\r\n<span style=\"color: #0000FF\">end<\/span><\/pre><p>To obtain our &#8220;pure noise&#8221; signal, we use a 1-second portion of the signal that has only the vuvuzela sound, but no speech.<\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\"><span style=\"color: #0000FF\">for<\/span> noise_frames = 1:20\r\n    noise = step(hSound);\r\n    step(hLogNoise, noise);\r\n<span style=\"color: #0000FF\">end<\/span><\/pre><p>Plot the noise spectrum.<\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">pwelch(hLogNoise.Buffer, [], [], 8192, Fs);<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/loren\/238\/vuvuzela_denoising_parameq_blog_01.png\"> <h3>Identifying vuvuzela frequencies<a name=\"11\"><\/a><\/h3>\r\n   <p>Let&#8217;s zoom into the plot to identify noise peaks.<\/p>\r\n   <p><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/loren\/238\/noise_peaks.png\"> <\/p>\r\n   <p>The fundamental and the first 4 harmonic frequencies of the vuvuzela are at: 235Hz, 476Hz, 735Hz, 940Hz, 1180Hz.<\/p>\r\n   <h3>Designing the parametric equalizers<a name=\"13\"><\/a><\/h3>\r\n   <p>Our next task is to design notch parametric equalizers that will selectively filter out these frequencies.  Since the notch\r\n      filters may also remove components of the speech signal at the above frequencies, we will apply a peak filter to boost the\r\n      speech signal at the output of the notch filters.\r\n   <\/p>\r\n   <p>To design parametric equalizers, we need to specify filter order, center frequency, and the bandwidth or Q-factor of the filter.\r\n       To determine the bandwidth or Q-factor of the filter, let&#8217;s take a closer look at the noise spectrum. At the fundamental\r\n      and 1st harmonic frequeuncies (235Hz, 465Hz), we see that the peaks are sharp. Beyond the 1st harmonic, the noise peaks are\r\n      spread across a wider band.  So, for the notch filters at the lower frequencies, we specify a narrow stopband, i.e., a high\r\n      Q, and for higher harmonics, we specify a wider stopband, i.e., a low Q.  After some trial and error, I arrived at the following\r\n      parameters:\r\n   <\/p>\r\n   <p>Here are the specifications for notch parametric equalizers.<\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">F01 = 235; F02 = 476; F03 = 735; F04 = 940; F05 = 1180;\r\n\r\nN = 2; <span style=\"color: #228B22\">%Filter order<\/span>\r\nGref = 0; <span style=\"color: #228B22\">%Reference gain<\/span>\r\nG0 = -20; <span style=\"color: #228B22\">%Attenuate by 20dB<\/span>\r\nQa = 10;  <span style=\"color: #228B22\">%Higher Q-factor for fundamental and 1st harmonic<\/span>\r\nQb = 5;   <span style=\"color: #228B22\">%Lower Q-factor for higher harmonics<\/span>\r\n\r\nf1 = fdesign.parameq(<span style=\"color: #A020F0\">'N,F0,Qa,Gref,G0'<\/span>,N,F01,Qa,Gref,G0,Fs);\r\nf2 = fdesign.parameq(<span style=\"color: #A020F0\">'N,F0,Qa,Gref,G0'<\/span>,N,F02,Qa,Gref,G0,Fs);\r\nf3 = fdesign.parameq(<span style=\"color: #A020F0\">'N,F0,Qa,Gref,G0'<\/span>,N,F03,Qb,Gref,G0,Fs);\r\nf4 = fdesign.parameq(<span style=\"color: #A020F0\">'N,F0,Qa,Gref,G0'<\/span>,N,F04,Qb,Gref,G0,Fs);\r\nf5 = fdesign.parameq(<span style=\"color: #A020F0\">'N,F0,Qa,Gref,G0'<\/span>,N,F05,Qb,Gref,G0,Fs);<\/pre><p>Here are specifications for peak parametric equalizer.<\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">F06 = 480;  <span style=\"color: #228B22\">%Center frequency to boost<\/span>\r\n\r\nN_peak = 8; <span style=\"color: #228B22\">%Use higher filter order for peak filter<\/span>\r\nQc = 0.5;   <span style=\"color: #228B22\">%Use very low Q-factor for boost equalizer filter<\/span>\r\nG1 = 9;     <span style=\"color: #228B22\">%Boost gain by 9dB<\/span>\r\n\r\nf6 = fdesign.parameq(<span style=\"color: #A020F0\">'N,F0,Qa,Gref,G0'<\/span>,N_peak,F06,Qc,Gref,G1,Fs);<\/pre><h3>Visualizing magnitude response of the filters<a name=\"16\"><\/a><\/h3><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">Hp1 = design(f1, <span style=\"color: #A020F0\">'butter'<\/span>);\r\nHp2 = design(f2, <span style=\"color: #A020F0\">'butter'<\/span>);\r\nHp3 = design(f3, <span style=\"color: #A020F0\">'butter'<\/span>);\r\nHp4 = design(f4, <span style=\"color: #A020F0\">'butter'<\/span>);\r\nHp5 = design(f5, <span style=\"color: #A020F0\">'butter'<\/span>);\r\nHp6 = design(f6, <span style=\"color: #A020F0\">'butter'<\/span>);<\/pre><pre>  hFV = fvtool([Hp1 Hp2 Hp3 Hp4 Hp5 Hp6]);\r\n  legend(hFV,'NotchEQ #1', 'NotchEQ #2', 'NotchEQ #3',...\r\n        'NotchEQ #4','NotchEQ #5','Peak EQ');<\/pre><p><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/loren\/238\/filters.png\"> <\/p>\r\n   <h3>Implementing the parametric equalizers<a name=\"18\"><\/a><\/h3>\r\n   <p>To implement each filter, we use a System object that realizes an SOS (second-order-sections) structure.  Using a System object\r\n      to implement the filter enables us to apply the filter in a stream processing loop, without having to worry about updating\r\n      filter states between each iteration.\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">hEQ1 = signalblks.BiquadFilter(<span style=\"color: #A020F0\">'SOSMatrix'<\/span>,Hp1.sosMatrix,<span style=\"color: #A020F0\">'ScaleValues'<\/span>,Hp1.ScaleValues);\r\nhEQ2 = signalblks.BiquadFilter(<span style=\"color: #A020F0\">'SOSMatrix'<\/span>,Hp2.sosMatrix,<span style=\"color: #A020F0\">'ScaleValues'<\/span>,Hp2.ScaleValues);\r\nhEQ3 = signalblks.BiquadFilter(<span style=\"color: #A020F0\">'SOSMatrix'<\/span>,Hp3.sosMatrix,<span style=\"color: #A020F0\">'ScaleValues'<\/span>,Hp3.ScaleValues);\r\nhEQ4 = signalblks.BiquadFilter(<span style=\"color: #A020F0\">'SOSMatrix'<\/span>,Hp4.sosMatrix,<span style=\"color: #A020F0\">'ScaleValues'<\/span>,Hp4.ScaleValues);\r\nhEQ5 = signalblks.BiquadFilter(<span style=\"color: #A020F0\">'SOSMatrix'<\/span>,Hp5.sosMatrix,<span style=\"color: #A020F0\">'ScaleValues'<\/span>,Hp5.ScaleValues);\r\nhEQ6 = signalblks.BiquadFilter(<span style=\"color: #A020F0\">'SOSMatrix'<\/span>,Hp6.sosMatrix,<span style=\"color: #A020F0\">'ScaleValues'<\/span>,Hp6.ScaleValues);<\/pre><h3>Filtering the input in a stream processing loop<a name=\"19\"><\/a><\/h3>\r\n   <p>To filter out the vuvuzela noise, we use a stream processing loop that reads in one frame of input data from the WAV file,\r\n      applies the series of 6 parametric equalizers, and then plays back the filtered output through the sound card.\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\"><span style=\"color: #0000FF\">for<\/span> frames = 1:400\r\n    <span style=\"color: #228B22\">%Step through input WAV file one frame at a time<\/span>\r\n    input = step(hSound);\r\n\r\n    <span style=\"color: #228B22\">%Apply notch EQ filters first, then peak EQ filter<\/span>\r\n    out1 = step(hEQ1, input);\r\n    out2 = step(hEQ2, out1);\r\n    out3 = step(hEQ3, out2);\r\n    out4 = step(hEQ4, out3);\r\n    out5 = step(hEQ5, out4);\r\n    filt_output = step(hEQ6, out5);\r\n\r\n    <span style=\"color: #228B22\">%Play 200 frames of input signal, then 200 frames of filtered output<\/span>\r\n    <span style=\"color: #228B22\">%to compare original and filtered signals<\/span>\r\n    <span style=\"color: #0000FF\">if<\/span> frames &lt; 200\r\n      step(hPlayer, input);\r\n    <span style=\"color: #0000FF\">else<\/span>\r\n       step(hPlayer, filt_output);\r\n    <span style=\"color: #0000FF\">end<\/span>\r\n\r\n   <span style=\"color: #228B22\">%Log filtered output to buffer, WAV file<\/span>\r\n   step(hLogOutput, filt_output);\r\n   step(hOutputFile, filt_output);\r\n\r\n<span style=\"color: #0000FF\">end<\/span><\/pre><h3>Visualizing and listening to output results<a name=\"20\"><\/a><\/h3>\r\n   <p>Listen to the noisy and filtered signals here.<\/p>\r\n\r\n<br>\r\n<div style=\"width: 320px;\" class=\"wp-video\"><!--[if lt IE 9]><script>document.createElement('video');<\/script><![endif]-->\n<video class=\"wp-video-shortcode\" id=\"video-238-1\" width=\"320\" height=\"240\" preload=\"metadata\" controls=\"controls\"><source type=\"video\/mp4\" src=\"https:\/\/blogs.mathworks.com\/loren\/files\/224_Denoise.mp4?_=1\" \/><a href=\"https:\/\/blogs.mathworks.com\/loren\/files\/224_Denoise.mp4\">https:\/\/blogs.mathworks.com\/loren\/files\/224_Denoise.mp4<\/a><\/video><\/div>\r\n<br>\r\n   <p>Visualize the spectrum of the filtered signal.  If you zoom into the plot, you can see that the vuvuzela frequencies have\r\n      been attenuated by our notch parametric equalizers.\r\n   <\/p><pre>    pwelch(hLogOutput.Buffer, [], [], 8192, Fs);<\/pre><p><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/loren\/238\/vuvuzela_denoising_parameq_blog_02.png\"> <\/p>\r\n   <h3>Cleanup<a name=\"23\"><\/a><\/h3>\r\n   <p>Close input and output System objects to ensure that we leave all objects in their proper states.<\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">close(hSound);\r\nclose(hPlayer);\r\nclose(hOutputFile);<\/pre><h3>Things to Try<a name=\"24\"><\/a><\/h3>\r\n   <p>This demo was a lot of fun to work on and I hope you&#8217;ll find it interesting.  Feel free to download the code and try your\r\n      own equalizer parameters.  Have fun!\r\n   <\/p><script language=\"JavaScript\">\r\n<!--\r\n\r\n    function grabCode_0311dcb076be47759dd905e8d49b009b() {\r\n        \/\/ Remember the title so we can use it in the new page\r\n        title = document.title;\r\n\r\n        \/\/ Break up these strings so that their presence\r\n        \/\/ in the Javascript doesn't mess up the search for\r\n        \/\/ the MATLAB code.\r\n        t1='0311dcb076be47759dd905e8d49b009b ' + '##### ' + 'SOURCE BEGIN' + ' #####';\r\n        t2='##### ' + 'SOURCE END' + ' #####' + ' 0311dcb076be47759dd905e8d49b009b';\r\n    \r\n        b=document.getElementsByTagName('body')[0];\r\n        i1=b.innerHTML.indexOf(t1)+t1.length;\r\n        i2=b.innerHTML.indexOf(t2);\r\n \r\n        code_string = b.innerHTML.substring(i1, i2);\r\n        code_string = code_string.replace(\/REPLACE_WITH_DASH_DASH\/g,'--');\r\n\r\n        \/\/ Use \/x3C\/g instead of the less-than character to avoid errors \r\n        \/\/ in the XML parser.\r\n        \/\/ Use '\\x26#60;' instead of '<' so that the XML parser\r\n        \/\/ doesn't go ahead and substitute the less-than character. \r\n        code_string = code_string.replace(\/\\x3C\/g, '\\x26#60;');\r\n\r\n        author = 'Loren Shure';\r\n        copyright = 'Copyright 2010 The MathWorks, Inc.';\r\n\r\n        w = window.open();\r\n        d = w.document;\r\n        d.write('<pre>\\n');\r\n        d.write(code_string);\r\n\r\n        \/\/ Add author and copyright lines at the bottom if specified.\r\n        if ((author.length > 0) || (copyright.length > 0)) {\r\n            d.writeln('');\r\n            d.writeln('%%');\r\n            if (author.length > 0) {\r\n                d.writeln('% _' + author + '_');\r\n            }\r\n            if (copyright.length > 0) {\r\n                d.writeln('% _' + copyright + '_');\r\n            }\r\n        }\r\n\r\n        d.write('<\/pre>\\n');\r\n      \r\n      d.title = title + ' (MATLAB code)';\r\n      d.close();\r\n      }   \r\n      \r\n-->\r\n<\/script><p style=\"text-align: right; font-size: xx-small; font-weight:lighter;   font-style: italic; color: gray\"><br><a href=\"javascript:grabCode_0311dcb076be47759dd905e8d49b009b()\"><span style=\"font-size: x-small;        font-style: italic;\">Get \r\n            the MATLAB code \r\n            <noscript>(requires JavaScript)<\/noscript><\/span><\/a><br><br>\r\n      Published with MATLAB&reg; 7.10<br><\/p>\r\n<\/div>\r\n<!--\r\n0311dcb076be47759dd905e8d49b009b ##### SOURCE BEGIN #####\r\n%% Vuvuzela Denoising with Parametric Equalizers\r\n% Today I\u00e2\u20ac\u2122d like to introduce a guest blogger, Charu, who is a Technical\r\n% Marketing Manager at The MathWorks.  Charu regularly presents webinars on\r\n% signal processing topics, and \r\n% <https:\/\/www.mathworks.com\/company\/events\/webinars\/wbnr43430.html most recently \r\n% presented a webinar> highlighting R2010a capabilities in the area of\r\n% MATLAB signal processing. \r\n\r\n%% Introduction\r\n% Hi there - soccer fever is in full swing, and the incessant sounds of the\r\n% vuvuzela are a big part of the soccer viewing experience. This South\r\n% African instrument has become so popular that it now has a dedicated\r\n% Facebook page!  Well, 2 weeks ago, Loren featured a\r\n% <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/27912-vuvuzela-sound-denoising-algorithm\r\n% very interesting MATLAB Central submission> by Vincent\r\n% Choqueuse on vuvuzela denoising using the spectral subtraction method.\r\n% This cool demo inspired me to try out other filtering techniques for the\r\n% vuvuzela denoising problem.\r\n%\r\n% A quick search on \u00e2\u20ac\u0153vuvuzela filtering\u00e2\u20ac\ufffd suggested that notch filters and\r\n% adaptive filters were the most common methods, so I decided to try a\r\n% method that uses notch parametric equalizers. Parametric equalizers allow\r\n% the user to control the bandwidth, gain and center frequency of the\r\n% filter.  Since the game commentary is a streaming broadcast, I also\r\n% wanted to see if I could apply stream processing techniques using new\r\n% <https:\/\/www.mathworks.com\/programs\/system-objects.html System objects\r\n% in MATLAB.>\r\n%\r\n% This example demonstrates how you can design parametric equalizer filters\r\n% in MATLAB and use them to filter out vuvuzela sounds from an audio\r\n% stream. This demo uses Signal Processing Toolbox, Filter Design Toolbox\r\n% and Signal Processing Blockset.\r\n\r\n%% Set up input and output streams\r\n% First, let us look at how we can create streaming inputs and outputs in\r\n% MATLAB.  To get the data, download 'vuvuzela.wav' from\r\n% <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/27912-vuvuzela-sound-denoising-algorithm\r\n% this MATLAB Central entry>.\r\n%\r\n% We need an input audio stream that can read data from a WAV file. To do\r\n% this, we can use the MultimediaFileReader System object.\r\n%%\r\n% Create a System object to read an audio stream from the WAV file\r\nhSound = signalblks.MultimediaFileReader(...\r\n    'Vuvuzela.wav',...\r\n    'SamplesPerAudioFrame', 1024, ...\r\n    'AudioOutputDataType', 'double');\r\n%%                                   \r\n% Get the input sampling frequency\r\nhInfo = info(hSound);\r\nFs = hInfo.AudioSampleRate;\r\n%%\r\n% Create a System object to play back audio to the sound card\r\nhPlayer = signalblks.AudioPlayer('SampleRate', Fs);\r\n%%\r\n% Create a System object to write the demo output to a WAV file.\r\nhOutputFile = signalblks.MultimediaFileWriter(...\r\n    'with_then_without_vuvuzela.wav',...\r\n    'SampleRate', Fs);\r\n%% \r\n% Create System objects for logging pure noise and filtered output signals.\r\nhLogNoise = signalblks.SignalLogger;\r\nhLogOutput = signalblks.SignalLogger;\r\n\r\n%% Extracting the noise spectrum\r\n% The first 5 seconds of the input do not contain any significant data, ie,\r\n% no noise even, so let\u00e2\u20ac\u2122s advance the input by 5 seconds, i.e., about 100\r\n% frames (@Fs = 22050Hz, 1024 samples per frame).\r\nfor frame_index=1:100\r\n    step(hSound);\r\nend\r\n\r\n%%\r\n% To obtain our \u00e2\u20ac\u0153pure noise\u00e2\u20ac\ufffd signal, we use a 1-second portion of the signal\r\n% that has only the vuvuzela sound, but no speech.\r\nfor noise_frames = 1:20\r\n    noise = step(hSound);\r\n    step(hLogNoise, noise);\r\nend\r\n\r\n%%\r\n% Plot the noise spectrum.\r\npwelch(hLogNoise.Buffer, [], [], 8192, Fs);\r\n%% Identifying vuvuzela frequencies\r\n% Let\u00e2\u20ac\u2122s zoom into the plot to identify noise peaks.\r\n%\r\n%\r\n% \r\n% <<noise_peaks.png>>\r\n% \r\n%\r\n%%\r\n% The fundamental and the first 4 harmonic frequencies of the vuvuzela are\r\n% at: 235Hz, 476Hz, 735Hz, 940Hz, 1180Hz.\r\n\r\n%% Designing the parametric equalizers\r\n% Our next task is to design notch parametric equalizers that will\r\n% selectively filter out these frequencies.  Since the notch filters may\r\n% also remove components of the speech signal at the above frequencies, we\r\n% will apply a peak filter to boost the speech signal at the output of the\r\n% notch filters.\r\n%\r\n% To design parametric equalizers, we need to specify filter order, center\r\n% frequency, and the bandwidth or Q-factor of the filter.  To determine the\r\n% bandwidth or Q-factor of the filter, let\u00e2\u20ac\u2122s take a closer look at the\r\n% noise spectrum. At the fundamental and 1st harmonic frequeuncies (235Hz,\r\n% 465Hz), we see that the peaks are sharp. Beyond the 1st harmonic, the\r\n% noise peaks are spread across a wider band.  So, for the notch filters at\r\n% the lower frequencies, we specify a narrow stopband, i.e., a high Q, and\r\n% for higher harmonics, we specify a wider stopband, i.e., a low Q.  After\r\n% some trial and error, I arrived at the following parameters:\r\n%%\r\n% Here are the specifications for notch parametric equalizers.\r\nF01 = 235; F02 = 476; F03 = 735; F04 = 940; F05 = 1180;\r\n \r\nN = 2; %Filter order\r\nGref = 0; %Reference gain\r\nG0 = -20; %Attenuate by 20dB\r\nQa = 10;  %Higher Q-factor for fundamental and 1st harmonic\r\nQb = 5;   %Lower Q-factor for higher harmonics\r\n \r\nf1 = fdesign.parameq('N,F0,Qa,Gref,G0',N,F01,Qa,Gref,G0,Fs);\r\nf2 = fdesign.parameq('N,F0,Qa,Gref,G0',N,F02,Qa,Gref,G0,Fs);\r\nf3 = fdesign.parameq('N,F0,Qa,Gref,G0',N,F03,Qb,Gref,G0,Fs);\r\nf4 = fdesign.parameq('N,F0,Qa,Gref,G0',N,F04,Qb,Gref,G0,Fs);\r\nf5 = fdesign.parameq('N,F0,Qa,Gref,G0',N,F05,Qb,Gref,G0,Fs);\r\n%%\r\n% Here are specifications for peak parametric equalizer.\r\nF06 = 480;  %Center frequency to boost\r\n\r\nN_peak = 8; %Use higher filter order for peak filter\r\nQc = 0.5;   %Use very low Q-factor for boost equalizer filter\r\nG1 = 9;     %Boost gain by 9dB\r\n \r\nf6 = fdesign.parameq('N,F0,Qa,Gref,G0',N_peak,F06,Qc,Gref,G1,Fs);\r\n\r\n%% Visualizing magnitude response of the filters\r\n\r\nHp1 = design(f1, 'butter');\r\nHp2 = design(f2, 'butter');\r\nHp3 = design(f3, 'butter');\r\nHp4 = design(f4, 'butter');\r\nHp5 = design(f5, 'butter');\r\nHp6 = design(f6, 'butter');\r\n\r\n\r\n%%\r\n%    hFV = fvtool([Hp1 Hp2 Hp3 Hp4 Hp5 Hp6]);\r\n%    legend(hFV,'NotchEQ #1', 'NotchEQ #2', 'NotchEQ #3',...\r\n%          'NotchEQ #4','NotchEQ #5','Peak EQ');\r\n%\r\n%\r\n% <<filters.png>>\r\n% \r\n\r\n \r\n%% Implementing the parametric equalizers\r\n% To implement each filter, we use a System object that realizes an SOS\r\n% (second-order-sections) structure.  Using a System object to implement\r\n% the filter enables us to apply the filter in a stream processing loop,\r\n% without having to worry about updating filter states between each\r\n% iteration.\r\n\r\nhEQ1 = signalblks.BiquadFilter('SOSMatrix',Hp1.sosMatrix,'ScaleValues',Hp1.ScaleValues);\r\nhEQ2 = signalblks.BiquadFilter('SOSMatrix',Hp2.sosMatrix,'ScaleValues',Hp2.ScaleValues);\r\nhEQ3 = signalblks.BiquadFilter('SOSMatrix',Hp3.sosMatrix,'ScaleValues',Hp3.ScaleValues);\r\nhEQ4 = signalblks.BiquadFilter('SOSMatrix',Hp4.sosMatrix,'ScaleValues',Hp4.ScaleValues);\r\nhEQ5 = signalblks.BiquadFilter('SOSMatrix',Hp5.sosMatrix,'ScaleValues',Hp5.ScaleValues);\r\nhEQ6 = signalblks.BiquadFilter('SOSMatrix',Hp6.sosMatrix,'ScaleValues',Hp6.ScaleValues);\r\n\r\n%% Filtering the input in a stream processing loop\r\n% To filter out the vuvuzela noise, we use a stream processing loop that\r\n% reads in one frame of input data from the WAV file, applies the series of\r\n% 6 parametric equalizers, and then plays back the filtered output through\r\n% the sound card.\r\n\r\nfor frames = 1:400\r\n    %Step through input WAV file one frame at a time\r\n    input = step(hSound);\r\n    \r\n    %Apply notch EQ filters first, then peak EQ filter\r\n    out1 = step(hEQ1, input);\r\n    out2 = step(hEQ2, out1);\r\n    out3 = step(hEQ3, out2);\r\n    out4 = step(hEQ4, out3);        \r\n    out5 = step(hEQ5, out4);\r\n    filt_output = step(hEQ6, out5);\r\n    \r\n    %Play 200 frames of input signal, then 200 frames of filtered output\r\n    %to compare original and filtered signals\r\n    if frames < 200\r\n      step(hPlayer, input);\r\n    else \r\n       step(hPlayer, filt_output);\r\n    end\r\n   \r\n   %Log filtered output to buffer, WAV file\r\n   step(hLogOutput, filt_output);\r\n   step(hOutputFile, filt_output);\r\n   \r\nend\r\n\r\n%% Visualizing and listening to output results\r\n% Listen to the noisy and filtered signals here.\r\n%\r\n%\r\n\r\n%%\r\n% Visualize the spectrum of the filtered signal.  If you zoom into the\r\n% plot, you can see that the vuvuzela frequencies have been attenuated by\r\n% our notch parametric equalizers.\r\n%%\r\n%      pwelch(hLogOutput.Buffer, [], [], 8192, Fs);\r\n%\r\n% <<vuvuzela_denoising_parameq_blog_02.png>>\r\n%\r\n \r\n%% Cleanup\r\n% Close input and output System objects to ensure that we leave all objects in their\r\n% proper states.\r\nclose(hSound);\r\nclose(hPlayer);\r\nclose(hOutputFile);\r\n\r\n%% Things to Try\r\n% This demo was a lot of fun to work on and I hope you\u00e2\u20ac\u2122ll find it\r\n% interesting.  Feel free to download the code and try your own equalizer\r\n% parameters.  Have fun!\r\n\r\n##### SOURCE END ##### 0311dcb076be47759dd905e8d49b009b\r\n-->","protected":false},"excerpt":{"rendered":"<p>\r\n   \r\n      Today I&#8217;d like to introduce a guest blogger, Charu, who is a Technical Marketing Manager at The MathWorks.  Charu regularly\r\n         presents webinars on signal processing topics,... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/loren\/2010\/06\/30\/vuvuzela-denoising-with-parametric-equalizers\/\">read more >><\/a><\/p>","protected":false},"author":39,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[33,6,37],"tags":[],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/posts\/238"}],"collection":[{"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/users\/39"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/comments?post=238"}],"version-history":[{"count":3,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/posts\/238\/revisions"}],"predecessor-version":[{"id":2393,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/posts\/238\/revisions\/2393"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/media?parent=238"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/categories?post=238"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/tags?post=238"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}