-1
\$\begingroup\$

I want to remove all the frequency components from a .wav file except those within \$\pm 25\$ of \$523 \ Hz\$ as well as its harmonics.

Attempt:

I used a high pass filter first to suppress frequencies below 523 - 25 Hz. Then I used a low pass filter to suppress frequencies higher than 523 + 25. If my method is correct, is there a more efficient way of doing this?

Also, to take into account the harmonics, I used a 'for loop'. Is that right? But it only removes up to a certain harmonic. Is it possible to modify this code so that it repeats the procedure for all of the harmonics of 523 Hz, up to the Nyquist frequency?

Here's my Matlab code:

[s, Fs] = wavread('chord.wav');

wavplay(s, Fs);

n=input('Number of harmonics? ');

for N=2:1:n

% High-pass filter

FsNorm =  (523-25).*N / (Fs/2);
[b, a] = butter(10, FsNorm, 'high');
sHigh = filtfilt(b, a, s);

% Low pass filter
FsNorm = (523+25).*N / (Fs/2);
[b,a] = butter(10, FsNorm, 'low');
sLow = filtfilt(b, a, s);

end

wavplay(sLow,Fs);
\$\endgroup\$
4
  • \$\begingroup\$ Your description is not clear. Do you want to suppress 523 Hz and its harmonics, or do you want to pass 523 Hz and its harmonics and suppress all other frequencies? \$\endgroup\$
    – The Photon
    Commented Apr 23, 2016 at 15:46
  • \$\begingroup\$ I want to remove all the frequencies except those within ±25 Hz of 523 Hz and each of its harmonics. So, suppress frequencies below 523-25 and the ones higher than 523+25 Hz. I want to then repeat the process for all of the harmonics of 523 Hz. \$\endgroup\$
    – Merin
    Commented Apr 24, 2016 at 8:32
  • \$\begingroup\$ Then please edit your question to make it clear. Particularly the part where you asked, "Is it possible to modify this to remove all harmonics..." Future readers should not have to read the comments to understand the question. \$\endgroup\$
    – The Photon
    Commented Apr 24, 2016 at 15:12
  • \$\begingroup\$ I have edited my question so hopefully it is clear now. Thanks. \$\endgroup\$
    – Merin
    Commented Apr 24, 2016 at 23:54

1 Answer 1

1
\$\begingroup\$
  1. Your approach will probably work okay, but for this narrow a pass-band you should probably use a designed band-pass filter rather than a combination of series low-pass and high-pass filters, particularly if you want to retain the features of the Butterworth design (maximally flat pass-band).

  2. It appears that your intention is to apply each filter to the output of the previous filter, but it doesn't appear that your Matlab code does that. It applies one filter and throws away the result. Then applies another filter and throws that result away. Finally it returns the output of the final filter appplied to the input signal. So basically you only get the result from applying the final low-pass filter to the input signal.

  3. Even if you did apply all the filters to the outputs of the previous filter, your first filter that suppresses frequencies above 548 Hz has removed all the harmonics (at 1046 Hz, 1569 Hz, ...), so there's no content there for the higher harmonic filters to pass through. Also, the 2nd-harmonic filter that supresses content below 1021 Hz will eliminate the fundamental signal that you passed through in the first filter.

    One option is, instead of applying each (band-pass) filters to the output of the previous filter, apply each filter to the original input signal, then sum all the filter outputs together to form the final output.

It only removes up to a certain harmonic. Is it possible to modify this to remove all harmonics of 523 Hz there is, up to the Nyquist frequency?

Yes, choose N large enough to include all the harmonics up to the Nyquist frequency.

\$\endgroup\$
7
  • \$\begingroup\$ Also, I am not a regular Matlab user, but filtfilt does "zero-phase digital filtering by processing the input data, x, in both the forward and reverse directions". This is only possible with block data and only in digital filtering. If you are trying to simulate an analog filter, or intend your final design to be applied continuously, this is probably not the correct function to use to apply your filter. \$\endgroup\$
    – The Photon
    Commented Apr 24, 2016 at 15:28
  • \$\begingroup\$ Is it correct to use a very large N value and then place a while loop like while (523-25).*N < (Fs/2) at the beginning of the filters, so that we stay within the Nyquist frequency band? Also, what would be the syntax to sum the output of the two filters? Finally, do I need to use designfilt instead of filtfilt? My input is from a .wav file (it's a digitized recording of a piano chord). \$\endgroup\$
    – Merin
    Commented Apr 25, 2016 at 0:32
  • 1
    \$\begingroup\$ I will say it should be easy to calculate how big an N you need to reach the Nyquist frequency, and just use that in the limit of your for loop. \$\endgroup\$
    – The Photon
    Commented Apr 25, 2016 at 2:27
  • 1
    \$\begingroup\$ You should definitely choose \$N_{max}\$ so that \$(523+25)N_{max}<F_s/2\$. But honestly you might want to stop a couple harmonics below that to be sure the skirts of your filter don't have an unexpected interaction in the alias band. \$\endgroup\$
    – The Photon
    Commented Apr 25, 2016 at 3:41
  • 1
    \$\begingroup\$ Also realize that by the time you get to the 20th harmonic, the size of your passband window will be bigger than the separation between harmonics. \$\endgroup\$
    – The Photon
    Commented Apr 25, 2016 at 5:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.